File size: 1,595 Bytes
d7dde45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import os
from sklearn.model_selection import train_test_split

def prepare_data(input_csv_path='engine1/data/engine.csv', output_dir='engine1/data'):
    # 1. Load the dataset
    df = pd.read_csv(input_csv_path)

    # 2. Define column renaming mapping
    column_name_mapping = {
        'Engine rpm': 'engine_rpm',
        'Lub oil pressure': 'lub_oil_pressure',
        'Fuel pressure': 'fuel_pressure',
        'Coolant pressure': 'coolant_pressure',
        'lub oil temp': 'lub_oil_temp',
        'Coolant temp': 'coolant_temp',
        'Engine Condition': 'engine_condition'
    }

    # 3. Rename columns
    df.rename(columns=column_name_mapping, inplace=True)

    # 4. Separate features (X) and target (y)
    X = df.drop('engine_condition', axis=1)
    y = df['engine_condition']

    # 5. Split data into training and testing sets with stratification
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

    # 6. Create output directory if it doesn't exist
    os.makedirs(output_dir, exist_ok=True)

    # 7. Save prepared datasets
    X_train.to_csv(os.path.join(output_dir, 'X_train.csv'), index=False)
    X_test.to_csv(os.path.join(output_dir, 'X_test.csv'), index=False)
    y_train.to_csv(os.path.join(output_dir, 'y_train.csv'), index=False)
    y_test.to_csv(os.path.join(output_dir, 'y_test.csv'), index=False)

    print(f"Data preparation complete. Saved files to {output_dir}")

# Example usage (can be called from another script or notebook)
# if __name__ == '__main__':
#     prepare_data()