-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.py
72 lines (57 loc) · 3.06 KB
/
cleanup.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
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
# reading both the data sets
trainDf = pd.read_csv("dataset/train.csv")
testDf = pd.read_csv("dataset/test.csv")
# checking lengths
trainingSetIndex = len(trainDf)
print(len(trainDf), len(testDf))
# data type check
print(trainDf.dtypes)
# null check
# OP: Item_Weight, Outlet_Size
print(trainDf.isnull().sum())
# combining both
combination = trainDf.append(testDf)
print(len(combination))
# dropping identifier columns
combination = combination.drop(["Item_Identifier", "Outlet_Identifier"], axis=1)
# replacing the null in the ItemWeight
combination = combination.fillna(combination.median())
# replacing nominal values
combination["Item_Fat_Content"] = combination["Item_Fat_Content"].replace({"LF": 0, "reg": 1})
combination["Item_Fat_Content"] = combination["Item_Fat_Content"].replace({"Low Fat": 0, "Regular": 1})
combination["Item_Fat_Content"] = combination["Item_Fat_Content"].replace({"low fat": 0, "Regular": 1})
print(combination["Item_Fat_Content"].unique())
perishable = ["Breads", "Breakfast", "Dairy", "Fruits and Vegetables", "Meat", "Seafood"]
non_perishable = ["Baking Goods", "Canned", "Frozen Foods", "Hard Drinks", "Health and Hygiene", "Household",
"Soft Drinks", "Snack Foods", "Starchy Foods", "Others"]
combination["Item_Type"] = combination["Item_Type"].replace(to_replace=perishable, value="perishable")
combination["Item_Type"] = combination["Item_Type"].replace(to_replace=non_perishable, value="non_perishable")
combination["Item_Type"] = combination["Item_Type"].replace({"perishable": 0, "non_perishable": 1})
print(combination["Item_Type"].unique())
combination["Outlet_Size"] = combination["Outlet_Size"].replace({"Small": 0,
"High": 1,
"Medium": 2,
np.nan: 3})
print(combination["Outlet_Size"].unique())
combination["Outlet_Location_Type"] = combination["Outlet_Location_Type"].replace({"Tier 3": 0,
"Tier 2": 1,
"Tier 1": 2})
print(combination["Outlet_Location_Type"].unique())
combination["Outlet_Type"] = combination["Outlet_Type"].replace({"Grocery Store": 0,
"Supermarket Type1": 1,
"Supermarket Type2": 2,
"Supermarket Type3": 3})
print(combination["Outlet_Type"].unique())
# splitting again the cleaned data sets
trainDfClean = combination[:trainingSetIndex]
testDfClean = combination[trainingSetIndex:]
# saving the sets
trainDfClean.to_csv("./dataset/train_processed.csv", index=False)
testDfClean.to_csv("./dataset/test_processed.csv", index=False)
# plotting the hist
trainDfClean.hist()
testDfClean.hist()
plt.show()