Logistics Regression
February 16, 2021
[43]: import pandas as pd
from scipy import stats
import scipy
import statsmodels.api as sm
from statsmodels.formula.api import ols
import matplotlib as mpl
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import statsmodels.api as s
from sklearn.metrics import mean_squared_error
import seaborn as sns
[17]: lgit=pd.read_excel(r'C:\Users\Nazakat ali\Desktop\python\logitdata.xlsx')
lgit
[17]: y x1 x2 x3 x4 age
0 1 0 1 1 1 4.67
1 1 0 1 1 1 5.67
2 1 0 1 0 1 6.00
3 1 0 1 1 1 7.08
4 1 0 1 1 1 8.00
.. .. .. .. .. .. …
90 2 0 2 0 2 6.25
91 2 0 2 1 2 7.50
92 2 0 2 1 2 8.50
93 2 0 2 0 2 9.67
94 1 0 1 1 2 10.17
[95 rows x 6 columns]
[41]: x=lgit[['y','x2','x1','x4','age']]
Y=lgit['x3']
[19]: plt.scatter(lgit.age,lgit.y,marker='+',color='b')
[19]: <matplotlib.collections.PathCollection at 0x27f0e545880>
1
1 Logistics Regression (Binary logistics Reg ’y always 0 and 1 form
response)
[44]: sns.countplot(x='x4',data=lgit)
[44]: <AxesSubplot:xlabel='x4', ylabel='count'>
2
[47]: sns.countplot(x='x3',data=lgit)
[47]: <AxesSubplot:xlabel='x3', ylabel='count'>
3
[48]: lgit['age'].plot.hist()
[48]: <AxesSubplot:ylabel='Frequency'>
[42]: lgitm=sm.Logit(Y,x)
result=lgitm.fit()
print(result.summary2())
Optimization terminated successfully.
Current function value: 0.541433
Iterations 5
Results: Logit
===============================================================
Model: Logit Pseudo R-squared: 0.061
Dependent Variable: x3 AIC: 112.8723
Date: 2021-02-16 23:01 BIC: 125.6417
No. Observations: 95 Log-Likelihood: -51.436
Df Model: 4 LL-Null: -54.752
Df Residuals: 90 LLR p-value: 0.15671
Converged: 1.0000 Scale: 1.0000
No. Iterations: 5.0000
-----------------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------------
y -0.4384 0.5701 -0.7691 0.4418 -1.5558 0.6789
4
x2 -0.1829 0.5830 -0.3137 0.7538 -1.3256 0.9598
x1 -1.3874 0.7530 -1.8424 0.0654 -2.8633 0.0886
x4 0.5163 0.5915 0.8729 0.3827 -0.6430 1.6755
age 0.1106 0.0644 1.7187 0.0857 -0.0155 0.2367
===============================================================
[ ]: