-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_processing.py
58 lines (50 loc) · 1.74 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import pandas as pd
import os
def load_hr_data():
"""
Load HR data from CSV files in the input directory.
"""
hr_data = None
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
if filename.endswith('.csv'):
file_path = os.path.join(dirname, filename)
print(f"Loading data from: {file_path}")
if hr_data is None:
hr_data = pd.read_csv(file_path)
else:
hr_data = hr_data.append(pd.read_csv(file_path), ignore_index=True)
return hr_data
def preprocess_hr_data(df):
"""
Preprocess the HR data.
"""
# Handle missing values
df = df.dropna()
# Convert categorical variables to numerical
categorical_columns = df.select_dtypes(include=['object']).columns
for col in categorical_columns:
df[col] = pd.Categorical(df[col]).codes
# Normalize numerical columns
numerical_columns = df.select_dtypes(include=['int64', 'float64']).columns
df[numerical_columns] = (df[numerical_columns] - df[numerical_columns].mean()) / df[numerical_columns].std()
return df
def get_processed_hr_data():
"""
Load and preprocess HR data.
"""
raw_data = load_hr_data()
if raw_data is not None:
processed_data = preprocess_hr_data(raw_data)
return processed_data
else:
print("No data found in the input directory.")
return None
if __name__ == "__main__":
processed_data = get_processed_hr_data()
if processed_data is not None:
print(processed_data.head())
print(f"Processed data shape: {processed_data.shape}")
else:
print("Failed to process HR data.")