[云炬python3玩转机器学习笔记] 3-6Numpy数组和矩阵的合并和分割
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                [云炬python3玩转机器学习笔记] 3-6Numpy数组和矩阵的合并和分割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            import numpy as np
 
x=np.arange(10)
x
 
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 
X= np.arange(15).reshape(3,5)
X
 
array([[ 0,  1,  2,  3,  4],[ 5,  6,  7,  8,  9],[10, 11, 12, 13, 14]])
 
                        
                        
                        基本屬性
x.ndim # 查看數組維數 1 X.ndim 2 x.shape (10,) X.shape (3, 5) x.size 10 X.size 15numpy.array的數據訪問
x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x[0] 0 x[-1] 9 X array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]]) X[0][0] 0 X[(0,0)] 0 X[2,2] 12 x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x[0:5] array([0, 1, 2, 3, 4]) x[:5] array([0, 1, 2, 3, 4]) x[5:] array([5, 6, 7, 8, 9]) x[::2] #間隔 array([0, 2, 4, 6, 8]) x[::-1] #倒序 array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) X array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]]) X[:2,:3] array([[0, 1, 2],[5, 6, 7]]) X[:2][:3] #前兩行的前三列 array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) X[:2][:3] #X[:2]的前3行 array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) X[:2,::2] array([[0, 2, 4],[5, 7, 9]]) X[::-1,::-1] array([[14, 13, 12, 11, 10],[ 9, 8, 7, 6, 5],[ 4, 3, 2, 1, 0]]) X[0] array([0, 1, 2, 3, 4]) X[0,:] array([0, 1, 2, 3, 4]) X[0,:].ndim 1 X[:,0] array([ 0, 5, 10]) X[:,0].ndim 1 subX=X[:2,:3] subX array([[0, 1, 2],[5, 6, 7]]) subX[0,0]=100 subX array([[100, 1, 2],[ 5, 6, 7]]) X #子矩陣對原矩陣右影響,子矩陣引用的原矩陣的元素 array([[100, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[ 10, 11, 12, 13, 14]]) X[0,0]=0 X array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]]) subX array([[0, 1, 2],[5, 6, 7]]) subX =X[:2,:3].copy() #副本 subX array([[0, 1, 2],[5, 6, 7]]) subX[0,0]=100 subX array([[100, 1, 2],[ 5, 6, 7]]) X array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]])Reshape
x.shape (10,) x.ndim 1 x.reshape(2,5) array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) A= x.reshape(2,5) A array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) B= x.reshape(1,10) B #一維轉二維 array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) B.shape (1, 10) x.reshape(10,-1) #指定行數,智能計算列數 array([[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]) x.reshape(-1,10) array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) x.reshape(2,-1) array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) x.reshape(3,-1) ---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-54-27fb2acd3ab6> in <module> ----> 1 x.reshape(3,-1)ValueError: cannot reshape array of size 10 into shape (3,newaxis)合并操作
x=np.array([1,2,3]) y=np.array([3,2,1]) x array([1, 2, 3]) y array([3, 2, 1]) np.concatenate([x,y]) array([1, 2, 3, 3, 2, 1]) z=np.array([666,666,666]) np.concatenate([x,y,z]) array([ 1, 2, 3, 3, 2, 1, 666, 666, 666]) A=np.array([[1,2,3],[4,5,6]]) np.concatenate([A,A]) #沿行的方向拼接 array([[1, 2, 3],[4, 5, 6],[1, 2, 3],[4, 5, 6]]) np.concatenate([A,A], axis=1) #沿列的方向拼接 array([[1, 2, 3, 1, 2, 3],[4, 5, 6, 4, 5, 6]]) np.concatenate([A,z.reshape(1,-1)]) #拼接的矩陣維度必須相同 array([[ 1, 2, 3],[ 4, 5, 6],[666, 666, 666]]) A2 =np.concatenate([A,z.reshape(1,-1)]) #拼接的矩陣維度必須相同 A2 array([[ 1, 2, 3],[ 4, 5, 6],[666, 666, 666]]) np.vstack([A,z]) #在垂直的防線疊加,維度可以不同 array([[ 1, 2, 3],[ 4, 5, 6],[666, 666, 666]]) B=np.full((2,2), 100) B array([[100, 100],[100, 100]]) np.hstack([A,B])#在水平的防線疊加,維度可以不同 array([[ 1, 2, 3, 100, 100],[ 4, 5, 6, 100, 100]]) np.hstack([A,z]) ---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-78-54818211901c> in <module> ----> 1 np.hstack([A,z])<__array_function__ internals> in hstack(*args, **kwargs)~\anaconda3\lib\site-packages\numpy\core\shape_base.py in hstack(tup)344 return _nx.concatenate(arrs, 0)345 else: --> 346 return _nx.concatenate(arrs, 1)347 348 <__array_function__ internals> in concatenate(*args, **kwargs)ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)分割操作
x=np.arange(10) x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x1,x2,x3=np.split(x,[3,7]) #分割 x1 array([0, 1, 2]) x2 array([3, 4, 5, 6]) x3 array([7, 8, 9]) x1,x2=np.split(x,[5]) x1 array([0, 1, 2, 3, 4]) x2 array([5, 6, 7, 8, 9]) A=np.arange(16).reshape((4,4)) A array([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11],[12, 13, 14, 15]]) A1,A2=np.split(A,[2]) A1 array([[0, 1, 2, 3],[4, 5, 6, 7]]) A2 array([[ 8, 9, 10, 11],[12, 13, 14, 15]]) A1,A2=np.split(A,[2],axis=1) A1 array([[ 0, 1],[ 4, 5],[ 8, 9],[12, 13]]) A2 array([[ 2, 3],[ 6, 7],[10, 11],[14, 15]]) upper,lower=np.vsplit(A,[2]) upper array([[0, 1, 2, 3],[4, 5, 6, 7]]) lower array([[ 8, 9, 10, 11],[12, 13, 14, 15]]) left,right=np.hsplit(A,[2]) left array([[ 0, 1],[ 4, 5],[ 8, 9],[12, 13]]) right array([[ 2, 3],[ 6, 7],[10, 11],[14, 15]]) data=np.arange(16).reshape([4,4]) data array([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11],[12, 13, 14, 15]]) x,y=np.hsplit(data,[-1])#取下最后一列 x array([[ 0, 1, 2],[ 4, 5, 6],[ 8, 9, 10],[12, 13, 14]]) y array([[ 3],[ 7],[11],[15]]) y[:,0] #抽出所有行的第一列 array([ 3, 7, 11, 15])總結
以上是生活随笔為你收集整理的[云炬python3玩转机器学习笔记] 3-6Numpy数组和矩阵的合并和分割的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: D3.js、echar.js 前端必备大
- 下一篇: [云炬python3玩转机器学习笔记]
