-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormal Distribution.py
More file actions
437 lines (220 loc) · 7.68 KB
/
Normal Distribution.py
File metadata and controls
437 lines (220 loc) · 7.68 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python
# coding: utf-8
# ## Team StackOverflow :
# ## Stack OverFlow Dataset Analysys
# ## 
# In[162]:
#Importing the some inbuit anyalysis functions
import os
import struct
import pandas as pd
import math
import numpy as np
from scipy import stats
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
import scipy.stats
import warnings
warnings.filterwarnings('ignore')
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
import matplotlib.pyplot as plt
import collections
# In[4]:
from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
# In[58]:
#This is Here We are importing the Data set.(Questions,Answers And Tags)
df_questions = pd.read_csv("/home/pranavhegde/Documents/Stack_Over_Flow_Dataset/Questions.csv", nrows=50000,usecols =['Id', 'OwnerUserId', 'CreationDate', 'ClosedDate', 'Score','Title','Body'],encoding='latin1')
df_questions = df_questions.dropna()
df_Answers= pd.read_csv("/home/pranavhegde/Documents/Stack_Over_Flow_Dataset/Answers.csv", nrows=10000,usecols =['Id', 'OwnerUserId', 'CreationDate', 'ParentId', 'Score','Body'],encoding='latin1')
df_Answers = df_Answers.dropna()
df_Tags = pd.read_csv("/home/pranavhegde/Documents/Stack_Over_Flow_Dataset/Tags.csv", nrows=10000,usecols =['Id', 'Tag'],encoding='latin1')
df_tags = df_Tags.dropna()
df_questions.head(10)
# In[9]:
#Fetching the Data
df_Answers.head(10)
# In[10]:
df_Tags.head(10)
# ### Cleaning up the Data ,The results Shows there is 0 null value here
# In[70]:
print('isNull\n\n',df_questions.isnull().sum())
print('isDuplicate', df_questions.duplicated().sum())
# In[163]:
print('Questions shape: ', df_questions.shape)
print('Answers shape: ', df_Answers.shape)
print('Tags shape: ', df_Tags.shape)
# In[164]:
#Collecting the parameter of the data Distribution of the Answers
ans_per_question = collections.Counter(df_Answers['ParentId'])
quesId,nosAnswers = zip(*ans_per_question.most_common())
N=20
plt.bar(range(N), nosAnswers[:N], align='center', alpha=0.5)
plt.ylabel('Number of Answers per Question Id')
plt.xlabel('Question Id')
plt.title('Distribution of Answers per question')
plt.text(3,85,"Avegrage answers per question "+str(math.ceil((np.mean(nosAnswers)))))
plt.show()
# In[38]:
ans_freq_counter = collections.Counter(ans_per_question.values())
ans_count,nosQuestions = zip(*ans_freq_counter.most_common())
N=10
plt.bar(ans_count[:N], nosQuestions[:N], align='center', alpha=0.5)
plt.ylabel('Number of Questions')
plt.xlabel('Answer count')
plt.title('Questions vs Their Answer count')
plt.text(5,400,"Avegrage answers per question "+str(math.ceil((np.mean(nosAnswers)))))
plt.show()
# In[52]:
tags_per_question = collections.Counter(df_Tags['Id'])
tags_freq_counter = collections.Counter(tags_per_question.values())
tags_count,nosQuestions = zip(*tags_freq_counter.most_common())
N=100
plt.bar(tags_count[:N], nosQuestions[:N], align='center', alpha=0.5)
plt.ylabel('Number of Questions')
plt.xlabel('Tags count')
plt.title('Questions vs Their tags count')
plt.text(2,900,"Avegrage Tags per question "+str(math.ceil((np.mean(tags_count)))))
plt.show()
# In[53]:
tagCount = collections.Counter(list(df_Tags['Tag']))
tagName,freq = zip(*tagCount.most_common(15))
plt.bar(tagName, freq )
plt.xticks(rotation='vertical')
plt.ylabel('Tag Count')
plt.xlabel('Tags name')
plt.title('Tags vs tags count')
plt.show()
# In[59]:
import datetime
df_questions['datetime'] = pd.to_datetime(df_questions['CreationDate'])
df_questions.set_index('datetime', inplace=True)
monthlyQues = df_questions.resample('M').count()
monthlyQues['datetime'] = monthlyQues.index
monthlyQues.plot(x='datetime', y='Title', kind='line', lw=0.75, c='r')
# In[60]:
df_Tags['Tag'] = df_Tags['Tag'].astype(str)
grouped_tags = df_Tags.groupby("Id")['Tag'].apply(lambda tags: ' '.join(tags))
grouped_tags_final = pd.DataFrame({'Id':grouped_tags.index, 'Tags':grouped_tags})
grouped_tags.reset_index()
grouped_tags_final.head()
# In[61]:
df_questions.drop(columns=['OwnerUserId', 'CreationDate', 'ClosedDate'], inplace=True)
# selecting the only questions which have score equal to or more than 5
score_gt_5 = df_questions['Score'] >= 5
ques = df_questions[score_gt_5]
ques.head()
# In[66]:
merged_ques = df_questions.merge(grouped_tags_final, on='Id')
merged_ques.drop(columns=['Id', 'Score'], inplace=True)
merged_ques.head()
# In[83]:
print(len(df_questions))
print(len(df_Answers))
print(len(df_tags))
# In[86]:
plt.figure(figsize=(8,5))
counts = df_tags['Id'].value_counts()
counts = counts.value_counts()
print("Total number of questions " + str(len(counts)))
sns.barplot(x=counts,y=counts.index)
plt.xlabel("Number of questions")
plt.ylabel("Number of tags")
# In[165]:
# Here graph is generated for Random value
sns.distplot(random.normal(size=20000), hist=False)
# # Calculation Of Mean ,Median,Standard Devation
# ## Mean meadian And Standard Diviation of question.csv
# ### ID:
# In[89]:
mean_id = df_questions['Id'].mean()
print ('Mean Id: ' + str(mean_id))
# In[90]:
median_id = df_questions['Id'].median()
print ('Median Id: ' + str(median_id))
# In[91]:
std_id = df_questions['Id'].std()
print ('std_id: ' + str(std_id))
# ### Scores:
# In[98]:
mean_id = df_questions['Score'].mean()
print ('Mean Id: ' + str(mean_id))
# In[99]:
median_id = df_questions['Score'].median()
print ('Median Id: ' + str(median_id))
# In[100]:
std_id = df_questions['Score'].std()
print ('std_id: ' + str(std_id))
# ## Mean meadian And Standard Diviation of Answer.csv
# ### ID:
# In[92]:
mean_id = df_Answers['Id'].mean()
print ('Mean Id: ' + str(mean_id))
# In[93]:
median_id = df_Answers['Id'].median()
print ('Median Id: ' + str(median_id))
# In[94]:
std_id = df_Answers['Id'].std()
print ('std_id: ' + str(std_id))
# ### Scores:
# In[101]:
mean_id = df_Answers['Score'].mean()
print ('Mean Id: ' + str(mean_id))
# In[102]:
median_id = df_Answers['Score'].median()
print ('Median Id: ' + str(median_id))
# In[103]:
std_id = df_Answers['Score'].std()
print ('std_id: ' + str(std_id))
# ## Mean meadian And Standard Diviation of Tags.csv
# ### ID:
# In[95]:
mean_id = df_Tags['Id'].mean()
print ('Mean Id: ' + str(mean_id))
# In[96]:
median_id = df_Tags['Id'].median()
print ('Median Id: ' + str(median_id))
# In[97]:
std_id = df_Tags['Id'].std()
print ('std_id: ' + str(std_id))
# ## Discribing the Dataset
# ### Question Dataset
# In[19]:
df_questions.info()
# In[21]:
df_questions['Id'].describe()
# ### Answer Dataset
# In[106]:
df_Answers.info()
# In[107]:
df_Answers['Id'].describe()
# ### Tags Dataset
# In[114]:
df_Tags.info()
# In[115]:
df_Tags['Id'].describe()
# ### Plotting of Graph under Distrbition
# #### Comapring of The Answer Id And Scores
# In[146]:
ax = sns.distplot(df_questions['Score'],kde=True, hist=False,bins=20,kde_kws={"shade":True, "linewidth":"2"})
ax.set(title="Density_Plot Of Scores",xlabel="Scores", ylabel="Id")
# In[153]:
sns.distplot(df_questions['Id'], fit=norm);
fig = plt.figure()
res = stats.probplot(df_questions['Id'], plot=plt)
# ## Plotting The Graph
# In[155]:
sns.distplot(df_Answers['Id'], fit=norm);
fig = plt.figure()
res = stats.probplot(df_Answers['Id'], plot=plt)
# ### Visualisation Of Answers dataset Scores
# In[160]:
ax = sns.distplot(df_Answers['Score'],kde=True, hist=False,bins=20,kde_kws={"shade":True, "linewidth":"2"})
ax.set(title="Density_Plot Of Scores",xlabel="Scores", ylabel="Id")
# In[161]:
ax = sns.distplot(df_Answers['Id'],kde=True, hist=False,bins=20,kde_kws={"shade":True, "linewidth":"2"})
ax.set(title="Density_Plot Of Scores",xlabel="Scores", ylabel="Id")
# In[ ]: