from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test =train_test_split(x, y, test_size=0.2, random_state=666)from sklearn.linear_model import LinearRegressionlin_reg =LinearRegression()lin_reg.fit(X_train, y_train)lin_reg.score(X_test, y_test)
KNN Regressor
默认算法
from sklearn.neighbors import KNeighborsRegressorknn_reg =KNeighborsRegressor()knn_reg.fit(X_train, y_train)knn_reg.score(X_test, y_test)
网络搜索
from sklearn.neighbors import KNeighborsRegressorfrom sklearn.model_selection import GridSearchCVparam_grid = [{'weights':['uniform'],'n_neighbors': [i for i inrange(1, 11)]},{'weights':['distance'],'n_neighbors': [i for i inrange(1, 11)],'p': [i for i inrange(1, 6)]}]knn_reg =KNeighborsRegressor()grid_search =GridSearchCV(knn_reg, param_grid, n_jobs=-1, verbose=1)grid_search.fit(X_train, y_train)