<jupyter_start><jupyter_text>0. Import des modules<jupyter_code># pour créer des graphes
%matplotlib inline
import matplotlib.pyplot as plt

# pour générer les aléas
import numpy as np

# pour gérer des tables de données
import pandas as pd

# pour faire des graphes statistiques
import seaborn as sns
sns.set()<jupyter_output><empty_output><jupyter_text>1. Régression simple<jupyter_code>x = np.random.uniform(0, 15, size=100)
y = 3 * np.sin(x) + np.random.normal(size=len(x))

plt.scatter(x, y)
from sklearn.linear_model import LinearRegression
X_train = np.array(x).reshape(-1, 1)
y_train = y

X_test = np.arange(0, 15, 0.1).reshape(-1, 1)

reg = LinearRegression()

reg.fit(X_train, y_train)

y_pred = reg.predict(X_test)

plt.scatter(X_train, y_train)
plt.plot(X_test, y_pred, color='red')
from sklearn.neighbors import KNeighborsRegressor
reg = KNeighborsRegressor()

reg.fit(X_train, y_train)

y_pred = reg.predict(X_test)

plt.scatter(X_train, y_train)
plt.plot(X_test, y_pred, color='red')
from sklearn.svm import SVR
reg = SVR()

reg.fit(X_train, y_train)

y_pred = reg.predict(X_test)

plt.scatter(X_train, y_train)
plt.plot(X_test, y_pred, color='red')<jupyter_output>/home/girardea/anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)<jupyter_text>1.2. Score<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
for reg in [LinearRegression(), KNeighborsRegressor(), SVR()]:
    scores = []
    for i in range(100):
        X_train, X_test, y_train, y_test = train_test_split(np.array(x).reshape(-1, 1), y,
                                                            test_size=0.3)
        
        reg.fit(X_train, y_train)

        scores.append(r2_score(y_test, reg.predict(X_test)))
    
    moy = np.mean(scores)
    std = np.std(scores)
    
    print(f"R2-score={moy:.2f} +/- {2*std:.2f} (95% IC)")<jupyter_output>R2-score=-0.04 +/- 0.13 (95% IC)
R2-score=0.74 +/- 0.12 (95% IC)
R2-score=0.74 +/- 0.10 (95% IC)<jupyter_text>2. Jeu de données réel : immobilier<jupyter_code>df_train = pd.read_csv('../tutoriels/data/house-prices-advanced-regression-techniques/train.csv')

df_train.info()

df_train.sample(5)
plt.scatter(df_train['GrLivArea'], df_train['SalePrice'])
X = df_train[['GrLivArea']]
y = df_train['SalePrice']

reg = LinearRegression()

reg.fit(X, y)

X_new = np.arange(df_train['GrLivArea'].min(), df_train['GrLivArea'].max(), step=100).reshape(-1, 1)
y_pred = reg.predict(X_new)

plt.scatter(X, y)
plt.plot(X_new, y_pred, color='red')
from sklearn.metrics import mean_squared_log_error
np.sqrt(mean_squared_log_error(y, reg.predict(X)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1418709009319986, 'fraction_numerical': 0.026924404556437694, 'mean_word_length': 4.583815028901734, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '486734', 'license': 'MIT', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1147, 'n_words': 230, 'path': 'sessions/190416_ESPL_PM.ipynb', 'repo_name': 'girardea/cours-data', 'size': '2897'}