-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modelling Customer's Feedback on the Product
129 lines (99 loc) · 3.75 KB
/
Modelling Customer's Feedback on the Product
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
#Task 1 - Data loading and splitting
# import the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings('ignore')
# Code starts here
df = pd.read_json(path,lines=True)
df.columns=df.columns.str.strip().str.lower().str.replace(' ', '_')
##df.dropna(inplace=True)
df.drop(columns=['waist', 'bust', 'user_name','review_text','review_summary','shoe_size','shoe_width'],axis=1,inplace=True)
print(df.columns)
y=df['fit']
X=df.drop('fit',axis=1)
print(X.columns)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.33,random_state=6)
print(y_train.shape)
print(y_test.shape)
# Code ends here
#Task 2 - Category vs fit feedback
def plot_barh(df,col, cmap = None, stacked=False, norm = None):
df.plot(kind='barh', colormap=cmap, stacked=stacked)
fig = plt.gcf()
fig.set_size_inches(24,12)
plt.title("Category vs {}-feedback - cloth {}".format(col, '(Normalized)' if norm else ''), fontsize= 20)
plt.ylabel('Category', fontsize = 18)
plot = plt.xlabel('Frequency', fontsize=18)
# Code starts here
g_by_category = df.groupby('category')
cat_fit=g_by_category.count()['fit']
# Code ends here
#Task 3 - Category vs Length
# Code starts here
g_by_category = df.groupby('category')
cat_len = g_by_category['length'].value_counts()
print(cat_len)
cat_len = cat_len.unstack()
print(cat_len)
plot_barh(cat_len, 'length')
# Code ends here
#Task 4 - Convert feet to centimeters
# Code starts here
def get_cms(x):
if type(x) == type(1.0):
return
try:
return (int(x[0])*30.48) + (int(x[4:-2])*2.54)
except:
return (int(x[0])*30.48)
print(X_train['height'].isnull().sum())
###X_train['height'].dropna(inplace=True)
##for i in range(len(X_train)):
## X_train['height'][i]= (int(X_train['height'][i][0])*30.48) + (int(X_train['height'][i][4:-2])*2.54)
X_train['height']=X_train['height'].apply(get_cms)
X_test['height']=X_test['height'].apply(get_cms)
# Code ends here
#Task 5 - Missing value imputation
# Code starts here
print(X_train.isnull().sum())
X_train.dropna(subset=['height','length','quality'],inplace=True)
X_test.dropna(subset=['height','length','quality'],inplace=True)
X_train['bra_size'].fillna((X_train['bra_size'].mean()), inplace=True)
X_test['bra_size'].fillna((X_test['bra_size'].mean()), inplace=True)
mode_1=X_train['cup_size'].mode()[0]
mode_2=X_test['cup_size'].mode()[0]
X_train['cup_size']=X_train['cup_size'].replace(np.nan,mode_1)
X_test['cup_size']=X_test['cup_size'].replace(np.nan,mode_2)
# Code ends here
#Task 6 - One hot encoding
# Code starts here
print(X_test.head(2))
X_train=pd.get_dummies(data=X_train,columns=["category", "cup_size","length"],prefix=["category", "cup_size","length"])
X_test=pd.get_dummies(data=X_test,columns=["category", "cup_size","length"],prefix=["category", "cup_size","length"])
# Code ends here
#Task 7 - Is my prediction right?
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import precision_score
from sklearn.metrics import accuracy_score
# Code starts here
model=DecisionTreeClassifier(random_state=6)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
score=accuracy_score(y_pred,y_test)
precision=precision_score(y_test,y_pred,average=None)
# Code ends here
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
# parameters for grid search
parameters = {'max_depth':[5,10],'criterion':['gini','entropy'],'min_samples_leaf':[0.5,1]}
# Code starts here
model=DecisionTreeClassifier(random_state=6)
grid=GridSearchCV(estimator=model,param_grid=parameters)
grid.fit(X_train,y_train)
y_pred=grid.predict(X_test)
accuracy=accuracy_score(y_test,y_pred)
print(accuracy)