import numpy as np
numpy的基本操作
a = np.array([[1,2,3],[4,5,6]])
b = np.array([1,2,3,4])
c = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
print(a.shape)
print(b.shape)
print(c.shape)
(2, 3)
(4,)
(2, 2, 3)
a.dtype
dtype('int64')
a.itemsize
8
a.size
6
c.ndim
3
生成数组
d = np.array([[1,2,3],[4,5,6]],dtype=np.float32)
d.dtype
dtype('float32')
e=np.zeros([3,4],dtype="float32")
e
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]], dtype=float32)
e.dtype
dtype('float32')
e=np.zeros([3,4],dtype=np.float32)
e
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]], dtype=float32)
f=np.ones([3,4],dtype=np.int32)
f
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], dtype=int32)
# 从现有数组生成
score = np.array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])
score
array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])
score1=np.array(score)
score2=np.asarray(score)
score3=np.copy(score)
score[0][0]=100
score
array([[100, 89, 86, 67, 79],
[ 78, 97, 89, 67, 81],
[ 90, 94, 78, 67, 74],
[ 91, 91, 90, 67, 69],
[ 76, 87, 75, 67, 86],
[ 70, 79, 84, 67, 84],
[ 94, 92, 93, 67, 64],
[ 86, 85, 83, 67, 80]])
score2
array([[100, 89, 86, 67, 79],
[ 78, 97, 89, 67, 81],
[ 90, 94, 78, 67, 74],
[ 91, 91, 90, 67, 69],
[ 76, 87, 75, 67, 86],
[ 70, 79, 84, 67, 84],
[ 94, 92, 93, 67, 64],
[ 86, 85, 83, 67, 80]])
# 生成固定范围的数组
np.linspace(0, 100, 10)
array([ 0. , 11.11111111, 22.22222222, 33.33333333,
44.44444444, 55.55555556, 66.66666667, 77.77777778,
88.88888889, 100. ])
np.arange(10, 50, 15)
array([10, 25, 40])
a=np.random.uniform(0,100,1000000)
import matplotlib.pyplot as plt
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a)
plt.show()
<Figure size 1600x640 with 1 Axes>
b=np.random.normal(0,1,(100,100))
b
array([[-6.54070831e-01, -8.58237690e-01, 1.12840828e-03, ...,
-5.69480307e-02, 3.55998992e-01, -4.75176936e-01],
[ 6.76076814e-01, 1.47650320e+00, 4.61246907e-01, ...,
2.76736091e+00, 2.13077009e-01, -3.92740201e-01],
[ 6.14808294e-01, -1.56088314e-01, 8.86520985e-03, ...,
1.47150511e+00, 4.02858641e-01, 2.24224565e+00],
...,
[-1.26753533e+00, -1.46173745e-01, -1.42753958e+00, ...,
-8.50436456e-02, 5.38332925e-01, -1.05123949e+00],
[-7.50317584e-02, -4.60182497e-01, 2.16468102e-01, ...,
-1.38952087e-01, -4.67473834e-01, 1.08061128e+00],
[-2.42025812e-01, -9.83338435e-02, 1.08822672e-01, ...,
-1.26860857e+00, 3.39079875e-01, 2.23547985e-01]])
plt.figure(figsize=(20,8),dpi=80)
plt.hist(b,100)
plt.show()
数组的基本操作
# 创建符合正态分布的8只股票10天的涨跌幅数据
stock_change = np.random.normal(0, 1, (8, 10))
stock_change
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
0.2366924 , 0.91934989, 0.95733701, 0.7036584 , -0.25490962],
[-0.99235101, 0.70940651, -0.66934701, -0.33760595, 1.75578837,
-0.17527665, 1.127985 , 1.16480656, 0.60324222, 2.220063 ],
[-1.3326412 , -1.01831572, -1.09038797, 0.35145548, -1.52407572,
1.24380911, 0.53576183, -0.87476354, 0.94061172, -1.54695238],
[-0.6141568 , 0.34004292, 0.53421929, 0.93273841, -2.39181704,
0.73553393, -0.37974303, 0.12472411, 0.31243104, -0.09388522],
[ 0.63699391, 0.54513968, -1.45114066, 0.12302606, 0.8179262 ,
-0.98861413, -1.37164055, 0.04710424, 0.35452202, -1.77008598],
[ 0.05821616, -0.10157081, -0.0177392 , -0.80556969, -0.644111 ,
1.62028286, 0.12565102, 0.83290996, 1.06604483, 0.88367563],
[ 1.82724499, -0.625139 , -1.00688955, -1.31627874, -0.81109591,
0.96967537, 0.75956849, -0.80162364, 0.44827989, 2.05341272],
[-1.7788185 , 0.79750866, -0.29012385, 0.60888444, -1.05643106,
0.17126915, -2.24288296, 0.30024122, -0.44027555, 2.33346992]])
stock_change[0, 0:3]
array([-0.5889853 , -0.93535336, -0.0675299 ])
stock_change.reshape((10,8))
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
0.2366924 , 0.91934989, 0.95733701],
[ 0.7036584 , -0.25490962, -0.99235101, 0.70940651, -0.66934701,
-0.33760595, 1.75578837, -0.17527665],
[ 1.127985 , 1.16480656, 0.60324222, 2.220063 , -1.3326412 ,
-1.01831572, -1.09038797, 0.35145548],
[-1.52407572, 1.24380911, 0.53576183, -0.87476354, 0.94061172,
-1.54695238, -0.6141568 , 0.34004292],
[ 0.53421929, 0.93273841, -2.39181704, 0.73553393, -0.37974303,
0.12472411, 0.31243104, -0.09388522],
[ 0.63699391, 0.54513968, -1.45114066, 0.12302606, 0.8179262 ,
-0.98861413, -1.37164055, 0.04710424],
[ 0.35452202, -1.77008598, 0.05821616, -0.10157081, -0.0177392 ,
-0.80556969, -0.644111 , 1.62028286],
[ 0.12565102, 0.83290996, 1.06604483, 0.88367563, 1.82724499,
-0.625139 , -1.00688955, -1.31627874],
[-0.81109591, 0.96967537, 0.75956849, -0.80162364, 0.44827989,
2.05341272, -1.7788185 , 0.79750866],
[-0.29012385, 0.60888444, -1.05643106, 0.17126915, -2.24288296,
0.30024122, -0.44027555, 2.33346992]])
stock_change.T
array([[-0.5889853 , -0.99235101, -1.3326412 , -0.6141568 , 0.63699391,
0.05821616, 1.82724499, -1.7788185 ],
[-0.93535336, 0.70940651, -1.01831572, 0.34004292, 0.54513968,
-0.10157081, -0.625139 , 0.79750866],
[-0.0675299 , -0.66934701, -1.09038797, 0.53421929, -1.45114066,
-0.0177392 , -1.00688955, -0.29012385],
[-0.72403031, -0.33760595, 0.35145548, 0.93273841, 0.12302606,
-0.80556969, -1.31627874, 0.60888444],
[-0.86873673, 1.75578837, -1.52407572, -2.39181704, 0.8179262 ,
-0.644111 , -0.81109591, -1.05643106],
[ 0.2366924 , -0.17527665, 1.24380911, 0.73553393, -0.98861413,
1.62028286, 0.96967537, 0.17126915],
[ 0.91934989, 1.127985 , 0.53576183, -0.37974303, -1.37164055,
0.12565102, 0.75956849, -2.24288296],
[ 0.95733701, 1.16480656, -0.87476354, 0.12472411, 0.04710424,
0.83290996, -0.80162364, 0.30024122],
[ 0.7036584 , 0.60324222, 0.94061172, 0.31243104, 0.35452202,
1.06604483, 0.44827989, -0.44027555],
[-0.25490962, 2.220063 , -1.54695238, -0.09388522, -1.77008598,
0.88367563, 2.05341272, 2.33346992]])
stock_change.dtype
dtype('float64')
stock_change.astype(np.float32)
array([[-0.5889853 , -0.93535334, -0.0675299 , -0.7240303 , -0.86873674,
0.2366924 , 0.9193499 , 0.957337 , 0.7036584 , -0.25490963],
[-0.992351 , 0.7094065 , -0.669347 , -0.33760595, 1.7557883 ,
-0.17527665, 1.127985 , 1.1648066 , 0.6032422 , 2.220063 ],
[-1.3326412 , -1.0183157 , -1.090388 , 0.35145548, -1.5240757 ,
1.2438091 , 0.53576183, -0.87476355, 0.9406117 , -1.5469524 ],
[-0.6141568 , 0.34004292, 0.53421926, 0.9327384 , -2.391817 ,
0.73553395, -0.37974304, 0.12472411, 0.31243104, -0.09388521],
[ 0.6369939 , 0.5451397 , -1.4511406 , 0.12302607, 0.8179262 ,
-0.98861414, -1.3716406 , 0.04710424, 0.35452202, -1.7700859 ],
[ 0.05821616, -0.10157081, -0.0177392 , -0.8055697 , -0.644111 ,
1.6202829 , 0.12565102, 0.83290994, 1.0660448 , 0.88367563],
[ 1.827245 , -0.625139 , -1.0068896 , -1.3162787 , -0.8110959 ,
0.96967536, 0.7595685 , -0.80162364, 0.4482799 , 2.0534127 ],
[-1.7788185 , 0.79750866, -0.29012385, 0.60888445, -1.056431 ,
0.17126915, -2.242883 , 0.3002412 , -0.44027555, 2.3334699 ]],
dtype=float32)
数组的运算
# 逻辑运算
stock_change
stock_change1=np.copy(stock_change)
stock_change>0.5 # 逻辑判断, 如果涨跌幅大于0.5就标记为True 否则为False:
array([[False, False, False, False, False, False, True, True, True,
False],
[False, True, False, False, True, False, True, True, True,
True],
[False, False, False, False, False, True, True, False, True,
False],
[False, False, True, True, False, True, False, False, False,
False],
[ True, True, False, False, True, False, False, False, False,
False],
[False, False, False, False, False, True, False, True, True,
True],
[ True, False, False, False, False, True, True, False, False,
True],
[False, True, False, True, False, False, False, False, False,
True]])
stock_change1[stock_change1>0.5]=1 # 如果涨跌幅大于0.5就把值改为1
stock_change1
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
0.2366924 , 1. , 1. , 1. , -0.25490962],
[-0.99235101, 1. , -0.66934701, -0.33760595, 1. ,
-0.17527665, 1. , 1. , 1. , 1. ],
[-1.3326412 , -1.01831572, -1.09038797, 0.35145548, -1.52407572,
1. , 1. , -0.87476354, 1. , -1.54695238],
[-0.6141568 , 0.34004292, 1. , 1. , -2.39181704,
1. , -0.37974303, 0.12472411, 0.31243104, -0.09388522],
[ 1. , 1. , -1.45114066, 0.12302606, 1. ,
-0.98861413, -1.37164055, 0.04710424, 0.35452202, -1.77008598],
[ 0.05821616, -0.10157081, -0.0177392 , -0.80556969, -0.644111 ,
1. , 0.12565102, 1. , 1. , 1. ],
[ 1. , -0.625139 , -1.00688955, -1.31627874, -0.81109591,
1. , 1. , -0.80162364, 0.44827989, 1. ],
[-1.7788185 , 1. , -0.29012385, 1. , -1.05643106,
0.17126915, -2.24288296, 0.30024122, -0.44027555, 1. ]])
stock_change
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
0.2366924 , 0.91934989, 0.95733701, 0.7036584 , -0.25490962],
[-0.99235101, 0.70940651, -0.66934701, -0.33760595, 1.75578837,
-0.17527665, 1.127985 , 1.16480656, 0.60324222, 2.220063 ],
[-1.3326412 , -1.01831572, -1.09038797, 0.35145548, -1.52407572,
1.24380911, 0.53576183, -0.87476354, 0.94061172, -1.54695238],
[-0.6141568 , 0.34004292, 0.53421929, 0.93273841, -2.39181704,
0.73553393, -0.37974303, 0.12472411, 0.31243104, -0.09388522],
[ 0.63699391, 0.54513968, -1.45114066, 0.12302606, 0.8179262 ,
-0.98861413, -1.37164055, 0.04710424, 0.35452202, -1.77008598],
[ 0.05821616, -0.10157081, -0.0177392 , -0.80556969, -0.644111 ,
1.62028286, 0.12565102, 0.83290996, 1.06604483, 0.88367563],
[ 1.82724499, -0.625139 , -1.00688955, -1.31627874, -0.81109591,
0.96967537, 0.75956849, -0.80162364, 0.44827989, 2.05341272],
[-1.7788185 , 0.79750866, -0.29012385, 0.60888444, -1.05643106,
0.17126915, -2.24288296, 0.30024122, -0.44027555, 2.33346992]])
np.all(stock_change[0:2,0:5]>0)
False
np.any(stock_change[0:2,0:5]>0)
True
temp=stock_change[0:4,0:4]
np.where(temp>0,1,0)
array([[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 1, 1, 1]])
np.where(np.logical_and(temp > 0.5, temp < 1), 1, 0)
array([[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 1]])
np.where(np.logical_or(temp > 0.5, temp < -0.5), 1, 0)
array([[1, 1, 0, 1],
[1, 1, 1, 0],
[1, 1, 1, 0],
[1, 0, 1, 1]])
temp
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031],
[-0.99235101, 0.70940651, -0.66934701, -0.33760595],
[-1.3326412 , -1.01831572, -1.09038797, 0.35145548],
[-0.6141568 , 0.34004292, 0.53421929, 0.93273841]])
np.max(temp)
0.9327384109856901
np.max(temp,axis=1)
array([-0.0675299 , 0.70940651, 0.35145548, 0.93273841])
np.max(temp, axis=0)
array([-0.5889853 , 0.70940651, 0.53421929, 0.93273841])
np.std(temp,axis=1)
array([0.320048 , 0.63915226, 0.65926327, 0.56843862])
np.argmax(temp,axis=1)
array([2, 1, 3, 3])
数组间的运算
arr=np.array([[1,2,3,4],[5,6,7,8]])
arr
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
arr+10
array([[11, 12, 13, 14],
[15, 16, 17, 18]])
arr*10
array([[10, 20, 30, 40],
[50, 60, 70, 80]])
arr/10
array([[0.1, 0.2, 0.3, 0.4],
[0.5, 0.6, 0.7, 0.8]])
### 矩阵的计算
a = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
b = np.array([[0.7], [0.3]])
a_mat=np.mat(a)
a_mat
matrix([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
np.matmul(a,b)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
np.dot(a,b)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
a@b
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
b_mat=np.mat(b)
a_mat*b_mat
matrix([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])