machine learning
|python
目的:
- 解釋data過去現象
- 利用自變數(
)來預測應變數( )的未來可能數值
方程式:
(圖形為拋物線)
Why "Linear"?
項與項之間都是線性組合的關係(都是相乘再相加)
from sklearn.preprocessing import PolynomialFeatures
- 使用sklearn中的preprocessing的PolynomialFeatures類別
提供多項式特徵處理的方法
poly_reg = PolynomialFeatures(degree = 2)
- degree = 2 代表最高次方為2
x_poly = poly_reg.fit_transform(x)
- 使用PolynomialFeatures中的fit_transfor()進行資料擬合與轉換
x_grid = np.arange(min(x), max(x), 0.1)
- 將x的差距改為0.1(原本差距為1)
有更多點,使曲線更平滑
new_x = 6.5
new_x = np.array(new_x).reshape(-1, 1)
lin_reg.predict(new_x)
lin_reg2.predict(poly_reg.fit_transform(new_x))
- 假設有一位級數為6.5級的應徵者,分別用簡單線性回歸和多項式回歸來操作,結果如下
由此可知,若用簡單線性回歸模型,公司需支付給此人的薪水超出太多
機器學習-作業10
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv("Position_Salaries.csv")
x = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
'''
# Missing Data
# Categorical Data
# Splitting the Dataset into the Training set and Test set
1.只有10筆資料,訓練集合會太小,模型誤差大
2.level希望全部做分析,才能完整觀察與薪水的關係
# Feature Scaling
Linear Regression自帶特徵縮放,不做
'''
# Simple Linear Regression
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(x, y)
# Graph of Simple Linear Regression
plt.scatter(x, y, color = 'red')
plt.plot(x, lin_reg.predict(x), color = 'blue')
plt.title("Truth or Bluff (Simple Linear Regression)")
plt.xlabel("Position Level")
plt.ylabel("Salary")
plt.show()
# Polynomial Regression
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 5)
x_poly = poly_reg.fit_transform(x)
lin_reg2 = LinearRegression()
lin_reg2.fit(x_poly, y)
# Graph of Polynomial Regression
plt.scatter(x, y, color = 'red')
plt.plot(x, lin_reg2.predict(x_poly), color = 'blue')
plt.title("Truth or Bluff (Polynomial Regression)")
plt.xlabel("Position Level")
plt.ylabel("Salary")
plt.show()
'''
讓線條平滑
'''
x_grid = np.arange(min(x), max(x), 0.1)
x_grid = x_grid.reshape(len(x_grid), 1)
plt.scatter(x, y, color = 'red')
plt.plot(x_grid, lin_reg2.predict(poly_reg.fit_transform(x_grid)), color = 'blue')
plt.title("Truth or Bluff (Polynomial Regression)")
plt.xlabel("Position Level")
plt.ylabel("Salary")
plt.show()
new_x = 6.5
new_x = np.array(new_x).reshape(-1, 1)
lin_reg.predict(new_x)
lin_reg2.predict(poly_reg.fit_transform(new_x))
:::warning ctrl + i 可尋找方法或類別的相關資訊 dataset.info() # 資料類別、是否有缺失資料等資訊 養成習慣: 執行完每列程式後,記得檢查變數是否取用正確 ::: Get the dataset
Oct 29, 2024<font color="#3355FF">一個</font>應變數(Y)和<font color="#3355FF">一個</font>自變數(X)之<font color="#008000">線性關係</font>(皆為<font color="#f00">連續型數</font>)
Dec 14, 2023介紹 假設特徵之間強(樸素)獨立下運用<font color="#3355FF">貝氏定理</font>為基礎的簡單機率分類器 特徵之間為相互獨立的(但現實中不太可能完全獨立),公式為$P(A|X)=\cfrac{P(X|A)*P(A)}{P(X)}$($X$為特徵,$A$為類別) 貝式定理 公式:$P(A|B)=\cfrac{P(B|A)*P(A)}{P(B)}$ $P(A|B)$表示==在B發生的情況下,A發生的機率為何== 例子
May 24, 2023:::info 可參考機器學習 - 多元線性回歸(Multiple Linear Regression) ::: 介紹 方程式:$R^2=1-\cfrac{SS_{res}}{SS_{tot}}$ $R^2$大代表模型是理想的 $R^2$落在0~1之間 自變量不論是否改變,都不會影響<font color=indigo>總平方和$SS_{tot}$</font>,故分母不變
May 17, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up