SandeepMM commited on
Commit
1dc0d1f
·
verified ·
1 Parent(s): a648b7a

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py CHANGED
@@ -47,3 +47,64 @@ def predict_churn():
47
  # Run the Flask app in debug mode
48
  if __name__ == '__main__':
49
  app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # Run the Flask app in debug mode
48
  if __name__ == '__main__':
49
  app.run(debug=True)
50
+
51
+
52
+ class FeatureEngineer(BaseEstimator, TransformerMixin):
53
+ def __init__(self):
54
+ # We need to store the LabelEncoders
55
+ # so they can be applied consistently to new data.
56
+ self.le_prod = LabelEncoder()
57
+ self.le_store = LabelEncoder()
58
+
59
+ def fit(self, X, y=None):
60
+ # Create a new feature 'Product_Id_Cd' from the first two characters of Product_Id.
61
+ X['Product_Id_Cd'] = X['Product_Id'].apply(lambda x: x[:2])
62
+
63
+ # Correct 'Product_Sugar_Content' to 'Product_Sugar_Content_Corr'
64
+ X['Product_Sugar_Content_Corr'] = X['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
65
+
66
+
67
+ # Calculate 'Operation_Years'
68
+ X['Operation_Years'] = 2025 - X['Store_Establishment_Year']
69
+
70
+ self.le_prod.fit(X['Product_Id_Cd'])
71
+ le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
72
+ for i in le_feat:
73
+ self.le_prod.fit(X[i])
74
+
75
+ # Fit LabelEncoder for 'Store'
76
+ self.le_store.fit(X['Store_Id'])
77
+ return self
78
+
79
+ def transform(self, X):
80
+ X_copy = X.copy()
81
+
82
+ # Apply the transformations
83
+ X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
84
+
85
+ X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
86
+
87
+ X_copy['Operation_Years'] = 2013 - X_copy['Store_Establishment_Year']
88
+
89
+ # Using a try-except block to handle unseen categories gracefully
90
+ try:
91
+
92
+ le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
93
+ for i in le_feat:
94
+ X_copy[i] = self.le_prod.transform(X_copy[i])
95
+ except ValueError:
96
+ # Handling unknown categories in production data
97
+ X_copy['Product_Id_Cd'] = -1
98
+
99
+ # Apply LabelEncoder to 'Store_Id'
100
+ try:
101
+ X_copy['Store'] = self.le_store.transform(X_copy['Store_Id'])
102
+ except ValueError:
103
+ X_copy['Store'] = -1
104
+
105
+
106
+ # Droping the features which have been processed into new features already
107
+ rem_feat=['Product_Id','Store_Id','Product_Sugar_Content','Product_Type', 'Store_Establishment_Year']
108
+ X_copy.drop(rem_feat, axis=1, inplace=True)
109
+
110
+ return X_copy