1. 程式人生 > >Python基礎入門(Numpy)

Python基礎入門(Numpy)

#numpy 矩陣運算庫
import numpy as np
​
#常用的建立函式
#array 從列表或者元祖建立陣列
ar1 = np.array([1,2,3,4,5,6])
print(ar1)
​
#建立陣列0-10之間,步長為1
ar2 = np.arange(0,10,1)
print(ar2)
​
#將0-10分成5個元素的陣列
ar3 = np.linspace(0,10,5)
print(ar3)
​
#建立3行3列的陣列
ar4 = np.indices((3,3))
print(ar4)
​
#建立3行3列的隨機陣列
ar5 = np.random.rand(3,3)
print(ar5)
​
#建立3行3列的全為1的陣列
ar6 = np.ones((3,3),float)
print(ar6)
​
#建立3行3列全為0的陣列 np.zeros((3,3))
ar8 = np.zeros((3,3))
print(ar8)
​
#對角線為1,其他為0
ar7 = np.eye(3,3)
print(ar7)
[1 2 3 4 5 6]
[0 1 2 3 4 5 6 7 8 9]
[ 0.   2.5  5.   7.5 10. ]
[[[0 0 0]
  [1 1 1]
  [2 2 2]]

 [[0 1 2]
  [0 1 2]
  [0 1 2]]]
[[0.13282886 0.50496395 0.7787284 ]
 [0.80039057 0.40068522 0.1187407 ]
 [0.51833548 0.11563461 0.72263001]]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
In [32]:

#陣列的基礎屬性
​
#陣列軸的個數,秩
print(ar6.ndim)
​
#每個維度的大小
print(ar6.shape)
​
#元素的總數
print(ar6.size)
​
#元素資料的型別
print(ar6.dtype)
​
#每個元素的位元組數
print(ar6.itemsize)
​
#陣列元素緩衝區的地址
print(ar6.data)
​
#元素迭代器
print(ar6.flat)
2
(3, 3)
9
float64
8
<memory at 0x00000000053A47E0>
<numpy.flatiter object at 0x00000000065992D0>
In [16]:

#陣列的形態操作
ar7 = np.ones((3,3))
print(ar7)
#np.resize 修改陣列
ar7.resize(2,2)
print(ar7)
​
#對陣列進行將維度
ar8= np.ones((3,4))
print(ar8.flatten())
​
ar8= np.ones((3,4))
print(ar8.ravel())
​
#reshape
ar9= np.ones((3,4))
print(ar9)
print(ar9.reshape(2,6))
​
#astype 型別轉換
ar10 = np.arange(10,dtype=float).reshape(2,5)
print(ar10)
print(ar10.astype(np.int32))
​
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[1. 1.]
 [1. 1.]]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
[[1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]]
[[0. 1. 2. 3. 4.]
 [5. 6. 7. 8. 9.]]
[[0 1 2 3 4]
 [5 6 7 8 9]]
In [34]:

#橫向相加,垂直相加
a = np.arange(5)
b = np.arange(5,10)
print(a,b)
#按行拼接
ar = np.vstack((a,b))
print(ar)
#按列拼接
ar = np.hstack((a,b))
print(ar)
ar = np.stack((a,b),1)
print(ar)
ar = np.stack((a,b),0)
print(ar)
[0 1 2 3 4] [5 6 7 8 9]
[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4 5 6 7 8 9]
[[0 5]
 [1 6]
 [2 7]
 [3 8]
 [4 9]]
[[0 1 2 3 4]
 [5 6 7 8 9]]
In [28]:

#陣列的拆分
ar = np.arange(16).reshape(4,4)
print(ar)
#按列拆分
ar1 = np.hsplit(ar,2)
print(ar1)
#按行拆分
ar1 = np.vsplit(ar,2)
print(ar1)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]])]
[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]
In [44]:

#陣列的簡單運算
ar = np.arange(10)
print(ar)
print(ar*2)
print(ar/2)
print(ar+2)
print(ar//2)
print(ar**2)
[0 1 2 3 4 5 6 7 8 9]
[ 0  2  4  6  8 10 12 14 16 18]
[0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5]
[ 2  3  4  5  6  7  8  9 10 11]
[0 0 1 1 2 2 3 3 4 4]
[ 0  1  4  9 16 25 36 49 64 81]
import numpy as np
In [12]:

ar = np.arange(20)
#一維陣列切片
ar = np.arange(20)
print(ar[10])
print(ar[0:10])
print(ar[0:10:2])
10
[0 1 2 3 4 5 6 7 8 9]
[0 2 4 6 8]
In [35]:

#二維陣列的切片
ar = np.arange(16).reshape(4,4)
print(ar)
print(ar[0],ar[0,1])
print(ar[0:2])
print(ar[2:3])
print(ar[:4,2:])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[0 1 2 3] 1
[[0 1 2 3]
 [4 5 6 7]]
[[ 8  9 10 11]]
[[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]
In [38]:

#複製
#複製
br = ar.copy()
cr = ar
print(id(cr),id(br),id(ar))
92916432 92916352 92916432
In [41]:

_new = ar[ar > 5]
print(ar_new)
 #小作業
ar = np.arange(10).reshape(2,5)
ar_new = ar[ar > 5]
print(ar_new)
[6 7 8 9]
In [48]:

#隨機數  正態分佈
samples = np.random.normal(size=(100))
print(samples)
import matplotlib.pyplot as plt
% matplotlib inline
plt.hist(samples)
[-0.61227469 -0.36103937  0.28734323  0.63352977  1.13658948  1.15243507
  0.70852784  0.40504143  0.2712728  -0.27399682 -0.68746028  0.15983535
 -0.02442818 -1.71875477 -1.90132349 -1.52038551  2.04971197 -0.97968765
 -0.7398707   2.58470461 -0.06499621 -0.52215928  1.11283527  1.77455139
 -0.29510978 -0.3529555   0.25065179 -3.32293627  0.39312473  0.64401673
 -0.24950078  1.29350219 -0.10785972  0.82839765 -0.14743582  1.28908868
  1.38144382  1.1830453   1.55924739  1.00124522 -0.31871122  0.66595038
  0.37629854 -0.20744411 -0.44468078 -0.57489172  0.16270152  1.96241238
  1.69363656 -1.89301945 -0.80169326 -0.20132487 -0.4355692  -0.69024891
  0.58913554 -1.62332398 -0.46826792  0.12339841  0.12177624  0.60421575
 -0.00350087  1.28861972  0.10536274  1.36020219 -0.4496176   0.65117662
  1.03193623  0.82908065  0.18313433 -0.02936144 -2.60726463 -1.58424282
 -0.09221244  0.39805004 -0.46286613 -0.13994535  0.46165815  1.7918481
  1.34881523  0.49391726  0.10413002  1.03006967 -1.00628197  0.556884
 -0.86332625 -1.02390836 -0.46605733 -0.32249655  0.61331111  0.39275608
 -0.70372193  0.37659617 -0.39106357  0.22215355 -1.40161871  1.04150908
 -0.12478619  2.40395458  1.26045816 -0.45474683]
Out[48]:
(array([ 1.,  1.,  5.,  5., 17., 25., 21., 17.,  5.,  3.]),
 array([-3.32293627, -2.73217218, -2.1414081 , -1.55064401, -0.95987992,
        -0.36911583,  0.22164826,  0.81241234,  1.40317643,  1.99394052,
         2.58470461]),
 <a list of 10 Patch objects>)

#生成100個隨機浮點數   均勻分佈
a = np.random.rand(1000)
% matplotlib inline
plt.hist(a)
(array([110.,  93.,  89., 106.,  95., 105., 115.,  90.,  97., 100.]),
 array([5.47661376e-05, 1.00025836e-01, 1.99996906e-01, 2.99967976e-01,
        3.99939045e-01, 4.99910115e-01, 5.99881185e-01, 6.99852255e-01,
        7.99823325e-01, 8.99794394e-01, 9.99765464e-01]),
 <a list of 10 Patch objects>)

#生成一個浮點數或者浮點陣列  正態分佈
samples1 = np.random.randn(1000)
samples2 = np.random.randn(1000)

plt.scatter(samples1,samples2)
<matplotlib.collections.PathCollection at 0x112d2710>