Regression coefficients on the Boston housing dataset

from sklearn.linear_model import LogisticRegression, LassoLars from sklearn.preprocessing import LabelEncoder from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.metrics import r2_score import numpy as np import matplotlib.pyplot as plt import seaborn as sns sns.set_style('darkgrid') fig, axes = plt.subplots() boston = load_boston() X, y = boston.data, boston.target le = LabelEncoder() encodedy = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X, encodedy, test_size=0.1, random_state=0) # Logistic regression lr = LogisticRegression() lr.fit(X_train, y_train) y_pred = lr.predict(X_test) print(y_pred) """ [206 157 112 10 114 80 170 119 128 39 3 44 44 0 228 132 89 195 123 128 146 135 93 128 117 157 80 57 133 91 89 89 74 87 171 97 4 157 44 41 164 114 91 89 133 165 156 76 49 152 135] """ logistic_regression_score = r2_score(y_test, y_pred) print('Logistic regression R2 score: {0}'.format(logistic_regression_score)) # 0.5219120764620884 coef = lr.coef_ color = plt.cm.summer(0) coef = np.mean(coef, axis=0) plt.plot(range(len(coef)), coef, 'k-', color='red', lw=3, alpha=0.5, label='Logistic regression') plt.title('The coefficients of logistic regression and lasso with least angle regression on the Boston housing dataset') # LassoLars ll = LassoLars(alpha=0.1) ll.fit(X_train, y_train) y_pred = ll.predict(X_test) print(y_pred) """ [ 144.00855241 105.81838504 146.48796771 46.390195 115.68751685 106.9519096 95.46405365 112.94277933 89.46864253 94.81725815 21.89405738 53.75730352 70.66433856 2.29924045 211.17709604 179.05892698 106.41834458 200.49462218 159.80244247 119.40110953 133.09827847 125.40752868 94.70456445 148.59271167 119.78563538 33.16621829 90.02146355 93.33257466 187.44525363 98.70927987 74.40987569 81.55160718 117.97438919 125.13376477 144.18885577 83.0650129 33.13450503 109.30874208 65.03267708 50.08174938 144.31435422 107.45914905 132.78777115 59.73299254 145.57305269 137.01703352 110.18974348 116.77117848 36.41484243 122.70731454 104.63823014] """ lassolars_score = r2_score(y_test, y_pred) print('Lasso with least angle regression R2 score: {0}'.format(lassolars_score)) # 0.5722134691664309 coef = ll.coef_ plt.plot(range(len(coef)), coef, 'k-', color='blue', lw=3, alpha=0.5, label='LassoLARS') legend1 = plt.legend(loc='upper right', frameon=False) legend2 = plt.legend(['R2: ' + str(it) for it in [logistic_regression_score, lassolars_score]], loc='lower right', frameon=False) axes.add_artist(legend1) axes.add_artist(legend2) plt.tight_layout() plt.show() The coefficients of logistic regression on the Boston housing dataset Note: The coefficients as a result of the logistic regression have been averaged.