1. 程式人生 > >python numpy 基本操作

python numpy 基本操作

首先匯入import模組

import numpy as np

建立list和matrix

vector = np.array([5, 10, 15, 20]) # list
matrix = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) # matrix
print vector
print matrix
#列印輸出
[ 5 10 15 20]
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]

求取矩陣的各種屬性

print(vector.shape) #列印list形狀
print(matrix.
shape) #列印矩陣形狀 #列印輸出 (4L,) (3L, 3L) numbers = np.array([1, 2, 3, 4]) numbers.dtype #輸出list的資料型別 #列印輸出 dtype('int32')

提取矩陣元素

vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])  
#列印輸出
[ 5 10 15]

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [
35, 40, 45] ]) print(matrix[:,1]) #列印輸出 [10 25 40] matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix[:,0:2]) [[ 5 10] [20 25] [35 40]] matrix = numpy.array([ [5, 10
, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix[1:3,0:2]) [[20 25] [35 40]]

==操作

vector = numpy.array([5, 10, 15, 20])
vector == 10
#列印輸出
array([False,  True, False, False], dtype=bool)

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
matrix == 25
array([[False, False, False],
       [False,  True, False],
       [False, False, False]], dtype=bool)

vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print equal_to_ten
print(vector[equal_to_ten])
#列印輸出
[False True False False]
[10]

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
second_column_25 = (matrix[:,1] == 25)
print second_column_25
print(matrix[second_column_25, :])
[False  True False]
[[20 25 30]]

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print equal_to_ten_and_five
[False False False False]

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print equal_to_ten_or_five
[True True False False]

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
vector[equal_to_ten_or_five] = 50
print(vector)
[50 50 15 20]

matrix = numpy.array([
            [5, 10, 15], 
            [20, 25, 30],
            [35, 40, 45]
         ])
second_column_25 = matrix[:,1] == 25
print second_column_25
matrix[second_column_25, 1] = 10
print matrix
[False  True False]
[[ 5 10 15]
 [20 10 30]
 [35 40 45]]

改變矩陣元素資料型別 astype

vector = numpy.array(["1", "2", "3"])
print vector.dtype
print vector
vector = vector.astype(float)
print vector.dtype
print vector
#列印輸出
|S1
['1' '2' '3']
float64
[ 1.  2.  3.]

sum, axis = 0 列相加, axis = 1 行相加

vector = numpy.array([5, 10, 15, 20])
vector.sum()
50

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=1)
array([ 30,  75, 120])

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=0)
array([60, 75, 90])

reshape(變形),shape(檢視陣列形狀),ndim(維度),dtype(檢視資料型別),size(總的元素個數),zeros(全0矩陣陣列),ones(全1矩陣陣列)

a = np.arange(15).reshape(3, 5) #arange生成0-14間隔為1的陣列
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
       
a.shape
(3, 5)

a.ndim
2

a.dtype.name
'int32'

a.size
15

np.zeros ((3,4))
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

np.ones( (2,3,4), dtype=np.int32 )
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

np.arange(a,b,n)生成間隔為n,資料範圍為[a,b)的序列

np.arange( 10, 30, 5 )
array([10, 15, 20, 25])

np.arange( 0, 2, 0.3 )
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

np.arange(12).reshape(4,3)
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

np.random.random(a,b)(生成0-1之間矩陣大小為(a,b)隨機數)
np.linspace(a,b,num)(生成a-b之間等間隔的num個數據)

np.random.random((2,3))
array([[ 0.40130659,  0.45452825,  0.79776512],
       [ 0.63220592,  0.74591134,  0.64130737]])
from numpy import pi
np.linspace( 0, 2*pi, 100 )

array([ 0.        ,  0.06346652,  0.12693304,  0.19039955,  0.25386607,
        0.31733259,  0.38079911,  0.44426563,  0.50773215,  0.57119866,
        0.63466518,  0.6981317 ,  0.76159822,  0.82506474,  0.88853126,
        0.95199777,  1.01546429,  1.07893081,  1.14239733,  1.20586385,
        1.26933037,  1.33279688,  1.3962634 ,  1.45972992,  1.52319644,
        1.58666296,  1.65012947,  1.71359599,  1.77706251,  1.84052903,
        1.90399555,  1.96746207,  2.03092858,  2.0943951 ,  2.15786162,
        2.22132814,  2.28479466,  2.34826118,  2.41172769,  2.47519421,
        2.53866073,  2.60212725,  2.66559377,  2.72906028,  2.7925268 ,
        2.85599332,  2.91945984,  2.98292636,  3.04639288,  3.10985939,
        3.17332591,  3.23679243,  3.30025895,  3.36372547,  3.42719199,
        3.4906585 ,  3.55412502,  3.61759154,  3.68105806,  3.74452458,
        3.8079911 ,  3.87145761,  3.93492413,  3.99839065,  4.06185717,
        4.12532369,  4.1887902 ,  4.25225672,  4.31572324,  4.37918976,
        4.44265628,  4.5061228 ,  4.56958931,  4.63305583,  4.69652235,
        4.75998887,  4.82345539,  4.88692191,  4.95038842,  5.01385494,
        5.07732146,  5.14078798,  5.2042545 ,  5.26772102,  5.33118753,
        5.39465405,  5.45812057,  5.52158709,  5.58505361,  5.64852012,
        5.71198664,  5.77545316,  5.83891968,  5.9023862 ,  5.96585272,
        6.02931923,  6.09278575,  6.15625227,  6.21971879,  6.28318531])

numpy數學運算

a = np.array( [20,30,40,50] )
b = np.arange( 4 )
print a 
print b
c = a-b
print c
b**2
print b**2
print a<35

[20 30 40 50]
[0 1 2 3]
[20 29 38 47]
[0 1 4 9]
[True True False False]


A = np.array( [[1,1],
               [0,1]] )
B = np.array( [[2,0],
               [3,4]] )
print A
[[1 1]
 [0 1]]
print B
[[2 0]
 [3 4]]
print A*B # 矩陣對應元素相乘
[[2 0]
 [0 4]]
print A.dot(B) # 矩陣相乘
[[5 4]
 [3 4]]
print np.dot(A, B) 
[[5 4]
 [3 4]]

np.sqrt (開根號),np.floor(向下取整),ravel(拉伸為一維),.T(轉置)

a = 10*np.random.random((3,4))
print a
[[ 2.82298425  7.14369753  8.69695365  3.25215523]
 [ 4.05086915  6.52158344  7.14745109  3.37805305]
 [ 5.08484091  7.04078148  7.7110627   4.13750478]]
 
a = np.floor(a)
print a
[[ 2.  7.  8.  3.]
 [ 4.  6.  7.  3.]
 [ 5.  7.  7.  4.]]
 
print a.ravel()
[ 2.  7.  8.  3.  4.  6.  7.  3