-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRating distribution & correlation .py
200 lines (157 loc) · 5.18 KB
/
Rating distribution & correlation .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
# coding: utf-8
# In[1]:
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from pylab import plot, show
from matplotlib.lines import Line2D
import matplotlib.colors as mcolors
# In[2]:
fig, ax = plt.subplots()
sns.set()
def rate_distribution(x):
'''
purpose: plots a histogram to show the rating distribution for the books.
:param x: input average rating of the books
:type x: data
'''
sns.distplot(x['rating'],color="g")
ax.set_xlabel('Rating distribution')
ax.set_ylabel('Frequency')
data = pd.read_csv("datav3.csv")
rate_distribution(data)
# In[3]:
data = pd.read_csv("datav3.csv")
def segregation(x):
'''
purpose: segragate the ratings for the books to 5 intervals.
:param x: input data
:type x: data
'''
values = []
for val in x.rating:
if val>=0 and val<=1:
values.append("Between 0 and 1")
elif val>1 and val<=2:
values.append("Between 1 and 2")
elif val>2 and val<=3:
values.append("Between 2 and 3")
elif val>3 and val<=4:
values.append("Between 3 and 4")
elif val>4 and val<=5:
values.append("Between 4 and 5")
else:
values.append("NaN")
print(len(values))
return values
# In[4]:
data = pd.read_csv("datav3.csv")
def rating_pie(data):
'''
purpose: plot a pie chart of ratings distribution for the books .
:param x: input data
:type x: data
'''
data['rating'] = segregation(data)
ratings_pie = data['rating'].value_counts().reset_index()
labels = ratings_pie['index']
colors = ['lightblue','coral','darkmagenta','bisque', 'black']
percent = 100.*ratings_pie['rating']/ratings_pie['rating'].sum()
fig, ax1 = plt.subplots()
ax1.pie(ratings_pie['rating'],colors = colors,
pctdistance=0.85, startangle=90, explode=(0.05, 0.05, 0.05, 0.05, 0.05))
#Draw a circle now:
centre_circle = plt.Circle((0,0), 0.70, fc ='white')
fig1 = plt.gcf()
fig1.gca().add_artist(centre_circle)
#Equal Aspect ratio ensures that pie is drawn as a circle
plt.axis('equal')
plt.tight_layout()
labels = ['{0} - {1:1.2f} %'.format(i,j) for i,j in zip(labels, percent)]
plt.legend( labels, loc = 'best',bbox_to_anchor=(0.1, 1.),)
rating_pie(data)
# In[5]:
data = pd.read_csv("datav3.csv")
def reviews_rating(data):
'''
Checking for any relation between text_reviews_count and ratings.
'''
plt.figure(figsize=(15,10))
data.dropna(0, inplace=True)
sns.set_context('paper')
ax =sns.jointplot(x="rating",y='text_reviews_count', kind='scatter', data= data[['text_reviews_count', 'rating']],color='g')
ax.set_axis_labels("Rating", "Text Review Count")
plt.show()
reviews_rating(data)
# In[11]:
data = pd.read_csv("datav3.csv")
def reviews_rating2(data):
'''
Checking for any relation between text_reviews_count and ratings.
'''
plt.figure(figsize=(15,10))
data.dropna(0, inplace=True)
sns.set_context('paper')
trial =data[~(data['text_reviews_count']>500)]
ax =sns.jointplot(x="rating",y='text_reviews_count', kind='scatter', data= trial, color='g')
ax.set_axis_labels("Rating", "Text Review Count")
plt.show()
reviews_rating2(data)
# In[7]:
data = pd.read_csv("datav3.csv")
def rat_counts_rating(data):
'''
Checking for any relation between ratings_count and ratings.
'''
plt.figure(figsize=(15,10))
data.dropna(0, inplace=True)
sns.set_context('paper')
ax =sns.jointplot(x="rating",y='ratings_count', kind='scatter', data= data[['ratings_count', 'rating']],color='brown')
ax.set_axis_labels("Rating", "Rating Count")
plt.show()
rat_counts_rating(data)
# In[14]:
data = pd.read_csv("datav3.csv")
def rat_counts_rating2(data):
'''
Checking for any relation between ratings_count and ratings.
'''
plt.figure(figsize=(15,10))
data.dropna(0, inplace=True)
sns.set_context('paper')
trial = data[~(data['ratings_count']>1000)]
ax =sns.jointplot(x="rating",y='ratings_count', kind='scatter', data= trial,color='brown')
ax.set_axis_labels("Rating", "Rating Count")
plt.show()
rat_counts_rating2(data)
# In[8]:
data = pd.read_csv("datav3.csv")
def pages_rating(data):
'''
Checking for any relation between num_pages and ratings.
'''
plt.figure(figsize=(15,10))
data.dropna(0, inplace=True)
sns.set_context('paper')
ax =sns.jointplot(x="rating",y='num_pages', kind='scatter', data= data[['num_pages', 'rating']], color='darkcyan')
ax.set_axis_labels("Rating", "Number of Pages")
plt.show()
pages_rating(data)
# In[17]:
data = pd.read_csv("datav3.csv")
def pages_rating2(data):
'''
Checking for any relation between num_pages and ratings.
'''
plt.figure(figsize=(15,10))
data.dropna(0, inplace=True)
sns.set_context('paper')
trial = data[~(data['num_pages']>1500)]
ax =sns.jointplot(x="rating",y='num_pages', kind='scatter', data=trial, color='darkcyan')
ax.set_axis_labels("Rating", "Number of Pages")
plt.show()
pages_rating2(data)