import numpy as np import pandas as pd df = pd.read_csv('./wine3.csv') data = df.dropna(axis=1,thresh=df.shape[0]*0.8,inplace=False) data['type'].replace(np.nan,data['type'].mode()[0]) diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True) diabetes_X = diabetes_X[:, np.newaxis, 2] diabetes_X_train = diabetes_X[:-20] diabetes_X_test = diabetes_X[-20:] diabetes_y_train = diabetes_y[:-20] diabetes_y_test = diabetes_y[-20:] regr = linear_model.LinearRegression() regr.fit(diabetes_X_train, diabetes_y_train) diabetes_y_pred = regr.predict(diabetes_X_test) print("Coefficients: \n", regr.coef_) print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred)) plt.scatter(diabetes_X_test, diabetes_y_test, color="black") plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3) plt.xticks(()) plt.yticks(()) 1.plt.show()
理解问题import numpy as np import pandas as pd df = pd.read_csv('./wine3.csv') data = df.dropna(axis=1,thresh=df.shape[0]*0.8,inplace=False) data['type'].replace(np.nan,data['type'].mode()[0]) diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True) diabetes_X = diabetes_X[:, np.newaxis, 2] diabetes_X_train = diabetes_X[:-20] diabetes_X_test = diabetes_X[-20:] diabetes_y_train = diabetes_y[:-20] diabetes_y_test = diabetes_y[-20:] regr = linear_model.LinearRegression() regr.fit(diabetes_X_train, diabetes_y_train) diabetes_y_pred = regr.predict(diabetes_X_test) print("Coefficients: \n", regr.coef_) print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred)) plt.scatter(diabetes_X_test, diabetes_y_test, color="black") plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3) plt.xticks(()) plt.yticks(()) 1.plt.show()
已完成理解「import numpy as np import pandas as pd df = pd.read_csv('./wine3.csv') data = df.dropna(axis=1,thresh=df.shape[0]*0.8,inplace=False) data['type'].replace(np.nan,data['type'].mode()[0]) diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True) diabetes_X = diabetes_X[:, np.newaxis, 2] diabetes_X_train = diabetes_X[:-20] diabetes_X_test = diabetes_X[-20:] diabetes_y_train = diabetes_y[:-20] diabetes_y_test = diabetes_y[-20:] regr = linear_model.LinearRegression() regr.fit(diabetes_X_train, diabetes_y_train) diabetes_y_pred = regr.predict(diabetes_X_test) print("Coefficients: \n", regr.coef_) print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred)) plt.scatter(diabetes_X_test, diabetes_y_test, color="black") plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3) plt.xticks(()) plt.yticks(()) 1.plt.show()」
展开阅读网页