-
Notifications
You must be signed in to change notification settings - Fork 2
/
Regression_Playground.py
59 lines (48 loc) · 1.58 KB
/
Regression_Playground.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
#!/usr/bin/env python
# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
from sklearn import linear_model
from sklearn.metrics import confusion_matrix
data = pd.read_csv("datasets_2607_4342_indian_liver_patient_labelled.csv")
df = pd.DataFrame(data)
for col in df.columns:
df[col] = df[col].fillna(0)
# Linear Regression
headers = list(df.columns)
headers.remove('Dataset')
headers.remove('Gender')
X = df[headers]
Y = df['Dataset']
linregmodel = linear_model.LinearRegression()
linregmodel.fit(X, Y)
# print("Intercept is %f" % linregmodel.intercept_)
# print("Coefficients are", linregmodel.coef_)
# print("R2 score is", linregmodel.score(X, Y))
Y_pred = linregmodel.predict(X)
# Logistic Regression
df['intercept'] = linregmodel.intercept_
df['Dataset'] = df['Dataset'].replace([1], 0)
df['Dataset'] = df['Dataset'].replace([2], 1)
logregmodel = sm.Logit(Y, X)
result = logregmodel.fit()
print(result.summary())
print("Confidence intervals are", result.conf_int())
Y_pred = logregmodel.predict(X.T)
cnf_matrix = confusion_matrix(Y, Y_pred[:, 0:1])
class_names = [0, 1]
fig, ax = plt.subplots()
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks, class_names)
plt.yticks(tick_marks, class_names)
sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="viridis", fmt='g')
ax.xaxis.set_label_position("top")
plt.tight_layout()
plt.title('Confusion matrix', y=1.1)
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
# everything seems to be labelled 1?
print("Accuracy is thus", (416-167) / 416)