Download data_preparation.py from grkavi0912/engine1: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/datasets/grkavi0912/engine1/resolve/refs%2Fpr%2F16/data_preparation.py
- Command line
-
hf download hf://datasets/grkavi0912/engine1@refs/pr/16/data_preparation.py
-
curl -L -o data_preparation.py https://huggingface.co/datasets/grkavi0912/engine1/resolve/refs%2Fpr%2F16/data_preparation.py
1.6 kB
| 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() | |