Skip to content

Instantly share code, notes, and snippets.

@Nick3523
Created January 27, 2020 10:37
Show Gist options
  • Save Nick3523/b17fa0ba5571532378451e07034ec71b to your computer and use it in GitHub Desktop.
Save Nick3523/b17fa0ba5571532378451e07034ec71b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf-8
# # Imports & Params
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
get_ipython().run_line_magic('matplotlib', 'inline')
# On charge le dataset
house_data = pd.read_csv('house.csv').rename(columns={"price" : "loyer"}).dropna().reset_index(drop = True)
house_data = house_data[house_data['loyer'] < 8000]
#print(house_data['arrondissement'].unique()) : On voit que tous les arrondissement ne sont pas présents
ax = house_data.plot(x="loyer", y="surface", kind="scatter", c="arrondissement", colormap='viridis', title="Prix des loyers en fonction de la surface, colorés par arrondissement")
ax1 = sns.violinplot(x="arrondissement", y="loyer", hue='arrondissement', data=house_data, scale="count",gridsize=200,dodge=False)
ax1.set_title("Violinplot représentant le loyer par arrondissement")
ax1.set_xlabel('Arrondissement')
ax1.set_ylabel('Loyer')
#from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(8,8)).gca(projection='3d')
# Pour faciliter la visualisation, on va changer la valeur de l'arrondissement (10), il sera rennomé en 5
house_data['arrondissement'][house_data['arrondissement'] == 10] = 5
fig.set_title("Evolution du loyer en fonction de la surface et de l'arrondissement")
fig.set_xlim(0, 5)
fig.set_xlabel('Arrondissement')
fig.set_ylabel('Surface')
fig.set_zlabel('Loyer')
fig.scatter(house_data['arrondissement'],house_data['surface'], house_data['loyer'], c=house_data['arrondissement'], cmap="viridis")
plt.show()
# ### One hot encoding :
#Utilisiation du one hot encoding, pour retraiter les données catégorielles du numéro de l'arrondissement :
one_hot = pd.get_dummies(house_data['arrondissement'],prefix='arr')
house_data = house_data.drop('arrondissement',axis = 1)
house_data = house_data.join(one_hot)
house_data.head()
#house_data.describe()
# ### Découpage en train et en test set :
xtrain, xtest, ytrain, ytest = train_test_split(house_data.drop('loyer',axis = 1), house_data['loyer'], train_size=0.8, test_size = 0.2)
# ### Entrainement du modèle :
def customPrediction(surface, arrondissement):
lr = LinearRegression()
lr.fit(xtrain, ytrain)
erreur = 1-lr.score(xtrain, ytrain)
print('Taux d\'erreurs : %f' % erreur)
g = np.matrix([surface,arrondissement==1,arrondissement==2,arrondissement==3,arrondissement==4,arrondissement==10])
return lr.predict(g)[0]
# ### Prédictions :
superficie = 31
arrondissement = 1
print("Pour un logement de",superficie,"m² au",arrondissement,"arrondissement, le loyer selon le modèle est de : ",round(customPrediction(superficie,arrondissement)),"€")
superficie = 31
arrondissement = 10
print("Pour un logement de",superficie,"m² au",arrondissement,"arrondissement, le loyer selon le modèle est de : ",round(customPrediction(superficie,arrondissement)),"€")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment