import pandas as pd from sklearn.datasets import load_boston boston = load_boston() df = pd.DataFrame(boston['data'], columns=boston['feature_names']) df.head() dfX = df[['RM', 'AGE', 'DIS', 'LSTAT']] X = dfX.values y = boston['target'] x = X[:, 0].reshape(-1, 1) plt.plot(x, y, 'o') import numpy as np from sklearn.linear_model import LinearRegression reg = LinearRegression() reg.fit(x, y) y_hat = reg.predict(x) reg.coef_, reg.intercept_ model_x = np.arange(3, 10, 0.1).reshape(-1, 1) model_y = reg.predict(model_x) plt.plot(x, y, 'o') plt.plot(model_x, model_y) from sklearn.metrics import r2_score r2_score(y, y_hat)