-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_processing.py
36 lines (28 loc) · 1.11 KB
/
data_processing.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
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import MinMaxScaler
from scipy.sparse import hstack
def load_data(file_path):
"""
Load the dataset from a CSV file.
"""
df = pd.read_csv(file_path)
return df
def preprocess_data(df):
"""
Preprocess the data: feature engineering and combining features.
"""
# Define the target variables (multi-labels)
y = df[['Combination', 'Dry', 'Normal', 'Oily', 'Sensitive']]
# Initialize the TF-IDF Vectorizer
tfidf = TfidfVectorizer(stop_words='english', max_features=1000)
# Fit and transform the 'Ingredients' column
X_ingredients = tfidf.fit_transform(df['Ingredients'])
# Normalize 'Price' and 'Rank' columns using MinMaxScaler
scaler = MinMaxScaler()
df[['Price', 'Rank']] = scaler.fit_transform(df[['Price', 'Rank']])
# Extract the normalized 'Price' and 'Rank' as numpy arrays
X_price_rank = df[['Price', 'Rank']].values
# Combine all features into a single feature set
X = hstack([X_ingredients, X_price_rank])
return X, y, tfidf, scaler