import numpy as np
score = np.array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
score
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
type(score)
numpy.ndarray
numpy与list 对比
import random
import time
python_list=[]
for i in range(100000000):
python_list.append(random.random())
ndarray_list=np.array(python_list)
len(ndarray_list)
100000000
t1 = time.time()
a = sum(python_list)
t2 = time.time()
d1 =t2- t1
t3 = time.time()
b=np.sum(ndarray_list)
t4 = time.time()
d2 = t4-t3
d1
0.3330507278442383
d2
0.06101274490356445
ndarray的属性
score
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
score.shape
(8, 5)
score.ndim
2
score.size
40
score.dtype
dtype('int32')
score.itemsize
4
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
array([[1, 2, 3],
[4, 5, 6]])
b
array([1, 2, 3, 4])
c
array([[[1, 2, 3],
[4, 5, 6]],
[[1, 2, 3],
[4, 5, 6]]])
a.shape
(2, 3)
a.reshape(4,-1)
array([[ 1.1 , 0.46486002],
[-1.61786434, 0.11906454],
[-0.59387934, 1.1 ],
[ 0.25657148, -1.95080153]])
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.float32)
array([1.1, 2.2, 3.3], dtype=float32)
生成数组的方法
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.int32)
array([[1, 1, 1],
[1, 1, 1]])
score
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
data1 = np.array(score)
data1
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
data2 = np.asarray(score)
data2
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
data3 = np.copy(score)
data3
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
score[3,1]
89
score[3,1] = 10000
score
array([[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 10000, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79]])
data1
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
data2
array([[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 10000, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79],
[ 80, 89, 86, 67, 79]])
data3
array([[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79],
[80, 89, 86, 67, 79]])
np.linspace(0,10,5)
array([ 0. , 2.5, 5. , 7.5, 10. ])
np.arange(0,10,5)
array([0, 5])
np.arange(0,11,5)
array([ 0, 5, 10])
均匀分布和正态分布
data1 = np.random.uniform(low=-1,high=1,size=1000000)
data1
array([ 0.60416086, 0.12537264, 0.87938927, ..., -0.27511781,
-0.8248633 , -0.11864051])
import matplotlib.pyplot as plt
plt.figure(figsize=(20,8),dpi=20)
plt.hist(data1,1000)
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fcdaa89f59556c1e6fabab06cf279fa6.png#pic_center)
data2=np.random.normal(loc=1.75,scale=0.1,size=1000000)
data2
array([1.5880087 , 1.91353129, 1.85618572, ..., 1.6376302 , 1.72127443,
1.92223794])
plt.figure(figsize=(20,8),dpi=20)
plt.hist(data2,1000)
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7f638541fdb1bfa409991dcab60c60e5.png#pic_center)
随机生成8只股票2周的交易日涨幅数据案例
stock_change = np.random.normal(loc=0,scale = 1,size=(8,10))
stock_change
array([[ 0.86535835, 1.32748828, 0.119368 , -1.45008918, 0.27322722,
0.11545142, 0.54136865, -2.26024006, -0.50250959, -0.56330494],
[ 0.05739357, -1.08807875, 1.83402645, 0.62271996, -1.0323532 ,
0.01519282, -0.57755933, 0.11212502, 0.25199048, 1.50675596],
[ 1.67717168, -2.1645031 , -1.0597855 , -1.22317734, 0.59855031,
-2.1363636 , 1.2269474 , 0.1051736 , 0.76156584, 0.95709548],
[-0.52282848, -0.27326557, 0.67962168, -1.3064003 , 0.46515538,
-0.41745562, -0.08448161, 1.18754525, -0.19445 , 1.28665287],
[-0.03962928, -0.71634026, -0.0513155 , -0.95734692, 0.37444264,
0.66686253, 0.71308877, -0.2676316 , 0.11894867, -0.16560205],
[-0.12458793, 0.89749669, 0.20515216, -0.28750428, 0.29443521,
-0.12536528, 0.0632028 , -1.65736249, 1.94054161, -0.09121584],
[ 0.2543928 , 0.20969513, -2.08981621, 0.73860983, -1.32672728,
-1.99945488, 1.0182702 , -0.62680182, 0.65736266, 2.40533051],
[ 1.38167085, -0.39879269, 1.21162789, 0.28650195, -0.69020696,
-2.03748556, 0.65675971, 1.64693366, -0.11617524, -0.30542202]])
stock_change[0,:3]
array([0.86535835, 1.32748828, 0.119368 ])
a1 = np.array([[[1, 2, 3], [4, 5, 6]], [[11, 22, 33], [44, 55, 66]]])
a1
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[11, 22, 33],
[44, 55, 66]]])
a1.shape
(2, 2, 3)
a1[1,0,2] = 100000
a1
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 11, 22, 100000],
[ 44, 55, 66]]])
stock_change
array([[ 0.86535835, 1.32748828, 0.119368 , -1.45008918, 0.27322722,
0.11545142, 0.54136865, -2.26024006, -0.50250959, -0.56330494],
[ 0.05739357, -1.08807875, 1.83402645, 0.62271996, -1.0323532 ,
0.01519282, -0.57755933, 0.11212502, 0.25199048, 1.50675596],
[ 1.67717168, -2.1645031 , -1.0597855 , -1.22317734, 0.59855031,
-2.1363636 , 1.2269474 , 0.1051736 , 0.76156584, 0.95709548],
[-0.52282848, -0.27326557, 0.67962168, -1.3064003 , 0.46515538,
-0.41745562, -0.08448161, 1.18754525, -0.19445 , 1.28665287],
[-0.03962928, -0.71634026, -0.0513155 , -0.95734692, 0.37444264,
0.66686253, 0.71308877, -0.2676316 , 0.11894867, -0.16560205],
[-0.12458793, 0.89749669, 0.20515216, -0.28750428, 0.29443521,
-0.12536528, 0.0632028 , -1.65736249, 1.94054161, -0.09121584],
[ 0.2543928 , 0.20969513, -2.08981621, 0.73860983, -1.32672728,
-1.99945488, 1.0182702 , -0.62680182, 0.65736266, 2.40533051],
[ 1.38167085, -0.39879269, 1.21162789, 0.28650195, -0.69020696,
-2.03748556, 0.65675971, 1.64693366, -0.11617524, -0.30542202]])
stock_change.shape
(8, 10)
stock_change.reshape(10,8)
array([[ 0.86535835, 1.32748828, 0.119368 , -1.45008918, 0.27322722,
0.11545142, 0.54136865, -2.26024006],
[-0.50250959, -0.56330494, 0.05739357, -1.08807875, 1.83402645,
0.62271996, -1.0323532 , 0.01519282],
[-0.57755933, 0.11212502, 0.25199048, 1.50675596, 1.67717168,
-2.1645031 , -1.0597855 , -1.22317734],
[ 0.59855031, -2.1363636 , 1.2269474 , 0.1051736 , 0.76156584,
0.95709548, -0.52282848, -0.27326557],
[ 0.67962168, -1.3064003 , 0.46515538, -0.41745562, -0.08448161,
1.18754525, -0.19445 , 1.28665287],
[-0.03962928, -0.71634026, -0.0513155 , -0.95734692, 0.37444264,
0.66686253, 0.71308877, -0.2676316 ],
[ 0.11894867, -0.16560205, -0.12458793, 0.89749669, 0.20515216,
-0.28750428, 0.29443521, -0.12536528],
[ 0.0632028 , -1.65736249, 1.94054161, -0.09121584, 0.2543928 ,
0.20969513, -2.08981621, 0.73860983],
[-1.32672728, -1.99945488, 1.0182702 , -0.62680182, 0.65736266,
2.40533051, 1.38167085, -0.39879269],
[ 1.21162789, 0.28650195, -0.69020696, -2.03748556, 0.65675971,
1.64693366, -0.11617524, -0.30542202]])
stock_change.resize(10,8)
stock_change.shape
(10, 8)
stock_change
array([[ 0.86535835, 1.32748828, 0.119368 , -1.45008918, 0.27322722,
0.11545142, 0.54136865, -2.26024006],
[-0.50250959, -0.56330494, 0.05739357, -1.08807875, 1.83402645,
0.62271996, -1.0323532 , 0.01519282],
[-0.57755933, 0.11212502, 0.25199048, 1.50675596, 1.67717168,
-2.1645031 , -1.0597855 , -1.22317734],
[ 0.59855031, -2.1363636 , 1.2269474 , 0.1051736 , 0.76156584,
0.95709548, -0.52282848, -0.27326557],
[ 0.67962168, -1.3064003 , 0.46515538, -0.41745562, -0.08448161,
1.18754525, -0.19445 , 1.28665287],
[-0.03962928, -0.71634026, -0.0513155 , -0.95734692, 0.37444264,
0.66686253, 0.71308877, -0.2676316 ],
[ 0.11894867, -0.16560205, -0.12458793, 0.89749669, 0.20515216,
-0.28750428, 0.29443521, -0.12536528],
[ 0.0632028 , -1.65736249, 1.94054161, -0.09121584, 0.2543928 ,
0.20969513, -2.08981621, 0.73860983],
[-1.32672728, -1.99945488, 1.0182702 , -0.62680182, 0.65736266,
2.40533051, 1.38167085, -0.39879269],
[ 1.21162789, 0.28650195, -0.69020696, -2.03748556, 0.65675971,
1.64693366, -0.11617524, -0.30542202]])
stock_change.resize(8,10)
stock_change
array([[ 0.86535835, 1.32748828, 0.119368 , -1.45008918, 0.27322722,
0.11545142, 0.54136865, -2.26024006, -0.50250959, -0.56330494],
[ 0.05739357, -1.08807875, 1.83402645, 0.62271996, -1.0323532 ,
0.01519282, -0.57755933, 0.11212502, 0.25199048, 1.50675596],
[ 1.67717168, -2.1645031 , -1.0597855 , -1.22317734, 0.59855031,
-2.1363636 , 1.2269474 , 0.1051736 , 0.76156584, 0.95709548],
[-0.52282848, -0.27326557, 0.67962168, -1.3064003 , 0.46515538,
-0.41745562, -0.08448161, 1.18754525, -0.19445 , 1.28665287],
[-0.03962928, -0.71634026, -0.0513155 , -0.95734692, 0.37444264,
0.66686253, 0.71308877, -0.2676316 , 0.11894867, -0.16560205],
[-0.12458793, 0.89749669, 0.20515216, -0.28750428, 0.29443521,
-0.12536528, 0.0632028 , -1.65736249, 1.94054161, -0.09121584],
[ 0.2543928 , 0.20969513, -2.08981621, 0.73860983, -1.32672728,
-1.99945488, 1.0182702 , -0.62680182, 0.65736266, 2.40533051],
[ 1.38167085, -0.39879269, 1.21162789, 0.28650195, -0.69020696,
-2.03748556, 0.65675971, 1.64693366, -0.11617524, -0.30542202]])
stock_change.T
array([[ 0.86535835, 0.05739357, 1.67717168, -0.52282848, -0.03962928,
-0.12458793, 0.2543928 , 1.38167085],
[ 1.32748828, -1.08807875, -2.1645031 , -0.27326557, -0.71634026,
0.89749669, 0.20969513, -0.39879269],
[ 0.119368 , 1.83402645, -1.0597855 , 0.67962168, -0.0513155 ,
0.20515216, -2.08981621, 1.21162789],
[-1.45008918, 0.62271996, -1.22317734, -1.3064003 , -0.95734692,
-0.28750428, 0.73860983, 0.28650195],
[ 0.27322722, -1.0323532 , 0.59855031, 0.46515538, 0.37444264,
0.29443521, -1.32672728, -0.69020696],
[ 0.11545142, 0.01519282, -2.1363636 , -0.41745562, 0.66686253,
-0.12536528, -1.99945488, -2.03748556],
[ 0.54136865, -0.57755933, 1.2269474 , -0.08448161, 0.71308877,
0.0632028 , 1.0182702 , 0.65675971],
[-2.26024006, 0.11212502, 0.1051736 , 1.18754525, -0.2676316 ,
-1.65736249, -0.62680182, 1.64693366],
[-0.50250959, 0.25199048, 0.76156584, -0.19445 , 0.11894867,
1.94054161, 0.65736266, -0.11617524],
[-0.56330494, 1.50675596, 0.95709548, 1.28665287, -0.16560205,
-0.09121584, 2.40533051, -0.30542202]])
stock_change.T.shape
(10, 8)
类型修改
stock_change.astype("int32")
array([[ 0, 1, 0, -1, 0, 0, 0, -2, 0, 0],
[ 0, -1, 1, 0, -1, 0, 0, 0, 0, 1],
[ 1, -2, -1, -1, 0, -2, 1, 0, 0, 0],
[ 0, 0, 0, -1, 0, 0, 0, 1, 0, 1],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, -1, 1, 0],
[ 0, 0, -2, 0, -1, -1, 1, 0, 0, 2],
[ 1, 0, 1, 0, 0, -2, 0, 1, 0, 0]])
stock_change.tostring()
C:\Users\PC\AppData\Local\Temp\ipykernel_27612\2974151914.py:1: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
stock_change.tostring()
b'\x92\x8b\xa9\xfc\x03\xb1\xeb?\x8e\x83\nZd=\xf5?\x82\x064\xb5\xe6\x8e\xbe?\x13\xf0e\xb6\x903\xf7\xbf?\xfe\x8c\t\x8e|\xd1?_z5r9\x8e\xbd?T-\x18W\xe4R\xe1?\xbd0\xb7\xbd\xf8\x14\x02\xc0b+ \xfd\x8e\x14\xe0\xbf\xdbP8\x13\x98\x06\xe2\xbf\x7fV\xca\x94\xb0b\xad?\xf4+aB\xc5h\xf1\xbf9E\xf8\x1d,X\xfd?\xcbn=jR\xed\xe3?<\xa3\xe0\xc9\x84\x84\xf0\xbf*\xb1\xdd\xa2i\x1d\x8f?\x19\xae\x92\xb4]{\xe2\xbf\xf8\x16O\xb09\xb4\xbc?\xfc"\x86\xa8\x9c \xd0?\xed\xc3*#\xac\x1b\xf8?\x1d8y\xf9\xb1\xd5\xfa?V\xf0\xdd\x00\xe7P\x01\xc0I\xee\x10\xa3\xe1\xf4\xf0\xbfj\xb2\xbbg"\x92\xf3\xbf\xdd^\x02\xfcR\'\xe3?\xa9\xeb\x1c\xcdE\x17\x01\xc0!\x9b\n\x98\x93\xa1\xf3?\xab\xb0D:\xa8\xec\xba?t>\xc2R\xbf^\xe8?oM#\xb4\x86\xa0\xee?\xf0\xd2i\xca\x02\xbb\xe0\xbf@\xe6\x00\xdb.}\xd1\xbf\x1e\x0eu\xf8u\xbf\xe5?\x84(\xfa\x00\x04\xe7\xf4\xbf\x86\x8bn\x0f\x1b\xc5\xdd?\xf5\x9e\x99\xc9\x97\xb7\xda\xbf[\x9c\xf8E\x96\xa0\xb5\xbf\xd9gEs/\x00\xf3?\x88\x8fm\xd0\xbc\xe3\xc8\xbf\x8d\xe0\x8fQ!\x96\xf4?0\xe1}\xf3IJ\xa4\xbf\xb1\x8c\x0fiB\xec\xe6\xbf\xe8\xa0@N\x06F\xaa\xbf\x0eS\xe9\xff\x95\xa2\xee\xbf\x81\x9c\xe0A\xde\xf6\xd7?j\\?\x17\xf0V\xe5?\xaf|\xa2\x8c\x9f\xd1\xe6?\x90\xb6\xd6H\xe0 \xd1\xbfg\x80\xc8\x87ks\xbe?g\xe6\'\xb9r2\xc5\xbfm\xa5y\xa3\xfe\xe4\xbf\xbf\xf9\xf1d\xf9J\xb8\xec?]o\x17\x0emB\xca?^\xbe\xa4Zxf\xd2\xbf\xe7&\xed\xc7\x06\xd8\xd2?W\xa1\xdd9\xf8\x0b\xc0\xbf\x93U\xf5\x00\x0f.\xb0?\x15\xc1\xb6\x87\x8e\x84\xfa\xbf\x9b\x85\xb3[u\x0c\xff?\xd4\x1f\xa5\xd0\xebY\xb7\xbf\x83@\xc7\xb7\xf8G\xd0?\xde\x01\xba4J\xd7\xca?@\xc0\xc4\x8f\xf1\xb7\x00\xc0v\xffH\x13\xb1\xa2\xe7?\xed}\xcaaF:\xf5\xbf\xe1\xf5\xbfg\xc4\xfd\xff\xbf~U\xb4\xb0\xd5J\xf0?\x9a\xa2\xe6\xb0\xc2\x0e\xe4\xbf\xbd\x8d\xdbk\x1d\t\xe5?\x90\x19\xee\xeb\x1d>\x03@\xf9|\xd5\xe4R\x1b\xf6?83b\xca\xd1\x85\xd9\xbf\r\xad\xc0\xec\xd3b\xf3?\xd2\xed^C\x0cV\xd2?\xf4X\x8b\xe5,\x16\xe6\xbfzYX:\xc5L\x00\xc0]#l\xee,\x04\xe5?g\xdf\x01\x1d\xd7Y\xfa?r\xf3]&\xa9\xbd\xbd\xbf\x8c\x0eN\xcf\x08\x8c\xd3\xbf'
temp = np.array([[1, 2, 3, 4], [3, 4, 5, 6]])
temp
array([[1, 2, 3, 4],
[3, 4, 5, 6]])
np.unique(temp)
array([1, 2, 3, 4, 5, 6])
set(temp.flatten())
{1, 2, 3, 4, 5, 6}
ndarray的运算
stock_change = np.random.normal(loc=0,scale=1,size=(8,10))
stock_change
array([[ 1.04751667, 0.46486002, -1.61786434, 0.11906454, -0.68363202,
0.44428458, -0.89320011, -0.53622568, -0.04982269, -1.37548725],
[-0.59387934, 1.03760708, 0.25657148, -1.95080153, 0.9333926 ,
0.3309002 , 0.34197949, -1.41123087, -1.2785237 , -0.99897667],
[-0.1673152 , -1.16306811, 0.83782136, 1.21692891, 0.12162377,
0.02345734, -1.19401328, 1.69296728, 0.52260451, -1.08154369],
[-1.28653761, -1.22515179, -0.22371267, 0.54140464, 0.86739731,
-0.45585076, -1.12682035, -1.36204496, 1.10002775, -0.44349517],
[-0.06124892, 0.23700134, 0.58331525, 0.35952943, 0.78413974,
0.93241559, 0.71465741, 1.03538431, 0.14607477, 0.20083777],
[-2.84961053, 0.88840185, 1.83522977, -0.1142927 , -1.44721851,
2.1728862 , -0.82129029, 0.44726567, -0.4111181 , 1.53027437],
[ 0.84042996, 0.21890664, 1.2651368 , -1.14924987, -0.24219501,
-0.32279737, 0.20622132, 0.63059689, 1.11997916, 0.82184066],
[-0.83045758, -0.51879743, 0.38548503, 0.77859703, -0.2464859 ,
-0.52022002, -0.70104983, 1.9108389 , 0.13960665, -0.05175743]])
stock_change>0.5
array([[ True, False, False, False, False, False, False, False, False,
False],
[False, True, False, False, True, False, False, False, False,
False],
[False, False, True, True, False, False, False, True, True,
False],
[False, False, False, True, True, False, False, False, True,
False],
[False, False, True, False, True, True, True, True, False,
False],
[False, True, True, False, False, True, False, False, False,
True],
[ True, False, True, False, False, False, False, True, True,
True],
[False, False, False, True, False, False, False, True, False,
False]])
stock_change[stock_change>0.5] = 1.1
stock_change
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454, -0.68363202,
0.44428458, -0.89320011, -0.53622568, -0.04982269, -1.37548725],
[-0.59387934, 1.1 , 0.25657148, -1.95080153, 1.1 ,
0.3309002 , 0.34197949, -1.41123087, -1.2785237 , -0.99897667],
[-0.1673152 , -1.16306811, 1.1 , 1.1 , 0.12162377,
0.02345734, -1.19401328, 1.1 , 1.1 , -1.08154369],
[-1.28653761, -1.22515179, -0.22371267, 1.1 , 1.1 ,
-0.45585076, -1.12682035, -1.36204496, 1.1 , -0.44349517],
[-0.06124892, 0.23700134, 1.1 , 0.35952943, 1.1 ,
1.1 , 1.1 , 1.1 , 0.14607477, 0.20083777],
[-2.84961053, 1.1 , 1.1 , -0.1142927 , -1.44721851,
1.1 , -0.82129029, 0.44726567, -0.4111181 , 1.1 ],
[ 1.1 , 0.21890664, 1.1 , -1.14924987, -0.24219501,
-0.32279737, 0.20622132, 1.1 , 1.1 , 1.1 ],
[-0.83045758, -0.51879743, 0.38548503, 1.1 , -0.2464859 ,
-0.52022002, -0.70104983, 1.1 , 0.13960665, -0.05175743]])
np.all(stock_change[0:2,0:5] >0)
False
np.any(stock_change[:5,:]>0)
True
temp = stock_change[:4,:4]
temp
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454],
[-0.59387934, 1.1 , 0.25657148, -1.95080153],
[-0.1673152 , -1.16306811, 1.1 , 1.1 ],
[-1.28653761, -1.22515179, -0.22371267, 1.1 ]])
np.where(temp>0,1,0)
array([[1, 1, 0, 1],
[0, 1, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 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]])
np.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.where(np.logical_or(temp>0.5,temp>-0.5),1,0)
array([[1, 1, 0, 1],
[0, 1, 1, 0],
[1, 0, 1, 1],
[0, 0, 1, 1]])
temp
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454],
[-0.59387934, 1.1 , 0.25657148, -1.95080153],
[-0.1673152 , -1.16306811, 1.1 , 1.1 ],
[-1.28653761, -1.22515179, -0.22371267, 1.1 ]])
temp.max()
1.1
np.max(temp,axis=1)
array([1.1, 1.1, 1.1, 1.1])
np.argmax(temp,axis=-1)
array([0, 1, 2, 3], dtype=int64)
数组间的运算
arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr+1
array([[2, 3, 4, 3, 2, 5],
[6, 7, 2, 3, 4, 2]])
arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr2 = np.array([[1], [3]])
arr1.shape
(2, 6)
arr2.shape
(2, 1)
arr1+arr2
array([[2, 3, 4, 3, 2, 5],
[8, 9, 4, 5, 6, 4]])
矩阵运算
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]])
data_mat = np.mat([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
type(data_mat)
numpy.matrix
weight = np.array([[0.3],[0.7]])
weight_mat=np.mat([[0.3],[0.7]])
np.matmul(data,weight)
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
np.dot(data,weight)
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
data_mat*weight_mat
matrix([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
data@weight
array([[84.2],
[80.6],
[80.1],
[90. ],
[83.2],
[87.6],
[79.4],
[93.4]])
合并与分割
a = stock_change[:2,0:4]
b = stock_change[4:6,0:4]
a
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454],
[-0.59387934, 1.1 , 0.25657148, -1.95080153]])
b
array([[-0.06124892, 0.23700134, 1.1 , 0.35952943],
[-2.84961053, 1.1 , 1.1 , -0.1142927 ]])
np.hstack((a,b))
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454, -0.06124892,
0.23700134, 1.1 , 0.35952943],
[-0.59387934, 1.1 , 0.25657148, -1.95080153, -2.84961053,
1.1 , 1.1 , -0.1142927 ]])
np.concatenate((a,b),axis=1)
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454, -0.06124892,
0.23700134, 1.1 , 0.35952943],
[-0.59387934, 1.1 , 0.25657148, -1.95080153, -2.84961053,
1.1 , 1.1 , -0.1142927 ]])
np.vstack((a,b))
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454],
[-0.59387934, 1.1 , 0.25657148, -1.95080153],
[-0.06124892, 0.23700134, 1.1 , 0.35952943],
[-2.84961053, 1.1 , 1.1 , -0.1142927 ]])
np.concatenate((a,b),axis=0)
array([[ 1.1 , 0.46486002, -1.61786434, 0.11906454],
[-0.59387934, 1.1 , 0.25657148, -1.95080153],
[-0.06124892, 0.23700134, 1.1 , 0.35952943],
[-2.84961053, 1.1 , 1.1 , -0.1142927 ]])