小象机器学习(邹博老师)学习笔记
生活随笔
收集整理的這篇文章主要介紹了
小象机器学习(邹博老师)学习笔记
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、線性回歸
1. 基本線性回歸
from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1) # print x_train, y_train linreg = LinearRegression() model = linreg.fit(x_train, y_train) # model # linreg.coef_ # linreg.intercept_ y_hat = linreg.predict(np.array(x_test)) mse = np.average((y_hat - np.array(y_test)) ** 2) # Mean Squared Error rmse = np.sqrt(mse) # Root Mean Squared Error t = np.arange(len(x_test)) plt.plot(t, y_test, 'r-', linewidth=2, label='Test') plt.plot(t, y_hat, 'g-', linewidth=2, label='Predict')?2. CV
from sklearn.model_selection import train_test_split from sklearn.linear_model import Lasso, Ridge from sklearn.model_selection import GridSearchCV x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1) # print x_train, y_train model = Lasso() # model = Ridge() alpha_can = np.logspace(-3, 2, 10) lasso_model = GridSearchCV(model, param_grid={'alpha': alpha_can}, cv=5) lasso_model.fit(x, y) # lasso_model.best_params_ # pd.DataFrame(lasso_model.cv_results_)?3. pipline和meshgrid畫圖(鳶尾花數(shù)據(jù))
lr = Pipeline([('sc', StandardScaler()),('clf', LogisticRegression()) ]) lr.fit(x, y.ravel())N, M = 500, 500 # 橫縱各采樣多少個值 x1_min, x1_max = x[:, 0].min(), x[:, 0].max() # 第0列的范圍 x2_min, x2_max = x[:, 1].min(), x[:, 1].max() # 第1列的范圍 t1 = np.linspace(x1_min, x1_max, N) t2 = np.linspace(x2_min, x2_max, M) x1, x2 = np.meshgrid(t1, t2) # 生成網(wǎng)格采樣點 x_test = np.stack((x1.flat, x2.flat), axis=1) # 測試點 cm_light = mpl.colors.ListedColormap(['#77E0A0', '#FF8080', '#A0A0FF']) cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b']) y_hat = lr.predict(x_test) # 預測值 y_hat = y_hat.reshape(x1.shape) # 使之與輸入的形狀相同 plt.pcolormesh(x1, x2, y_hat, cmap=cm_light) # 預測值的顯示 plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=50, cmap=cm_dark) # 樣本的顯示 plt.xlabel('petal length') plt.ylabel('petal width') plt.xlim(x1_min, x1_max) plt.ylim(x2_min, x2_max) plt.grid()?
轉載于:https://www.cnblogs.com/figo-studypath/p/10384652.html
總結
以上是生活随笔為你收集整理的小象机器学习(邹博老师)学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【项目实战】——Python打包正装换底
- 下一篇: 【图像处理】——图像特效处理(马赛克、图