forked from mishnit/housing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
executable file
·155 lines (140 loc) · 3.22 KB
/
features.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
import pdb
import random
import MySQLdb
import matplotlib.pyplot as plot
def randomColor():
result = '%x' % random.randint(0, 16777215)
if len(result) == 5:
return '#0'+result
elif len(result) == 4:
return '#00'+result
return '#'+result
def createPlot(axes,connection,query):
# Query for the data.
cursor = connection.cursor()
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
# Reformat the data.
keys = []
values = []
keyAverage = 0
valueAverage = 0
for key, value in results:
# Sometimes key comes out as None. Not sure why.
if key and value:
keys.append(key)
values.append(float(value))
keyAverage += key
valueAverage += value
if len(keys) == 0:
return
keyAverage = float(keyAverage)/float(len(keys))
valueAverage = float(valueAverage)/float(len(values))
# Create the new axes.
color = randomColor()
axes.plot(values,keys,'x',color=color)
axes.plot((valueAverage,),(keyAverage,),'o',color=color)
def main():
# Seed random numbers.
random.seed()
# Connect to the database.
connection = MySQLdb.connect (host = 'localhost',
user = 'root',
passwd = '80017001',
db = 'housing')
# Create the figure.
figure = plot.figure()
axes = figure.add_subplot(111)
labels = []
# Add properties that have 4 bedrooms.
features = ['Gas Fireplace',
'Master Bedroom Ensuite',
'Patio(s)',
'Main Floor Laundry',
'Separate Dining Room',
'Family Room',
'Rec Room',
'Central Air',
'Fenced Yard',
'Water Softener',
'Whirlpool',
'Central Vac',
'Cold Room/Cellar',
'Alarm System',
'Satellite Dish',
'TV Cable',
'Year-Round Road Access',
'Year-Round Living',
'Air Exchanger',
'Walk-Out Basement',
'Den/Office',
'Handicap Provisions',
'Lawn Sprinkler System',
'Workshop',
'In-Ground Pool',
'Main Floor Master Bedroom',
'Inlaw Suite',
'Heating Stove',
'Hot Tub',
'Exercise Room',
'Carpet Free',
'Backs on Greenbelt',
'Sauna',
'Lovely 3 bedroom',
'gleaming hardwood floors',
'open concept lr.',
'Auto-Garage Door/Remotes',
'Walk-in Closet',
'Dishwasher',
'Refrigerator',
'Stove',
'Microwave',
'Washer',
'Dryer',
'Wood Fireplace',
'Deck(s)',
'Shed',
'Inside Entry (from Garage)',
'Bathroom-Main Floor 2 Piece',
'Freezer',
'Above-Ground Pool',
'Hi-Speed Internet',
'On-Ground Pool',
'Games Room',
'Bathroom-Main Floor 3 Piece or More',
'Pool-Equipment',
'Bathroom-Rough-In',
'Intercom',
'Oriented to Seniors',
'Detached Workshop',
'Water Heater Owned',
'Bathroom-Ensuite Privilege',
'Security System',
'Electric Fireplace',
'Seasonal Road Access',
'Separate Heating Controls',
'Central Vac Rough-In']
for feature in features:
labels.append(feature)
query = '''
select %i, h.list
from houses h, features f
where h.list is not null
and h.bedrooms = 4
and h.bathrooms = 3
and h.id = f.house_id
and f.feature = '%s'
and listdate between MAKEDATE(2011,01) and MAKEDATE(2012,01)
''' % (len(labels), feature)
createPlot(axes,connection,query)
# Setup the axes.
axes.set_ylim(0,len(labels)+1)
axes.set_yticks(range(1,len(labels)+1))
axes.set_yticklabels(labels)
# Plot the data.
plot.show()
# Close the connection.
connection.close()
if __name__ == '__main__':
main()