-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogreg.py
More file actions
63 lines (58 loc) · 1.85 KB
/
Copy pathlogreg.py
File metadata and controls
63 lines (58 loc) · 1.85 KB
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
# from sklearn import datasets, model_selection
# import matplotlib.pyplot as plt
import numpy as np
import streamlit as st
@st.cache
def Predict(w, X, yTruth):
X = np.hstack((np.ones((X.shape[0], 1)), X))
pred = sigmoid(np.dot(X, w)).round()
# Confusion Matrix as a list
# ConfMat[0] is True Positive
# ConfMat[1] is False Positive
# ConfMat[2] is False Negative
# ConfMat[3] is True Negative
ConfMat = [0, 0, 0, 0]
for i in range(pred.size):
if pred[i] and yTruth[i]:
# true positive
ConfMat[0] += 1
elif pred[i] and not yTruth[i]:
# false positive
ConfMat[1] += 1
elif not pred[i] and yTruth[i]:
# false negative
ConfMat[2] += 1
elif not pred[i] and not yTruth[i]:
# true negative
ConfMat[3] += 1
return ConfMat
@st.cache
def Metrics(ConfMat):
if not ConfMat[0] and not ConfMat[2]:
recall = 0.0001
else:
recall = ConfMat[0] / (ConfMat[0] + ConfMat[2])
if not ConfMat[0] and not ConfMat[1]:
precision = 0.0001
else:
precision = ConfMat[0] / (ConfMat[0] + ConfMat[1])
f1score = 2 * ((precision * recall) / (precision + recall))
return [recall, precision, f1score]
@st.cache
def sigmoid(z):
return 1 / (1 + np.exp(-z))
@st.cache
def Gradient(w, X, y):
return np.dot(X.T, sigmoid(np.dot(X, w)) - y)
@st.cache
def FindWeight(tolerance, learningrate, X, y):
# stop when reach descent tolerance
iterations = 1
X = np.hstack((np.ones((X.shape[0], 1)), X))
wLast = np.ones(X.shape[1])
wNext = wLast - learningrate * Gradient(wLast, X, y)
while abs(np.sqrt((wLast - wNext).dot(wLast - wNext))) >= tolerance:
iterations += 1
wLast = wNext
wNext = wNext - learningrate * Gradient(wNext, X, y)
return wNext, iterations