tile()至關於複製當前行元素或者列元素python
import numpy as np
m1 = np.array([1, 2, 3, 4])
# 行復制兩次,列複製一次到一個新數組中
print(np.tile(m1, (2, 1)))
print("===============")
# 行復制一次,列複製兩次到一個新數組中
print(np.tile(m1, (1, 2)))
print("===============")
# 行復制兩次,列複製兩次到一個新數組中
print(np.tile(m1, (2, 2)))複製代碼
輸出:數組
D:\Python\python.exe E:/ML_Code/test_code.py
[[1 2 3 4]
[1 2 3 4]]
===============
[[1 2 3 4 1 2 3 4]]
===============
[[1 2 3 4 1 2 3 4]
[1 2 3 4 1 2 3 4]]複製代碼
sum函數是對元素進行求和,對於二維數組以上則能夠根據參數axis進行分別對行和列進行求和,axis=0表明按列求和,axis=1表明行求和。bash
import numpy as np
m1 = np.array([1, 2, 3, 4])
# 元素逐個求和
print(sum(m1))
m2 = np.array([[6, 2, 2, 4], [1, 2, 4, 7]])
# 按列相加
print(m2.sum(axis=0))
# 按行相加
print(m2.sum(axis=1))複製代碼
輸出:dom
D:\Python\python.exe E:/ML_Code/test_code.py
10
[ 7 4 6 11]
[14 14]
Process finished with exit code 0複製代碼
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)
b = np.reshape(a, 6)
print(b)
# -1是根據數組大小進行維度的自動推斷
c = np.reshape(a, (3, -1)) # 爲指定的值將被推斷出爲2
print(c)複製代碼
輸出:函數
D:\python-3.5.2\python.exe E:/ML_Code/test_code.py
(2, 3)
---
[1 2 3 4 5 6]
---
[[1 2]
[3 4]
[5 6]]複製代碼
import numpy as np
# 建立一個給定類型的數組,將其填充在一個均勻分佈的隨機樣本[0, 1)中
print(np.random.rand(3))
print(np.random.rand(2, 2))複製代碼
輸出:學習
D:\python-3.5.2\python.exe E:/ML_Code/test_code.py
[ 0.03568079 0.68235136 0.64664722]
---
[[ 0.43591417 0.66372315]
[ 0.86257381 0.63238434]]複製代碼
zip() 函數用於將可迭代的對象做爲參數,將對象中對應的元素打包成一個個元組,而後返回由這些元組組成的列表。
若是各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同,利用 * 號操做符,能夠將元組解壓爲列表。ui
import numpy as np
a1 = np.array([1, 2, 3, 4])
a2 = np.array([11, 22, 33, 44])
z = zip(a1, a2)
print(list(z))複製代碼
輸出:spa
D:\Python\python.exe E:/ML_Code/test_code.py
[(1, 11), (2, 22), (3, 33), (4, 44)]
Process finished with exit code 0複製代碼
注意點:在python 3之後的版本中zip()是可迭代對象,使用時必須將其包含在一個list中,方便一次性顯示出全部結果。不然會報以下錯誤:
<zip object at 0x01FB2E90>複製代碼
import numpy as np
# 生成隨機矩陣
myRand = np.random.rand(3, 4)
print(myRand)
# 生成單位矩陣
myEye = np.eye(3)
print(myEye)
from numpy import *
# 矩陣全部元素求和
myMatrix = mat([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(sum(myMatrix))
# 計算矩陣的秩
print(linalg.det(myMatrix))
# 計算矩陣的逆
print(linalg.inv(myMatrix))複製代碼
注意:code
from numpy import *
import numpy as np
vector1 = mat([[1, 2], [1, 1]])
vector2 = mat([[1, 2], [1, 1]])
vector3 = np.array([[1, 2], [1, 1]])
vector4 = np.array([[1, 2], [1, 1]])
# Python自帶的mat矩陣的運算規則是二者都按照矩陣乘法的規則來運算
print(vector1 * vector2)
# Python自帶的mat矩陣的運算規則是二者都按照矩陣乘法的規則來運算
print(dot(vector1, vector2))
# numpy乘法運算中"*"是數組元素逐個計算
print(vector3 * vector4)
# numpy乘法運算中dot是按照矩陣乘法的規則來運算
print(dot(vector3, vector4))複製代碼
輸出:cdn
D:\python-3.5.2\python.exe D:/PyCharm/py_base/py_numpy.py
[[3 4]
[2 3]]
---
[[3 4]
[2 3]]
---
[[1 4]
[1 1]]
---
[[3 4]
[2 3]]複製代碼
from numpy import *
# 計算兩個向量的歐氏距離
vector1 = mat([1, 2])
vector2 = mat([3, 4])
print(sqrt((vector1 - vector2) * ((vector1 - vector2).T)))複製代碼
from numpy import *
import numpy as np
arrayOne = np.array([[1, 2, 3, 4, 5], [7, 4, 3, 3, 3]])
# 計算第一列的平均數
mv1 = mean(arrayOne[0])
# 計算第二列的平均數
mv2 = mean(arrayOne[1])
# 計算第一列的標準差
dv1 = std(arrayOne[0])
# 計算第二列的標準差
dv2 = std(arrayOne[1])
print(mv1)
print(mv2)
print(dv1)
print(dv2)複製代碼
歡迎你們關注個人我的公衆號