<jupyter_start><jupyter_text>0. Check version & import des modules<jupyter_code>import sklearn

print(sklearn.__version__)
!pip install --upgrade sklearn
import numpy as np

import pandas as pd

%matplotlib inline
import matplotlib.pyplot as plt

import seaborn as sns
sns.set()<jupyter_output><empty_output><jupyter_text>1. Chargement du jeu de données 'IRIS'<jupyter_code>df = sns.load_dataset('iris')

df.info()

df.sample(5)
df['species'].value_counts()<jupyter_output><empty_output><jupyter_text>2. Exploration visuelle des données (EDA)<jupyter_code>sns.pairplot(df, hue='species')<jupyter_output>/home/girardea/anaconda3/lib/python3.6/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>3. Premier algo : plus proches voisins<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
# instanciation
clf = KNeighborsClassifier()

# quels sont les facteurs explicatifs ? les 4 premières colonnes
X = df.iloc[: ,:4]

# quel est le libellé (ce qu'on cherche à deviner) ?
y = df['species']

# apprentissage
clf.fit(X, y)
# prédiction
clf.predict(X.iloc[:4])
clf.predict_proba(X.sample(5))<jupyter_output><empty_output><jupyter_text>Comparons sur l'ensemble du jeu de données.<jupyter_code>y_pred = clf.predict(X)
y_pred[:4]
(y_pred == y).mean()
df['perf'] = (y_pred != y )

sns.pairplot(df, hue='perf', vars=df.columns[:4])
neighbors_max = 150

scores = []

for i in range(1, neighbors_max):
    clf = KNeighborsClassifier(n_neighbors=i)
    
    clf.fit(X, y)
    
    y_pred = clf.predict(X)
    
    scores.append((y == y_pred).mean())
plt.plot(scores)<jupyter_output><empty_output><jupyter_text>Validation croisée**Ne pas utiliser le même jeu de données pour l'apprentissage et le test.**<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
                                                    stratify=y)

print(len(X_train), len(X_test))
neighbors_max = 105

scores = []

for i in range(1, neighbors_max):
    clf = KNeighborsClassifier(n_neighbors=i)
    
    clf.fit(X_train, y_train)
    
    y_pred = clf.predict(X_test)
    
    scores.append((y_test == y_pred).mean())
    
plt.plot(scores)<jupyter_output><empty_output><jupyter_text>Il faut aussi moyenner sur tous les splits possibles.<jupyter_code>neighbors_max = 15

nb = 1000

scores = []

for i in range(1, neighbors_max):
    
    clf = KNeighborsClassifier(n_neighbors=i)
    
    score = 0
    
    for j in range(nb):
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
                                                           stratify=y)
    
        clf.fit(X_train, y_train)

        y_pred = clf.predict(X_test)
        
        score += (y_test == y_pred).mean()
    
    scores.append(score / nb)
    
plt.plot(scores)<jupyter_output><empty_output><jupyter_text>2. Un nouvel exemple<jupyter_code>df = sns.load_dataset('titanic')

df.info()

df.sample(5)<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
survived       891 non-null int64
pclass         891 non-null int64
sex            891 non-null object
age            714 non-null float64
sibsp          891 non-null int64
parch          891 non-null int64
fare           891 non-null float64
embarked       889 non-null object
class          891 non-null category
who            891 non-null object
adult_male     891 non-null bool
deck           203 non-null category
embark_town    889 non-null object
alive          891 non-null object
alone          891 non-null bool
dtypes: bool(2), category(2), float64(2), int64(4), object(5)
memory usage: 80.6+ KB<jupyter_text>Extraire X et y<jupyter_code># On vire lees lignes avec des âges vides
df = df.dropna(subset=['age'])
X = df.loc[:, ['age', 'fare', 'pclass']]
X['male'] = (df['sex'] == 'male')

X.sample(5)
y = (df['survived'] == 1)

y[:4]
neighbors_max = 50

nb = 100

scores = []

for i in range(1, neighbors_max):
    
    clf = KNeighborsClassifier(n_neighbors=i)
    
    score = 0
    
    for j in range(nb):
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
                                                           stratify=y)
    
        clf.fit(X_train, y_train)

        y_pred = clf.predict(X_test)
        
        score += (y_test == y_pred).mean()
    
    scores.append(score / nb)
    
plt.plot(scores)
plt.ylim(0, 1)
# faux positifs (on a dit qu'ils survivraient mais en fait non)
((y_pred == True) & (y_test == False)).sum()
len(y_test)
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test, y_pred, labels=[True, False]))
# Précision
vrais_positifs = ((y_pred == True) & (y_test == True)).sum()
positifs = (y_pred == True).sum()

precision = vrais_positifs / positifs
precision
# Rappel
vivants = (y_test == True).sum()

rappel = vrais_positifs / vivants
rappel
f1_score = 2 * rappel * precision / (rappel + precision)
f1_score
from sklearn.metrics import f1_score
f1_score(y_test, y_pred)
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()

clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

f1_score(y_test, y_pred)
from sklearn.svm import SVC
clf = SVC()

clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

f1_score(y_test, y_pred)
from sklearn.metrics import precision_score, recall_score
precision_score(y_test, automl.predict(X_test))
recall_score(y_test, automl.predict(X_test))
from sklearn.linear_model import SGDClassifier
from sklearn.dummy import DummyClassifier
algos = [
    (DummyClassifier(), 'dummy'),
    (KNeighborsClassifier(), 'knn'),
    (RandomForestClassifier(n_estimators=10), 'rfc'),
    (RandomForestClassifier(n_estimators=20), 'rfc +'),
    (SVC(gamma='auto'), 'svc'),
    (SGDClassifier(max_iter=1000, tol=1e-3), 'sgd')
]

nb_splits = 100

for clf, name in algos:
    scores = []
    
    for i in range(nb_splits):
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=0.3, stratify=y)
        
        clf.fit(X_train, y_train)
        
        y_pred = clf.predict(X_test)
        
        scores.append(f1_score(y_test, y_pred))
    
    plt.plot(np.sort(scores), label=name)

plt.ylim(0, 1)
plt.legend()<jupyter_output>/home/girardea/anaconda3/lib/python3.6/site-packages/sklearn/dummy.py:227: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
  k in range(self.n_outputs_)).T
/home/girardea/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.
  'precision', 'predicted', average, warn_for)
/home/girardea/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.
  'precision', 'predicted', average, warn_for)
/home/girardea/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.
  'precision',[...]<jupyter_text>Quelle colonne est la plus utile ?<jupyter_code>clf = RandomForestClassifier(n_estimators=10)

nb_splits = 100

for col in X.columns:
    scores = []
    
    if col == 'male':
        continue
        
    for i in range(nb_splits):
        X_train, X_test, y_train, y_test = train_test_split(
            X[['male', col]], y, test_size=0.3, stratify=y)
        
        clf.fit(X_train, y_train)
        
        y_pred = clf.predict(X_test)
        
        scores.append(f1_score(y_test, y_pred))
    
    plt.plot(np.sort(scores), label=col)

plt.ylim(0, 1)
plt.legend()<jupyter_output><empty_output><jupyter_text>Nouveau jeu de données : résiliations<jupyter_code># https://query.data.world/s/jb75k2kxn5ajlcxsmvlnorxyj4pisx
df = pd.read_csv('https://bit.ly/2Pkupqx')

df.info()

df.sample(3)
for time in ['day', 'eve', 'night', 'intl']:
    df[f'avg{time}duration'] = (
        df[f'total{time}minutes'] / df[f'total{time}calls']
    ).fillna(-1.0)<jupyter_output><empty_output><jupyter_text>Rapide analyse des valeurs extrêmes<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>Performance d'un algo stupide<jupyter_code># Facteurs explicatifs : tout sauf 'churn'
X = df.drop(columns=['churn'])

# On change les yes/no en True/False
X['internationalplan'] = (X['internationalplan'] == 'yes')
X['voicemailplan'] = (X['voicemailplan'] == 'yes')

# Cible
y = (df['churn'] == 'Yes')
def compute_scores(clf, X, y, nb_splits=100):
    
    scores = []
    
    for i in range(nb_splits):
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=0.3, stratify=y)
        
        clf.fit(X_train, y_train)
        
        y_pred = clf.predict(X_test)
        
        scores.append(f1_score(y_test, y_pred))
    
    return scores
import warnings

warnings.filterwarnings("ignore")
algos = [
    (DummyClassifier(), 'dummy'),
    (KNeighborsClassifier(), 'knn'),
    (RandomForestClassifier(n_estimators=10), 'rfc'),
    (RandomForestClassifier(n_estimators=20), 'rfc +'),
    (SVC(gamma='auto'), 'svc'),
    (SGDClassifier(max_iter=1000, tol=1e-3), 'sgd')
]

plt.figure(figsize=(8, 8))

for clf, name in algos:
    scores = compute_scores(clf, X, y)
    plt.plot(np.sort(scores), label=name)

plt.ylim(0, 1)
plt.legend()
clf = RandomForestClassifier()

plt.figure(figsize=(8, 8))

for col in X.columns:
    scores = compute_scores(clf, X[[col]], y)
    plt.plot(np.sort(scores), label=col)
    
    print(col, np.mean(scores))

plt.ylim(0, 1)
plt.legend()
!pip install auto-sklearn
import autosklearn.classification
import autosklearn.metrics
automl = autosklearn.classification.AutoSklearnClassifier(
    time_left_for_this_task=5*60
)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, stratify=y
)

automl.fit(X_train, y_train, metric=autosklearn.metrics.f1)

y_pred = automl.predict(X_test)

f1_score(y_test, y_pred)
dft = pd.DataFrame(automl.cv_results_)

dft.sort_values(by='rank_test_scores').head()
dft.sort_values(by='rank_test_scores').iloc[0, :].to_dict()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1277726001863933, 'fraction_numerical': 0.022739981360671017, 'mean_word_length': 3.7757009345794392, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '184855', 'license': 'MIT', 'n_tokens_mistral': 4261, 'n_tokens_neox': 4051, 'n_words': 843, 'path': 'sessions/190329_IMIE_PM.ipynb', 'repo_name': 'girardea/cours-data', 'size': '10730'}