charantejapolavarapu commited on
Commit
0021de5
·
verified ·
1 Parent(s): d180f5a

Create train_local.py

Browse files
Files changed (1) hide show
  1. train_local.py +38 -0
train_local.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import xgboost as xgb
4
+ import joblib
5
+
6
+ # 1. Download the dataset
7
+ url = "https://raw.githubusercontent.com/datasets-machine-learning/nasa-turbofan-failure-prediction/master/data/train_FD001.txt"
8
+ cols = ['unit', 'cycles', 'os1', 'os2', 'os3'] + [f's{i}' for i in range(1, 22)]
9
+ df = pd.read_csv(url, sep='\s+', header=None, names=cols)
10
+
11
+ # 2. Calculate Remaining Useful Life (RUL) - This is our 'Target'
12
+ # We find the maximum cycle for each engine and subtract the current cycle
13
+ max_cycles = df.groupby('unit')['cycles'].max().reset_index()
14
+ max_cycles.columns = ['unit', 'max_of_unit']
15
+ df = df.merge(max_cycles, on='unit', how='left')
16
+ df['RUL'] = df['max_of_unit'] - df['cycles']
17
+
18
+ # 3. Feature Selection
19
+ # We use the most important sensors for a jet engine
20
+ features = ['cycles', 's2', 's3', 's4', 's7', 's8', 's11', 's12', 's13', 's15', 's17', 's20', 's21']
21
+ X = df[features]
22
+ y = df['RUL']
23
+
24
+ # 4. Train the Model (XGBoost)
25
+ print("Training the model... please wait.")
26
+ model = xgb.XGBRegressor(
27
+ n_estimators=100,
28
+ learning_rate=0.1,
29
+ max_depth=5,
30
+ objective='reg:squarederror'
31
+ )
32
+ model.fit(X, y)
33
+
34
+ # 5. Save the 'Brain' of the AI
35
+ joblib.dump(model, 'engine_model.pkl')
36
+
37
+ print("✅ Success! 'engine_model.pkl' has been created in your folder.")
38
+ print("Now, upload this file to your Hugging Face Space.")