-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 Average.py
151 lines (128 loc) · 5.96 KB
/
2 Average.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
import numpy as np
import pickle
import csv
def make_features(): #defining a function
features = {} #creating a dictionary
index = 0 #instantiating value
with open('DataDictionary.csv') as csvfile: # opening file
reader = csv.reader(csvfile, dialect='excel', ) #reading through file
next(reader) #going to next line
for row in reader: #for loop to go through all read lines
if row[0] == "'heatingorsystemtypeid'": #checking if the value read from file is equal or not to specified location in row
with open('HeatingOrSystemTypeID.csv') as subfile: #if above condition is correct open given file
subreader = csv.reader(subfile, dialect='excel') #reading through file
subfeatures = {} #creating another dictionary
next(subreader) #going to next line and read it
for subrow in subreader: #looping through all read data
subfeatures[subrow[0]] = index #checking if the specified value is equal index (index=0)
index += 1
features[row[0]] = subfeatures
elif row[0] == "'propertylandusetypeid'":
with open('PropertyLandUseTypeID.csv') as subfile:
subreader = csv.reader(subfile, dialect='excel')
subfeatures = {}
next(subreader)
for subrow in subreader:
subfeatures[subrow[0]] = index
index += 1
features[row[0]] = subfeatures
elif row[0] == "'storytypeid'":
with open('StoryTypeID.csv') as subfile:
subreader = csv.reader(subfile, dialect='excel')
subfeatures = {}
next(subreader)
for subrow in subreader:
subfeatures[subrow[0]] = index
index += 1
features[row[0]] = subfeatures
elif row[0] == "'airconditioningtypeid'":
with open('AirConditioningTypeID.csv') as subfile:
subreader = csv.reader(subfile, dialect='excel')
subfeatures = {}
next(subreader)
for subrow in subreader:
subfeatures[subrow[0]] = index
index += 1
features[row[0]] = subfeatures
elif row[0] == "'architecturalstyletypeid'":
with open('ArchitecturalStyleTypeID.csv') as subfile:
subreader = csv.reader(subfile, dialect='excel')
subfeatures = {}
next(subreader)
for subrow in subreader:
subfeatures[subrow[0]] = index
index += 1
features[row[0]] = subfeatures
elif row[0] == "'typeconstructiontypeid'":
with open('TypeConstructionTypeID.csv') as subfile:
subreader = csv.reader(subfile, dialect='excel')
subfeatures = {}
next(subreader)
for subrow in subreader:
subfeatures[subrow[0]] = index
index += 1
features[row[0]] = subfeatures
elif row[0] == "'buildingclasstypeid'":
with open('BuildingClassTypeID.csv') as subfile:
subreader = csv.reader(subfile, dialect='excel')
subfeatures = {}
next(subreader)
for subrow in subreader:
subfeatures[subrow[0]] = index
index += 1
features[row[0]] = subfeatures
else:
features[row[0]] = index
index += 1
return features
def make_ides():
ides = {}
index = 0
with open('properties_2016.csv') as csvfile:
reader = csv.reader(csvfile, dialect='excel')
next(reader)
for row in reader:
ides[row[0]] = index
index += 1
return ides
def size_dict(d):
count = 0
for feature in d:
if type(d[feature]) == dict:
for sub in d[feature]:
count += 1
else:
count += 1
return count
#Replace all zeros in column number 'col' in 'array' with
#the average of the values in said column
#Output: modified array
def avg_fill(array, col):
total = 0
numIdes = 0
for row in array:
if row[col] != "":
total += row[col]
numIdes += 1
average = float(total)/numIdes
print("average: " + str(average))
for r in range(size_dict(ides)):
if array[r, col] == 0.0:
array[r, col] = average
print(array[r, col])
return array
feat = pickle.load(open("feat.p", "rb"))
ides = pickle.load(open("ides.p", "rb"))
"""
feat = make_features() #dictionary of features mapped to column indexes
ides = make_ides() #dictionary of parcel id's mapped to row indexes
pickle.dump(feat, open("feat.p", "wb")) #pickle features dictionary
pickle.dump(ides, open("ides.p", "wb")) #pickle id's dictionary
xvector = np.fromfile('xvector.bin', dtype=float, count=-1, sep='') #load 1D array from bin file
xvector = np.reshape(xvector, (-1, size_dict(feat))) #resize 1D array as 2D array
x = avg_fill(xvector, feat["'taxvaluedollarcnt'"])
x = avg_fill(xvector, feat["'structuretaxvaluedollarcnt'"])
x = avg_fill(xvector, feat["'landtaxvaluedollarcnt'"])
x = avg_fill(xvector, feat["'taxamount'"])
x.tofile('xvector_avg.bin')
"""