reshape:
matrix = numpy.arange(15)? #創(chuàng)建一個從0開始的有15個元素的numpy array
new_matrix = matrix.reshape(5,3)?? #向量->矩陣
print (matrix)
print (new_matrix)
print (new_matrix.shape)?? #輸出行列數(shù)
print (new_matrix.size)? #輸出元素數(shù)
print (new_matrix.dtype)? #輸出type
創(chuàng)建全是0或1的矩陣:
matrix_zero = numpy.zeros((3,4),dtype = numpy.int32)
matrix_one = numpy.ones((3,4),dtype = numpy.int32)
print (matrix_zero)
print (matrix_one)
創(chuàng)建從10開始每個元素+5加到30的numpy array:
numpy.arange(10,30,5)? ?? #從10開始不包括30
創(chuàng)建隨機數(shù)(0~1)的矩陣
numpy.random.random((2,3))
創(chuàng)建從0到2π之間的且累加的共100個元素的矩陣:
from numpy import pi
numpy.linspace(0,2*pi,100)? (結(jié)果是1行100列)
矩陣轉(zhuǎn)置:
a = numpy.arange(4).reshape(2,-1)
print (a)
print ("----------")
print (a.T)
floor(向下取整)與ravel(矩陣->向量):
matrix = 10*numpy.random.random((3,4))
print (matrix)
print (numpy.floor(matrix))
print (matrix.ravel())
print (matrix.reshape(6,-1)) ? #向量->矩陣 闷畸,? -1是已知行數(shù)的情況下自動計算列數(shù)
矩陣拼接:
a = numpy.floor(10*numpy.random.random((2,2)))
b = numpy.floor(10*numpy.random.random((2,2)))
print (a)
print (b)
print ("---------")
print (numpy.hstack((a,b)))???? #橫著拼接
print (numpy.vstack((a,b)))???? #豎著拼接
矩陣分割:
a = numpy.arange(24).reshape(2,-1)
print (a)
print (numpy.hsplit(a,4))
print (numpy.hsplit(a,(3,4)))
b = a.T
print (b)
print ("------------")
print (numpy.vsplit(b,3))
print ("------------")
print (numpy.vsplit(b,(3,4)))