Update python_numpy.py

master
李中梁 2018-02-23 16:35:08 +08:00 committed by GitHub
parent 098c310b85
commit 7337fa7dc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 0 deletions

View File

@ -147,4 +147,60 @@ print(arr2_flat)
for i in arr2.flat: # 也可以用arr2.flatten()
print(i)
# 矩阵合并与分割
# 矩阵合并
arr1=np.array([1,2,3,6])
arr2=np.arange(4)
arr3=np.arange(2,16+1,2).reshape(2,4)
print(arr1)
print(arr2)
print(arr3)
arr_hor=np.hstack((arr1,arr2)) # 水平合并horizontal
arr_ver=np.vstack((arr1,arr3)) # 垂直合并vertical
print(arr_hor)
print(arr_ver)
# 矩阵分割
print('arr3: ',arr3)
print(np.split(arr3,4,axis=1)) # 将矩阵按列均分成4块
print(np.split(arr3,2,axis=0)) # 将矩阵按行均分成2块
print(np.hsplit(arr3,4)) # 将矩阵按列均分成4块
print(np.vsplit(arr3,2)) # 将矩阵按行均分成2块
print(np.array_split(arr3,3,axis=1)) # 将矩阵进行不均等划分
# numpy复制浅复制深复制
# 浅复制
arr1=np.array([3,1,2,3])
print(arr1)
a1=arr1
b1=a1
# 通过上述赋值运算arr1,a1,b1都指向了同一个地址浅复制
print(a1 is arr1)
print(b1 is arr1)
print(id(a1))
print(id(b1))
print(id(arr1))
# 会发现通过b1[0]改变内容arr1,a1,b1的内容都改变了
b1[0]=6
print(b1)
print(a1)
print(arr1)
# 深复制
arr2=np.array([3,1,2,3])
print('\n')
print(arr2)
b2=arr2.copy() # 深复制此时b2拥有不同于arr2的空间
a2=b2.copy()
# 通过上述赋值运算arr1,a1,b1都指向了不同的地址深复制
print(id(arr2))
print(id(a2))
print(id(b2))
# 此时改变b2,a2的值互不影响
b2[0]=1
a2[0]=2
print(b2)
print(a2)
print(arr2)