GVKun编程网logo

Python 数据科学常用包 (一) Numpy(python数据科学入门)

4

本文将介绍Python数据科学常用包(一)Numpy的详细情况,特别是关于python数据科学入门的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于"

本文将介绍Python 数据科学常用包 (一) Numpy的详细情况,特别是关于python数据科学入门的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、Difference between import numpy and import numpy as np的知识。

本文目录一览:

Python 数据科学常用包 (一) Numpy(python数据科学入门)

Python 数据科学常用包 (一) Numpy(python数据科学入门)

NumPy是什么?

今天开始会陆续为大家带来数据科学常用包的基础用法

数据分析的工作涉及到大量的数值运算,一个高效方便的科学计算工具是必不可少的。Python语言一开始并不是设计为科学计算使用的语言,随着越来越多的人发现Python的易用性,逐渐出现了关于Python的大量外部扩展,Numpy (Numeric Python)就是其中之一。

Numpy提供了大量的数值编程工具,可以方便地处理向量、矩阵等运算,极大地便利了人们在科学计算方面的工作。另一方面,Python是免费,相比于花费高额的费用使用Matlab,Numpy的出现使Python得到了更多人的青睐。

我们可以简单看一下如何开始使用NumPy:

import numpy as np
numpy.version.full_version
''1.16.4''


二、NumPy对象:数组

NumPy中的基本对象是同类型的多维数组(homogeneous multidimensional array),这和C++中的数组是一致的,例如字符型和数值型就不可共存于同一个数组中。先上例子:

a = np.arange(20)
print(a)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

这里我们生成了一个一维数组a,从0开始,步长为1,长度为20。Python中的计数是从0开始的,R和Matlab的使用者需要小心。

我们可以通过"type"函数查看a的类型,这里显示a是一个array:

type(a)
numpy.ndarray


通过函数"reshape",我们可以重新构造一下这个数组,例如,我们可以构造一个4*5的二维数组,其中"reshape"的参数表示各维度的大小,且按各维顺序排列(两维时就是按行排列,这和R中按列是不同的):

a = a.reshape(4, 5)
print(a)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

构造更高维的也没问题:

a = a.reshape(2, 2, 5)
print(a)
[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]]

 [[10 11 12 13 14]
  [15 16 17 18 19]]]

既然a是array,我们还可以调用array的函数进一步查看a的相关属性:"ndim"查看维度;"shape"查看各维度的大小;"size"查看全部的元素个数,等于各维度大小的乘积;"dtype"可查看元素类型;"dsize"查看元素占位(bytes)大小。

a.ndim
3



a.shape
(2, 2, 5)



a.size
20



a.dtype
dtype(''int32'')


三、创建数组

数组的创建可通过转换列表实现,高维数组可通过转换嵌套列表实现:

raw = [0,1,2,3,4]
a = np.array(raw)
a
array([0, 1, 2, 3, 4])



raw = [[0,1,2,3,4], [5,6,7,8,9]]
b = np.array(raw)
b
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])


一些特殊的数组有特别定制的命令生成,如4*5的全零矩阵:

d = (4, 5)
np.zeros(d)
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])


默认生成的类型是浮点型,可以通过指定类型改为整型:

d = (4, 5)
np.ones(d, dtype=int)
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])


[0, 1)区间的随机数数组:

np.random.rand(5)
array([0.80378557, 0.09833667, 0.95280995, 0.17707594, 0.80651926])


服从正态分布的随机数组:

np.random.randn(5)
array([ 0.678737  , -1.14965615, -1.40492579,  1.22479651,  0.2751816 ])


四、数组操作

简单的四则运算已经重载过了,全部的''+'',''-'',''*'',''/''运算都是基于全部的数组元素的,以加法为例:

a = np.array([[1.0, 2], [2, 4]])
print ("a:\n",a)

b = np.array([[3.2, 1.5], [2.5, 4]])
print ("b:\n",b)
print ("a+b:\n",a+b)
a:
 [[1. 2.]
 [2. 4.]]
b:
 [[3.2 1.5]
 [2.5 4. ]]
a+b:
 [[4.2 3.5]
 [4.5 8. ]]

这里可以发现,a中虽然仅有一个与元素是浮点数,其余均为整数,在处理中Python会自动将整数转换为浮点数(因为数组是同质的),并且,两个二维数组相加要求各维度大小相同。当然,NumPy里这些运算符也可以对标量和数组操作,结果是数组的全部元素对应这个标量进行运算,还是一个数组:

print ("3 * a \n",3*a)
print ("b + 1.8 \n",b )
3 * a :
 [[ 3.  6.]
 [ 6. 12.]]
b + 1.8 
 [[3.2 1.5]
 [2.5 4. ]]

类似C++,''+=''、''-=''、''*=''、''/=''操作符在NumPy中同样支持:

a /= 2
a
array([[0.5, 1. ],
       [1. , 2. ]])


开根号求指数也很容易:

print(a)
[[0.5 1. ]
 [1.  2. ]]


print ("np.exp:\n",np.exp(a))
np.exp:
 [[1.64872127 2.71828183]
 [2.71828183 7.3890561 ]]


print ("np.sqrt:\n",np.sqrt(a))
np.sqrt:
 [[0.70710678 1.        ]
 [1.         1.41421356]]


print ("np.square:\n",np.square(a))
np.square:
 [[0.25 1.  ]
 [1.   4.  ]]


print ("np.power:\n",np.power(a,3))
np.power:
 [[0.125 1.   ]
 [1.    8.   ]]

需要知道二维数组的最大最小值怎么办?想计算全部元素的和、按行求和、按列求和怎么办?NumPy的ndarray类已经做好函数了:

a = np.arange(20).reshape(4,5)
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])



print("sum of all elements in a: " + str(a.sum()))
print("maximum element in a: " + str(a.max()))
print("minimum element in a: " + str(a.min()))
print("maximum element in each row of a: " + str(a.max(axis=1)))
print("minimum element in each column of a: " + str(a.min(axis=0)))
sum of all elements in a: 190
maximum element in a: 19
minimum element in a: 0
maximum element in each row of a: [ 4  9 14 19]
minimum element in each column of a: [0 1 2 3 4]

科学计算中大量使用到矩阵运算,除了数组,NumPy同时提供了矩阵对象(matrix)。矩阵对象和数组的主要有两点差别:一是矩阵是二维的,而数组的可以是任意正整数维;二是矩阵的''*''操作符进行的是矩阵乘法,乘号左侧的矩阵列和乘号右侧的矩阵行要相等,而在数组中''*''操作符进行的是每一元素的对应相乘,乘号两侧的数组每一维大小需要一致。数组可以通过asmatrix或者mat转换为矩阵,或者直接生成也可以:

a = np.arange(20).reshape(4, 5)
a = np.asmatrix(a)
print(type(a))

b = np.matrix(''1.0 2.0; 3.0 4.0'')
print(type(b))
<class ''numpy.matrix''>
<class ''numpy.matrix''>

再来看一下矩阵的乘法,这使用arange生成另一个矩阵b,arange函数还可以通过arange(起始,终止,步长)的方式调用生成等差数列,注意含头不含尾。

b = np.arange(2, 45, 3).reshape(5, 3)
b = np.mat(b)
print(b)
[[ 2  5  8]
 [11 14 17]
 [20 23 26]
 [29 32 35]
 [38 41 44]]

回到我们的问题,矩阵a和b做矩阵乘法:

print ("matrix a:\n",a)
print("matrix b:\n",b)

c = a * b
print("matrix c:\n",c)
matrix a:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
matrix b:
 [[ 2  5  8]
 [11 14 17]
 [20 23 26]
 [29 32 35]
 [38 41 44]]
matrix c:
 [[ 290  320  350]
 [ 790  895 1000]
 [1290 1470 1650]
 [1790 2045 2300]]

五、数组元素访问

数组和矩阵元素的访问可通过下标进行,以下均以二维数组(或矩阵)为例:

a = np.array([[3.2, 1.5], [2.5, 4]])
print(a[0][1])
print(a[0, 1])
1.5
1.5

可以通过下标访问来修改数组元素的值:

b = a
a[0][1] = 2.0
print(a)
print(b)
[[3.2 2. ]
 [2.5 4. ]]
[[3.2 2. ]
 [2.5 4. ]]

现在问题来了,明明改的是a0,怎么连b0也跟着变了?这个陷阱在Python编程中很容易碰上,其原因在于Python不是真正将a复制一份给b,而是将b指到了a对应数据的内存地址上。想要真正的复制一份a给b,可以使用copy:

a = np.array([[3.2, 1.5], [2.5, 4]])
b = a.copy()
a[0][1] = 2.0
print ("a:",a)
print ("b:",b)
a: [[3.2 2. ]
 [2.5 4. ]]
b: [[3.2 1.5]
 [2.5 4. ]]

若对a重新赋值,即将a指到其他地址上,b仍在原来的地址上:

a = np.array([[3.2, 1.5], [2.5, 4]])
b = a
a = np.array([[2, 1], [9, 3]])
print ("a:\n",a)
print ("b:\n",b)
a:
 [[2 1]
 [9 3]]
b:
 [[3.2 1.5]
 [2.5 4. ]]

利用'':''可以访问到某一维的全部数据,例如取矩阵中的指定列:

a = np.arange(20).reshape(4, 5)
print ("a:\n",a)
print ("the 2nd and 4th column of a::\n",a[:,[1,3]])
a:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
the 2nd and 4th column of a::
 [[ 1  3]
 [ 6  8]
 [11 13]
 [16 18]]

稍微复杂一些,我们尝试取出满足某些条件的元素,这在数据的处理中十分常见,通常用在单行单列上。下面这个例子是将第一列大于5的元素(10和15)对应的第三列元素(12和17)取出来:

a[:, 2][a[:, 0] > 5]
array([12, 17])


可使用where函数查找特定值在数组中的位置:

loc = numpy.where(a==11)
print(loc)
print(a[loc[0][0], loc[1][0]])
(array([2], dtype=int64), array([1], dtype=int64))
11

六、数组操作

还是拿矩阵(或二维数组)作为例子,首先来看矩阵转置:

a = np.random.rand(2,4)
print ("a:\n",a)
a = np.transpose(a)
print ("a is an array, by using transpose(a):\n",a)

b = np.random.rand(2,4)
b = np.mat(b)
print ("b:\n",b)
print ("b is a matrix, by using b.T:\n",b.T)
a:
 [[0.49632956 0.65061015 0.36037379 0.29664563]
 [0.18319505 0.45525932 0.08422801 0.75167911]]
a is an array, by using transpose(a):
 [[0.49632956 0.18319505]
 [0.65061015 0.45525932]
 [0.36037379 0.08422801]
 [0.29664563 0.75167911]]
b:
 [[0.51087064 0.2058778  0.88659661 0.78428426]
 [0.62716285 0.46838085 0.63015861 0.69754748]]
b is a matrix, by using b.T:
 [[0.51087064 0.62716285]
 [0.2058778  0.46838085]
 [0.88659661 0.63015861]
 [0.78428426 0.69754748]]

矩阵求逆:

import numpy.linalg as nlg
a = np.random.rand(2,2)
print ("a:\n",a)
ia = nlg.inv(a)
print ("inverse of a:\n",ia)
print ("a * inv(a):\n",a * ia)
a:
 [[0.7748124  0.08125528]
 [0.99696367 0.73251292]]
inverse of a:
 [[ 1.50551971 -0.16700242]
 [-2.04904025  1.59245703]]
a * inv(a):
 [[ 1.16649535 -0.01356983]
 [-2.04281868  1.16649535]]

求特征值和特征向量

a = np.random.rand(3,3)
eig_value, eig_vector = nlg.eig(a)

print ("eigen value:\n",eig_value)
print ("eigen vector:\n",eig_vector)
eigen value:
 [ 1.75590394+0.j         -0.25188941+0.08867887j -0.25188941-0.08867887j]
eigen vector:
 [[ 0.33976986+0.j          0.47679494-0.21597791j  0.47679494+0.21597791j]
 [ 0.81509742+0.j          0.24255425+0.21077809j  0.24255425-0.21077809j]
 [ 0.46922557+0.j         -0.78915154+0.j         -0.78915154-0.j        ]]

按列拼接两个向量成一个矩阵:

a = np.array((1,2,3))
b = np.array((2,3,4))
print(np.column_stack((a,b)))
[[1 2]
 [2 3]
 [3 4]]

在循环处理某些数据得到结果后,将结果拼接成一个矩阵是十分有用的,可以通过vstack和hstack完成:

a = np.random.rand(2,2)
b = np.random.rand(2,2)
print ("a:\n",a)
print ("b:\n",b)
c = np.hstack([a,b])
d = np.vstack([a,b])
print("horizontal stacking a and b:\n",c)
print("vertical stacking a and b:\n",d)
a:
 [[0.50331973 0.49651025]
 [0.89325327 0.31245265]]
b:
 [[0.35846554 0.56841584]
 [0.88041789 0.81287829]]
horizontal stacking a and b:
 [[0.50331973 0.49651025 0.35846554 0.56841584]
 [0.89325327 0.31245265 0.88041789 0.81287829]]
vertical stacking a and b:
 [[0.50331973 0.49651025]
 [0.89325327 0.31245265]
 [0.35846554 0.56841584]
 [0.88041789 0.81287829]]

七、缺失值

缺失值在分析中也是信息的一种,NumPy提供nan作为缺失值的记录,通过isnan判定。

a = np.random.rand(2,2)
a[0, 1] = np.nan
print(np.isnan(a))
[[False  True]
 [False False]]

nan_to_num可用来将nan替换成0,pandas中提供能指定nan替换值的函数。

print(np.nan_to_num(a))
[[0.04279427 0.        ]
 [0.08386045 0.3567586 ]]

参考文献

  1. http://wiki.scipy.org/Tentati...
  2. Sheppard K. Introduction to Python for econometrics, statistics and data analysis. Self-published, University of Oxford, version, 2012, 2.

"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 周一

Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案

Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案

如何解决Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案?

希望有人能在这里提供帮助。我一直在绕圈子一段时间。我只是想设置一个 python 脚本,它将一些 json 数据从 REST API 加载到云数据库中。我在 Anaconda 上设置了一个虚拟环境(因为 GCP 库推荐这样做),安装了依赖项,现在我只是尝试导入库并向端点发送请求。 我使用 Conda(和 conda-forge)来设置环境并安装依赖项,所以希望一切都干净。我正在使用带有 Python 扩展的 VS 编辑器作为编辑器。 每当我尝试运行脚本时,我都会收到以下消息。我已经尝试了其他人在 Google/StackOverflow 上找到的所有解决方案,但没有一个有效。我通常使用 IDLE 或 Jupyter 进行脚本编写,没有任何问题,但我对 Anaconda、VS 或环境变量(似乎是相关的)没有太多经验。 在此先感谢您的帮助!

  \Traceback (most recent call last):
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\__init__.py",line 22,in <module>
from . import multiarray
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\multiarray.py",line 12,in <module>
from . import overrides
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\overrides.py",line 7,in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load Failed while importing _multiarray_umath: The specified module Could not be found.

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
File "c:\API\citi-bike.py",line 4,in <module>
import numpy as np
File "C:\Conda\envs\gcp\lib\site-packages\numpy\__init__.py",line 150,in <module>
from . import core
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\__init__.py",line 48,in <module>
raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions Failed. This error can happen for
many reasons,often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

* The Python version is: python3.9 from "C:\Conda\envs\gcp\python.exe"
* The NumPy version is: "1.21.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load Failed while importing _multiarray_umath: The specified module Could not be found.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

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

今天关于Python 数据科学常用包 (一) Numpypython数据科学入门的讲解已经结束,谢谢您的阅读,如果想了解更多关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、Difference between import numpy and import numpy as np的相关知识,请在本站搜索。

本文标签: