SandeepMM commited on
Commit
41c96b2
·
verified ·
1 Parent(s): 0085d6b

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py CHANGED
@@ -1,8 +1,59 @@
1
 
2
  import joblib
3
  import pandas as pd
 
4
  from flask import Flask, request, jsonify
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Initialize Flask app with a name
7
  app = Flask("SuperKart Sales Predictor")
8
 
 
1
 
2
  import joblib
3
  import pandas as pd
4
+ import numpy as np
5
  from flask import Flask, request, jsonify
6
 
7
+ from sklearn.pipeline import Pipeline
8
+ from sklearn.compose import ColumnTransformer
9
+ from sklearn.base import BaseEstimator, TransformerMixin
10
+ from sklearn.preprocessing import OneHotEncoder, StandardScaler, LabelEncoder
11
+ from sklearn.impute import SimpleImputer
12
+ from sklearn.ensemble import RandomForestRegressor
13
+ from xgboost import XGBRegressor # Included for compatibility if you switch models
14
+
15
+ class FeatureEngineer(BaseEstimator, TransformerMixin):
16
+ def __init__(self):
17
+ self.le_prod = LabelEncoder()
18
+ self.le_store = LabelEncoder()
19
+
20
+ def fit(self, X, y=None):
21
+ X_copy = X.copy()
22
+ X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
23
+ X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
24
+ X_copy['Operation_Years'] = 2025 - X_copy['Store_Establishment_Year']
25
+
26
+ self.le_prod.fit(X_copy['Product_Id_Cd'])
27
+ le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
28
+ for i in le_feat:
29
+ self.le_prod.fit(X_copy[i])
30
+
31
+ self.le_store.fit(X_copy['Store_Id'])
32
+ return self
33
+
34
+ def transform(self, X):
35
+ X_copy = X.copy()
36
+ X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
37
+ X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
38
+ X_copy['Operation_Years'] = 2013 - X_copy['Store_Establishment_Year']
39
+
40
+ try:
41
+ le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
42
+ for i in le_feat:
43
+ X_copy[i] = self.le_prod.transform(X_copy[i])
44
+ except ValueError:
45
+ X_copy['Product_Id_Cd'] = -1
46
+
47
+ try:
48
+ X_copy['Store'] = self.le_store.transform(X_copy['Store_Id'])
49
+ except ValueError:
50
+ X_copy['Store'] = -1
51
+
52
+ rem_feat=['Product_Id','Store_Id','Product_Sugar_Content','Product_Type', 'Store_Establishment_Year']
53
+ X_copy.drop(rem_feat, axis=1, inplace=True)
54
+
55
+ return X_copy
56
+
57
  # Initialize Flask app with a name
58
  app = Flask("SuperKart Sales Predictor")
59