In [1]:
#!pip install mglearn
import mglearn
import sklearn
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import IPython

from sklearn.datasets import load_boston
boston = load_boston()
X, y = mglearn.datasets.load_extended_boston()

from sklearn.model_selection import train_test_split
X, y=mglearn.datasets.make_forge()

X_train, X_test, y_train, y_test=train_test_split(X, y, random_state=0)

from sklearn.neighbors import KNeighborsClassifier
clf=KNeighborsClassifier(n_neighbors=3), y_train)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski')
clf.score(X_test, y_test)

from sklearn.linear_model import LinearRegression
lr=LinearRegression().fit(X_train, y_train)

print("training set score: %f" % lr.score(X_train, y_train))
print("test set score: %f" % lr.score(X_test, y_test))

from sklearn.linear_model import Ridge
ridge = Ridge().fit(X_train, y_train)
print("training set score: %f" % ridge.score(X_train, y_train))
print("test set score: %f" % ridge.score(X_test, y_test))

ridge01 = Ridge(alpha=0.1).fit(X_train, y_train)
print("training set score: %f" % ridge01.score(X_train, y_train))
print("test set score: %f" % ridge01.score(X_test, y_test))

print ("--------------------")

from sklearn.linear_model import Lasso
lasso00001 = Lasso(alpha=0.0001).fit(X_train, y_train)
print("training set score: %f" % lasso00001.score(X_train, y_train))
print("test set score: %f" % lasso00001.score(X_test, y_test))
print("number of features used: %d" % np.sum(lasso00001.coef_ != 0))

(506, 104)
<Figure size 640x480 with 1 Axes>
training set score: 0.771865
test set score: 0.725968
training set score: 0.771686
test set score: 0.722415
training set score: 0.771863
test set score: 0.725626
training set score: 0.771865
test set score: 0.725916
number of features used: 2
/usr/local/lib/python3.7/site-packages/sklearn/linear_model/ RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
  linalg.lstsq(X, y)

In [ ]: