機器學習----python基礎
阿新 • • 發佈:2022-04-04
機器學習----python基礎
-
shape函式
import numpy as np
x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])
# 輸出陣列的行和列數
print x.shape # (4, 3)
# 只輸出行數
print x.shape[0] # 4
# 只輸出列數
print x.shape[1] # 3
-
dot函式
import numpy as np np.dot(x,y) # 1、矩陣乘法,例如np.dot(X, X.T)。 # 2、點積,比如np.dot([1, 2, 3], [4, 5, 6]) = 1 * 4 + 2 * 5 + 3 * 6 = 32。
-
np.reshape
import numpy as np
a = np.array([[1,2,3], [4,5,6]])
np.reshape(a, 6)
# array([1, 2, 3, 4, 5, 6])
np.reshape(a, 6, order='F')
# array([1, 4, 2, 5, 3, 6])
np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2
# array([[1, 2],
# [3, 4],
# [5, 6]])
-
np.sum
- 求和函式
import numpy as np >>> a = np.sum([6,7,3]) >>> a 16 >>> a = np.sum([[0,4],[2,6]]) >>> a 12 >>> a = np.sum([[0,4],[2,6]],axis=0) >>> a array([ 2, 10]) >>> a = np.sum([[0,4],[2,6]],axis=1) >>> a array([4, 8]) >>> a = np.sum([[0,4],[2,6]],axis=-1) >>> a array([4, 8])
-
np.hstack
- np.hstack將引數元組的元素陣列按水平方向進行疊加
import numpy as np
arr1 = np.array([[1,3], [2,4] ])
arr2 = np.array([[1,4], [2,6] ])
res = np.hstack((arr1, arr2))
print (res)
#
# [[1 3 1 4]
# [2 4 2 6]]
arr1 = [1,2,3]
arr2 = [4,5]
arr3 = [6,7]
res = np.hstack((arr1, arr2,arr3))
print (res)
#
[1 2 3 4 5 6 7]
-
梯度下降演算法流程
- 隨機初始引數
- 確定學習率
- 求出損失函式對引數梯度
- 按照公式更新引數
- 重複3、4直到滿足終止條件(如:損失函式或引數更新變化值小於某個閾值,或者訓練次數達到設定閾值)