机器学习----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直到满足终止条件(如:损失函数或参数更新变化值小于某个阈值,或者训练次数达到设定阈值)