Skip to content

Instantly share code, notes, and snippets.

@Nick3523
Last active January 18, 2020 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nick3523/7a3ed8d5586cc0d0c1b1ab76a95ff2af to your computer and use it in GitHub Desktop.
Save Nick3523/7a3ed8d5586cc0d0c1b1ab76a95ff2af to your computer and use it in GitHub Desktop.
## Imports & Params
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
# On charge le dataset
house_data = pd.read_csv('ParisHouse.csv')
house_data = house_data[house_data['loyer'] < 10000] #Garder uniquement les loyer < 10k
# On décompose le dataset et on le transforme en matrices pour pouvoir effectuer notre calcul
X = np.matrix([np.ones(house_data.shape[0]), house_data['surface'].values]).T
y = np.matrix(house_data['loyer']).T
# On effectue le calcul exact du paramètre theta
theta = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
'La fonction customePrediction recrée la régression linéaire'
'comme la solution est : (XTX)^−1. XT. y (Avec T : Transposé de la matrice)'
'Il est possible de se passer de sklearn pour ce problème relativement simple'
def customPrediction(theta, superficie):
return theta.item(0) + theta.item(1) * superficie
'Cette fonction, utilise le modèle déjà implementé dans sklearn'
'Le but est de comparer les prédictions avec mon modèle custom'
def kitPrediction(superficie):
regr = linear_model.LinearRegression()
regr.fit(X, y)
g = np.matrix([[0, superficie]])
return regr.predict(g)[0][0]
superficie = 31
print("Custom - Pour un logement de ",superficie,"m² le loyer selon le modèle est de : ",round(customePrediction(theta, superficie)),"€")
print("Sklearn - Pour un logement de",superficie,"m² le loyer selon le modèle est de : ",round(kitPrediction(superficie)),"€")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment