Commit 3404be75 authored by Florent Chatelain's avatar Florent Chatelain
Browse files

up slides

parent ff39108a
...@@ -163,14 +163,10 @@ ...@@ -163,14 +163,10 @@
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "AttributeError", "name": "stdout",
"evalue": "'RidgeClassifier' object has no attribute 'scoef_'", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "boudary equation x_2=-0.5596406428840415 x_1 + 0.6410047882764905\n"
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-23b595ae4fed>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# What are the parameter values of the linear boundary equation x_2=a x_1 + b?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscoef_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcoef_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mintercept_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcoef_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'boudary equation x_2={} x_1 + {}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'RidgeClassifier' object has no attribute 'scoef_'"
] ]
} }
], ],
...@@ -191,9 +187,158 @@ ...@@ -191,9 +187,158 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mInit signature:\u001b[0m\n",
"\u001b[0mlinear_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRidgeClassifier\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0malpha\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mfit_intercept\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mnormalize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mcopy_X\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mmax_iter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mtol\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.001\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mclass_weight\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0msolver\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'auto'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mrandom_state\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m \n",
"Classifier using Ridge regression.\n",
"\n",
"This classifier first converts the target values into ``{-1, 1}`` and\n",
"then treats the problem as a regression task (multi-output regression in\n",
"the multiclass case).\n",
"\n",
"Read more in the :ref:`User Guide <ridge_regression>`.\n",
"\n",
"Parameters\n",
"----------\n",
"alpha : float, default=1.0\n",
" Regularization strength; must be a positive float. Regularization\n",
" improves the conditioning of the problem and reduces the variance of\n",
" the estimates. Larger values specify stronger regularization.\n",
" Alpha corresponds to ``1 / (2C)`` in other linear models such as\n",
" :class:`~sklearn.linear_model.LogisticRegression` or\n",
" :class:`sklearn.svm.LinearSVC`.\n",
"\n",
"fit_intercept : bool, default=True\n",
" Whether to calculate the intercept for this model. If set to false, no\n",
" intercept will be used in calculations (e.g. data is expected to be\n",
" already centered).\n",
"\n",
"normalize : bool, default=False\n",
" This parameter is ignored when ``fit_intercept`` is set to False.\n",
" If True, the regressors X will be normalized before regression by\n",
" subtracting the mean and dividing by the l2-norm.\n",
" If you wish to standardize, please use\n",
" :class:`sklearn.preprocessing.StandardScaler` before calling ``fit``\n",
" on an estimator with ``normalize=False``.\n",
"\n",
"copy_X : bool, default=True\n",
" If True, X will be copied; else, it may be overwritten.\n",
"\n",
"max_iter : int, default=None\n",
" Maximum number of iterations for conjugate gradient solver.\n",
" The default value is determined by scipy.sparse.linalg.\n",
"\n",
"tol : float, default=1e-3\n",
" Precision of the solution.\n",
"\n",
"class_weight : dict or 'balanced', default=None\n",
" Weights associated with classes in the form ``{class_label: weight}``.\n",
" If not given, all classes are supposed to have weight one.\n",
"\n",
" The \"balanced\" mode uses the values of y to automatically adjust\n",
" weights inversely proportional to class frequencies in the input data\n",
" as ``n_samples / (n_classes * np.bincount(y))``.\n",
"\n",
"solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga'}, default='auto'\n",
" Solver to use in the computational routines:\n",
"\n",
" - 'auto' chooses the solver automatically based on the type of data.\n",
"\n",
" - 'svd' uses a Singular Value Decomposition of X to compute the Ridge\n",
" coefficients. More stable for singular matrices than 'cholesky'.\n",
"\n",
" - 'cholesky' uses the standard scipy.linalg.solve function to\n",
" obtain a closed-form solution.\n",
"\n",
" - 'sparse_cg' uses the conjugate gradient solver as found in\n",
" scipy.sparse.linalg.cg. As an iterative algorithm, this solver is\n",
" more appropriate than 'cholesky' for large-scale data\n",
" (possibility to set `tol` and `max_iter`).\n",
"\n",
" - 'lsqr' uses the dedicated regularized least-squares routine\n",
" scipy.sparse.linalg.lsqr. It is the fastest and uses an iterative\n",
" procedure.\n",
"\n",
" - 'sag' uses a Stochastic Average Gradient descent, and 'saga' uses\n",
" its unbiased and more flexible version named SAGA. Both methods\n",
" use an iterative procedure, and are often faster than other solvers\n",
" when both n_samples and n_features are large. Note that 'sag' and\n",
" 'saga' fast convergence is only guaranteed on features with\n",
" approximately the same scale. You can preprocess the data with a\n",
" scaler from sklearn.preprocessing.\n",
"\n",
" .. versionadded:: 0.17\n",
" Stochastic Average Gradient descent solver.\n",
" .. versionadded:: 0.19\n",
" SAGA solver.\n",
"\n",
"random_state : int, RandomState instance, default=None\n",
" Used when ``solver`` == 'sag' or 'saga' to shuffle the data.\n",
" See :term:`Glossary <random_state>` for details.\n",
"\n",
"Attributes\n",
"----------\n",
"coef_ : ndarray of shape (1, n_features) or (n_classes, n_features)\n",
" Coefficient of the features in the decision function.\n",
"\n",
" ``coef_`` is of shape (1, n_features) when the given problem is binary.\n",
"\n",
"intercept_ : float or ndarray of shape (n_targets,)\n",
" Independent term in decision function. Set to 0.0 if\n",
" ``fit_intercept = False``.\n",
"\n",
"n_iter_ : None or ndarray of shape (n_targets,)\n",
" Actual number of iterations for each target. Available only for\n",
" sag and lsqr solvers. Other solvers will return None.\n",
"\n",
"classes_ : ndarray of shape (n_classes,)\n",
" The classes labels.\n",
"\n",
"See Also\n",
"--------\n",
"Ridge : Ridge regression.\n",
"RidgeClassifierCV : Ridge classifier with built-in cross validation.\n",
"\n",
"Notes\n",
"-----\n",
"For multi-class classification, n_class classifiers are trained in\n",
"a one-versus-all approach. Concretely, this is implemented by taking\n",
"advantage of the multi-variate response support in Ridge.\n",
"\n",
"Examples\n",
"--------\n",
">>> from sklearn.datasets import load_breast_cancer\n",
">>> from sklearn.linear_model import RidgeClassifier\n",
">>> X, y = load_breast_cancer(return_X_y=True)\n",
">>> clf = RidgeClassifier().fit(X, y)\n",
">>> clf.score(X, y)\n",
"0.9595...\n",
"\u001b[0;31mFile:\u001b[0m ~/miniconda3/envs/calc/lib/python3.7/site-packages/sklearn/linear_model/_ridge.py\n",
"\u001b[0;31mType:\u001b[0m ABCMeta\n",
"\u001b[0;31mSubclasses:\u001b[0m \n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"#get the documentation for sklearn RidgeClassification object\n", "#get the documentation for sklearn RidgeClassification object\n",
"linear_model.RidgeClassifier?" "linear_model.RidgeClassifier?"
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
This notebook can be run on mybinder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/git/https%3A%2F%2Fgricad-gitlab.univ-grenoble-alpes.fr%2Fchatelaf%2Fml-sicom3a/master?filepath=notebooks%2F/1_introduction/N1_Linear_Classification.ipynb) This notebook can be run on mybinder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/git/https%3A%2F%2Fgricad-gitlab.univ-grenoble-alpes.fr%2Fchatelaf%2Fml-sicom3a/master?filepath=notebooks%2F/1_introduction/N1_Linear_Classification.ipynb)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Import modules # Import modules
%matplotlib inline %matplotlib inline
import matplotlib import matplotlib
import numpy as np import numpy as np
import scipy as sp import scipy as sp
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Select random seed # Select random seed
random_state = 0 random_state = 0
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We use scikit-learn to generate a toy 2D data set (two features $x_1$ and $x_2$) for binary classification (two classes) We use scikit-learn to generate a toy 2D data set (two features $x_1$ and $x_2$) for binary classification (two classes)
- each sample $(x_1,x_2)$ in the dataset is plotted as a 2D point where the two features $x_1$ and $x_2$ are displayed along the abscissa and ordinate axes respectively - each sample $(x_1,x_2)$ in the dataset is plotted as a 2D point where the two features $x_1$ and $x_2$ are displayed along the abscissa and ordinate axes respectively
- the corresponding class label $y$ is displayed as a color mark (e.g., yellow or purple) - the corresponding class label $y$ is displayed as a color mark (e.g., yellow or purple)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from sklearn.datasets import make_classification from sklearn.datasets import make_classification
#X are the features (aka inputs, ...), y the labels (aka responses, targets, output...) #X are the features (aka inputs, ...), y the labels (aka responses, targets, output...)
X,y = make_classification(n_features=2, n_redundant=0, n_informative=2, n_samples=150, X,y = make_classification(n_features=2, n_redundant=0, n_informative=2, n_samples=150,
random_state=random_state, n_clusters_per_class=1) random_state=random_state, n_clusters_per_class=1)
# make the class labels y_i as +1 or -1 # make the class labels y_i as +1 or -1
y[y==0]=-1 y[y==0]=-1
# display the dataset # display the dataset
plt.figure(figsize=(8,6)) plt.figure(figsize=(8,6))
plt.scatter(X[:,0], X[:,1], c=y) plt.scatter(X[:,0], X[:,1], c=y)
plt.grid(True) plt.grid(True)
plt.xlabel('$x_1$') plt.xlabel('$x_1$')
plt.ylabel('$x_2$') plt.ylabel('$x_2$')
#plt.savefig("2d_binary_classif.pdf") #plt.savefig("2d_binary_classif.pdf")
``` ```
%%%% Output: execute_result %%%% Output: execute_result
Text(0, 0.5, '$x_2$') Text(0, 0.5, '$x_2$')
%%%% Output: display_data %%%% Output: display_data
[Hidden Image Output] [Hidden Image Output]
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Then, a linear model is used to learn the classification function/rule. Then, a linear model is used to learn the classification function/rule.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from sklearn import linear_model from sklearn import linear_model
# Train a linear model, namely RidgeClassifier, # Train a linear model, namely RidgeClassifier,
# this includes standard linear regression as particular case (alpha=0) # this includes standard linear regression as particular case (alpha=0)
model = linear_model.RidgeClassifier(alpha=0) model = linear_model.RidgeClassifier(alpha=0)
model.fit(X,y) model.fit(X,y)
``` ```
%%%% Output: execute_result %%%% Output: execute_result
RidgeClassifier(alpha=0) RidgeClassifier(alpha=0)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Plot the decision functions # Plot the decision functions
XX, YY = np.meshgrid(np.linspace(X[:,0].min(), X[:,0].max(),200), XX, YY = np.meshgrid(np.linspace(X[:,0].min(), X[:,0].max(),200),
np.linspace(X[:,1].min(), X[:,1].max(),200)) np.linspace(X[:,1].min(), X[:,1].max(),200))
XY = np.vstack([XX.flatten(), YY.flatten()]).T XY = np.vstack([XX.flatten(), YY.flatten()]).T
yp = model.predict(XY) yp = model.predict(XY)
plt.figure(figsize=(8,6)) plt.figure(figsize=(8,6))
plt.contour(XX,YY,yp.reshape(XX.shape),[0]) plt.contour(XX,YY,yp.reshape(XX.shape),[0])
plt.scatter(X[:,0], X[:,1], c=y) plt.scatter(X[:,0], X[:,1], c=y)
plt.grid("on") plt.grid("on")
plt.xlabel('$x_1$') plt.xlabel('$x_1$')
plt.ylabel('$x_2$') plt.ylabel('$x_2$')
``` ```
%%%% Output: execute_result %%%% Output: execute_result
Text(0, 0.5, '$x_2$') Text(0, 0.5, '$x_2$')
%%%% Output: display_data %%%% Output: display_data
[Hidden Image Output] [Hidden Image Output]
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# What are the parameter values of the linear boundary equation x_2=a x_1 + b? # What are the parameter values of the linear boundary equation x_2=a x_1 + b?
a = -model.coef_[0][0]/model.coef_[0][1] a = -model.coef_[0][0]/model.coef_[0][1]
b = -model.intercept_[0]/model.coef_[0][1] b = -model.intercept_[0]/model.coef_[0][1]
print('boudary equation x_2={} x_1 + {}'.format(a,b)) print('boudary equation x_2={} x_1 + {}'.format(a,b))
``` ```
%%%% Output: error %%%% Output: stream
--------------------------------------------------------------------------- boudary equation x_2=-0.5596406428840415 x_1 + 0.6410047882764905
AttributeError Traceback (most recent call last)
<ipython-input-6-23b595ae4fed> in <module>
1 # What are the parameter values of the linear boundary equation x_2=a x_1 + b?
----> 2 a = -model.scoef_[0][0]/model.coef_[0][1]
3 b = -model.intercept_[0]/model.coef_[0][1]
4 print('boudary equation x_2={} x_1 + {}'.format(a,b))
AttributeError: 'RidgeClassifier' object has no attribute 'scoef_'
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Exercise ### Exercise
Change the number of informative features from `n_informative=2̀` to `n_informative=1` in the `make_classification()` procedure, regenerate the data set and fit the classification rule. Interpret now the new decision boundary: are the two variables of equal importance in predicting the class of the data? Change the number of informative features from `n_informative=2̀` to `n_informative=1` in the `make_classification()` procedure, regenerate the data set and fit the classification rule. Interpret now the new decision boundary: are the two variables of equal importance in predicting the class of the data?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#get the documentation for sklearn RidgeClassification object #get the documentation for sklearn RidgeClassification object
linear_model.RidgeClassifier? linear_model.RidgeClassifier?
``` ```
%%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment