Contenuti del Corso
Regressione Lineare con Python
Regressione Lineare con Python
Costruzione della Regressione Lineare Multipla
La classe OLS
consente di costruire una Regressione Lineare Multipla nello stesso modo della Regressione Lineare Semplice. Tuttavia, la funzione np.polyfit()
non gestisce il caso con più variabili indipendenti.
Utilizzeremo la classe OLS
.
Costruzione della matrice X̃
Abbiamo lo stesso dataset dell'esempio di regressione lineare semplice, ma ora include l'altezza della madre come seconda variabile. Lo caricheremo e osserveremo la sua variabile X
:
import pandas as pd import statsmodels.api as sm file_link='https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/heights_two_feature.csv' df = pd.read_csv(file_link) # Open the file # Assign the variables X = df[['Father', 'Mother']] y = df['Height'] print(X.head())
Ricorda che è necessario utilizzare OLS(y, X_tilde)
per inizializzare l'oggetto OLS
. Come puoi vedere, la variabile X contiene già due caratteristiche in colonne separate. Quindi, per ottenere X_tilde, è sufficiente aggiungere una colonna di 1 come prima colonna. La funzione sm.add_constant(X)
svolge esattamente questa operazione!
import pandas as pd import statsmodels.api as sm file_link='https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/heights_two_feature.csv' df = pd.read_csv(file_link) # Open the file # Assign the variables X = df[['Father', 'Mother']] y = df['Height'] # Create X_tilde X_tilde = sm.add_constant(X) print(X_tilde.head())
Trovare i Parametri
Ottimo! Ora possiamo costruire il modello, trovare i parametri ed effettuare le previsioni nello stesso modo in cui abbiamo fatto nella sezione precedente.
import pandas as pd import statsmodels.api as sm import numpy as np file_link='https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/heights_two_feature.csv' df = pd.read_csv(file_link) # Open the file X,y = df[['Father', 'Mother']], df['Height'] # Assign the variables X_tilde = sm.add_constant(X) # Create X_tilde # Initialize an OLS object regression_model = sm.OLS(y, X_tilde) # Train the object regression_model = regression_model.fit() # Get the paramters beta_0, beta_1, beta_2 = regression_model.params print('beta_0 is: ', beta_0) print('beta_1 is: ', beta_1) print('beta_2 is: ', beta_2) # Predict new values X_new = np.array([[65, 62],[70, 65],[75, 70]]) # Feature values of new instances X_new_tilde = sm.add_constant(X_new) # Preprocess X_new y_pred = regression_model.predict(X_new_tilde) # Predict the target print('Predictions:', y_pred)
Ora che il nostro set di addestramento ha 2 caratteristiche, dobbiamo fornire 2 caratteristiche per ogni nuova istanza che vogliamo prevedere. Ecco perché np.array([[65, 62],[70, 65],[75, 70]])
è stato utilizzato nell'esempio sopra. Prevede y
per 3 nuove istanze: [Father:65,Mother:62]
, [Father:70, Mother:65]
, [Father:75, Mother:70]
.
Grazie per i tuoi commenti!