<jupyter_start><jupyter_text>Importation de la donnée<jupyter_code>df = pd.read_csv(r"/home/apprenant/simplon_projects/house_price/data/train.csv")
numeric_cols = df.select_dtypes(include=[ 'int64','float64' ])
categorical_cols = df.select_dtypes(include=[ 'object' ])
all_numeric = numeric_cols.join(pd.get_dummies(categorical_cols))
all_numeric = all_numeric.drop(columns=['SalePrice'])
all_numeric.head()
all_numeric.isna().sum().sort_values()
all_numeric = all_numeric.drop(columns=['MasVnrArea', 'GarageYrBlt', 'LotFrontage'])
dummies = pd.get_dummies(categorical_cols)<jupyter_output><empty_output><jupyter_text>Sélection des variables explicatives<jupyter_code>y = df.SalePrice
X = all_numeric
from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(X, y, train_size=0.8)
from sklearn import linear_model
from sklearn.metrics import r2_score
regr = linear_model.LinearRegression()

list = [xtrain, ytrain]

regr.fit(*list)

print("Régression linéaire")
print(f"Les coefficients de régression sont : {regr.coef_}")
print(f"L'ordonnée à l'origine est : {round(regr.intercept_)}\n")


ridge_regr = linear_model.Ridge(alpha=1)
ridge_regr.fit(*list)

print("Ridge")
print(f"Les coefficients de régression sont : {ridge_regr.coef_}")
print(f"L'ordonnée à l'origine est : {round(ridge_regr.intercept_)}\n")


lasso_regr = linear_model.Lasso(alpha=177)
lasso_regr.fit(*list)

print("Lasso")
print(f"Les coefficients de régression sont : {lasso_regr.coef_}")
print(f"L'ordonnée à l'origine est : {round(lasso_regr.intercept_)}\n")
ytrainpred_linear = regr.predict(xtrain)
ytestpred_linear = regr.predict(xtest)
ytrainpred_ridge = ridge_regr.predict(xtrain)
ytestpred_ridge = ridge_regr.predict(xtest)
ytrainpred_lasso = lasso_regr.predict(xtrain)
ytestpred_lasso = lasso_regr.predict(xtest)

from sklearn.metrics import mean_absolute_error, mean_squared_error

def display_metrics(train, pred):
    print(f'R2 : {r2_score(train, pred)}')
    print(f'Root mean squared error : {mean_squared_error(train, pred, squared=False)}\n')



print('Train - Linear regression')
display_metrics(ytrain, ytrainpred_linear)

print('Train - Ridge')
display_metrics(ytrain, ytrainpred_ridge)

print('Train - Lasso')
display_metrics(ytrain, ytrainpred_lasso)

print('Test - Linear regression')
display_metrics(ytest, ytestpred_linear)

print('Test - Ridge')
display_metrics(ytest, ytestpred_ridge)

print('Test - Lasso')
display_metrics(ytest, ytestpred_lasso)

sns.scatterplot(x=ytest, y=ytestpred_linear)
sns.scatterplot(x=ytest, y=ytestpred_lasso)
sns.scatterplot(x=ytest, y=ytestpred_ridge)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12754716981132075, 'fraction_numerical': 0.004905660377358491, 'mean_word_length': 8.535971223021583, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '634783', 'license': 'Apache-2.0', 'n_tokens_mistral': 1027, 'n_tokens_neox': 998, 'n_words': 159, 'path': 'notebooks/.ipynb_checkpoints/price_prediction-checkpoint.ipynb', 'repo_name': 'ATelders/house_price', 'size': '2650'}