-
Notifications
You must be signed in to change notification settings - Fork 0
/
Correlation in Python.py
243 lines (112 loc) · 3.31 KB
/
Correlation in Python.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#first install packages
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib
plt.style.use('ggplot')
from matplotlib.pyplot import figure
get_ipython().run_line_magic('matplotlib', 'inline')
matplotlib.rcParams['figure.figsize'] = (12,8)
pd.options.mode.chained_assignment = None
#read in the data
df = pd.read_csv(r'C:\Users\HP\Downloads\movie data\movies.csv')
# In[2]:
df.head()
# In[3]:
#check for missing data
#loop through the data and see if there is anything missing
for col in df.columns:
pct_missing = np.mean(df[col].isnull())
print('{} - {}%'.format(col, round(pct_missing*100)))
# In[4]:
df = df.dropna(how='any',axis=0)
# In[5]:
for col in df.columns:
pct_missing = np.mean(df[col].isnull())
print('{} - {}%'.format(col, round(pct_missing*100)))
# In[6]:
# Data Types for our columns
print(df.dtypes)
# In[7]:
#change data type of columns to remove decimal point
df['budget'] = df['budget'].astype('int64')
df['gross'] = df['gross'].astype('int64')
# In[54]:
df.head()
# In[58]:
#create correct year column
df['yearcorrect'] = df['released'].str.extract(pat = '([0-9]{4})').astype(int)
# In[10]:
# In[11]:
#rearrange 'gross' in descending order
df.sort_values(by=['gross'], inplace=False, ascending = False)
# In[12]:
#takes away empty rows as displayed above
pd.set_option('display.max_rows', None)
# In[13]:
#eliminate duplicates
df.drop_duplicates()
# In[14]:
#are there any outliers?
df.boxplot(column=['gross'])
# In[18]:
#prediction: high correlation between budget and gross
#scatter plot to compare budget and scatter plot
plt.scatter(x=df['budget'], y=df['gross'])
plt.title('Budget vs Gross Earnings')
plt.xlabel('Gross Earnings')
plt.ylabel('Film Budgets')
plt.show()
# In[34]:
#plot includes the regression line usinf seaborn
sns.regplot(x='budget', y='gross', data=df)
# In[38]:
#next determine actual correlation figures
df.corr(method='pearson')
# In[ ]:
#high positive correlation between budget and gross
# In[41]:
#heatmap of correlation matrix
correlation_matrix = df.corr(method='pearson')
sns.heatmap(correlation_matrix, annot = True)
plt.title("Correlation matrix for Numeric Features")
plt.xlabel("Movie features")
plt.ylabel("Movie features")
plt.show()
# In[42]:
df.head()
# In[43]:
#here, numbers have been assigned to the strings
df_numerized = df
for col_name in df_numerized.columns:
if(df_numerized[col_name].dtype == 'object'):
df_numerized[col_name]= df_numerized[col_name].astype('category')
df_numerized[col_name] = df_numerized[col_name].cat.codes
df_numerized
# In[44]:
correlation_matrix = df_numerized.corr(method='pearson')
sns.heatmap(correlation_matrix, annot = True)
plt.title("Correlation matrix for Movies")
plt.xlabel("Movie features")
plt.ylabel("Movie features")
plt.show()
# In[45]:
df_numerized.corr()
# In[50]:
correlation_mat = df_numerized.corr()
corr_pairs = correlation_mat.unstack()
corr_pairs
# In[52]:
sorted_pairs = corr_pairs.sort_values()
sorted_pairs
# In[53]:
high_corr = sorted_pairs[(sorted_pairs)>0.5]
high_corr
# In[ ]:
#budget and votes have the highest correlation with gross earnings
# In[ ]: