计算A2+B3 其中A和B是一维数组,区分二者的区别
def pySum():
a = [0,1,2,3,4]
b = [9,8,7,6,5]
c = []
for i in range(len(a)):
c.append(a[i]**2 + b[i]**3)
return c
print(pySum())
import numpy as np #引入numpy库
def npSum():
a = np.array([0,1,2,3,4])
b = np.array([9,8,7,6,5])
c = a**2 + b**3
return c
print(npSum())
以下代码请在IPython平台运行
Ipython的%magic魔法指令
错误案例分析,未引入numpy库会报错
正确代码,请在IPython平台运行。
注意:Out行为输出结果
import numpy as np #引入NumPy库,模块的别名为np
a = np.array([[],\[]])
File "" , line 1
a = np.array([[],\[]])
^
SyntaxError: unexpected character after line continuation character
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a
Out[4]:
array([[0, 1, 2, 3, 4],
[9, 8, 7, 6, 5]])
print(a)
[[0 1 2 3 4]
[9 8 7 6 5]]
== ndarray实例,请在IPython平台运行==
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a.ndim
Out[7]: 2
a.shape
Out[8]: (2, 5)
a.size
Out[9]: 10
a.dtype
Out[10]: dtype('int32')
a.itemsize
Out[11]: 4
非同质的ndarray对象,请在IPython平台运行
x = np.array([[0,1,2,3,4],[9,8,7,6,]])
x.shape
Out[13]: (2,)
x.dtype
Out[14]: dtype('O') #ndarray数组可以由非同质对象构成
x
Out[15]: array([list([0, 1, 2, 3, 4]),
list([9, 8, 7, 6])], dtype=object) #非同质ndarray元素为对象类型
x.itemsize
Out[16]: 8
x.size
Out[17]: 2 #非同质ndarrat对象无法有效发挥Numpy优势,尽量避免使用
请在IPython平台运行
x = np.array([0,1,2,3]) #从列表类型创建
print(x)
[0 1 2 3]
x = np.array((4,5,6,7)) #从元组类型创建
print(x)
[4 5 6 7]
x = np.array([[1,2],[9,8],(0.1,0.2)]) #从列表和元组混合类型创建
print(x)
[[1. 2. ]
[9. 8. ]
[0.1 0.2]]
np.arange(10)
Out[26]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(x)
[[1. 2. ]
[9. 8. ]
[0.1 0.2]]
np.arange(10)
Out[28]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.ones((3,6))
Out[29]:
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
np.zeros((3,6),dtype=np.int32)
Out[30]:
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
np.eye(5)
Out[31]:
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
x = np.ones((2,3,4))
print(x)
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
x.shape
Out[34]: (2, 3, 4)
a = np.linspace(1,10,4)
a
Out[36]: array([ 1., 4., 7., 10.])
b = np.linspace(1,10,4,endpoint=False)
b
Out[38]: array([1. , 3.25, 5.5 , 7.75])
c = np.concatenate((a,b))
c
Out[40]: array([ 1. , 4. , 7. , 10. , 1. , 3.25, 5.5 , 7.75])
a = np.ones((2,3,4),dtype=np.int32)
a.reshape((3,8))
Out[42]:
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]])
a
Out[43]:
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]]])
a.resize((3,8))
a
Out[45]:
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]])
a.flatten()
Out[46]:
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])
a
Out[47]:
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]])
b = a.flatten()
b
Out[49]:
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])
new_a = a.astype(new_type)
a = np.ones((2,3,4),dtype=np.int)
s
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-52-ded5ba42480f> in <module>
----> 1 s
NameError: name 's' is not defined
a
Out[53]:
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]]])
b = a.astype(np.float)
b
Out[55]:
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.]]])
astype()方法一定会创建新的数组(原始数据的一个拷贝),即使两个类型一致
ls = a.tolist()
a = np.full((2,3,4),25,dtype=np.int32)
a
Out[57]:
array([[[25, 25, 25, 25],
[25, 25, 25, 25],
[25, 25, 25, 25]],
[[25, 25, 25, 25],
[25, 25, 25, 25],
[25, 25, 25, 25]]])
a.tolist()
Out[58]:
[[[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]],
[[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]]]
一维数组的索引和切片:与Python的列表类似
a = np.array([9,8,7,6,5])
a[2]
Out[60]: 7
a[1:4:2] #起始编号:终止编号(不含):步长,3元素冒号分割
Out[61]: array([8, 6])
多维数组的索引
a = np.arange(24).reshape((2,3,4))
a
Out[63]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
a[1,2,3]
Out[64]: 23
a[0,1,,2] #错误案例,注意规范代码编写,找一找哈
File "" , line 1
a[0,1,,2]
^
SyntaxError: invalid syntax
a[0,1,2]
Out[66]: 6
a[-1,-2,-3] #每个维度一个索引值,逗号分割
Out[67]: 17
多维数组的切片:
a = np.arange(24).reshape((2,3,4))
a
Out[69]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
a[:,1,-3]
Out[70]: array([ 5, 17])
a[:,1:3,:]
Out[71]:
array([[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
a[:,:,::2]
Out[72]:
array([[[ 0, 2],
[ 4, 6],
[ 8, 10]],
[[12, 14],
[16, 18],
[20, 22]]])
数组与标量之间的运算作用于数组的每一个元素
a =np.arange(24).reshape((2,3,4))
a
Out[74]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
a.mean()
Out[75]: 11.5
a = a/a.mean() #计算a与元素平均值的商
a
Out[77]:
array([[[0. , 0.08695652, 0.17391304, 0.26086957],
[0.34782609, 0.43478261, 0.52173913, 0.60869565],
[0.69565217, 0.7826087 , 0.86956522, 0.95652174]],
[[1.04347826, 1.13043478, 1.2173913 , 1.30434783],
[1.39130435, 1.47826087, 1.56521739, 1.65217391],
[1.73913043, 1.82608696, 1.91304348, 2. ]]])
Numpy一元函数实例
a = np.arange(24).reshape((2,3,4))
np.square(a) #注意数组是否被真实改变
Out[79]:
array([[[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]],
[[144, 169, 196, 225],
[256, 289, 324, 361],
[400, 441, 484, 529]]], dtype=int32)
a =np.sqrt(a) #注意数组是否被真实改变
a
Out[81]:
array([[[0. , 1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974, 2.64575131],
[2.82842712, 3. , 3.16227766, 3.31662479]],
[[3.46410162, 3.60555128, 3.74165739, 3.87298335],
[4. , 4.12310563, 4.24264069, 4.35889894],
[4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
np.modf(a)
Out[82]:
(array([[[0. , 0. , 0.41421356, 0.73205081],
[0. , 0.23606798, 0.44948974, 0.64575131],
[0.82842712, 0. , 0.16227766, 0.31662479]],
[[0.46410162, 0.60555128, 0.74165739, 0.87298335],
[0. , 0.12310563, 0.24264069, 0.35889894],
[0.47213595, 0.58257569, 0.69041576, 0.79583152]]]),
array([[[0., 1., 1., 1.],
[2., 2., 2., 2.],
[2., 3., 3., 3.]],
[[3., 3., 3., 3.],
[4., 4., 4., 4.],
[4., 4., 4., 4.]]]))
a = np.arange(24).reshape((2,3,4))
b = np.sqrt(a)
a
Out[85]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
b
Out[86]:
array([[[0. , 1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974, 2.64575131],
[2.82842712, 3. , 3.16227766, 3.31662479]],
[[3.46410162, 3.60555128, 3.74165739, 3.87298335],
[4. , 4.12310563, 4.24264069, 4.35889894],
[4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
np.masimum(a,b)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-87-8059f5768af0> in <module>
----> 1 np.masimum(a,b)
~\anaconda3\lib\site-packages\numpy\__init__.py in __getattr__(attr)
218 else:
219 raise AttributeError("module {!r} has no attribute "
--> 220 "{!r}".format(__name__, attr))
221
222 def __dir__():
AttributeError: module 'numpy' has no attribute 'masimum'
np.maximum(a,b) #运算结果为浮点数
Out[88]:
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[12., 13., 14., 15.],
[16., 17., 18., 19.],
[20., 21., 22., 23.]]])
a > b
Out[89]:
array([[[False, False, True, True],
[ True, True, True, True],
[ True, True, True, True]],
[[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]]])