<jupyter_start><jupyter_text>Load Dataset<jupyter_code>folderpath = '../../../data/interim/'
data_op_03 = pd.read_csv(folderpath + 'data_op_03.csv')
data_op_03.head()<jupyter_output><empty_output><jupyter_text>Data preprocessing  Get rid of informations there are not sensor readings Wang (2008) reports Sensor 15 has importat information. However, there are no relevant informations of this sensor. The data seems to be corrupted like this: Let's remove it from our database creating an object transformer.<jupyter_code>from phm08ds.features.feature_selection import RemoveSensor

tf_remove_sensor_15 = RemoveSensor(sensors=[15])
data_op_03 = tf_remove_sensor_15.fit_transform(data_op_03)
data_op_03.head()<jupyter_output><empty_output><jupyter_text>Before feeding to the classifier, let's remove unwanted information, such as unit, time_step and operational settings.<jupyter_code>from phm08ds.features.feature_selection import RemoveInfo

tf_remove_info = RemoveInfo()

data_with_features = tf_remove_info.fit_transform(data_op_03)
data_with_features.head()<jupyter_output><empty_output><jupyter_text>We need to normalize our data. Let's use Z-score standardization.<jupyter_code>from sklearn.preprocessing import StandardScaler

tf_std_scaller = preprocessing.StandardScaler()
data_with_features_std = tf_std_scaller.fit_transform(data_with_features.drop(labels='Health_state', axis=1))
data_with_features_std
labels = np.array(data_with_features['Health_state'])
labels<jupyter_output><empty_output><jupyter_text>Classification steps  Load Experiment model<jupyter_code>from phm08ds.models import experiment<jupyter_output><empty_output><jupyter_text>Define classifiers and its specifications<jupyter_code>from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
# SVM
svm_linear_clf = SVC(kernel='linear')
svm_rbf_clf = SVC(kernel='rbf')
svm_poly_clf = SVC(kernel='poly')
svm_sigmoid_clf = SVC(kernel='sigmoid')<jupyter_output><empty_output><jupyter_text>Put all clf in a dictionary:<jupyter_code>classifiers = {'SVM-Linear': svm_linear_clf, 'SVM-RBF': svm_rbf_clf, 'SVM-Poly': svm_poly_clf, 'SVM-Sigmoid': svm_sigmoid_clf}<jupyter_output><empty_output><jupyter_text>Since we are using SVM and MLP we need to extract all power from those methods. Let's perform a Random Search to parameters optimizations.  Hyperparameter tunning<jupyter_code>from sklearn.pipeline import Pipeline

data_preprocessing = Pipeline([('remove_sensor_15', tf_remove_sensor_15),
                               ('remove_info', tf_remove_info),
                               ('std_scaler', tf_std_scaller)
                              ])
from sklearn.model_selection import RandomizedSearchCV

random_search = dict((k,[]) for k in classifiers.keys())
param_dist_dict = {
                   'SVM-Linear': {'C': [2**i for i in range(-5,15)]},
                   'SVM-RBF': {'gamma': [2**i for i in range(-15,3)], 'C': [2**i for i in range(-5,15)]},
                   'SVM-Poly': {'gamma': [2**i for i in range(-15,3)], 'C': [2**i for i in range(-5,15)]},
                   'SVM-Sigmoid': {'gamma': [2**i for i in range(-15,3)], 'C': [2**i for i in range(-5,15)]}
                  }
for clf in param_dist_dict.keys():
    start = time()
    random_search[clf] = RandomizedSearchCV(classifiers[clf], param_dist_dict[clf], cv=10, n_iter=5, verbose=5, n_jobs=1, scoring='accuracy')
    random_search[clf].fit(data_with_features_std, labels)
    
    experiment.save_models(random_search, name='clf_svm')
    experiment.save_pipeline(data_preprocessing)
    
    print('Elapsed time:')
    print(time() - start)<jupyter_output>Fitting 10 folds for each of 5 candidates, totalling 50 fits
[CV] C=0.03125 .......................................................
[CV] .............. C=0.03125, score=0.6097560975609756, total=   0.5s
[CV] C=0.03125 .......................................................<jupyter_text>Savel results, models and pipeline to a .pkl file<jupyter_code>from sklearn.pipeline import Pipeline

data_preprocessing = Pipeline([('remove_sensor_15', tf_remove_sensor_15),
                               ('remove_info', tf_remove_info),
                               ('std_scaler', tf_std_scaller)
                              ])
experiment.save_models(random_search, name='clf_svm')
experiment.save_pipeline(data_preprocessing)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.136654478976234, 'fraction_numerical': 0.026508226691042046, 'mean_word_length': 4.137323943661972, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '39318', 'license': 'MIT', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1428, 'n_words': 300, 'path': 'notebooks/E02_PHM08-train_MLP_and_SVM/model_selection-OP_03-navarmn/0.1-model_selection_SVM-OP_03-navarmn.ipynb', 'repo_name': 'navarmn/Turbofan-Airplane-fault-prognosis', 'size': '4376'}