sklearn——线性回归例子
生活随笔
收集整理的這篇文章主要介紹了
sklearn——线性回归例子
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
注:本案例為黑馬的課堂案例,上傳僅為方便查看
# 導(dǎo)包 from sklearn.datasets import load_boston # 獲取數(shù)據(jù)集 from sklearn.model_selection import train_test_split # 數(shù)據(jù)分割 from sklearn.preprocessing import StandardScaler # 標(biāo)準(zhǔn)化處理 from sklearn.linear_model import LinearRegression # 線性模型 from sklearn.metrics import mean_squared_error # 模型評(píng)估均方誤差 # 1.獲取數(shù)據(jù)集 boston = load_boston() # 2.數(shù)據(jù)基本處理 # 2.1 數(shù)據(jù)集劃分 x_train,x_test,y_train,y_test = train_test_split(boston.data,boston.target,test_size=0.2) # 3.特征工程_標(biāo)準(zhǔn)化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) # 4.機(jī)器學(xué)習(xí)(線性回歸) estimator = LinearRegression() estimator.fit(x_train, y_train) print("這個(gè)模型的偏置是:\n", estimator.intercept_) # 5.模型評(píng)估 # 5.1 預(yù)測值和準(zhǔn)確率 y_pre = estimator.predict(x_test) print("預(yù)測值是:\n", y_pre) score = estimator.score(x_test, y_test) print("準(zhǔn)確率是:\n", score)# 5.2 均方誤差 ret = mean_squared_error(y_test, y_pre) print("均方誤差是:\n", ret)總結(jié)
以上是生活随笔為你收集整理的sklearn——线性回归例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大型仪器一般都是用计算机,问题:大型仪器
- 下一篇: ioswebview混编_iOS 原生和