import numpy as np
arr=np.arange(4)*2
arr
array([0, 2, 4, 6])
np.savetxt('arr.txt',arr)
np.savetxt('arr.csv',arr)
np.loadtxt(
fname, 文件名
dtype=
comments=’#’,
delimiter=None, 分隔符
converters=None,
skiprows=0,
usecols=None, 读取哪几列
unpack=False, 是否按列读取
ndmin=0,
encoding=‘bytes’,
max_rows=None,
)
data=np.loadtxt('data.csv',dtype=object,delimiter=',',skiprows=1)
data
array([['AAPL', '28-01-2011', ' ', '344.17', '344.4', '333.53', '336.1',
'21144800'],
['AAPL', '31-01-2011', ' ', '335.8', '340.04', '334.3', '339.32',
'13473000'],
['AAPL', '01-02-2011', ' ', '341.3', '345.65', '340.98', '345.03',
'15236800'],
.....
['AAPL', '11-03-2011', ' ', '345.4', '352.32', '345', '351.99',
'16824200']], dtype=object)
data[::,-2:].T
array([['336.1', '339.32', '345.03', '344.32', '343.44', '346.5',
'351.88', '355.2', '358.16', '354.54', '356.85', '359.18',
'359.9', '363.13', '358.3', '350.56', '338.61', '342.62',
'342.88', '348.16', '353.21', '349.31', '352.12', '359.56',
'360', '355.36', '355.76', '352.47', '346.67', '351.99'],
['21144800', '13473000', '15236800', '9242600', '14064100',
'11494200', '17322100', '13608500', '17240800', '33162400',
'13127500', '11086200', '10149000', '17184100', '18949000',
'29144500', '31162200', '23994700', '17853500', '13572000',
'14395400', '16290300', '21521000', '17885200', '16188000',
'19504300', '12718000', '16192700', '18138800', '16824200']],
dtype=object)
data2=np.loadtxt('data.csv',delimiter=',',skiprows=1,usecols=(-2,-1),unpack=True)
data2
array([[3.36100e+02, 3.39320e+02, 3.45030e+02, 3.44320e+02, 3.43440e+02,
3.46500e+02, 3.51880e+02, 3.55200e+02, 3.58160e+02, 3.54540e+02,
3.56850e+02, 3.59180e+02, 3.59900e+02, 3.63130e+02, 3.58300e+02,
3.50560e+02, 3.38610e+02, 3.42620e+02, 3.42880e+02, 3.48160e+02,
3.53210e+02, 3.49310e+02, 3.52120e+02, 3.59560e+02, 3.60000e+02,
3.55360e+02, 3.55760e+02, 3.52470e+02, 3.46670e+02, 3.51990e+02],
[2.11448e+07, 1.34730e+07, 1.52368e+07, 9.24260e+06, 1.40641e+07,
1.14942e+07, 1.73221e+07, 1.36085e+07, 1.72408e+07, 3.31624e+07,
1.31275e+07, 1.10862e+07, 1.01490e+07, 1.71841e+07, 1.89490e+07,
2.91445e+07, 3.11622e+07, 2.39947e+07, 1.78535e+07, 1.35720e+07,
1.43954e+07, 1.62903e+07, 2.15210e+07, 1.78852e+07, 1.61880e+07,
1.95043e+07, 1.27180e+07, 1.61927e+07, 1.81388e+07, 1.68242e+07]])
data3=np.loadtxt('data.csv',dtype=object,delimiter=',',skiprows=1,usecols=[1])
data3
array(['28-01-2011', '31-01-2011', '01-02-2011', '02-02-2011',
'03-02-2011', '04-02-2011', '07-02-2011', '08-02-2011',
'09-02-2011', '10-02-2011', '11-02-2011', '14-02-2011',
'15-02-2011', '16-02-2011', '17-02-2011', '18-02-2011',
'22-02-2011', '23-02-2011', '24-02-2011', '25-02-2011',
'28-02-2011', '01-03-2011', '02-03-2011', '03-03-2011',
'04-03-2011', '07-03-2011', '08-03-2011', '09-03-2011',
'10-03-2011', '11-03-2011'], dtype=object)
close,amount=np.loadtxt('data.csv',delimiter=',',skiprows=1,usecols=(-2,-1),unpack=True)
close,amount
(array([336.1 , 339.32, 345.03, 344.32, 343.44, 346.5 , 351.88, 355.2 ,
358.16, 354.54, 356.85, 359.18, 359.9 , 363.13, 358.3 , 350.56,
338.61, 342.62, 342.88, 348.16, 353.21, 349.31, 352.12, 359.56,
360. , 355.36, 355.76, 352.47, 346.67, 351.99]),
array([21144800., 13473000., 15236800., 9242600., 14064100., 11494200.,
17322100., 13608500., 17240800., 33162400., 13127500., 11086200.,
10149000., 17184100., 18949000., 29144500., 31162200., 23994700.,
17853500., 13572000., 14395400., 16290300., 21521000., 17885200.,
16188000., 19504300., 12718000., 16192700., 18138800., 16824200.]))
np.average(close,weights=amount)
350.5895493532009
score=np.array([70,80,90])
weight=np.array([0.3,0.25,0.45])
np.average(score,weights=weight)
81.5
np.mean(close)
351.0376666666667
np.max(close)
363.13
np.min(close)
336.1
#np.max(close)-np.min(close)
np.ptp(close)
27.029999999999973
np.median(close)
352.055
np.var(close)
50.126517888888884
np.std(close)
7.080008325481608
np.sort(close)
array([336.1 , 338.61, 339.32, 342.62, 342.88, 343.44, 344.32, 345.03,
346.5 , 346.67, 348.16, 349.31, 350.56, 351.88, 351.99, 352.12,
352.47, 353.21, 354.54, 355.2 , 355.36, 355.76, 356.85, 358.16,
358.3 , 359.18, 359.56, 359.9 , 360. , 363.13])
np.prod([1,2,3,4])
24
mat1=np.mat([1,2,3])
mat1
matrix([[1, 2, 3]])
mat2=np.mat(np.arange(9).reshape(3,3))
mat2
matrix([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
np.random.rand(2,3)
array([[0.44419882, 0.82325373, 0.63462374],
[0.55088572, 0.10708544, 0.80662628]])
np.random.rand(6)
array([0.68211911, 0.03584266, 0.68636721, 0.36829578, 0.05455165,
0.9214382 ])
np.random.randn(3,2)
array([[ 0.56679901, -0.73834043],
[ 1.72355506, 0.75541262],
[ 0.04785598, 0.8975127 ]])
np.random.randint(1,10,8)
array([2, 1, 9, 8, 9, 4, 6, 4])
np.random.random([2,3])
array([[0.14919847, 0.43900093, 0.80230391],
[0.56655137, 0.31506227, 0.04710339]])
np.random.choice(list('abcde'),size=(2,2),replace=False)
array([['d', 'e'],
['b', 'c']], dtype='
np.random.choice([1,2,3,4],size=20,p=[0.6,0.2,0.1,0.1])
array([1, 3, 4, 2, 1, 1, 2, 3, 1, 1, 1, 1, 1, 4, 1, 1, 2, 1, 4, 3])