GVKun编程网logo

请让我理解为什么在下面的代码中使用 numpy.isfinite 函数的原因?(下面的代码输出是什么,为什么)

1

对于请让我理解为什么在下面的代码中使用numpy.isfinite函数的原因?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍下面的代码输出是什么,为什么,并为您提供关于"importnumpy

对于请让我理解为什么在下面的代码中使用 numpy.isfinite 函数的原因?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍下面的代码输出是什么,为什么,并为您提供关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Difference between import numpy and import numpy as np、ES6+ isFinite()&isNaN()的有用信息。

本文目录一览:

请让我理解为什么在下面的代码中使用 numpy.isfinite 函数的原因?(下面的代码输出是什么,为什么)

请让我理解为什么在下面的代码中使用 numpy.isfinite 函数的原因?(下面的代码输出是什么,为什么)

如何解决请让我理解为什么在下面的代码中使用 numpy.isfinite 函数的原因?

** 我有一个 CSV 文件,其中包含所有国家的夏季和冬季奥运会数据,问题陈述是 -> 哪个国家的夏季金牌数和冬季金牌数之间的差异最大他们的金牌总数?

仅包括在夏季和冬季都至少赢得 1 枚金牌的国家/地区。 **

import numpy as np
    def answer_three():
        copy_df = df.copy()
        copy_df = copy_df.where(df[''Gold''] > 0)
        copy_df = copy_df.where(df[''Gold.1''] > 0)
        copy_df[''Diff''] = copy_df[''Gold''] - copy_df[''Gold.1'']
        copy_df[''Gold_Ratio''] = copy_df[''Diff''] / (copy_df[''Gold''] + copy_df[''Gold.1''])
        copy_df_final = copy_df[np.isfinite(copy_df[''Gold_Ratio''])]
        max_ratio = max(copy_df_final[''Gold_Ratio''])
    
        return (str(copy_df_final[copy_df_final[''Gold_Ratio''] == max_ratio].index[0]))
    answer_three()

解决方法

我想我可能会在第三行和第四行代码 copy_df = copy_df.where(df[''Gold''] > 0)copy_df = copy_df.where(df[''Gold.1''] > 0) 中得到解决方案我只允许大于“0”的值not >0 用 NaN 值填充,因此使用 isfinite。如果我的解释有误,请纠正我。

"import numpy as np" ImportError: No module named numpy

问题:没有安装 numpy

解决方法:

下载文件,安装

numpy-1.8.2-win32-superpack-python2.7

安装运行 import numpy,出现

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import numpy
  File "C:\Python27\lib\site-packages\numpy\__init__.py", line 153, in <module>
    from . import add_newdocs
  File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 6, in <module>
    from . import multiarray
ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。

原因是:python 装的是 64 位的,numpy 装的是 32 位的

重新安装 numpy 为:numpy-1.8.0-win64-py2.7

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数

3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数

目录

[TOC]

前言

具体我们来学 Numpy 的统计函数

(一)函数一览表

调用方式:np.*

.sum(a) 对数组 a 求和
.mean(a) 求数学期望
.average(a) 求平均值
.std(a) 求标准差
.var(a) 求方差
.ptp(a) 求极差
.median(a) 求中值,即中位数
.min(a) 求最大值
.max(a) 求最小值
.argmin(a) 求最小值的下标,都处里为一维的下标
.argmax(a) 求最大值的下标,都处里为一维的下标
.unravel_index(index, shape) g 根据 shape, 由一维的下标生成多维的下标

(二)统计函数 1

(1)说明

(2)输出

.sum(a)

.mean(a)

.average(a)

.std(a)

.var(a)

(三)统计函数 2

(1)说明

(2)输出

.max(a) .min(a)

.ptp(a)

.median(a)

.argmin(a)

.argmax(a)

.unravel_index(index,shape)

作者:Mark

日期:2019/02/11 周一

Difference between import numpy and import numpy as np

Difference between import numpy and import numpy as np

Difference between import numpy and import numpy as np

up vote 18 down vote favorite

5

I understand that when possible one should use

import numpy as np

This helps keep away any conflict due to namespaces. But I have noticed that while the command below works

import numpy.f2py as myf2py

the following does not

import numpy as np
np.f2py #throws no module named f2py

Can someone please explain this?

python numpy

shareimprove this question

edited Mar 24 ''14 at 23:20

mu 無

24.7k104471

asked Mar 24 ''14 at 23:19

user1318806

3001311

 
1  

@roippi have you tried exit your python and enter it and just do import numpy then numpy.f2py ? It throws an error in my case too – aha Mar 24 ''14 at 23:24

1  

Importing a module doesn''t import sub-modules. You need to explicitly import the numpy.f2py module regardless of whether or not/how numpy itself has been imported. – alecb Mar 24 ''14 at 23:39

add a comment

4 Answers

active oldest votes

 

up vote 13 down vote

numpy is the top package name, and doing import numpy doesn''t import submodule numpy.f2py.

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

In your above code:

import numpy as np # np is an alias pointing to numpy, but at this point numpy is not linked to numpy.f2py
import numpy.f2py as myf2py # this command makes numpy link to numpy.f2py. myf2py is another alias pointing to numpy.f2py as well

Here is the difference between import numpy.f2py and import numpy.f2py as myf2py:

  • import numpy.f2py
    • put numpy into local symbol table(pointing to numpy), and numpy is linked to numpy.f2py
    • both numpy and numpy.f2py are accessible
  • import numpy.f2py as myf2py
    • put my2py into local symbol table(pointing to numpy.f2py)
    • Its parent numpy is not added into local symbol table. Therefore you can not access numpy directly

shareimprove this answer

edited Mar 25 ''14 at 0:31

answered Mar 24 ''14 at 23:33

aha

1,2291718

 

add a comment

 

up vote 7 down vote

The import as syntax was introduced in PEP 221 and is well documented there.

When you import a module via

import numpy

the numpy package is bound to the local variable numpy. The import as syntax simply allows you to bind the import to the local variable name of your choice (usually to avoid name collisions, shorten verbose module names, or standardize access to modules with compatible APIs).

Thus,

import numpy as np

is equivalent to,

import numpy
np = numpy
del numpy

When trying to understand this mechanism, it''s worth remembering that import numpy actually means import numpy as numpy.

When importing a submodule, you must refer to the full parent module name, since the importing mechanics happen at a higher level than the local variable scope. i.e.

import numpy as np
import numpy.f2py   # OK
import np.f2py      # ImportError

I also take issue with your assertion that "where possible one should [import numpy as np]". This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with numpy. It has never prevented a name collision for me (laziness of programmers actually suggests there''s a higher probability of causing a collision with np)

Finally, to round out my exposé, here are 2 interesting uses of the import as mechanism that you should be aware of:

1. long subimports

import scipy.ndimage.interpolation as warp
warp.affine_transform(I, ...)

2. compatible APIs

try:
    import pyfftw.interfaces.numpy_fft as fft
except:
    import numpy.fft as fft
# call fft.ifft(If) with fftw or the numpy fallback under a common name

shareimprove this answer

answered Mar 25 ''14 at 0:59

hbristow

68345

 

add a comment

 

up vote 1 down vote

numpy.f2py is actually a submodule of numpy, and therefore has to be imported separately from numpy. As aha said before:

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

when you call the statement import numpy as np, you are shortening the phrase "numpy" to "np" to make your code easier to read. It also helps to avoid namespace issues. (tkinter and ttk are a good example of what can happen when you do have that issue. The UIs look extremely different.)

shareimprove this answer

answered Mar 24 ''14 at 23:47

bspymaster

760923

 

add a comment

 

up vote 1 down vote

This is a language feature. f2py is a subpackage of the module numpy and must be loaded separately.

This feature allows:

  • you to load from numpy only the packages you need, speeding up execution.
  • the developers of f2py to have namespace separation from the developers of another subpackage.

Notice however that import numpy.f2py or its variant import numpy.f2py as myf2py are still loading the parent module numpy.

Said that, when you run

import numpy as np
np.f2py

You receive an AttributeError because f2py is not an attribute of numpy, because the __init__() of the package numpy did not declare in its scope anything about the subpackage f2py.

shareimprove this answer

answered Mar 24 ''14 at 23:57

gg349

7,67321739

 
    

when you do import numpy.f2py as myf2py, how do you access its parent numpy? it seems import numpy.f2py allows you to access its parent numpy, but import numpy.f2py as myf2py doesn''t – aha Mar 25 ''14 at 0:00

    

You don''t access it because you decided you didn''t want to use anything from numpy, and you only care of using the subpackage. It is similar to using from foo import bar: the name foo will not be accessible. See the comment after the first example of the docs, LINK – gg349 Mar 25 ''14 at 0:05

add a comment

ES6+ isFinite()&isNaN()

ES6+ isFinite()&isNaN()

ES6+ Number.isFinite()&Number.isNaN()

1. 前言

在 ES5 中,全局下的 isFinite ()isNaN () 两种方法存在类型转换,对最终的判断结果存在歧义。ES6 在 Number 对象上,新提供了 Number.isFinite ()Number.isNaN () 两个方法,更加健壮地进行数值的判断,接下来让我看看这两种方法。

2. Number.isFinite()

在 ES5 中有全局的 isFinite() 函数用来判断被传入的参数值是否为一个有限的数值,如果参数是字符串,会首先转为一个数值,然后在进行验证。

isFinite();  // false
isFinite();       // false
isFinite(-); // false

isFinite();         // true
isFinite();      // true
isFinite(''2e64'');    // true

isFinite("0");       // true

上面的代码可以看出,字符串也会被先转为数值再进行判断,而 ES6 Number 对象上提供的 isFinite() 更健壮,和全局的 isFinite() 函数相比,这个方法不会强制将一个非数值的参数转换成数值,这就意味着,只有数值类型的值,且是有穷的(finite),才返回 true

Number.isFinite();  // false
Number.isFinite();       // false
Number.isFinite(-); // false

Number.isFinite();         // true
Number.isFinite();      // true
Number.isFinite(''2e64'');	// false

Number.isFinite(''0'');       // false

3. Number.isNaN()

在 JavaScript 中与其它的值不同,NaN 不能通过相等操作符(== 和 ===)来判断 ,因为 NaN == NaNNaN === NaN 都会返回 false。 因此,判断一个值是不是 NaN 是有必要的。

3.1 NaN 值的产生

当算术运算的结果返回一个未定义的或无法表示的值时,NaN 就产生了。但是,NaN 并不一定用于表示某些值超出表示范围的情况。

  • 将某些非数值强制转换为数值的时候,会得到 NaN

  • 0 除以 0 会返回 NaN —— 但是其他数除以 0 则不会返回 NaN。

上一节 我们知道可以使用 Number() 方法进行类型转换,下面列举被强制类型转换为 NaN 的例子:

Number(undefined)				// NaN
Number(''undefined'')				// NaN
Number(''string'')				// NaN
Number({})						// NaN
Number(''10,3'')					// NaN
Number(''123ABC'')				// NaN
Number(new Date().toString())	// NaN

上面的例子可以看出,很多值在强制类型转换下转为 NaN,针对这样的值去进行判断无疑是有问题的,下面我们来看下 isNaN () 的问题。

3.2 isNaN () 的问题

默认情况全局下存在方法 isNaN () 用了判断是否为 NaN 值,它要求接收的是数值类型的参数,但是当参数不是 Number 类型, isNaN 函数会首先尝试将这个参数转换为数值,然后才会对转换后的结果是否是 NaN 进行判断。

实例:

isNaN();       // true
isNaN(undefined); // true
isNaN(''undefined'')// true
isNaN({});        // true

isNaN(true);      // false
isNaN(null);      // false
isNaN();        // false

// strings
isNaN("37");      // false: 可以被转换成数值37
isNaN("37.37");   // false: 可以被转换成数值37.37
isNaN("37,5");    // true
isNaN(''123ABC'');  // true:  parseInt("123ABC")的结果是 123, 但是Number("123ABC")结果是 NaN
isNaN("");        // false: 空字符串被转换成0
isNaN(" ");       // false: 包含空格的字符串被转换成0

// dates
isNaN(new Date());                // false
isNaN(new Date().toString());     // true

isNaN("imooc")   // true: "blabla"不能转换成数值
                 // 转换成数值失败, 返回NaN

结合上面 NaN 是如何产生的例子的结果可以看出,使用 isNaN 来判断返回的是 true,这显然不是我们想要的结果。针对这样的问题,ES6 做了修补,下面我们看 ES6 中的 isNaN 方法。

3.3 Number.isNaN () 详情

ES6 提供了 Number.isNaN(x) ,通过这个方法来检测变量 x 是否是一个 NaN 将会是一种可靠的做法,它不会对所判断的值进行强制类型转换。

Number.isNaN();        // true
Number.isNaN(Number.); // true
Number.isNaN( / )       // true

// 下面这几个如果使用全局的 isNaN() 时,会返回 true。
Number.isNaN("NaN");      // false,字符串 "NaN" 不会被隐式转换成数字 NaN。
Number.isNaN(undefined);  // false
Number.isNaN(''undefined'');// false
Number.isNaN({});         // false
Number.isNaN("blabla");   // false

Number.isNaN(true);   	 // false
Number.isNaN(null);   	 // false
Number.isNaN();   	 // false
Number.isNaN("37");   	 // false
Number.isNaN("37.37");	 // false
Number.isNaN("");   	 // false
Number.isNaN(" ");   	 // false

通过上面的实例,基本覆盖了现有程序的所有情况,不会出现使用全局下的 isNaN() 多带来的问题。所有推荐使用 Number.isNaN(x) 方式来判断是否是 NaN。在不支持 Number.isNaN 函数情况下,可以通过表达式 (x != x) 来检测变量 x 是不是 NaN 会更加可靠。

4. 小结

本节中传统的全局方法 isFinite()isNaN() 的区别在于,传统方法先调用 Number () 将非数值的值转为数值,再进行判断,而这两个新方法只对数值有效,Number.isFinite() 对于非数值一律返回 false,Number.isNaN() 只有对于 NaN 才返回 true,非 NaN 一律返回 false

我们今天的关于请让我理解为什么在下面的代码中使用 numpy.isfinite 函数的原因?下面的代码输出是什么,为什么的分享就到这里,谢谢您的阅读,如果想了解更多关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Difference between import numpy and import numpy as np、ES6+ isFinite()&isNaN()的相关信息,可以在本站进行搜索。

本文标签: