import numpy as nparray1 = np.arange(10)
array1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array1.shape
(10,)
array1.shape=2,5 #將array1分成2維陣列array1
array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
array1.reshape(1,10) #將array1陣列分成1維陣列一組有10個,大小不能改變
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
array1 = np.arange(10)array1.shape
(10,)
array1 = array1[np.newaxis,:]array1.shape
(1, 10)
array1 = np.arange(10)array1.shape
(10,)
array1 = array1[:,np.newaxis]array1.shape
(10, 1)
array1 = array1[:,np.newaxis,np.newaxis]array1.shape
(10, 1, 1, 1)
array1 = array1.squeeze()array1.shape
(10,)
array1.shape = 2,5array1
array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
array1.transpose() #將陣列陣列維度互換
array([[0, 5],[1, 6],
[2, 7],
[3, 8],
[4, 9]])
array1.t
array([[0, 5],[1, 6],
[2, 7],
[3, 8],
[4, 9]])
array1
array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
a = np.array([[123,456,678],[3214,456,134]])a
array([[ 123, 456, 678],[3214, 456, 134]])
b=np.array([[111,222,333],[999,888,777]])b
array([[111, 222, 333],[999, 888, 777]])
c = np.concatenate((a,b)) #將a,b陣列連線c
array([[ 123, 456, 678],[3214, 456, 134],
[ 111, 222, 333],
[ 999, 888, 777]])
c = np.concatenate((a,b),axis = 0) #按0維組合c
array([[ 123, 456, 678],[3214, 456, 134],
[ 111, 222, 333],
[ 999, 888, 777]])
c = np.concatenate((a,b),axis = 1) #按1維組合c
array([[ 123, 456, 678, 111, 222, 333],[3214, 456, 134, 999, 888, 777]])
c.shape
(2, 6)
np.vstack((a,b)) #垂直組合
array([[ 123, 456, 678],[3214, 456, 134],
[ 111, 222, 333],
[ 999, 888, 777]])
np.hstack((a,b)) #水平組合
array([[ 123, 456, 678, 111, 222, 333],[3214, 456, 134, 999, 888, 777]])
a
array([[ 123, 456, 678],[3214, 456, 134]])
a.flatten() #將陣列改為1維
array([ 123, 456, 678, 3214, 456, 134])
numpy 陣列的形狀
下面所說的陣列的形狀都是矩陣,因此除了用reshape方法改變陣列的形狀,還可以結合列表推導式,解析巢狀列表。巢狀列表解析講解 numpy import numpy as num 一維陣列 shuzu1 num.array 1,2,3,4,5 print shuzu1.shape 結果 5,一維陣列...
Numpy改變陣列的形狀
import numpy as np n np.arange 10 array 0,1,2,3,4,5,6,7,8,9 檢視陣列的大小 n.size 10 將陣列分為兩行五列 n.shape 2,5 array 0,1,2,3,4 5,6,7,8,9 顯示陣列的維度 n.shape 2,5 設定陣列...
day1 2 Numpy陣列相關操作
import numpy as np array 1,2,3,4,5 定義陣列 array 1,2,3,4,5 array1 np.array 1,2,3,4,5,6 type array1 獲取資料型別 numpy.ndarray num1 1 type num1 int array1.dtype...