对于Python科学计算库-Numpy感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python的科学计算库,并为您提供关于"importnumpyasnp"ImportError:Nomod
对于Python 科学计算库 - Numpy感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python的科学计算库,并为您提供关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、c++ 科学计算库、Difference between import numpy and import numpy as np的有用信息。
本文目录一览:- Python 科学计算库 - Numpy(python的科学计算库)
- "import numpy as np" ImportError: No module named numpy
- 3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数
- c++ 科学计算库
- Difference between import numpy and import numpy as np
Python 科学计算库 - Numpy(python的科学计算库)
NumPy 是 Python 语言的一个扩充程序库。支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库,也是学习 python 必学的一个库。
1. 读取文件
numpy.genfromtxt () 用于读取 txt 文件,其中传入的参数依次为:
- 需要读取的 txt 文件位置,此处文件与程序位于同一目录下
- 分割的标记
- 转换类型,如果文件中既有文本类型也有数字类型,就先转成文本类型
help (numpy.genfromtxt) 用于查看帮助文档:
如果不想看 API 可以启动一个程序用 help 查看指令的详细用法
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str)
print(type(world_alcohol))
print(world_alcohol)
print(help(numpy.genfromtxt))
2. 构造 ndarray
numpy.array () 构造 ndarray
numpy.array() 中传入数组参数,可以是一维的也可以是二维三维的。numpy 会将其转变成 ndarray 的结构。
vector = numpy.array([1,2,3,4])
matrix = numpy.array([[1,2,3],[4,5,6]])
传入的参数必须是同一结构,不是同一结构将发生转换。
vector = numpy.array([1,2,3,4])
array([1, 2, 3, 4])
均为 int 类型
vector = numpy.array([1,2,3,4.0])
array([ 1., 2., 3., 4.])
转为浮点数类型
vector = numpy.array([1,2,''3'',4])
array([''1'', ''2'', ''3'', ''4''],dtype=''<U21'')
转为字符类型
利用 .shape 查看结构
能够了解 array 的结构,debug 时通过查看结构能够更好地了解程序运行的过程。
print(vector.shape)
print(matrix.shape)
(4,)
(2, 3)
利用 dtype 查看类型
vector = numpy.array([1,2,3,4])
vector.dtype
dtype(''int64'')
ndim 查看维度
一维
vector = numpy.array([1,2,3,4])
vector.ndim
1
二维
matrix = numpy.array([[1,2,3],
[4,5,6], [7,8,9]]) matrix.ndim 2
size 查看元素数量
matrix.size
9
3. 获取与计算
numpy 能使用切片获取数据
matrix = numpy.array([[1,2,3],
[4,5,6], [7,8,9]])
根据条件获取
numpy 能够依次比较 vector 和元素之间是否相同
vector = numpy.array([5, 10, 15, 20])
vector == 10 array([False, True, False, False], dtype=bool)
根据返回值获取元素
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10) print(equal_to_ten) print(vector[equal_to_ten]) [False True False False] [10]
进行运算之后获取
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
类型转换
将整体类型进行转换
vector = numpy.array([5, 10, 15, 20])
print(vector.dtype)
vector = vector.astype(str)
print(vector.dtype)
int64
<U21
求和
sum () 能够对 ndarray 进行各种求和操作,比如分别按行按列进行求和
matrix = numpy.array([[1,2,3],
[4,5,6], [7,8,9]]) print(matrix.sum()) print(matrix.sum(1)) print(matrix.sum(0)) 45 [ 6 15 24] [12 15 18]
sum (1) 是 sum (axis=1)) 的缩写,1 表示按照 x 轴方向求和,0 表示按照 y 轴方向求和
4. 常用函数
reshape
生成从 0-14 的 15 个数字,使用 reshape (3,5) 将其构造成一个三行五列的 array。
import numpy as np
arr = np.arange(15).reshape(3, 5) arr array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
zeros
生成指定结构的默认为 0. 的 array
np.zeros ((3,4))
array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
ones
生成一个三维的 array, 通过 dtype 指定类型
np.ones( (2,3,4), dtype=np.int32 )
array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]])
range
指定范围和数值间的间隔生成 array,注意范围包左不包右
np.arange(0,10,2)
array([0, 2, 4, 6, 8])
random 随机数
生成指定结构的随机数,可以用于生成随机权重
np.random.random((2,3))
array([[ 0.86166627, 0.37756207, 0.94265883], [ 0.9768257 , 0.96915312, 0.33495431]])
5. ndarray 运算
元素之间依次相减相减
a = np.array([10,20,30,40])
b = np.array(4) a - b array([ 6, 16, 26, 36])
乘方
a**2
array([ 100, 400, 900, 1600])
开根号
np.sqrt(B)
array([[ 1.41421356, 0. ],
[ 1.73205081, 2. ]])
e 求方
np.exp(B)
array([[ 7.3890561 , 1. ],
[ 20.08553692, 54.59815003]])
向下取整
a = np.floor(10*np.random.random((2,2)))
a
array([[ 0., 0.], [ 3., 6.]])
行列变换
a.T
array([[ 0., 3.],
[ 0., 6.]])
变换结构
a.resize(1,4)
a
array([[ 0., 0., 3., 6.]])
6. 矩阵运算
矩阵之间的运算
A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0], [3,4]] )
对应位置一次相乘
A*B
array([[2, 0],
[0, 4]])
矩阵乘法
print (A.dot(B))
print(np.dot(A,B))
[[5 4]
[3 4]]
横向相加
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2))) print(a) print(b) print(np.hstack((a,b))) [[ 2. 3.] [ 9. 3.]] [[ 8. 1.] [ 0. 0.]] [[ 2. 3. 8. 1.] [ 9. 3. 0. 0.]]
纵向相加
print(np.vstack((a,b)))
[[ 2. 3.]
[ 9. 3.]
[ 8. 1.] [ 0. 0.]]
矩阵分割
#横向分割
print( np.hsplit(a,3))
#纵向风格
print(np.vsplit(a,3))
7. 复制的区别
地址复制
通过 b = a 复制 a 的值,b 与 a 指向同一地址,改变 b 同时也改变 a。
a = np.arange(12)
b = a
print(a is b)
print(a.shape)
print(b.shape)
b.shape = (3,4)
print(a.shape)
print(b.shape)
True (12,) (12,) (3, 4) (3, 4)
复制值
通过 a.view () 仅复制值,当对 c 值进行改变会改变 a 的对应的值,而改变 c 的 shape 不改变 a 的 shape
a = np.arange(12)
c = a.view()
print(c is a)
c.shape = 2,6
c[0,0] = 9999 print(a) print(c) False [9999 1 2 3 4 5 6 7 8 9 10 11] [[9999 1 2 3 4 5] [ 6 7 8 9 10 11]]
完整拷贝
a.copy () 进行的完整的拷贝,产生一份完全相同的独立的复制
a = np.arange(12)
c = a.copy()
print(c is a)
c.shape = 2,6
c[0,0] = 9999 print(a) print(c) False [ 0 1 2 3 4 5 6 7 8 9 10 11] [[9999 1 2 3 4 5] [ 6 7 8 9 10 11]]
"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 的统计函数
目录
[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 周一
c++ 科学计算库
可具体查看下面链接,以获取较全面的科学计算库
http://blog.csdn.net/lee_cv/article/details/17374685
http://www.oschina.net/project/tag/239/Mathematics-computin?sort=view&lang=0&os=0
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 This helps keep away any conflict due to namespaces. But I have noticed that while the command below works the following does not Can someone please explain this? python numpy
|
||||||||
add a comment |
4 Answers
active oldest votes
up vote 13 down vote |
numpy is the top package name, and doing When you do In your above code: Here is the difference between
|
|||
add a comment |
up vote 7 down vote |
The When you import a module via the numpy package is bound to the local variable Thus, is equivalent to, When trying to understand this mechanism, it''s worth remembering that 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. 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 Finally, to round out my exposé, here are 2 interesting uses of the 1. long subimports 2. compatible APIs
|
||
add a comment |
up vote 1 down vote |
when you call the statement
|
||
add a comment |
up vote 1 down vote |
This is a language feature. This feature allows:
Notice however that Said that, when you run You receive an
|
||||||||
add a comment |
今天的关于Python 科学计算库 - Numpy和python的科学计算库的分享已经结束,谢谢您的关注,如果想了解更多关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、c++ 科学计算库、Difference between import numpy and import numpy as np的相关知识,请在本站进行查询。
本文标签: