-
Notifications
You must be signed in to change notification settings - Fork 0
/
logistic_reg.py
136 lines (130 loc) · 5.4 KB
/
logistic_reg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pandas as pd
import numpy as np
from sklearn import linear_model
from sklearn import metrics
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.offline as py
import plotly.graph_objs as go
from plotly.graph_objs import *
from flask import Flask,render_template,request, redirect, url_for
def logistic_reg1(dataset,target,dataset_file_name):
dataset=dataset.dropna()
def plot_boxplot(dataset,cat_var):
cols=list(dataset.columns)
cols=[i for i in cols + list(cat_var) if i not in cols or i not in cat_var]
trace=[]
for i in cols:
trace0=go.Box(y=dataset[i].values,name=i)
trace.append(trace0)
layout = go.Layout(
title=go.layout.Title(
text='Boxplot of numerical variables',
),
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text='Attribute',
)
),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text='Values',
)
))
fig = go.Figure(data=trace, layout=layout)
fig=py.plot(fig, filename='static/Plots/Logistic Regression/'+dataset_file_name+'/Numerical Variables/Boxplot of numerical variables.html',auto_open=False)
return
num_plots=[]
filename1='static/Plots/Logistic Regression/'+dataset_file_name+'/Numerical Variables/Boxplot of numerical variables.html'
num_plots.append(filename1.replace(" ","%20"))
cat_plots=[]
cat_var=[]
for i in dataset.columns.values:
if len(set(dataset[i]))<=dataset.shape[0]//4:
cat_var.append(i)
for i in cat_var:
if i!=target:
if len(set(dataset[i]))<=10:
ax=sns.countplot(x=i,data=dataset)
plt.title('Values of Attribute \"'+i+"\"",loc='center')
plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')
filename='static/Plots/Logistic Regression/'+dataset_file_name+'/Categorical Variables/Attribute '+i+'.png'
cat_plots.append(filename.replace(" ","%20"))
plt.savefig(filename,bbox_inches='tight')
plt.cla()
i=target
if len(set(dataset[i]))<=10:
ax=sns.countplot(x=i,data=dataset)
plt.title('Values of Attribute \"'+i+"\"",loc='center')
plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')
plt.tight_layout()
filenams='static/Plots/Logistic Regression/'+dataset_file_name+'/Categorical Variables/Attribute '+i+'.png'
cat_plots.append(filename.replace(" ","%20"))
plt.savefig(filename,bbox_inches='tight',dpi=600)
plt.cla()
plt.close()
plot_boxplot(dataset,cat_var)
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
cat_var=dataset.select_dtypes(include=['O']).columns.values
for i in cat_var:
labelencoder.fit(list(dataset[i]))
dataset[i] = labelencoder.transform(list(dataset[i]))
X = dataset.iloc[:, dataset.columns != target]
y = dataset[target]
X = np.append ( arr = np.ones([X.shape[0],1]).astype(int), values = X, axis = 1)
X_opt = X[:,:]
import statsmodels.api as sm
logit_model=sm.Logit(y,X)
result=logit_model.fit()
flag=0
to_keep=[i for i in range(X.shape[1])]
dont_remove=[]
while flag!=1:
max_p=0
ind=0
for j in range(len(to_keep)):
if result.pvalues[j]>0.05:
if result.pvalues[j]>max_p and to_keep[j] not in dont_remove:
max_p=result.pvalues[j]
ind=j
if max_p>0.05:
inp=1
if inp!=1:
to_keep.remove(to_keep[ind])
else:
dont_remove.append(to_keep[ind])
continue
else:
flag=0
break
if X_opt.shape[1]==len(to_keep):
flag=0
else:
X_opt=X[:,to_keep]
result = sm.Logit(endog = y, exog = X_opt).fit()
X_train, X_test, y_train, y_test = train_test_split(X_opt, y, test_size = 1/3, random_state = 0)
mul_lr = linear_model.LogisticRegression(multi_class='multinomial', solver='newton-cg',max_iter=200).fit(X_train, y_train)
y_pred=mul_lr.predict(X_test)
conf_mat=[]
if len(set(dataset[target]))<=10:
ax=sns.heatmap(metrics.confusion_matrix(y_test,y_pred),annot=True)
ax.set_title('Confusion Matrix of Trained Logistic Regression Classifier',loc='center')
ax.set_xlabel('Actual target value')
ax.set_ylabel('Predicted Value')
filename='static/Plots/Logistic Regression/'+dataset_file_name+'/Metrics/confusion_matrix.png'
conf_mat.append(filename.replace(" ","%20"))
plt.savefig(filename,dpi=1000)
plt.cla()
plt.close()
acc=metrics.accuracy_score(y_test, y_pred)
if len(set(dataset[target]))!=2:
f1=metrics.f1_score(y_test, y_pred,average="macro")
precision=metrics.precision_score(y_test, y_pred,average="macro")
recall=metrics.recall_score(y_test, y_pred,average="macro")
else:
f1=metrics.f1_score(y_test, y_pred)
precision=metrics.precision_score(y_test, y_pred)
recall=metrics.recall_score(y_test, y_pred)
return acc,f1,precision,recall,num_plots,cat_plots,conf_mat