一、随机生成八只股票两周的交易日涨幅数据
>>> import numpy as np
>>> stock_change=np.random.normal(loc=0,scale=1,size=(8,10))#这里用正态分布随机数
>>> stock_change
array([[-1.1343391 , 1.65608029, -0.68222626, -0.4288315 , 0.19948599,
-0.61926558, 0.21516773, 0.7767751 , -0.26167027, -0.43091756],
[-0.29634732, 0.13890544, 0.45824194, 0.3184083 , -1.87233628,
-0.02544499, -1.7498489 , -0.49005395, -0.21800702, -0.67063429],
[-1.9517504 , 0.15462343, -0.08247815, -0.1475269 , 0.0056942 ,
-1.12796392, 0.59864058, 2.00443335, -0.44038852, 0.72490669],
[ 0.26197967, 1.22252265, -0.29807856, 0.16792887, -1.40108093,
0.86089209, -0.95521308, 1.63804955, 0.57357237, 1.05756125],
[ 0.79324084, 3.37549996, -0.43210609, -0.16073839, 0.64017597,
-0.39662389, 1.8220427 , -0.15538366, 0.2457401 , 0.16393938],
[ 0.20220074, -1.58643882, 1.52836969, 0.35649477, -1.18283582,
-0.2627466 , -1.62134852, -0.62051081, -0.03113397, -0.42981914],
[ 2.24448065, 0.24069295, -2.69817108, -1.60745796, 0.81643552,
-0.91676992, 1.33626747, 0.20102252, 1.02987241, 0.57122122],
[-0.03591242, -1.51726478, -0.01696559, -1.96890186, 0.21840141,
-1.35285592, 0.52658141, -0.64944062, -1.84926774, 1.03067173]])
二、获取第一个股票的前三个交易日的涨跌幅数据
>>> stock_change[0,0:3]
array([-1.1343391 , 1.65608029, -0.68222626])#二维数组索引
tips:三维的数组怎么索引
>>> a1=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
>>> a1
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
>>> #(2,2,3)
>>> a1.shape
(2, 2, 3)
>>> #索引9
>>> a1[1,0,2]
9
>>> a1[1,0,2]=100#把这个位置的数改成100
>>> a1
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 100],
[ 10, 11, 12]]])
>>>
三、形状修改(把股票的股票行,日期列反过来变成日期行,股票列)
1.用ndarray.reshape转换,会发现只是把数据重新分割变成10行8列,并没有达到我们想要的效果,且返回新的数组,原始数据没有改
>>> stock_change
array([[-1.1343391 , 1.65608029, -0.68222626, -0.4288315 , 0.19948599,
-0.61926558, 0.21516773, 0.7767751 , -0.26167027, -0.43091756],
[-0.29634732, 0.13890544, 0.45824194, 0.3184083 , -1.87233628,
-0.02544499, -1.7498489 , -0.49005395, -0.21800702, -0.67063429],
[-1.9517504 , 0.15462343, -0.08247815, -0.1475269 , 0.0056942 ,
-1.12796392, 0.59864058, 2.00443335, -0.44038852, 0.72490669],
[ 0.26197967, 1.22252265, -0.29807856, 0.16792887, -1.40108093,
0.86089209, -0.95521308, 1.63804955, 0.57357237, 1.05756125],
[ 0.79324084, 3.37549996, -0.43210609, -0.16073839, 0.64017597,
-0.39662389, 1.8220427 , -0.15538366, 0.2457401 , 0.16393938],
[ 0.20220074, -1.58643882, 1.52836969, 0.35649477, -1.18283582,
-0.2627466 , -1.62134852, -0.62051081, -0.03113397, -0.42981914],
[ 2.24448065, 0.24069295, -2.69817108, -1.60745796, 0.81643552,
-0.91676992, 1.33626747, 0.20102252, 1.02987241, 0.57122122],
[-0.03591242, -1.51726478, -0.01696559, -1.96890186, 0.21840141,
-1.35285592, 0.52658141, -0.64944062, -1.84926774, 1.03067173]])
>>> stock_change.shape
(8, 10)
>>> stock_change.reshape((10,8))
array([[-1.1343391 , 1.65608029, -0.68222626, -0.4288315 , 0.19948599,
-0.61926558, 0.21516773, 0.7767751 ],
[-0.26167027, -0.43091756, -0.29634732, 0.13890544, 0.45824194,
0.3184083 , -1.87233628, -0.02544499],
[-1.7498489 , -0.49005395, -0.21800702, -0.67063429, -1.9517504 ,
0.15462343, -0.08247815, -0.1475269 ],
[ 0.0056942 , -1.12796392, 0.59864058, 2.00443335, -0.44038852,
0.72490669, 0.26197967, 1.22252265],
[-0.29807856, 0.16792887, -1.40108093, 0.86089209, -0.95521308,
1.63804955, 0.57357237, 1.05756125],
[ 0.79324084, 3.37549996, -0.43210609, -0.16073839, 0.64017597,
-0.39662389, 1.8220427 , -0.15538366],
[ 0.2457401 , 0.16393938, 0.20220074, -1.58643882, 1.52836969,
0.35649477, -1.18283582, -0.2627466 ],
[-1.62134852, -0.62051081, -0.03113397, -0.42981914, 2.24448065,
0.24069295, -2.69817108, -1.60745796],
[ 0.81643552, -0.91676992, 1.33626747, 0.20102252, 1.02987241,
0.57122122, -0.03591242, -1.51726478],
[-0.01696559, -1.96890186, 0.21840141, -1.35285592, 0.52658141,
-0.64944062, -1.84926774, 1.03067173]])
>>> stock_change.shape
(8, 10)#原始数组没有改
2.用ndarray.resize同ndarray.reshape,但没有返回新的数组且改变原始数组
3.用ndarray.T(转置)才能达到我们想要的效果。
>>> stock_change.T
array([[-1.1343391 , -0.29634732, -1.9517504 , 0.26197967, 0.79324084,
0.20220074, 2.24448065, -0.03591242],
[ 1.65608029, 0.13890544, 0.15462343, 1.22252265, 3.37549996,
-1.58643882, 0.24069295, -1.51726478],
[-0.68222626, 0.45824194, -0.08247815, -0.29807856, -0.43210609,
1.52836969, -2.69817108, -0.01696559],
[-0.4288315 , 0.3184083 , -0.1475269 , 0.16792887, -0.16073839,
0.35649477, -1.60745796, -1.96890186],
[ 0.19948599, -1.87233628, 0.0056942 , -1.40108093, 0.64017597,
-1.18283582, 0.81643552, 0.21840141],
[-0.61926558, -0.02544499, -1.12796392, 0.86089209, -0.39662389,
-0.2627466 , -0.91676992, -1.35285592],
[ 0.21516773, -1.7498489 , 0.59864058, -0.95521308, 1.8220427 ,
-1.62134852, 1.33626747, 0.52658141],
[ 0.7767751 , -0.49005395, 2.00443335, 1.63804955, -0.15538366,
-0.62051081, 0.20102252, -0.64944062],
[-0.26167027, -0.21800702, -0.44038852, 0.57357237, 0.2457401 ,
-0.03113397, 1.02987241, -1.84926774],
[-0.43091756, -0.67063429, 0.72490669, 1.05756125, 0.16393938,
-0.42981914, 0.57122122, 1.03067173]])
>>> stock_change.T.shape
(10, 8)
四、类型修改
用ndarray.astype(type)
>>> stock_change.astype("int32")#修改成整数
array([[-1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, -1, 0, -1, 0, 0, 0],
[-1, 0, 0, 0, 0, -1, 0, 2, 0, 0],
[ 0, 1, 0, 0, -1, 0, 0, 1, 0, 1],
[ 0, 3, 0, 0, 0, 0, 1, 0, 0, 0],
[ 0, -1, 1, 0, -1, 0, -1, 0, 0, 0],
[ 2, 0, -2, -1, 0, 0, 1, 0, 1, 0],
[ 0, -1, 0, -1, 0, -1, 0, 0, -1, 1]])
>>> stock_change.tostring()#本地序列化
b'\x0eX\xd5\xc2@&\xf2\xbf\xdcFz\x0bN\x7f\xfa?\xc6\xe9\x18*\xcc\xd4\xe5\xbf\x811\xed\xa9\xf9q\xdb\xbf|\xa1\x94\xc8\xc1\x88\xc9?\xa4\x84D\x0e\x06\xd1\xe3\xbfr\xc3Y\xb5\x9d\x8a\xcb?\x0fS\xa1sW\xdb\xe8?A\xd9\x95\xa94\xbf\xd0\xbfb\xc3\':\'\x94\xdb\xbf\xe7\x99\xd1\xbdZ\xf7\xd2\xbf\x02\xa3%I\xa7\xc7\xc1?\xa8\x18!\x04\xd6S\xdd?\xba\xf6\xd96\xcd`\xd4?\x07z\xa4\xe2\x16\xf5\xfd\xbfR!\xd7*@\x0e\x9a\xbf\xf7\x94\xf7\x8fa\xff\xfb\xbf\x9d3u=\x0b]\xdf\xbfqR\xb8o\xa7\xe7\xcb\xbf\x0f0\xd3\x0c\xd6u\xe5\xbf\xdf\xd4\x7f\xa0^:\xff\xbf\xb1nMW\xb3\xca\xc3?\x8a\xaa\xb8\xd1I\x1d\xb5\xbf\x02\xde@\\)\xe2\xc2\xbf\xd7`\xbcn\xceRw?\x15\xd3}\xe4#\x0c\xf2\xbf\xca\xbf\x03J\x10(\xe3?\x1b\x91\xa5Z\x14\t\x00@\xfdO\x93OS/\xdc\xbf\xe5\x80\xb9\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9\xe0?\xa9\x10+\xb17\xc8\xe4\xbf\xbc\xa7\xa6\xc5\x99\x96\xfd\xbf\xafv\x92\xa3\xa1}\xf0?'
>>>
五、去重
用numpy.unique
>>> temp=np.array([[1,2,3,4],[3,4,5,6]])
>>> temp
array([[1, 2, 3, 4],
[3, 4, 5, 6]])
'''>>> set(temp)#直接用set会报错,set只能用在一维数组
Traceback (most recent call last):
~~File "<pyshell#26>", line 1, in <module>
set(temp)
TypeError: unhashable type: 'numpy.ndarray'~~
'''
>>> temp
array([[1, 2, 3, 4],
[3, 4, 5, 6]])
>>> set(temp.flatten())
{1, 2, 3, 4, 5, 6}####当然你可以把它降维之后再用set
>>> np.unique(temp)#一般用这个
array([1, 2, 3, 4, 5, 6])