日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习实战-回归算法-18

發布時間:2024/9/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习实战-回归算法-18 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

回歸算法-波士頓房價預測

from sklearn.datasets import load_boston import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LassoCV import seaborn as sns house = load_boston() print(house.DESCR)


x = house.data y = house.target df = pd.DataFrame(x, columns=house.feature_names) df['Target'] = pd.DataFrame(y, columns=['Target']) df.head()

plt.figure(figsize=(15,15)) # 畫熱力圖,數值為兩個變量之間的相關系數 p=sns.heatmap(df.corr(), annot=True, square=True) # 數據標準化 from sklearn.preprocessing import StandardScalerss = StandardScaler() x = ss.fit_transform(x) print(x[:5])

from sklearn.model_selection import train_test_split # 切分數據集 x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3) # 創建模型 model = LassoCV() model.fit(x_train, y_train)# lasso系數 print(model.alpha_) # 相關系數 print(model.coef_)

model.score(x_test, y_test)

回歸算法-葡萄酒質量和時間關系

import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split # 載入數據 data = np.genfromtxt('linear.csv', delimiter=',') # 畫圖 plt.scatter(data[1:,0],data[1:,1]) plt.title('Age Vs Quality (Test set)') plt.xlabel('Age') plt.ylabel('Quality') plt.show()

# 數據拆分 x_train, x_test, y_train, y_test = train_test_split(data[1:, 0], data[1:, 1], test_size = 0.3) # 1D->2D,給數據增加一個維度,主要是訓練模型的時候,函數要求傳入2維的數據 x_train = x_train[:, np.newaxis] x_test = x_test[:, np.newaxis] # 訓練模型 model = LinearRegression() model.fit(x_train, y_train)

# 訓練集的散點圖 plt.scatter(x_train, y_train, color = 'b') # 模型對訓練集的預測結果 plt.plot(x_train,model.predict(x_train), color ='r' , linewidth=5) # 畫表頭和xy坐標描述 plt.title('Age Vs Quality (Training set)') plt.xlabel('Age') plt.ylabel('Quality') plt.show()

# 測試集的散點圖 plt.scatter(x_test, y_test, color = 'b') # 模型對測試集的預測結果 plt.plot(x_test,model.predict(x_test), color ='r', linewidth=5) # 畫表頭和xy坐標描述 plt.title('Age Vs Quality (Test set)') plt.xlabel('Age') plt.ylabel('Quality') plt.show()

總結

以上是生活随笔為你收集整理的机器学习实战-回归算法-18的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。