-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopping.py
203 lines (166 loc) · 7.04 KB
/
shopping.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import csv
import sys
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
TEST_SIZE = 0.4
def main():
# Check command-line arguments
if len(sys.argv) != 2:
sys.exit("Usage: python shopping.py data")
# Load data from the CSV file and split it into training and test sets
evidence, labels = load_data(sys.argv[1])
# print(f"This is evidence: {evidence}")
# print(f"This is labels: {labels}")
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
evidence, labels, test_size=TEST_SIZE
)
# Train the model and make predictions
model = train_model(X_train, y_train)
predictions = model.predict(X_test)
sensitivity, specificity = evaluate(y_test, predictions)
# Print results
print(f"Correct: {(y_test == predictions).sum()}")
print(f"Incorrect: {(y_test != predictions).sum()}")
print(f"True Positive Rate: {100 * sensitivity:.2f}%")
print(f"True Negative Rate: {100 * specificity:.2f}%")
def load_data(filename):
"""
Load shopping data from a CSV file `filename` and convert it into a list of
evidence lists and a list of labels. Return a tuple (evidence, labels).
Evidence should be a list of lists, where each list contains the
following values, in order:
- Administrative, an integer
- Administrative_Duration, a floating-point number
- Informational, an integer
- Informational_Duration, a floating-point number
- ProductRelated, an integer
- ProductRelated_Duration, a floating-point number
- BounceRates, a floating-point number
- ExitRates, a floating-point number
- PageValues, a floating-point number
- SpecialDay, a floating-point number
- Month, an index from 0 (January) to 11 (December)
- OperatingSystems, an integer
- Browser, an integer
- Region, an integer
- TrafficType, an integer
- VisitorType, an integer 0 (not returning) or 1 (returning)
- Weekend, an integer 0 (if false) or 1 (if true)
Labels should be the corresponding list of labels, where each label
is 1 if Revenue is true, and 0 otherwise.
"""
evidence = []
labels = []
with open(filename) as f:
reader = csv.DictReader(f)
for row in reader:
# Append all necessary fields from the row to the evidence list
evidence.append(
[
int(row["Administrative"]),
float(row["Administrative_Duration"]),
int(row["Informational"]),
float(row["Informational_Duration"]),
int(row["ProductRelated"]),
float(row["ProductRelated_Duration"]),
float(row["BounceRates"]),
float(row["ExitRates"]),
float(row["PageValues"]),
float(row["SpecialDay"]),
# Convert month to the corresponding index
check_month(row["Month"]),
int(row["OperatingSystems"]),
int(row["Browser"]),
int(row["Region"]),
int(row["TrafficType"]),
# Convert VisitorType to 0 or 1
int(1 if "Returning_Visitor" == row["VisitorType"] else 0),
# Convert Weekend to 0 or 1
int(1 if "TRUE" == row["Weekend"] else 0),
]
)
# Convert Revenue to 0 or 1 and append to the labels list
labels.append(int(1 if "TRUE" == row["Revenue"] else 0))
# [0, 0.0, 0, 0.0, 1, 0.0, 0.2, 0.2, 0.0, 0.0, 1, 1, 1, 1, 1, 1, 0] expected result
# [0, 0.0, 0, 0.0, 1, 0.0, 0.2, 0.2, 0.0, 0.0, 1, 1, 1, 1, 1, 1, 0] sorted by me
# print(evidence[0])
return (evidence, labels)
def check_month(month):
"""
Convert a month abbreviation to an integer index (0 for January, 11 for December).
"""
months = {
"Jan": 0,
"Feb": 1,
"Mar": 2,
"Apr": 3,
"May": 4,
"June": 5,
"Jul": 6,
"Aug": 7,
"Sep": 8,
"Oct": 9,
"Nov": 10,
"Dec": 11,
}
for index in months:
if month == index:
# print(f"Month is: {month}, value is: {months[month]}")
return int(months[index])
def train_model(evidence, labels):
"""
Given a list of evidence lists and a list of labels, return a
fitted k-nearest neighbor model (k=1) trained on the data.
"""
# Create a k-nearest neighbor classifier with k=1
model = KNeighborsClassifier(n_neighbors=1)
# Train the model on the evidence and labels
model.fit(evidence, labels)
return model
def evaluate(labels, predictions):
"""
Given a list of actual labels and a list of predicted labels,
return a tuple (sensitivity, specificity).
Assume each label is either a 1 (positive) or 0 (negative).
`sensitivity` should be a floating-point value from 0 to 1
representing the "true positive rate": the proportion of
actual positive labels that were accurately identified.
`specificity` should be a floating-point value from 0 to 1
representing the "true negative rate": the proportion of
actual negative labels that were accurately identified.
"""
sensitivity = float(0)
specificity = float(0)
# Counter for the total number of true positive labels
total_true_positive = 0
# Counter for the total number of true negative labels
total_true_negative = 0
# Counter for the total number of labels (total instances)
total = 0
for label, prediction in zip(labels, predictions):
total += 1
if label == 1:
# Increment true positive count if both the actual and predicted labels are positive
total_true_positive += 1
# Increment sensitivity (true positive rate) if the actual positive label is accurately identified
# by checking if the predicted label is also positive
if label == prediction:
sensitivity += 1
if label == 0:
# Increment true negative count if both the actual and predicted labels are negative
total_true_negative += 1
# Increment specificity (true negative rate) if the actual negative label is accurately identified
# by checking if the predicted label is also negative
if label == prediction:
specificity += 1
# print(f"Total = {total}")
# print(f"Total positive = {total_true_positive}; Total negative = {total_true_negative}")
# Calculate sensitivity and specificity by dividing the respective counts by the total instances
sensitivity = sensitivity / total_true_positive
specificity = specificity / total_true_negative
# print(f"(sensitivity, specificity) = ({sensitivity}, {specificity})")
return (sensitivity, specificity)
if __name__ == "__main__":
main()
# USAGE: python shopping.py shopping.csv