-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollege_Score_Card_Ranking.py
315 lines (277 loc) · 9.07 KB
/
College_Score_Card_Ranking.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Final Project
# Tufang Xu, Yier Yin
# CSE 160 AB
import csv
import urllib
import heapq
import matplotlib.pyplot as plt
from enum import Enum
class Student(Enum):
Unknow =0
International = 1
Local = 2
Transfer = 3
class Profile:
sat_score =0
max_tuition =0
category =Student.Unknow
def __init__(self, sat_score, max_tuition, category):
self.sat_score = sat_score
self.max_tuition = max_tuition
self.category =category
class CollageInfo:
rank =0
city =''
state =''
tuition =0
sat =0
accept_rate =0
debt =0
male_ratio =0
def __init__(self, rank, city, state, tuition, sat, accept_rate, debt, male_ratio):
self.rank = rank
self.city = city
self.state =state
self.tuition = tuition
self.sat =sat
self.accept_rate =accept_rate
self.debt =debt
self.male_ratio =male_ratio
def ToString(self) :
return self.city + '\t' + self.state +'\t' + str(self.rank) +'\t' + str(self.tuition) +'\t' + str(self.sat) +'\t' + str(self.accept_rate) +'\t' + str(self.debt) +'\t' + str(self.male_ratio)
def ToStringWithName(self) :
return 'city:' + self.city + '\tstate:' + self.state +'\trank:' + str(self.rank) +'\ttuition:' + str(self.tuition) +'\tsat:' + str(self.sat) +'\tAC:' + str(self.accept_rate) +'\tdebt:' + str(self.debt) +'\tMal:' + str(self.male_ratio)
def extract_ranking_field(filename, column_names):
reader = open(filename)
input_file = csv.DictReader(reader)
ret ={};
for key in column_names:
ret[key]=[]
for row in input_file:
for key in column_names:
if key in row.keys():
ret[key].append(row[key])
reader.close()
return ret
def process_input() :
while True:
print "a) International \r\nb) Local \r\nc) Tranfer: \r\n? (Please Type a, b, c):"
input1_tmp = raw_input("")
if input1_tmp !="a" and input1_tmp !="b" and input1_tmp !="c" :
continue;
if input1_tmp =="a":
input1 =Student.International
if input1_tmp =="b":
input1 =Student.Local
if input1_tmp =="c":
input1 =Student.Transfer
break
while True:
input2_tmp = raw_input("please input your sat score:")
try:
input2 =float(input2_tmp)
except Exception as ex:
continue
break
while True:
input3_tmp = raw_input("please input your maximum tuition you can accept:")
try:
input3 =float(input3_tmp)
except Exception as ex:
continue
break
return Profile(input2, input3, input1)
def CrawlUniversityRank() :
url = "http://www.4icu.org/us/"
handle = urllib.urlopen(url)
html = handle.read()
ret ={}
loc =0
rank =1
while True :
try:
loc1 =html.index('class=\"lead\">', loc)
loc2 =html.index('</a>', loc1)
ret[html[(loc1 + len('class=\"lead\">')): loc2]] =rank
rank =rank +1
loc =loc2 +1
except Exception as ex:
break
return ret
def ProcessFinalData(collage_data, ranking_data):
size =len(collage_data['INSTNM'])
ret ={}
for i in range(size):
name =collage_data['INSTNM'][i]
if ranking_data.has_key(name) ==False:
continue
rank =ranking_data[name]
city =collage_data['CITY'][i]
state =collage_data['STABBR'][i]
try:
tuition =float(collage_data['TUITIONFEE_OUT'][i])
sat =float(collage_data['SAT_AVG_ALL'][i])
accept_rate =float(collage_data['ADM_RATE_ALL'][i])
tuition =float(collage_data['TUITIONFEE_OUT'][i])
debt =float(collage_data['DEBT_MDN_SUPP'][i])
male_ratio =float(collage_data['UGDS_MEN'][i])
ret[name] =CollageInfo(rank, city, state, tuition, sat, accept_rate, debt, male_ratio)
except Exception as ex:
pass
return ret
def saveDataToFile(final_data) :
f = open('cleanData.tsv', 'w')
for (k, v) in final_data.items():
f.write(k + '\t' + v.ToString()+'\n')
f.close()
def FilterCollage(user_profile, data):
ret ={}
for (k, v) in data.items():
if user_profile.sat_score >= v.sat and user_profile.max_tuition >= v.tuition:
ret[k] =v
return ret
def NormalizeData(data) :
# normalize data which is not between 0,1
mmax =max(data)
mmin =min(data)
ret =[]
for i in data:
if (mmax ==mmin):
ret.append(1)
else:
ret.append((i - mmin)*1.0 /(mmax -mmin))
return ret
def GetTopN(data, score, N):
Top = sorted(score.iteritems(), key=lambda x:-x[1])[:5]
for item in Top:
print "college : " + item[0]
print "score: " +str(item[1])
print data[item[0]].ToStringWithName() +'\n\n'
def InternationalStudentChoice(data) :
names =[]
tuitions =[]
sats =[]
accept_rates =[]
ranks =[]
for (k, v) in data.items():
names.append(k)
tuitions.append(v.tuition)
sats.append(v.sat)
accept_rates.append(v.accept_rate)
ranks.append(v.rank)
tuitions =NormalizeData(tuitions)
ranks =NormalizeData(ranks)
sats =NormalizeData(sats)
c2score={}
for i in range(len(names)):
c2score[names[i]] =0.15 * (1 - tuitions[i]) + 0.4* (1 -sats[i]) + 0.35*accept_rates[i] + 0.2*(1-ranks[i])
GetTopN(data, c2score, 5)
def LocalStudentChoice(data) :
names =[]
tuitions =[]
sats =[]
accept_rates =[]
debts =[]
male_ratios =[]
for (k, v) in data.items():
names.append(k)
tuitions.append(v.tuition)
sats.append(v.sat)
accept_rates.append(v.accept_rate)
male_ratios.append(v.male_ratio)
debts.append(v.debt)
tuitions =NormalizeData(tuitions)
debts =NormalizeData(debts)
sats =NormalizeData(sats)
c2score={}
for i in range(len(names)):
c2score[names[i]] =0.2* (1 -tuitions[i]) + 0.1* (1-debts[i]) + 0.1*(1-abs(male_ratios[i] -0.5)) + 0.25*(1-sats[i]) + 0.35 * accept_rates[i]
GetTopN(data, c2score, 5)
def TransferStudentChoice(data) :
names =[]
tuitions =[]
sats =[]
accept_rates =[]
ranks =[]
for (k, v) in data.items():
names.append(k)
tuitions.append(v.tuition)
sats.append(v.sat)
accept_rates.append(v.accept_rate)
ranks.append(v.rank)
tuitions =NormalizeData(tuitions)
ranks =NormalizeData(ranks)
sats =NormalizeData(sats)
c2score={}
for i in range(len(names)):
c2score[names[i]] =0.3 * (1-tuitions[i]) + 0.1* (1-sats[i]) + 0.4*accept_rates[i] + 0.2*(1-ranks[i])
GetTopN(data, c2score, 5)
def LoadAcceptRate():
year2file ={2004:'MERGED2004_05_PP 2.csv', 2005:'MERGED2005_06_PP 2.csv', 2006:'MERGED2006_07_PP 2.csv',
2008:'MERGED2008_09_PP 2.csv',2009:'MERGED2009_10_PP 2.csv',2011:'MERGED2011_12_PP 2.csv',2012:'MERGED2012_13_PP 2.csv',
2013:'MERGED2013_14_PP 2.csv'}
ret ={}
for (k, v) in year2file.items():
print 'Loading Data\\' +v
collage_data =extract_ranking_field(v, ['INSTNM', 'ADM_RATE_ALL'])
size =len(collage_data['INSTNM'])
for i in range(size):
name =collage_data['INSTNM'][i]
if ret.has_key(name) ==False:
ret[name]={}
try:
ret[name][k] =float(collage_data['ADM_RATE_ALL'][i])
except Exception as ex:
ret[name][k] =0
return ret
def plotGraph(data):
x =[]
y =[]
labels =[]
plt.clf()
for (k, v) in data.items():
x.append(k)
labels.append(str(k))
y.append(v)
plt.ylabel('AC rate')
plt.xlabel('year')
plt.xticks(x, labels, rotation=0)
plt.grid()
plt.plot(x, y, 'r--')
plt.show()
def main():
print "loading Recent University data"
colleage_data =extract_ranking_field('MERGED2013_14_PP 2.csv', ['INSTNM', 'CITY', 'STABBR' ,'TUITIONFEE_OUT', 'SAT_AVG_ALL', 'ADM_RATE_ALL', 'DEBT_MDN_SUPP', 'UGDS_MEN'])
ranking_data = CrawlUniversityRank()
final_data = ProcessFinalData(colleage_data, ranking_data)
saveDataToFile(final_data)
print "loading History accept rate data"
HistoryData =LoadAcceptRate()
while True:
while True:
choice =raw_input('Recommend College OR Check Accept Rate? Enter R or A or Exit: ')
if choice !='R' and choice !='A' and choice !='Exit':
continue;
break;
if choice =='R':
user_profile = process_input()
filter_data =FilterCollage(user_profile, final_data)
if len(filter_data) ==0 :
print "Sorry, There is no choice for you"
continue
if user_profile.category ==Student.International:
InternationalStudentChoice(filter_data)
elif user_profile.category ==Student.Transfer:
TransferStudentChoice(filter_data)
elif user_profile.category ==Student.Local:
LocalStudentChoice(filter_data)
elif choice =='A':
query =raw_input('Enter a University Name(Full Name):')
if HistoryData.has_key(query):
plotGraph(HistoryData[query])
else:
print "Sorry, No such univesity"
elif choice =='Exit':
break
if __name__ == "__main__":
main()