機器學習之線性回歸
- 1、機器學習
- 2、線性回歸
- 2.1、梯度下降法
- 3、python下調用scikit-learn
1、機器學習
2、線性回歸
####所以y可以當成我們需要的結果,根據公式可以求的y一撇的值更小,所以更接近需要的結果,所以y一撇擬合性更好
2.1、梯度下降法
已知:
J = f ( ( (p ) ) ) = 3.5p 2 ^2 2-14p+14
p i _i i? = 0.5 , α \alpha α = 0.01
p i + 1 _{i+1} i+1?= ??
∵ \because ∵ 一元二次函數 f ( ( (p ) ) ) = ap 2 ^2 2-bp+c的冪函數求導公式
f ( ( (x ) ) ) = x a ^a a -> f ′ f\prime f′( x x x) = ax a ? 1 ^{a-1} a?1
∵ \because ∵
3.5p 2 ^2 2其導數為2*3.5p ( 2 ? 1 ) ^{(2-1)} (2?1) = 7p
-14p其導數為-14*p ( 1 ? 1 ) ^{(1-1)} (1?1)=-14
14為常數項導數為0
∵ \because ∵ 3.5p 2 ^2 2-14p+14的導數是7p-14
∴ \therefore ∴ α \alpha α δ δ p i \frac{\delta}{\delta p_i} δpi?δ?f ( ( (p i _{i} i? ) ) ) = 7p-14
∵ \because ∵ p i _i i? = 0.5
∴ \therefore ∴ 代入 α \alpha α δ δ p i \frac{\delta}{\delta p_i} δpi?δ?f ( ( (p i _{i} i? ) ) )=7*0.5-14=-10.5
∴ \therefore ∴ 損失函數J = 0.5-0.01*(-10.5) = 0.605
∴ \therefore ∴ 損失函數梯度值為0.605
3、python下調用scikit-learn
https://scikit-learn.org/stable/
可以用簡短的代碼求解模型
import numpy as np
from sklearn.linear_model import LinearRegression
# 獲取數據
x = [1,2,3,4,5,6,7,8,9,10]
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
X = x.reshape(-1, 1)
y = np.array([7,9,11,13,15,17,19,21,23,25])
# 尋找a、b(y = ax + b)
lr_model = LinearRegression()
lr_model.fit(X,y)# 展示a、b
a = lr_model.coef_
b = lr_model.intercept_#打印系數a和截距b
print("系數a",a)
print("截距b",b)#對新數據進行預測
x_new = np.array([11,12])
X_new = x_new.reshape(-1, 1)
predictions = lr_model.predict(X_new)#打印預測數據
print("預測數據是:",predictions)
import numpy as np
from sklearn.linear_model import LinearRegression
# 獲取數據
x = [1,2,3,4,5,6,7,8,9,10]
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
X = x.reshape(-1, 1)
y = np.array([7,9,11,13,15,17,19,21,23,25])
# 尋找a、b(y = ax + b)
lr_model = LinearRegression()
lr_model.fit(X,y)# 展示a、b
a = lr_model.coef_
b = lr_model.intercept_#打印系數a和截距b
print("系數a",a)
print("截距b",b)#對新數據進行預測
x_new = np.array([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,11])
X_new = x_new.reshape(-1, 1)
predictions = lr_model.predict(X_new)#打印預測數據
print("預測數據是:",predictions)from sklearn.metrics import mean_squared_error,r2_score
MSE = mean_squared_error(y,predictions)
R2 = r2_score(y,predictions)#打印均方差,r方值
print("MSE:",MSE)
print("R2:",R2)#畫圖對比y/ 和y可視化模型表現
from matplotlib import pyplot as plt
plt.scatter(y,predictions)
matplotlib繪制散點圖
#繪制散點圖import matplotlib.pyplot as plt# 生成示例數據
np.random.seed(42)
x_values = np.random.rand(100)
y_values = x_values + np.random.randn(100) * 0.1 # 繪制散點圖
plt.scatter(x_values, y_values, color='blue', marker='o') # color參數定義點的顏色,marker定義點的形狀# 設置matplotlib的字體為微軟雅黑,確保你的系統中已安裝此字體,避免中文亂碼問題
plt.rcParams['font.family'] = 'Microsoft YaHei'
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 用于正常顯示中文標簽
# 解決保存圖像時負號'-'顯示為方塊的問題
plt.rcParams['axes.unicode_minus'] = False# 添加標題和標簽
plt.title('散點圖') # 添加標題
plt.xlabel('X軸') # X軸標簽
plt.ylabel('Y軸') # Y軸標簽# 顯示圖表
plt.show()
多張散點圖展示
#散點圖多張圖同時展示import matplotlib.pyplot as plt# 生成示例數據
np.random.seed(42)
x_values = np.random.rand(100)
y_values = x_values + np.random.randn(100) * 0.1# 如果需要四張圖則創建一個2x2的子圖布局
for i in range(1, 5): # 因為是從1開始計數,所以要到5(包括)plt.subplot(2, 2, i) # 2行2列的布局,當前是第i個子圖plt.scatter(x_values, y_values, color='blue', marker='p')