Vous êtes sur la page 1sur 4

Régression

linéaire simple

Importation des packages


import pandas as pd
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
import seaborn as sns

Chargement des données


#Lire les fichiers CSV
#Un moyen simple pour stocker de grands ensembles de données qui consiste à utiliser des fichiers CSV
#(fichiers séparés par des virgules).

df = pd.read_csv('C:/Users/pc/Downloads/dataset0.csv')

plt.plot(df['X'],df['Y'],'o')
df.head()

X Y

0 1714 2.40

1 1664 2.52

2 1760 2.54

3 1685 2.74

4 1693 2.83

Visualisation
#seaborn.regplot() :
#Cette méthode est utilisée pour tracer les données et un modèle de régression linéaire
#Syntax : seaborn.regplot( x, y, data=None,...)
#Paramètres :
#x, y : ce sont des variables d'entrée.
# S'il s'agit de chaînes de caractéres, celles-ci doivent correspondre aux noms de colonne dans "data".
#data : Il s'agit d'une base de données où chaque colonne est une variable et chaque ligne est une observation.

sns.regplot('X', 'Y', data=df)



#plt.show()

C:\Users\pc\anaconda3\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables


as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other
arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
<AxesSubplot:xlabel='X', ylabel='Y'>
Création du modèle
#Syntaxe : statsmodels.formula.api.ols(formula, data,...)
#Créer un modèle à partir d'une formule et un dataframe.
#r-squared : mesurer la corrélation entre deux variables.

model = smf.ols(formula='Y ~ X', data=df).fit()


print(model.summary())

OLS Regression Results


==============================================================================
Dep. Variable: Y R-squared: 0.406
Model: OLS Adj. R-squared: 0.399
Method: Least Squares F-statistic: 56.05
Date: Sun, 05 Mar 2023 Prob (F-statistic): 7.20e-11
Time: 17:11:07 Log-Likelihood: 12.672
No. Observations: 84 AIC: -21.34
Df Residuals: 82 BIC: -16.48
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 0.2750 0.409 0.673 0.503 -0.538 1.088
X 0.0017 0.000 7.487 0.000 0.001 0.002
==============================================================================
Omnibus: 12.839 Durbin-Watson: 0.950
Prob(Omnibus): 0.002 Jarque-Bera (JB): 16.155
Skew: -0.722 Prob(JB): 0.000310
Kurtosis: 4.590 Cond. No. 3.29e+04
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 3.29e+04. This might indicate that there are
strong multicollinearity or other numerical problems.

ypred = model.predict(df['X'])
print(ypred)
0 10.977501
1 12.604424
2 12.774782
3 13.106981
4 14.137650
5 14.810565
6 14.478367
7 13.532877
8 14.904263
9 15.662358
10 16.428970
11 17.706658
12 19.495422
13 16.940046
14 13.626575
15 14.648725
16 15.151282
17 16.088254
18 16.778205
19 15.236462
20 17.033743
21 17.204101
22 18.481789
dtype: float64

Régression multiple

Importation des packages


import pandas as pd
import statsmodels.api as sm

Chargement des données


# Read a csv file into Pandas Dataframe
df = pd.read_csv('C:/Users/pc/Downloads/dataset.csv')
print(df.head())

X2 X3 X4 X1
0 165349.20 136897.80 471784.10 192261.83
1 162597.70 151377.59 443898.53 191792.06
2 153441.51 101145.55 407934.54 191050.39
3 144372.41 118671.85 383199.62 182901.99
4 142107.34 91391.77 366168.42 166187.94

Création du modèle
y=df['X1']
x=df[['X2','X3','X4']]
x=sm.add_constant(x)
# Create a fitted model
lm = sm.OLS(y,x).fit()

# Print model summary


print(lm.summary())
OLS Regression Results
==============================================================================
Dep. Variable: X1 R-squared: 0.951
Model: OLS Adj. R-squared: 0.948
Method: Least Squares F-statistic: 296.0
Date: Sun, 05 Mar 2023 Prob (F-statistic): 4.53e-30
Time: 17:11:24 Log-Likelihood: -525.39
No. Observations: 50 AIC: 1059.
Df Residuals: 46 BIC: 1066.
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 5.012e+04 6572.353 7.626 0.000 3.69e+04 6.34e+04
X2 0.8057 0.045 17.846 0.000 0.715 0.897
X3 -0.0268 0.051 -0.526 0.602 -0.130 0.076
X4 0.0272 0.016 1.655 0.105 -0.006 0.060
==============================================================================
Omnibus: 14.838 Durbin-Watson: 1.282
Prob(Omnibus): 0.001 Jarque-Bera (JB): 21.442
Skew: -0.949 Prob(JB): 2.21e-05
Kurtosis: 5.586 Cond. No. 1.40e+06
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.4e+06. This might indicate that there are
strong multicollinearity or other numerical problems.

ypred = lm.predict(x)
print(ypred)

0 192521.252890
1 189156.768232
2 182147.279096
3 173696.700026
4 172139.514183
5 163580.780571
6 158114.096669
7 160021.363048
8 151741.699699
9 154884.684110
10 135509.016367
11 135573.712961
12 129138.054182
13 127487.991663
14 149548.646335
15 146235.159985
16 116915.405401
17 130192.447208
18 129014.226806
19 115635.216367
20 116639.669231
21 117319.451640
22 114706.981717
23 109996.615221
24 113362.966113
25 102237.725065
26 110600.575350
27 114408.071457
28 101660.026005
29 101794.983452
30 99452.372936
31 97687.856276
32 99001.328985
33 97915.007805
34 89039.273741
35 90511.599568
36 75286.174585
37 89619.537708
38 69697.430648
39 83729.011977
40 74815.953991
41 74802.556239
42 70620.411821
43 60167.039963
44 64611.354916
45 47650.649687
46 56166.206853
47 46490.588983
48 49171.388158
49 48215.134111
dtype: float64


Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

Vous aimerez peut-être aussi