numpy建立ndarray對象的三種方法dom
1.1.list轉化
In [8]: import numpy as np In [9]: a = [1,2,3,4] In [10]: x1 = np.array(a) In [11]: x1 Out[11]: array([1, 2, 3, 4]) In [12]: type(x1) Out[12]: numpy.ndarray
1.2.numpy內的函數生存
In [13]: x2 = np.arange(11) In [14]: x2 Out[14]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1.3.文件生存
01.csv文件以下函數
使用numpy的loadtxt方法打開post
- 第一個參數:文件名
- delimiter:以什麼分隔
- skiprows:跳過的行
- usecols:使用哪幾列
- unpack:默認False,表示是否把列分開
x = np.loadtxt('01.csv',delimiter=',',skiprows=1,usecols=(1,4,6),unpack=False)
顯示結果spa
In [18]: x.shape
Out[18]: (242, 3)
把每列分開保存3d
In [24]: open,close,volume = np.loadtxt('01.csv',delimiter=',',skiprows=1,usecols=(1,4,6),unpack=True)
結果:code
In [26]: open.shape
Out[26]: (242,)
1.4.numpy的經常使用函數
In [36]: c = np.random.randint(1,100,10) In [37]: c Out[37]: array([44, 26, 40, 87, 32, 82, 20, 70, 62, 14]) In [38]: c.min() Out[38]: 14 In [39]: c.max() Out[39]: 87 In [40]: c.mean() Out[40]: 47.7 In [43]: y = np.sort(c) In [44]: y Out[44]: array([14, 20, 26, 32, 40, 44, 62, 70, 82, 87])