This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RULModel(nn.Module): | |
| def __init__(self, n_features, n_hidden=256, n_layers=3): | |
| super().__init__() | |
| self.lstm = nn.LSTM( | |
| input_size=n_features, | |
| hidden_size=n_hidden, | |
| num_layers=n_layers, | |
| batch_first=True, | |
| dropout=0.75 | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from torch.utils.data import TensorDataset | |
| class RULDataModule(pl.LightningDataModule): | |
| def __init__(self, X_train, y_train, X_val, y_val, X_test, y_test, | |
| batch_size): | |
| super().__init__() | |
| self.X_train = X_train | |
| self.y_train = y_train | |
| self.X_val = X_val | |
| self.y_val = y_val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def avg_diff(series): | |
| return np.mean(np.diff(series)) | |
| all_rollings_grouped = all_rollings_df_X.drop(columns=['time']).groupby('instance_id').agg(['mean', avg_diff, 'std', 'max', 'min']) | |
| test_df_grouped = test_df.sort_values(['unit_number', 'time']).groupby('unit_number').\ | |
| apply(lambda group_df: group_df[[x for x in test_df.columns if 'sensor_' in x]].\ | |
| iloc[-WINDOW_SIZE:]).reset_index() | |
| test_df_aggregated = test_df_grouped.drop(columns=['level_1']).groupby('unit_number').agg(['mean', avg_diff, 'std', 'max', 'min']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| all_rollings_df_X = all_rollings_df.drop(columns=['RUL']) | |
| extracted_features = extract_features(all_rollings_df_X, | |
| column_id="instance_id", column_sort="time", | |
| n_jobs=4, default_fc_parameters=tsfresh.feature_extraction.settings.MinimalFCParameters()) | |
| impute(extracted_features) | |
| features_filtered = select_features(extracted_features, y_train_rolling, n_jobs=4) | |
| def get_last_window_from_unit(group_df): | |
| res_df = group_df[[x for x in test_df.columns if 'sensor_' in x]].iloc[-10:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| xgbr_windows_naive = XGBRegressor() | |
| xgbr_windows_naive.fit(X_train_rolling.reshape(X_train_rolling.shape[0], -1), y_train_rolling) | |
| print_train_test_results(X_train_rolling.reshape(X_train_rolling.shape[0], -1), | |
| X_test_rolling.reshape(X_test_rolling.shape[0], -1), | |
| y_train_rolling, y_test, xgbr_windows_naive) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WINDOW_SIZE = 20 | |
| def get_windowed_dataframes(df): | |
| df_groups = df.sort_values(['unit_number', 'time']).groupby('unit_number') | |
| all_rollings = [] | |
| for _, group_df in df_groups: | |
| group_df_rolling = group_df.rolling(window=WINDOW_SIZE) | |
| all_rollings.extend([wnd for wnd in group_df_rolling if len(wnd) == WINDOW_SIZE]) | |
| return all_rollings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test_df.drop(columns=[f'sensor_{i}' for i in [3, 4, 8, 9, 13, 19, 21, 22, 25, 26]], inplace=True, errors='ignore') | |
| for col_name in [x for x in test_df.columns if 'sensor_' in x]: | |
| test_df[col_name] = MIN_MAX_SCALERS[col_name].transform(test_df[col_name].values.reshape(-1, 1)).squeeze() | |
| X_test = test_df.groupby('unit_number').apply(lambda group_df: group_df.iloc[group_df['time'].argmax()])[[x for x in test_df.columns if 'sensor_' in x]].values | |
| y_test = pd.read_csv('/content/drive/MyDrive/Datasets/NASA_CMAPSS/RUL_FD001.txt', header=None).values.squeeze().clip(max=125) | |
| def print_train_test_results(X_train, X_test, y_train, y_test, model): | |
| y_pred_train = model.predict(X_train) | |
| y_pred_test = model.predict(X_test) | |
| print(f'RMSE on train set: {mean_squared_error(y_train, y_pred_train, squared=False)}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| xgbr = XGBRegressor() | |
| xgbr.fit(X_train, y_train) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train_df.drop(columns=[f'sensor_{i}' for i in [3, 4, 8, 9, 13, 19, 21, 22]], inplace=True, errors='ignore') | |
| RUL = train_df.groupby('unit_number').apply(lambda group_df: | |
| pd.concat([group_df['time'].max() - group_df['time'], group_df['time']], axis=1)).\ | |
| reset_index().drop(columns=['level_1']) | |
| RUL.columns = ['unit_number', 'RUL', 'time'] | |
| train_df = pd.merge(train_df, RUL, left_on=['unit_number', 'time'], right_on=['unit_number', 'time']) | |
| X_train = train_df[[x for x in train_df.columns if 'sensor_' in x]].values | |
| y_train = train_df['RUL'].values.clip(max=125) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MIN_MAX_SCALERS = {} | |
| for col_name in SENSOR_COLUMN_NAMES: | |
| scaler = MinMaxScaler() | |
| train_df[col_name] = scaler.fit_transform(train_df[col_name].values.reshape(-1, 1)).squeeze() | |
| MIN_MAX_SCALERS[col_name] = scaler |