-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBM.py
More file actions
174 lines (134 loc) · 5.27 KB
/
Copy pathBM.py
File metadata and controls
174 lines (134 loc) · 5.27 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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 13 04:53:11 2018
@author: Gurudeo
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
import sklearn.linear_model
import sklearn.ensemble
data=pd.read_csv('data.csv')
#lets looks at number od types of differnet categorical variable
data.Item_Fat_Content.value_counts()
data.Item_Type.value_counts()
data.Outlet_Size.value_counts()
data.Outlet_Type.value_counts()
data.Outlet_Location_Type.value_counts()
#imputing Missing value
#check which are missings
data_Missing=data.isnull().sum()
data_Missing
#Outlet_Size
data.Outlet_Size.value_counts()
plt.scatter(data['Outlet_Size'],data['Outlet_Type'])
twowaytable=pd.crosstab(data['Outlet_Size'],data['Outlet_Type'])
twowaytable
#Assumption from plot
#fill missing values
d={'Grocery Store':'Small'}
s=data.Outlet_Type.map(d)
data.Outlet_Size=data.Outlet_Size.combine_first(s)
d={'Grocery Store':'Small'}
#Hypothesis :Outlet_Size depends on Outlet_Location_Type
#Similariy for plot of outlet_Size and Outlet_Location Type
data.Outlet_Size.value_counts()
plt.scatter(data['Outlet_Size'],data['Outlet_Location_Type'])
twowaytable=pd.crosstab(data['Outlet_Size'],data['Outlet_Location_Type'])
twowaytable
#from plot and table it is observed that Tier2 Location type will have Outlet_Size of small
d={'Tier 2':'Small'}
s=data.Outlet_Location_Type.map(d)
data.Outlet_Size=data.Outlet_Size.combine_first(s)
data.Outlet_Size.value_counts()
data.Outlet_Size.isnull().any()
#Item_Weight
#Update Weight of Item by taking average of corresponding item type and
#Missing values in corresponding locations
Mean_values_Item_Type_data=data.groupby('Item_Type')['Item_Weight'].mean()
data.Item_Type.value_counts()
#Fruits and Vegetables
d={'Fruits and Vegetables':Mean_values_Item_Type_data['Fruits and Vegetables']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Snack Foods
d={'Snack Foods':Mean_values_Item_Type_data['Snack Foods']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Household
d={'Household':Mean_values_Item_Type_data['Household']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Frozen Foods
d={'Frozen Foods':Mean_values_Item_Type_data['Frozen Foods']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Dairy
d={'Dairy':Mean_values_Item_Type_data['Dairy']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Canned
d={'Canned':Mean_values_Item_Type_data['Canned']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Banking Goods
d={'Baking Goods':Mean_values_Item_Type_data['Baking Goods']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Health and Hygiene
d={'Health and Hygiene':Mean_values_Item_Type_data['Health and Hygiene']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Soft Drinks
d={'Soft Drinks':Mean_values_Item_Type_data['Soft Drinks']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Meat
d={'Meat':Mean_values_Item_Type_data['Meat']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Breads
d={'Breads':Mean_values_Item_Type_data['Breads']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Hard Drinks
d={'Hard Drinks':Mean_values_Item_Type_data['Hard Drinks']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Others
d={'Others':Mean_values_Item_Type_data['Others']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Starchy Foods
d={'Starchy Foods':Mean_values_Item_Type_data['Starchy Foods']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Breakfast
d={'Breakfast':Mean_values_Item_Type_data['Breakfast']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
#Seafood
d={'Seafood':Mean_values_Item_Type_data['Seafood']}
s=data.Item_Type.map(d)
data.Item_Weight=data.Item_Weight.combine_first(s)
data.Item_Weight.isnull().any()
##Feature Engineering
#combining Fat_Content_Type such as LF and Low Fat into single Types
data.Item_Fat_Content.value_counts()
#Fat_Content showing redudancy of differnt types
data['Item_Fat_Content']=data['Item_Fat_Content'].replace({'low fat':'Low Fat','reg':'Regular','LF':'Low Fat'})
data.Item_Fat_Content.value_counts()
#No of years of Outlet
data['Outlet_Years']=2017-data['Outlet_Establishment_Year']
#Item_Visiblity can not be zero .so impute it with corresponding aveerage value of visiblity
#of particular Item_Identifier Type
data['Item_Visibility']=data['Item_Visibility'].replace(0.0,np.nan)
data['Item_Visibility'].fillna(data.groupby('Item_Identifier')['Item_Visibility'].mean())
#
Mean_Visibility=data['Item_Visibility'].mean()
data['Item_Visibility_MeanRatio']=data.apply(lambda x:x['Item_Visibility']/Mean_Visibility,axis=1)
var_mod=['Item_Fat_Content','Outlet_Location_Type','Outlet_Size','Outlet_Type','Item_Type']
number=LabelEncoder()
for i in var_mod:
data[i]=number.fit_transform(data[i])