<jupyter_start><jupyter_text>sklearnLibrary de machine learn:  * Classification  * Regression  * Clustering  * Reduction de dimension (=features, pca...)http://scikit-learn.org/stable/Attetion sklearn travail sur des object numpy.ndarray donc il faut passer du DataFrame vers numpy avecs .values<jupyter_code>%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import sklearn
iris = sns.load_dataset("iris")
iris.describe()
sns.pairplot(iris, hue="species")<jupyter_output><empty_output><jupyter_text>Features reduction, example avec PCAattention en transform tout en numpy<jupyter_code>data = iris.drop('species', axis=1).values
labels = iris['species'].values
print(data.shape, type(data))
print(labels.shape, type(labels))
data
from sklearn.decomposition import PCA

pca = PCA(n_components=2)
data_pca = pca.fit(data).transform(data)
print(data_pca.shape, type(data_pca))
iris_pca = pd.DataFrame(data_pca, columns=['pca0', 'pca1'], index=iris.index)
iris_pca.head(10)


g = sns.PairGrid(iris_pca, diag_sharey=False)
g.map_lower(sns.kdeplot)
g.map_upper(plt.scatter)
g.map_diag(sns.kdeplot, lw=3)
iris_pca['species'] = iris['species']
iris_pca.head()
g = sns.PairGrid(iris_pca, diag_sharey=False, hue="species")
g.map_lower(sns.kdeplot)
g.map_upper(plt.scatter)
g.map_diag(sns.kdeplot, lw=3)
pca.explained_variance_ratio_

pca.components_
pca.components_.T<jupyter_output><empty_output><jupyter_text>Formule de la projection = multiplication matriciel (changement de repere)$proj = ((data - mean(data)) \cdot components^T $<jupyter_code>data_pca2 = np.dot(data-np.mean(data, axis=0), pca.components_.T)
np.all(data_pca2 == data_pca)
proj = pd.DataFrame(pca.components_.T, index=iris.columns[:-1], columns=['pca0', 'pca1'])
proj
fig, ax = plt.subplots()
#ax.plot(iris_pca['pca0'],iris_pca['pca1'], ls='None', marker='.' )
for feat, row in proj.iterrows():
    ax.plot([0,row['pca0']], [0, row['pca1']], label=feat)
ax.legend()
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)<jupyter_output><empty_output><jupyter_text>Clustering = regrouper par similaritéOn a une population dont on ne connais pas les labels on veut les regrouper.Il faut lire la doc de sklearn.http://scikit-learn.org/stable/modules/clustering.htmlclusteringExample avec kKMeans et MeanShift<jupyter_code>from sklearn.cluster import KMeans, MeanShift
iris = sns.load_dataset("iris")
data = iris.drop('species', axis=1).values
labels = KMeans(n_clusters=2).fit_predict(data)
iris['pred_kmeans'] = labels
iris.head()
data = iris.drop('species', axis=1).values
labels = MeanShift().fit_predict(data)
iris['pred_meanshift'] = labels
iris.head()

sns.pairplot(iris, hue="pred_kmeans")
sns.pairplot(iris, hue="pred_meanshift")<jupyter_output><empty_output><jupyter_text>Classification supervisé = apprendre puis predireOn des données avec plusieurs variables. On connais le label (ou une autre variable) pour un sous ensemble et on veut predir ce label (ou autre varibale) pour le reste du dataset.  Example avec SVC = C-Support Vector ClassificationBien lire la doc!!!!!http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.htmlsklearn.svm.SVC<jupyter_code>from sklearn.svm import SVC<jupyter_output><empty_output><jupyter_text>creation d'un categorie<jupyter_code>iris = sns.load_dataset("iris")
iris['species'] = iris['species'].astype('category')
iris['species'] = iris['species'].cat.codes
#category = iris['species'].cat
iris.head()

print(iris.shape)
train_iris = iris.sample(20)
print(train_iris.shape)
precdit_iris = iris[~iris.index.isin(train_iris.index)]
print(precdit_iris.shape)
predictor = SVC(kernel='linear', C=1.)
data = train_iris.drop('species', axis=1).values
labels = train_iris['species']
predictor.fit(data, labels)
data2 = precdit_iris.drop('species', axis=1).values
label2 = predictor.predict(data2)
print(precdit_iris['species'].values)
print(label2)
print('nb error', (precdit_iris['species']!=label2).sum())<jupyter_output>[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 1 1 2 1 1 1 1 1
 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
nb error 5<jupyter_text>**A noter : le nombre d'erreur dépend de la taille de train  et de predict!!!!!**<jupyter_code>df = precdit_iris
fig, axs = plt.subplots(ncols=2)
colors = {0:'r', 1:'g', 2:'b'}
for l, color in colors.items():
    keep = label2 ==l
    axs[0].plot(df.loc[keep, 'sepal_length'], df.loc[keep, 'sepal_width'], ls='None', marker='o', color=color)

# fig, ax = plt.subplots()
for l, color in colors.items():
    keep = precdit_iris['species'] ==l
    axs[1].plot(df.loc[keep, 'sepal_length'], df.loc[keep, 'sepal_width'], ls='None', marker='*', color=color)<jupyter_output><empty_output><jupyter_text>La même chose avec un autre classifier : KNeighbors<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(3)
classifier.fit(data, labels)
label2 = predictor.predict(data2)
print(precdit_iris['species'].values)
print(label2)
print('nb error', (precdit_iris['species']!=label2).sum())

df = precdit_iris
fig, axs = plt.subplots(ncols=2)
colors = {0:'r', 1:'g', 2:'b'}
for l, color in colors.items():
    keep = label2 ==l
    axs[0].plot(df.loc[keep, 'sepal_length'], df.loc[keep, 'sepal_width'], ls='None', marker='o', color=color)

# fig, ax = plt.subplots()
for l, color in colors.items():
    keep = precdit_iris['species'] ==l
    axs[1].plot(df.loc[keep, 'sepal_length'], df.loc[keep, 'sepal_width'], ls='None', marker='*', color=color)<jupyter_output><empty_output><jupyter_text>cross validation<jupyter_code>from sklearn.model_selection import cross_val_score, KFold, StratifiedKFold, RepeatedKFold
data = iris.values
label = iris['species'].values

clf = SVC(kernel='linear', C=1.)
clf = KNeighborsClassifier(15)
scores = cross_val_score(clf, data, label, cv=KFold(4, shuffle=False))
scores
scores = cross_val_score(clf, data, label, cv=KFold(4, shuffle=True))
scores
scores = cross_val_score(clf, data, label, cv=StratifiedKFold(4, shuffle=True))
scores<jupyter_output><empty_output><jupyter_text>Exercice 1On va utiliser le dataset titanic pour predire la survie des individus.Etapes: 1. Lire le dataset 2. Garder seulemennt les colonnes ['Survived', 'Age', 'Fare', 'Sex', 'Embarked'] 3. Effacer les lignes qui ont des élément vides (null) 4. Transformer les colonnes 'Sex', 'Embarked' avec la function `from sklearn.preprocessing import LabelEncoder` 5. Essayer diffrent classifier (RandomForestClassifier, KNeighborsClassifier, DecisionTreeClassifier, LogisticRegression)  Solution 1<jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
passenger = pd.read_excel('./data/dataset_titanic.xlsx')
passenger
columns = ['Survived', 'Age', 'Fare', 'Sex', 'Embarked']
data = passenger[columns]

# on enleve les valeurs manquantes
mask = np.all(data.notnull(), axis=1)
data = data[mask]

# on transform les labels en chiffre
for col in ('Sex', 'Embarked'):
    data[col] = LabelEncoder().fit_transform(data[col])
data

y = data['Survived'].values
x = data[columns[1:]].values

clf = RandomForestClassifier()
cross_val_score(clf, x, y, cv=5)
clf = KNeighborsClassifier(15)
cross_val_score(clf, x, y, cv=5)
clf = DecisionTreeClassifier()
cross_val_score(clf, x, y, cv=5)
clf = LogisticRegression()
cross_val_score(clf, x, y, cv=5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13602941176470587, 'fraction_numerical': 0.042849898580121705, 'mean_word_length': 5.754280821917808, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '373876', 'license': 'MIT', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3014, 'n_words': 839, 'path': 'notebooks/sklearn.ipynb', 'repo_name': 'crnl-lab/formation_python_crnl_2022', 'size': '7888'}