安全的關(guān)鍵.png)
如何高效爬取全球新聞網(wǎng)站 – 整合Scrapy、Selenium與Mediastack API實(shí)現(xiàn)自動(dòng)化新聞采集
我們將使用Kaggle中的「汽車(chē)燃油效率」數(shù)據(jù)集(或類(lèi)似的數(shù)據(jù)集),從頭實(shí)現(xiàn)SVR的訓(xùn)練過(guò)程,并進(jìn)行數(shù)據(jù)可視化分析。
1. 加載數(shù)據(jù)集
2. 數(shù)據(jù)預(yù)處理
3. 定義SVR的訓(xùn)練過(guò)程
4. 可視化分析
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 加載數(shù)據(jù)
data = pd.read_csv('auto-mpg.csv')
# 數(shù)據(jù)預(yù)處理
data = data[['mpg', 'horsepower', 'weight', 'acceleration']]
data = data.dropna() # 刪除缺失值
# 將'horsepower'列轉(zhuǎn)換為數(shù)值型,并處理無(wú)效數(shù)據(jù)
data['horsepower'] = pd.to_numeric(data['horsepower'], errors='coerce')
data = data.dropna() # 處理轉(zhuǎn)換后可能出現(xiàn)的缺失值
X = data[['horsepower', 'weight', 'acceleration']].values
y = data['mpg'].values
# 標(biāo)準(zhǔn)化輸入數(shù)據(jù)
X = (X - X.mean(axis=0)) / X.std(axis=0)
y = (y - y.mean()) / y.std()
為了簡(jiǎn)單,我們實(shí)現(xiàn)一個(gè)線(xiàn)性SVR,不使用現(xiàn)有的庫(kù)來(lái)直接調(diào)用SVR算法。
class SVR:
def __init__(self, C=1.0, epsilon=0.1, lr=0.001, max_iter=1000):
self.C = C
self.epsilon = epsilon
self.lr = lr
self.max_iter = max_iter
def fit(self, X, y):
n_samples, n_features = X.shape
self.w = np.zeros(n_features)
self.b = 0
for _ in range(self.max_iter):
for i in range(n_samples):
if np.abs(y[i] - (np.dot(X[i], self.w) + self.b)) > self.epsilon:
if y[i] > np.dot(X[i], self.w) + self.b:
self.w += self.lr * (X[i] - self.C * self.w)
self.b += self.lr * 1
else:
self.w -= self.lr * (X[i] + self.C * self.w)
self.b -= self.lr * 1
def predict(self, X):
return np.dot(X, self.w) + self.b
# 訓(xùn)練SVR模型
model = SVR(C=1.0, epsilon=0.1, lr=0.001, max_iter=10000)
model.fit(X, y)
# 預(yù)測(cè)
y_pred = model.predict(X)
我們將通過(guò)以下四個(gè)圖表來(lái)分析數(shù)據(jù)和模型表現(xiàn):
1. 原始數(shù)據(jù)的分布
2. 訓(xùn)練后的回歸線(xiàn)與實(shí)際數(shù)據(jù)的對(duì)比
3. 殘差分布
4. 預(yù)測(cè)值與實(shí)際值的對(duì)比
# 1. 原始數(shù)據(jù)的分布
plt.figure(figsize=(8,6))
plt.scatter(X[:,0], y, color='blue', label='Actual')
plt.title('Original Data Distribution')
plt.xlabel('Horsepower')
plt.ylabel('MPG')
plt.legend()
plt.show()
# 2. 回歸線(xiàn)與實(shí)際數(shù)據(jù)對(duì)比
plt.figure(figsize=(8,6))
plt.scatter(X[:,0], y, color='blue', label='Actual')
plt.plot(X[:,0], y_pred, color='red', label='Predicted')
plt.title('SVR Fit')
plt.xlabel('Horsepower')
plt.ylabel('MPG')
plt.legend()
plt.show()
# 3. 殘差分布
residuals = y - y_pred
plt.figure(figsize=(8,6))
plt.hist(residuals, bins=20, color='green')
plt.title('Residuals Distribution')
plt.xlabel('Residuals')
plt.ylabel('Frequency')
plt.show()
# 4. 預(yù)測(cè)值與實(shí)際值的對(duì)比
plt.figure(figsize=(8,6))
plt.scatter(y, y_pred, color='purple')
plt.title('Predicted vs Actual MPG')
plt.xlabel('Actual MPG')
plt.ylabel('Predicted MPG')
plt.show()
這個(gè)案例中,我們手動(dòng)實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的SVR模型,并且通過(guò)四個(gè)圖形分析了模型的表現(xiàn):
1. 原始數(shù)據(jù)分布 展示了我們正在處理的數(shù)據(jù)的基本特征。
2. 回歸線(xiàn)與實(shí)際數(shù)據(jù)對(duì)比 展示了SVR模型的擬合效果。
3. 殘差分布 讓我們直觀了解模型誤差的分布情況。
4. 預(yù)測(cè)值與實(shí)際值的對(duì)比 幫助我們?cè)u(píng)估模型的預(yù)測(cè)效果。
上面的代碼手動(dòng)實(shí)現(xiàn)了 SVR,通過(guò)圖形進(jìn)行數(shù)據(jù)分析。在這個(gè)過(guò)程中,希望幫助大家深入理解了SVR的數(shù)學(xué)原理,并通過(guò)手動(dòng)實(shí)現(xiàn)掌握了它的內(nèi)在工作邏輯。
文章轉(zhuǎn)自微信公眾號(hào)@深夜努力寫(xiě)Python
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)