numpy均匀分布_Numpy

import numpy as npscore=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]])

type(score)
numpy.ndarray

ndarray的属性

  • shape

    • ndim

    • size

  • dtype

    • itemsize

score.shape
(8, 5)
score.ndim
2
score.size
40
score.dtype
dtype('int32')
score.itemsize

4

ndarray的形状

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]]])
a #(2,3)
array([[1, 2, 3],

[4, 5, 6]])

b #(4, )
array([1, 2, 3, 4])
c #(2,2,3)
array([[[1, 2, 3],
[4, 5, 6]],
[[1, 2, 3],
[4, 5, 6]]])
a.shape
(2, 3)
b.shape
(4,)
c.shape

(2, 2, 3)

ndarray的类型

data = np.array([1.1,2.2,3.3])data
array([1.1, 2.2, 3.3])
data.dtype
dtype('float64')
np.array([1.1,2.2,3.3],dtype='float32')
array([1.1, 2.2, 3.3], dtype=float32)
np.array([1.1,2.2,3.3],dtype=np.int32)
array([1, 2, 3])

ndarray的基本操作

  • ndarray.方法()

  • np.函数名()

生成数组的方法

  • 生成0和1

    • np.zeros(shape)

    • np.ones(shape)

  • 从现有数组中生成

    • 深拷贝(新实体) np.array() np.copy()

    • 浅拷贝 np.asarray()

  • 生成固定范围的数组

    • np.linspace(0,10,100) 左闭右闭区间,等距离数

    • np.arange(a,b,c) 左闭右开区间,c是步长

  • 生成随机数

    • 均匀分布 np.random.uniform(low,high,size)

    • 正态分布 np.random.normal(μ,σ,size)

np.zeros(shape=(3,4),dtype="float32")
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]], dtype=float32)
np.ones(shape=[2,3],dtype=np.int64)
array([[1, 1, 1],
[1, 1, 1]], dtype=int64)
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]])
data1=np.array(score)data2=np.asarray(score)data3=np.copy(score)score[3,1]=10000
score
array([[   80,    89,    86,    67,    79],
[ 78, 97, 89, 67, 81],
[ 90, 94, 78, 67, 74],
[ 91, 10000, 90, 67, 69],
[ 76, 87, 75, 67, 86],
[ 70, 79, 84, 67, 84],
[ 94, 92, 93, 67, 64],
[ 86, 85, 83, 67, 80]])
data1#深拷贝
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]])
data2#浅拷贝
array([[   80,    89,    86,    67,    79],
[ 78, 97, 89, 67, 81],
[ 90, 94, 78, 67, 74],
[ 91, 10000, 90, 67, 69],
[ 76, 87, 75, 67, 86],
[ 70, 79, 84, 67, 84],
[ 94, 92, 93, 67, 64],
[ 86, 85, 83, 67, 80]])
data3#深拷贝
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]])
np.linspace(0,10,5)
array([ 0. ,  2.5,  5. ,  7.5, 10. ])
np.arange(0,10,5)
array([0, 5])
data1=np.random.uniform(low=-1,high=1,size=10000000)
import matplotlib.pyplot as pltplt.figure(figsize=(12,4),dpi=80)plt.hist(data1,1000,color='c')plt.show()

7e5663dc5f40536cb0c750f3159c1be7.png

data2=np.random.normal(loc=1.75,scale=0.1,size=10000000)
import matplotlib.pyplot as pltplt.figure(figsize=(12,4),dpi=80)plt.hist(data2,1000,color='c')plt.show()

16a90a96c8774e5a7506a0cab8443277.png

数组的索引,切片

案例:随机生成8只股票2周的交易日涨幅数据

stock_change=np.random.normal(loc=0,scale=1,size=(8,10))
stock_change
array([[-0.94758586, -0.99429974, -0.56441812,  1.21286308,  0.00678591,
1.5508201 , -0.72043692, -0.25372374, 0.77985115, 0.40609036],
[-0.18085306, -1.42451631, 1.33497567, -0.02862373, 0.51468414,
-1.3289159 , -0.06752783, 1.77760817, -0.84017325, -0.60968472],
[ 0.21934253, 0.24384749, 1.03611916, 0.1087004 , 0.10472653,
-0.90407768, 1.59917563, -1.65252822, 0.04966658, 0.70925202],
[-0.28025887, 0.05600375, -1.27855768, -0.21466243, 1.40471162,
0.25203635, -0.9567548 , 0.23570608, -0.27906228, -0.38646764],
[ 0.45008029, -0.21533407, -1.36918797, -1.13981864, 1.41289121,
1.85714097, 0.6217577 , -0.96777901, 0.97150175, -1.80307502],
[-0.12636312, 0.55092653, 0.07199359, -0.46115773, 0.53664362,
0.71278498, 3.26045744, -1.5051178 , -0.13831868, -0.22405879],
[ 0.24372249, -0.16969439, 1.52377637, -0.6580045 , -0.17173478,
-0.16013779, 0.60325849, 0.6959925 , -0.82993161, -0.31514202],
[ 0.62771902, -0.7630194 , 0.69394767, -0.11965727, -0.22044378,
-0.22508872, -0.30394071, -1.3931043 , 1.13333549, 0.4115906 ]])
#获取第一个股票前3个交易日的涨跌幅数据stock_change[0,:3]
array([-0.94758586, -0.99429974, -0.56441812])
a1 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])a1 #(2,2,3)
array([[[ 1,  2,  3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
a1[1,0,2]=100000a1
array([[[     1,      2,      3],
[ 4, 5, 6]],
[[ 7, 8, 100000],
[ 10, 11, 12]]])

形状修改

  • ndarray.reshape() 行列无转换,顺序不变,返回新的ndarray,原始数据没有改变

  • ndarray.resize() 无返回值,对原始ndarray进行修改

  • ndarray.T 行列转置,返回新的ndarray

stock_change
array([[-0.94758586, -0.99429974, -0.56441812,  1.21286308,  0.00678591,
1.5508201 , -0.72043692, -0.25372374, 0.77985115, 0.40609036],
[-0.18085306, -1.42451631, 1.33497567, -0.02862373, 0.51468414,
-1.3289159 , -0.06752783, 1.77760817, -0.84017325, -0.60968472],
[ 0.21934253, 0.24384749, 1.03611916, 0.1087004 , 0.10472653,
-0.90407768, 1.59917563, -1.65252822, 0.04966658, 0.70925202],
[-0.28025887, 0.05600375, -1.27855768, -0.21466243, 1.40471162,
0.25203635, -0.9567548 , 0.23570608, -0.27906228, -0.38646764],
[ 0.45008029, -0.21533407, -1.36918797, -1.13981864, 1.41289121,
1.85714097, 0.6217577 , -0.96777901, 0.97150175, -1.80307502],
[-0.12636312, 0.55092653, 0.07199359, -0.46115773, 0.53664362,
0.71278498, 3.26045744, -1.5051178 , -0.13831868, -0.22405879],
[ 0.24372249, -0.16969439, 1.52377637, -0.6580045 , -0.17173478,
-0.16013779, 0.60325849, 0.6959925 , -0.82993161, -0.31514202],
[ 0.62771902, -0.7630194 , 0.69394767, -0.11965727, -0.22044378,
-0.22508872, -0.30394071, -1.3931043 , 1.13333549, 0.4115906 ]])
stock_change.reshape((10,8)) #并没有进行行列互换,排列顺序无变化
array([[-0.94758586, -0.99429974, -0.56441812,  1.21286308,  0.00678591,
1.5508201 , -0.72043692, -0.25372374],
[ 0.77985115, 0.40609036, -0.18085306, -1.42451631, 1.33497567,
-0.02862373, 0.51468414, -1.3289159 ],
[-0.06752783, 1.77760817, -0.84017325, -0.60968472, 0.21934253,
0.24384749, 1.03611916, 0.1087004 ],
[ 0.10472653, -0.90407768, 1.59917563, -1.65252822, 0.04966658,
0.70925202, -0.28025887, 0.05600375],
[-1.27855768, -0.21466243, 1.40471162, 0.25203635, -0.9567548 ,
0.23570608, -0.27906228, -0.38646764],
[ 0.45008029, -0.21533407, -1.36918797, -1.13981864, 1.41289121,
1.85714097, 0.6217577 , -0.96777901],
[ 0.97150175, -1.80307502, -0.12636312, 0.55092653, 0.07199359,
-0.46115773, 0.53664362, 0.71278498],
[ 3.26045744, -1.5051178 , -0.13831868, -0.22405879, 0.24372249,
-0.16969439, 1.52377637, -0.6580045 ],
[-0.17173478, -0.16013779, 0.60325849, 0.6959925 , -0.82993161,
-0.31514202, 0.62771902, -0.7630194 ],
[ 0.69394767, -0.11965727, -0.22044378, -0.22508872, -0.30394071,
-1.3931043 , 1.13333549, 0.4115906 ]])
stock_change.resize((10,8))stock_change
array([[-0.94758586, -0.99429974, -0.56441812,  1.21286308,  0.00678591,
1.5508201 , -0.72043692, -0.25372374],
[ 0.77985115, 0.40609036, -0.18085306, -1.42451631, 1.33497567,
-0.02862373, 0.51468414, -1.3289159 ],
[-0.06752783, 1.77760817, -0.84017325, -0.60968472, 0.21934253,
0.24384749, 1.03611916, 0.1087004 ],
[ 0.10472653, -0.90407768, 1.59917563, -1.65252822, 0.04966658,
0.70925202, -0.28025887, 0.05600375],
[-1.27855768, -0.21466243, 1.40471162, 0.25203635, -0.9567548 ,
0.23570608, -0.27906228, -0.38646764],
[ 0.45008029, -0.21533407, -1.36918797, -1.13981864, 1.41289121,
1.85714097, 0.6217577 , -0.96777901],
[ 0.97150175, -1.80307502, -0.12636312, 0.55092653, 0.07199359,
-0.46115773, 0.53664362, 0.71278498],
[ 3.26045744, -1.5051178 , -0.13831868, -0.22405879, 0.24372249,
-0.16969439, 1.52377637, -0.6580045 ],
[-0.17173478, -0.16013779, 0.60325849, 0.6959925 , -0.82993161,
-0.31514202, 0.62771902, -0.7630194 ],
[ 0.69394767, -0.11965727, -0.22044378, -0.22508872, -0.30394071,
-1.3931043 , 1.13333549, 0.4115906 ]])
stock_change.T
array([[-0.94758586, -0.18085306,  0.21934253, -0.28025887,  0.45008029,
-0.12636312, 0.24372249, 0.62771902],
[-0.99429974, -1.42451631, 0.24384749, 0.05600375, -0.21533407,
0.55092653, -0.16969439, -0.7630194 ],
[-0.56441812, 1.33497567, 1.03611916, -1.27855768, -1.36918797,
0.07199359, 1.52377637, 0.69394767],
[ 1.21286308, -0.02862373, 0.1087004 , -0.21466243, -1.13981864,
-0.46115773, -0.6580045 , -0.11965727],
[ 0.00678591, 0.51468414, 0.10472653, 1.40471162, 1.41289121,
0.53664362, -0.17173478, -0.22044378],
[ 1.5508201 , -1.3289159 , -0.90407768, 0.25203635, 1.85714097,
0.71278498, -0.16013779, -0.22508872],
[-0.72043692, -0.06752783, 1.59917563, -0.9567548 , 0.6217577 ,
3.26045744, 0.60325849, -0.30394071],
[-0.25372374, 1.77760817, -1.65252822, 0.23570608, -0.96777901,
-1.5051178 , 0.6959925 , -1.3931043 ],
[ 0.77985115, -0.84017325, 0.04966658, -0.27906228, 0.97150175,
-0.13831868, -0.82993161, 1.13333549],
[ 0.40609036, -0.60968472, 0.70925202, -0.38646764, -1.80307502,
-0.22405879, -0.31514202, 0.4115906 ]])

类型修改

  • ndarray.astype(type)

  • ndarray.toString()

stock_change
array([[-0.94758586, -0.99429974, -0.56441812,  1.21286308,  0.00678591,
1.5508201 , -0.72043692, -0.25372374, 0.77985115, 0.40609036],
[-0.18085306, -1.42451631, 1.33497567, -0.02862373, 0.51468414,
-1.3289159 , -0.06752783, 1.77760817, -0.84017325, -0.60968472],
[ 0.21934253, 0.24384749, 1.03611916, 0.1087004 , 0.10472653,
-0.90407768, 1.59917563, -1.65252822, 0.04966658, 0.70925202],
[-0.28025887, 0.05600375, -1.27855768, -0.21466243, 1.40471162,
0.25203635, -0.9567548 , 0.23570608, -0.27906228, -0.38646764],
[ 0.45008029, -0.21533407, -1.36918797, -1.13981864, 1.41289121,
1.85714097, 0.6217577 , -0.96777901, 0.97150175, -1.80307502],
[-0.12636312, 0.55092653, 0.07199359, -0.46115773, 0.53664362,
0.71278498, 3.26045744, -1.5051178 , -0.13831868, -0.22405879],
[ 0.24372249, -0.16969439, 1.52377637, -0.6580045 , -0.17173478,
-0.16013779, 0.60325849, 0.6959925 , -0.82993161, -0.31514202],
[ 0.62771902, -0.7630194 , 0.69394767, -0.11965727, -0.22044378,
-0.22508872, -0.30394071, -1.3931043 , 1.13333549, 0.4115906 ]])
stock_change.astype("int64")
array([[ 0,  0,  0,  1,  0,  1,  0,  0,  0,  0],
[ 0, -1, 1, 0, 0, -1, 0, 1, 0, 0],
[ 0, 0, 1, 0, 0, 0, 1, -1, 0, 0],
[ 0, 0, -1, 0, 1, 0, 0, 0, 0, 0],
[ 0, 0, -1, -1, 1, 1, 0, 0, 0, -1],
[ 0, 0, 0, 0, 0, 0, 3, -1, 0, 0],
[ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, -1, 1, 0]], dtype=int64)
stock_change.tostring()
b'\xfe}\xc0\x94\x9fR\xee\xbf:\xf0\x01\xafM\xd1\xef\xbf\x93K>\x94\xb6\x0f\xe2\xbf\x8a\xf0\'\x1e\xe3g\xf3?\xf5\xb4b\x1f\x8b\xcb{?,\xbfS\xbc(\xd0\xf8?b\xfa|\xba\xd1\r\xe7\xbf\xc0[\x0f\x7f\x02=\xd0\xbf\xfbw\xdcc\x8a\xf4\xe8?\xaf_\xebhb\xfd\xd9?\xd6\xd7|c1&\xc7\xbfJ\x93\x85\x9d\xd1\xca\xf6\xbf\xe9\xa8\xb4s\x0f\\\xf5?\xc2ns4\x8aO\x9d\xbfd\xb4Z\xe1Jx\xe0?c%\xaeP=C\xf5\xbf\x12\xec\xa3\x0c\x81I\xb1\xbf\x83\xd2nC\x15q\xfc?=\xf2u\x04\xb3\xe2\xea\xbf\x1bV\x87\x88\x89\x82\xe3\xbf\xba\xe6G\x84j\x13\xcc?\xfc\x04V\x02e6\xcf?\x13\t\xde\xaf\xf1\x93\xf0?\xe2}4&\xca\xd3\xbb?\xe0\x8dn\xa9[\xcf\xba?j\x1f\xe2N4\xee\xec\xbf\x8a\x9a\x9f09\x96\xf9?\x02\xdeJm\xc1p\xfa\xbf\x10S}\xd5\xe5m\xa9?\x0c4pI1\xb2\xe6?R\x06I\xe9\xc2\xef\xd1\xbf\x0f+\x96\x00\x86\xac\xac?o\x82\xf0\xe4\xf8t\xf4\xbf\xc6\xe6\xfb\xef\x0ez\xcb\xbf4\xfco\xe4\xb2y\xf6?\xb6\xc2\x8d\r]!\xd0?\xb6\xf7\xf9?\xbc\x9d\xee\xbfc2k\xeb\x9d+\xce?\x88\xd5\t\x08(\xdc\xd1\xbf\xd2f\x93\xc3\xe2\xbb\xd8\xbf}\xc3\xe3\x8b\x1d\xce\xdc?\r\x9c*\x15\x11\x90\xcb\xbf\x1b\x131\xa61\xe8\xf5\xbf\xdd\'\xbfw\xb2

数组的去重

temp = np.array([[1,2,3,4],[3,4,5,6]])temp
array([[1, 2, 3, 4],
[3, 4, 5, 6]])
#Method 1np.unique(temp)
array([1, 2, 3, 4, 5, 6])
temp.flatten()#把n维数组拍扁
array([1, 2, 3, 4, 3, 4, 5, 6])
#Method 2set(temp.flatten())
{1, 2, 3, 4, 5, 6}

ndarray运算

逻辑运算

stock_change = np.random.normal(0,1,(8,10))stock_change
array([[-0.17045165, -1.27803466,  0.75530791, -0.73357337, -0.73708496,
0.75469309, 1.22124699, 0.31006652, -0.38246393, 0.50533674],
[ 0.88341943, 1.67608125, -1.3728071 , 0.2150275 , -0.14895326,
-0.03840289, 0.66371466, 1.03924355, -0.37438382, -0.71221502],
[ 1.48386376, -0.87462625, -0.10724436, -0.4754535 , -1.57845128,
-0.52896056, -2.0736274 , -1.21902016, -0.18348045, -0.92090409],
[-0.57683002, -1.14617721, -0.30599545, 0.98649245, 0.64485856,
0.65509851, -0.26665957, -1.03705705, -1.37634962, 0.60275735],
[-0.47940322, 3.46733753, 1.66901278, 0.32307921, -0.58092988,
-0.17438355, -0.02041639, 0.56601016, -0.6915273 , 0.40965903],
[ 0.69359999, 1.11660306, -0.93808192, -0.81192195, -0.39836896,
0.469936 , 0.74273805, 1.5657116 , 0.51146917, 0.44951733],
[-1.05450506, 0.63736471, -1.62599564, 0.23553031, -0.59277933,
-1.05085276, 0.47086831, 0.15136615, 0.915775 , -1.35913929],
[-0.72215364, 0.90160646, 1.07790517, -0.59392424, 0.2730316 ,
-0.15433431, -0.48680981, 1.15970411, 1.03098449, 0.06511026]])

逻辑判断

#如果涨跌大于0.5就标记为True,否则为Falsestock_change>0.5
array([[False, False,  True, False, False,  True,  True, False, False,
True],
[ True, True, False, False, False, False, True, True, False,
False],
[ True, False, False, False, False, False, False, False, False,
False],
[False, False, False, True, True, True, False, False, False,
True],
[False, True, True, False, False, False, False, True, False,
False],
[ True, True, False, False, False, False, True, True, True,
False],
[False, True, False, False, False, False, False, False, True,
False],
[False, True, True, False, False, False, False, True, True,
False]])

布尔索引¶

stock_change[stock_change>0.5] = 1.1stock_change
array([[-0.17045165, -1.27803466,  1.1       , -0.73357337, -0.73708496,
1.1 , 1.1 , 0.31006652, -0.38246393, 1.1 ],
[ 1.1 , 1.1 , -1.3728071 , 0.2150275 , -0.14895326,
-0.03840289, 1.1 , 1.1 , -0.37438382, -0.71221502],
[ 1.1 , -0.87462625, -0.10724436, -0.4754535 , -1.57845128,
-0.52896056, -2.0736274 , -1.21902016, -0.18348045, -0.92090409],
[-0.57683002, -1.14617721, -0.30599545, 1.1 , 1.1 ,
1.1 , -0.26665957, -1.03705705, -1.37634962, 1.1 ],
[-0.47940322, 1.1 , 1.1 , 0.32307921, -0.58092988,
-0.17438355, -0.02041639, 1.1 , -0.6915273 , 0.40965903],
[ 1.1 , 1.1 , -0.93808192, -0.81192195, -0.39836896,
0.469936 , 1.1 , 1.1 , 1.1 , 0.44951733],
[-1.05450506, 1.1 , -1.62599564, 0.23553031, -0.59277933,
-1.05085276, 0.47086831, 0.15136615, 1.1 , -1.35913929],
[-0.72215364, 1.1 , 1.1 , -0.59392424, 0.2730316 ,
-0.15433431, -0.48680981, 1.1 , 1.1 , 0.06511026]])

通用判断函数

  • np.all() 只要有一个False,返回False

  • np.any() 只要有一个True,返回True

stock_change[0:2,0:5]>0
array([[False, False,  True, False, False],
[ True, True, False, True, False]])
np.all(stock_change[0:2,0:5]>0)
False
np.any(stock_change[0:2,0:5]>0)
True

三元运算符 np.where(布尔值,True位置的值,False位置的值)

#判断前四个股票前四天的涨跌幅数据,大于0的置为1,否则为0temp = stock_change[:4,:4]temp
array([[-0.17045165, -1.27803466,  1.1       , -0.73357337],
[ 1.1 , 1.1 , -1.3728071 , 0.2150275 ],
[ 1.1 , -0.87462625, -0.10724436, -0.4754535 ],
[-0.57683002, -1.14617721, -0.30599545, 1.1 ]])
np.where(temp>0,1,0)
array([[0, 0, 1, 0],
[1, 1, 0, 1],
[1, 0, 0, 0],
[0, 0, 0, 1]])

复合逻辑运算

temp
array([[-0.17045165, -1.27803466,  1.1       , -0.73357337],
[ 1.1 , 1.1 , -1.3728071 , 0.2150275 ],
[ 1.1 , -0.87462625, -0.10724436, -0.4754535 ],
[-0.57683002, -1.14617721, -0.30599545, 1.1 ]])
np.logical_and(temp>0.5,temp<1)
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False]])
#判断前四个股票前四天的涨跌幅,大于0.5并且小于1,换为1,否则为0np.where(np.logical_and(temp>0.5,temp<1),1,0)
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
np.logical_or(temp>0.5,temp<-0.5)
array([[False,  True,  True,  True],
[ True, True, True, False],
[ True, True, False, False],
[ True, True, False, True]])
#判断前四个股票前四天的涨跌幅,大于0.5或者小于-0.5的,换为1,否则为0np.where(np.logical_or(temp>0.5,temp<-0.5),1,0)
array([[0, 1, 1, 1],
[1, 1, 1, 0],
[1, 1, 0, 0],
[1, 1, 0, 1]])

统计运算

  • 统计指标函数 min,max,mean,median,std(标准差),var

  • np.函数名

  • ndarray.方法名

股票涨跌幅统计运算

stock_change = np.random.normal(0,1,(8,10))temp = stock_change[:4,:3] #shape:(4,3)temp
array([[-0.7262681 , -1.00595494, -0.93030047],
[ 1.57692691, -0.09175566, -0.28297618],
[-0.62437275, -0.63660873, 0.30896392],
[ 1.08085307, 1.99583813, 0.3053529 ]])

最大、小值

temp.max()
1.9958381333944482
#按列求最大值temp.max(axis=0)
array([1.57692691, 1.99583813, 0.30896392])
#按行求最大值temp.max(axis=1)
array([-0.7262681 ,  1.57692691,  0.30896392,  1.99583813])
np.max(temp,axis=1)
array([-0.7262681 ,  1.57692691,  0.30896392,  1.99583813])

返回最大、小值位置

  • np.argmax(temp,axis=)

  • np.argmin(temp,axis=)

np.argmax(temp)
10
np.argmax(temp,axis=0)
array([1, 3, 2], dtype=int64)
np.argmax(temp,axis=1)
array([0, 0, 2, 1], dtype=int64)

数组间运算

  • 数组与数的运算

arr = np.array([[1,2,3,2,1,4],[5,6,1,2,3,1]]arr
array([[1, 2, 3, 2, 1, 4],
[5, 6, 1, 2, 3, 1]])
arr + 10
array([[11, 12, 13, 12, 11, 14],
[15, 16, 11, 12, 13, 11]])
arr / 2
array([[0.5, 1. , 1.5, 1. , 0.5, 2. ],
[2.5, 3. , 0.5, 1. , 1.5, 0.5]])
arr * 2
array([[ 2,  4,  6,  4,  2,  8],
[10, 12, 2, 4, 6, 2]])

数组与数组的运算

arr1 = np.array([[1,2,3,2,1,4],[5,6,1,2,3,1]])arr2 = np.array([[1,2,3,4],[3,4,5,6]]
arr1 #(2,6)
array([[1, 2, 3, 2, 1, 4],
[5, 6, 1, 2, 3, 1]])
arr2 #(2,4)
array([[1, 2, 3, 4],
[3, 4, 5, 6]])
arr1 + arr2 #不能被广播的broadcast
--------------------------------------------------------------------ValueError                                Traceback (most recent call last) in ----> 1 arr1 + arr2ValueError: operands could not be broadcast together with shapes (2,6) (2,4)

广播机制

  • 维度相等

  • shape(其中相对应的地方为1)

arr1 = np.array([[1,2,3,2,1,4],[5,6,1,2,3,1]])arr2 = np.array([[1],[3]])
arr1 #(2,6)
array([[1, 2, 3, 2, 1, 4],
[5, 6, 1, 2, 3, 1]])
arr2 #(2,1),维度相等,对应位置为1,能够进行运算
array([[1],
[3]])
arr1 + arr2
array([[2, 3, 4, 3, 2, 5],
[8, 9, 4, 5, 6, 4]])
arr1 * arr2
array([[ 1,  2,  3,  2,  1,  4],
[15, 18, 3, 6, 9, 3]])

矩阵运算

  • np.mat()将数组转换为矩阵

#ndarray存储矩阵data = np.array([[80,86],[82,80],[85,78],[90,90],[86,82],[82,90],[78,80],[92,94]])
data
array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
#matrix存储矩阵data_mat = np.mat([[80,86],[82,80],[85,78],[90,90],[86,82],[82,90],[78,80],[92,94]])
data_mat
matrix([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
data #(8,2)*(2,1)
array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
weights = np.array([[0.3],[0.7]])weights_mat=np.mat(weights)weights_mat
matrix([[0.3],
[0.7]])

矩阵相乘

  • np.matmul

  • np.dot

data
array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
weights
array([[0.3],
[0.7]])
#ndarraynp.matmul(data,weights)
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
#ndarraynp.dot(data,weights)
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
#ndarraydata @ weights
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
#matrixdata_mat * weights_mat
matrix([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])

合并、分割

合并

  • numpy.hstack(tup) 水平合并

  • numpy.vstack(tup) 垂直合并

  • numpy.concatenate((a1,a2),axis=0)

a = np.array((1,2,3))#[1,2,3]b = np.array((2,3,4))#[2,3,4]
np.vstack((a,b))
array([[1, 2, 3],
[2, 3, 4]])
a = np.array([[1],[2],[3]]) #(3,1)b = np.array([[2],[3],[4]]) #(3,1)
np.hstack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])
a = np.array([[1,2],[3,4]])b = np.array([[5,6]])
np.concatenate((a,b),axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
np.concatenate((a,b.T),axis=1)
array([[1, 2, 5],
[3, 4, 6]])

分割

  • numpy.split()

x = np.arange(9.0)x
array([0., 1., 2., 3., 4., 5., 6., 7., 8.])
np.split(x,3)
[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]
np.split(x,[3,5,6])
[array([0., 1., 2.]), array([3., 4.]), array([5.]), array([6., 7., 8.])]

IO操作与数据处理

  • Numpy读取

test = np.genfromtxt("test.csv",delimiter=',')test
array([[ nan,  nan],
[ 12., 2.],
[213., 412.],
[ 21., 434.],
[ 12., 433.],
[324., 223.]])

处理缺失值(后期用pandas处理缺失值更加方便)

  • 删除含有缺失值的样本

  • 替换/插补(平均值/中位数替换缺失值)

test[0,0]
nan
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值