-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore.py
More file actions
218 lines (160 loc) · 5.63 KB
/
explore.py
File metadata and controls
218 lines (160 loc) · 5.63 KB
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
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
def bar_plt4(df, feature, target):
'''
This function is specifically to create a bar chart for county and tax value.
Parameters:
df = data
feature = df.column
target = target variable
'''
#create the bar plot
ax = sns.barplot(data = df, x = feature, y = target, errorbar = None, color = 'skyblue', width = .9)
ax = plt.gca()
for p in ax.patches:
# Get the height (value) of each bar
value = int(p.get_height())
# Exclude zero values
if value != 0:
# Format value with commas
label = f"${value:,}"
# Customize the labels
ax.text(p.get_x() + p.get_width() / 2, p.get_height(), label, ha="center", va="bottom", fontsize = 8)
#create labels
plt.xlabel('County')
plt.ylabel('Tax Value')
#remove y-axis and borders
plt.yticks([])
sns.despine(ax=ax, left=True, bottom=True)
ax.yaxis.set_visible(False)
#set title for chart
plt.title("Average tax value", y= 1.10)
plt.show()
def bar_plt3(df, feature, target):
'''
This function is specifically to create a bar chart for decade and tax value.
Parameters:
df = data
feature = df.column
target = target variable
'''
#create the bar plot
ax = sns.barplot(data = df, x = feature, y = target, errorbar = None, color = 'skyblue', width = .9)
ax = plt.gca()
for p in ax.patches:
# Get the height (value) of each bar
value = int(p.get_height())
# Exclude zero values
if value != 0:
# Format value with commas
label = f"${value:,}"
# Customize the labels
ax.text(p.get_x() + p.get_width() / 2, p.get_height(), label, ha="center", va="bottom", fontsize = 8)
new_labels = ['<=1950', '1960', '1970', '1980', '1990', '2000', '2010']
plt.xticks(range(len(new_labels)), new_labels)
#create labels
plt.xlabel('Decade')
plt.ylabel('Tax Value')
#remove y-axis and borders
plt.yticks([])
sns.despine(ax=ax, left=True, bottom=True)
ax.yaxis.set_visible(False)
#set title for chart
plt.title("Average tax value", y= 1.10)
plt.show()
def bar_plt2(df, feature, target):
'''
This function is specifically to create a bar chart for square ft and tax value.
Parameters:
df = data
feature = df.column
target = target variable
'''
#create the bar plot
ax = sns.barplot(data = df, x = feature, y = target, errorbar = None, color = 'skyblue', width = .9)
ax = plt.gca()
for p in ax.patches:
# Get the height (value) of each bar
value = int(p.get_height())
# Exclude zero values
if value != 0:
# Format value with commas
label = f"${value:,}"
# Customize the labels
ax.text(p.get_x() + p.get_width() / 2, p.get_height(), label, ha="center", va="bottom", fontsize = 8)
new_labels = ['1000', '1500', '2000', '2500', '3000', '3500', '3600+']
plt.xticks(range(len(new_labels)), new_labels)
#create labels
plt.xlabel('Square Ft')
plt.ylabel('Tax Value')
#remove y-axis and borders
plt.yticks([])
sns.despine(ax=ax, left=True, bottom=True)
ax.yaxis.set_visible(False)
#set title for chart
plt.title("Average tax value", y= 1.10)
plt.show()
def bar_plt(df, feature, target):
'''
This function is specifically to create a bar chart for total rooms and tax value.
Parameters:
df = data
feature = df.column
target = target variable
'''
#create the bar plot
ax = sns.barplot(data = df, x = feature, y = target, errorbar = None, color = 'skyblue', width = .9)
ax = plt.gca()
for p in ax.patches:
# Get the height (value) of each bar
value = int(p.get_height())
# Exclude zero values
if value != 0:
# Format value with commas
label = f"${value:,}"
# Customize the labels
ax.text(p.get_x() + p.get_width() / 2, p.get_height(), label, ha="center", va="bottom", fontsize = 8)
new_labels = ['3', '4', '5', '6', '7+']
plt.xticks(range(len(new_labels)), new_labels)
#create labels
plt.xlabel('Total Rooms (bed & bath)')
plt.ylabel('Tax Value')
#remove y-axis and borders
plt.yticks([])
sns.despine(ax=ax, left=True, bottom=True)
ax.yaxis.set_visible(False)
#set title for chart
plt.title("Average tax value", y= 1.10)
plt.show()
def spear_test(df, continuous, continuous2):
'''
This function calls the spearmanr() function from stats and conducts a statistical test on two continuous variables to determine correlation.
Parameters:
df = data
continuous = continuous feature
continuous2 = second continuous feature
'''
r, p = stats.spearmanr(df[continuous], df[continuous2])
print('a = .05')
print(f'r = {r}')
print(f'p = {p}')
print('')
a = .05
if p < a:
print('We reject the null hypothesis')
else:
print('We fail to reject the null hypothesis')
def corr(x, y):
'''
This function applies pearson r correlation test and determines correlation between features.
Parameters:
x = df.column
y = df.column2
'''
corr, p = stats.pearsonr(x, y)
a = .5
if p < a:
print('We reject the null hypothesis')
else:
print('We fail to reject the null hypothesis')