-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathml.py
148 lines (112 loc) · 3.59 KB
/
ml.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
137
138
139
140
141
142
143
144
145
146
147
148
import csv
import sys
import string
import copy
from collections import Counter
from operator import itemgetter
import numpy as np
from math import exp
import matplotlib.pyplot as plt
#from tabulate import tabulate
from collections import defaultdict
from math import log
from scipy import stats
def sigmoid(X):
return sigmoid(X)
train = 1000
total=0
product_id = np.zeros((train,1))
customer_id = np.zeros((train,1))
price = np.zeros((train,1))
dataset=np.zeros((train*3,2))
dataset2=np.zeros((train*3,2))
test = 0
with open('test_values.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
i = 0
for string in row:
if i == 1 and total > 0 and total <=train:
#print(total)
product_id[total-1]= float(string)
if i == 2 and total > 0 and total <=train:
customer_id[total-1]=(float(string))
if i == 5 and total > 0 and total <=train:
price[total-1]=(float(string))
#print(i,string)
i=i+1
if(total<=train and total>0):
dataset[total-1] = [customer_id[total-1],price[total-1]]
dataset2[total-1] = [customer_id[total-1],product_id[total-1]]
total=total+1
test=test+1
if(total>train):
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
i = 0
for string in row:
if i == 1:
#print(total)
product_id[total-test]= float(string)
if i == 2:
customer_id[total-test]=(float(string))
if i == 5:
price[total-test]=(float(string))
#print(i,string)
i=i+1
dataset[total-1] = [customer_id[total-test],price[total-test]]
dataset2[total-1] = [customer_id[total-test],product_id[total-test]]
def lr_train(train_vectors, iterations=100, reg=0.01,
lr=0.01, stop_diff=int(1e-6)):
count_w = train
w = np.zeros((count_w,1 ))
for i in range(iterations):
grad = np.zeros((count_w, 2))
for vector in train_vectors:
y = vector[1]
x = vector[0]
y_hat = lr_calc_prob(w, y)
grad += (y - y_hat)*x
#print(grad,"grad",reg,"reg",w,"w")
w = [i*reg for i in w]
grad -= w
w += lr * grad
if np.linalg.norm(grad) <= stop_diff:
break
return w
def lr_calc_prob(w, x):
#print(np.sum(np.dot(w,x)))
denominator = 1 + exp(-1 * np.sum(np.dot(w,x)))
return denominator
def lr_classify(w, x):
return lr_calc_prob(w, x)
def lr_train_test(train_vectors, test_vectors):
w = lr_train(train_vectors)
incorrect = 0.0
for vector in test_vectors:
y = vector[0]
x = vector[1]
y_hat = lr_classify(w, x)
if y_hat is not y:
incorrect += 1
return incorrect/len(test_vectors)
zero_one_loss = lr_train(dataset)
i = test
op = [0];
for vector in dataset:
y = vector[1]
x = vector[0]
while i < total:
pred_value = zero_one_loss*op
if pred_value - x > 10:
print("failed")
zero_one_loss = lr_train(dataset2)
i = test
op = [0];
for vector in dataset2:
y = vector[1]
x = vector[0]
while i < total:
pred_value = zero_one_loss*op
if pred_value - x > 10:
print("failed second")