NumPy 基礎

NumPy 提供一個N維數組對象類ndarray。它是SciPy和Scikit-learn等的數據結構,它支持快速線性代數計算。

創建數組

從list到一維數組

import numpy as np
list_of_ints = [1, 2, 3]
array_1 = np.array(list_of_ints)
array_1
Out[22]: 
array([1, 2, 3])
type(array_1)
Out[23]: 
numpy.ndarray
array_1.dtype
Out[24]: 
dtype('int64')
指定數據類型,控制內存大小

array_1.nbytes
Out[30]: 
24
array_1 = np.array(list_of_ints, dtype='int8')
array_1.nbytes
Out[32]: 
3
array_1b = array_1.astype('float32')
array_1b
Out[34]: 
array([ 1.,  2.,  3.], dtype=float32)
array_1b.nbytes
Out[35]: 
12

異構列表(數據類型不同)

取精度更高的類型

import numpy as np
complex_list = [1,2,3] + [1.1,2.2,3.3] + ['a','b','c']
array_2 = np.array(complex_list[:3])
print(array_2.dtype)
int64
array_2 = np.array(complex_list[:6])
print(array_2.dtype)
float64
array_2 = np.array(complex_list)
print(array_2.dtype)
|S32

二維數組

import numpy as np
a_list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
array_2d = np.array(a_list_of_lists)
array_2d
Out[43]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
改變數組的大小reshape()、resize()和shape

array_2d_1 = array_2d.reshape(1,9).copy()
array_2d_1
Out[46]: 
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
用NumPy函數生產數組

import numpy as np
ordinal_values = np.arange(9).reshape(3,3)
ordinal_values
Out[49]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
np.arange(9)[::-1]
Out[51]: 
array([8, 7, 6, 5, 4, 3, 2, 1, 0])
np.arange(9)[:-1]
Out[52]: 
array([0, 1, 2, 3, 4, 5, 6, 7])
np.zeros((3,3))
Out[53]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
np.ones((3,3))
Out[54]: 
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
np.eye(3)
Out[55]: 
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
np.linspace(start=0, stop=1, num=10)
Out[56]: 
array([ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
        0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ])
growth = np.logspace(start=0, stop=1, num=10, base=10.0)
growth
Out[58]: 
array([  1.        ,   1.29154967,   1.66810054,   2.15443469,
         2.7825594 ,   3.59381366,   4.64158883,   5.9948425 ,
         7.74263683,  10.        ])
np.random.normal(size=(3,3))
Out[59]: 
array([[ 1.35884452,  0.40577809,  1.63872935],
       [-2.36304665,  0.35944907,  0.58849736],
       [ 1.1812921 ,  1.12403039,  0.07716541]])

np.random.normal(loc=1.0, scale=3.0, size=(3,3))
Out[61]: 
array([[ 4.72521417,  4.08956132,  0.53066751],
       [ 1.23055934, -1.30784141, -2.92347621],
       [ 2.43437376,  0.54167036,  3.13887534]])
np.random.uniform(low=0.0, high=1.0, size=(3,3))
Out[62]: 
array([[ 0.99686131,  0.44151759,  0.19651618],
       [ 0.56318673,  0.1481807 ,  0.35430769],
       [ 0.76847528,  0.76751349,  0.09021368]])
從文件裝載數據生成數組

np.loadtxt('',delimiter=',',dtype=float)

從pandas獲取數據

df.values

Numpy計算

import numpy as np
a = np.arange(5).reshape(1,5)
a += 1
a*a
Out[66]: 
array([[ 1,  4,  9, 16, 25]])

array([[ 1,  4,  9, 16, 25]])
import numpy as np
a = np.arange(5).reshape(1,5) + 1
b = np.arange(5).reshape(5,1) + 1
a*b
Out[67]: 
array([[ 1,  2,  3,  4,  5],
       [ 2,  4,  6,  8, 10],
       [ 3,  6,  9, 12, 15],
       [ 4,  8, 12, 16, 20],
       [ 5, 10, 15, 20, 25]])
import math
%timeit -n 1 -r 3 [math .sqrt(i) for i in range(10**6)]
1 loop, best of 3: 146 ms per loop
%timeit -n 1 -r 3 np.sqrt(np.arange(10**6))
1 loop, best of 3: 5.73 ms per loop
矩陣運算
import numpy as np
M = np.arange(5*5, dtype=float).reshape(5,5)
coefs = np.array([1., 0.5, 0.5, 0.5, 0.5])
coefs_matrix = np.column_stack((coefs, coefs[::-1]))
print("M --- ")
print(M)
print("coefs")
print(coefs)
print("dot")
np.dot(M,coefs)
M --- 
[[  0.   1.   2.   3.   4.]
 [  5.   6.   7.   8.   9.]
 [ 10.  11.  12.  13.  14.]
 [ 15.  16.  17.  18.  19.]
 [ 20.  21.  22.  23.  24.]]
coefs
[ 1.   0.5  0.5  0.5  0.5]
dot
Out[80]: 
array([  5.,  20.,  35.,  50.,  65.])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章