2012-02-24 23:51:49 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
Sparsity Example: Fitting only features 1 and 2
|
|
|
|
|
=========================================================
|
|
|
|
|
|
|
|
|
|
Features 1 and 2 of the diabetes-dataset are fitted and
|
|
|
|
|
plotted below. It illustrates that although feature 2
|
|
|
|
|
has a strong coefficient on the full model, it does not
|
2022-02-23 17:18:29 +08:00
|
|
|
give us much regarding `y` when compared to just feature 1.
|
2012-02-24 23:51:49 +08:00
|
|
|
"""
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
|
|
|
|
# Modified for documentation by Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-02-24 23:51:49 +08:00
|
|
|
|
2022-02-23 17:18:29 +08:00
|
|
|
# %%
|
|
|
|
|
# First we load the diabetes dataset.
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2022-02-23 17:18:29 +08:00
|
|
|
from sklearn import datasets
|
|
|
|
|
import numpy as np
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2019-08-25 11:17:01 +08:00
|
|
|
X, y = datasets.load_diabetes(return_X_y=True)
|
2011-12-18 19:39:53 +08:00
|
|
|
indices = (0, 1)
|
|
|
|
|
|
2019-08-25 11:17:01 +08:00
|
|
|
X_train = X[:-20, indices]
|
|
|
|
|
X_test = X[-20:, indices]
|
|
|
|
|
y_train = y[:-20]
|
|
|
|
|
y_test = y[-20:]
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2022-02-23 17:18:29 +08:00
|
|
|
# %%
|
|
|
|
|
# Next we fit a linear regression model.
|
|
|
|
|
|
|
|
|
|
from sklearn import linear_model
|
|
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
ols = linear_model.LinearRegression()
|
2022-02-23 17:18:29 +08:00
|
|
|
_ = ols.fit(X_train, y_train)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# %%
|
|
|
|
|
# Finally we plot the figure from three different views.
|
|
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
|
2012-02-24 23:51:49 +08:00
|
|
|
def plot_figs(fig_num, elev, azim, X_train, clf):
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure(fig_num, figsize=(4, 3))
|
|
|
|
|
plt.clf()
|
2022-02-23 17:18:29 +08:00
|
|
|
ax = fig.add_subplot(111, projection="3d", elev=elev, azim=azim)
|
2012-02-24 23:51:49 +08:00
|
|
|
|
|
|
|
|
ax.scatter(X_train[:, 0], X_train[:, 1], y_train, c="k", marker="+")
|
2012-04-28 18:04:36 +08:00
|
|
|
ax.plot_surface(
|
|
|
|
|
np.array([[-0.1, -0.1], [0.15, 0.15]]),
|
2012-02-24 23:51:49 +08:00
|
|
|
np.array([[-0.1, 0.15], [-0.1, 0.15]]),
|
|
|
|
|
clf.predict(
|
|
|
|
|
np.array([[-0.1, -0.1, 0.15, 0.15], [-0.1, 0.15, -0.1, 0.15]]).T
|
|
|
|
|
).reshape((2, 2)),
|
|
|
|
|
alpha=0.5,
|
|
|
|
|
)
|
|
|
|
|
ax.set_xlabel("X_1")
|
|
|
|
|
ax.set_ylabel("X_2")
|
|
|
|
|
ax.set_zlabel("Y")
|
|
|
|
|
ax.w_xaxis.set_ticklabels([])
|
|
|
|
|
ax.w_yaxis.set_ticklabels([])
|
|
|
|
|
ax.w_zaxis.set_ticklabels([])
|
|
|
|
|
|
2019-08-25 11:17:01 +08:00
|
|
|
|
|
|
|
|
# Generate the three different figures from different views
|
2012-02-24 23:51:49 +08:00
|
|
|
elev = 43.5
|
|
|
|
|
azim = -110
|
|
|
|
|
plot_figs(1, elev, azim, X_train, ols)
|
|
|
|
|
|
|
|
|
|
elev = -0.5
|
|
|
|
|
azim = 0
|
|
|
|
|
plot_figs(2, elev, azim, X_train, ols)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-02-24 23:51:49 +08:00
|
|
|
elev = -0.5
|
|
|
|
|
azim = 90
|
|
|
|
|
plot_figs(3, elev, azim, X_train, ols)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|