-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_assignment4.py
143 lines (116 loc) · 3.17 KB
/
run_assignment4.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
from datetime import datetime
import assignment4 as models
import numpy as np
import sys
from sklearn import metrics
if(sys.version_info[0] < 3):
raise Exception("This assignment must be completed using Python 3")
#==========================================================Data==========================================================
# Number of Instances:
# 653
# Number of Attributes:
# 35 numeric, predictive attributes and the class
# Attribute Information:
# We have 35 variables for 653 counties, including demographics, covid info, previous election
# results, work related information.
# percentage16_Donald_Trump
# percentage16_Hillary_Clinton
# total_votes20
# latitude
# longitude
# Covid Cases/Pop
# Covid Deads/Cases
# TotalPop
# Women/Men
# Hispanic
# White
# Black
# Native
# Asian
# Pacific
# VotingAgeCitizen
# Income
# ChildPoverty
# Professional
# Service
# Office
# Construction
# Production
# Drive
# Carpool
# Transit
# Walk
# OtherTransp
# WorkAtHome
# MeanCommute
# Employed
# PrivateWork
# SelfEmployed
# FamilyWork
# Unemployment
# Class Distribution:
# 328 - Candidate A (1), 325 - Candidate B (0)
#========================================================================================================================
def train_test_split(X, y, test_ratio):
tr = int(y.size*test_ratio)
return X[:tr], X[tr:], y[:tr], y[tr:]
def load_data(path):
data = np.genfromtxt(path, delimiter=',', dtype=float)
return data[:,:-1], data[:,-1].astype(int)
def load_cluster(path):
data = np.genfromtxt(path, delimiter='\n', dtype=int)
return data
#for testing the clustering algorithms
def silhouette(X,cluster):
return metrics.silhouette_score(X, cluster, metric='euclidean')
X, y = load_data("county_statistics.csv")
X_train, X_test, y_train, y_test = train_test_split(X, y, 0.75)
y_cluster = load_cluster("real_county_cluster.txt")
# print(y_cluster)
#Initialization
#MLP
lr = .0001
w1 = np.random.normal(0, .1, size=(X_train.shape[1], 10))
w2 = np.random.normal(0, .1, size=(10,1))
b1 = np.random.normal(0, .1, size=(1,10))
b2 = np.random.normal(0, .1, size=(1,1))
mlp = models.MLP(w1, b1, w2, b2, lr)
#Train
steps = 100*y_train.size
start_time = datetime.now()
mlp.train(X_train, y_train, steps)
print("MLP trained in ", datetime.now()-start_time)
#Check weights (For grading)
# mlp.w1
# mlp.b1
# mlp.w2
# mlp.b2
#Evaluate
def evaluate(solutions, real):
if(solutions.shape != real.shape):
raise ValueError("Output is wrong shape.")
predictions = np.array(solutions)
labels = np.array(real)
return (predictions == labels).sum() / float(labels.size)
solutions = mlp.predict(X_test)
print(f"MLP acc: {evaluate(solutions, y_test)*100.0}%")
#Initialization
#k_means
k = 3
t = 50 #max iterations
k_means = models.K_MEANS(k, t)
#train
start_time = datetime.now()
KM_cluster = k_means.train(X)
print("K-Means trained in ", datetime.now()-start_time)
#evaluate
print(f"K-Means silhouette: {silhouette(X, KM_cluster)*100.0}%")
#AGNES
k = 3
agnes = models.AGNES(k)
#train
start_time = datetime.now()
AG_cluster = agnes.train(X)
print("AGNES trained in ", datetime.now()-start_time)
#evaluate
print(f"AGNES silhouette: {silhouette(X, AG_cluster)*100.0}%")