机器学习----python基础

机器学习----python基础

  1. 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
  1. 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。
  1. 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]])
  1. 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])

  1. 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]
  1. 梯度下降算法流程

  • 随机初始参数
  • 确定学习率
  • 求出损失函数对参数梯度
  • 按照公式更新参数
  • 重复3、4直到满足终止条件(如:损失函数或参数更新变化值小于某个阈值,或者训练次数达到设定阈值)
posted @   ICE_棋  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示