<jupyter_start><jupyter_text>Random Forest Classifier Read Xy.pkl and create SQL table features_clf<jupyter_code>import sys
sys.path.append('..')
from seizurecast.models.pipeline import Pipeline, Config

import numpy as np
import pandas as pd

pipeline = Pipeline(Config())
X, y = pipeline.load_xy('../data/processed/xy.pkl')

from seizurecast.features.to_sql import SQLengine

pd.DataFrame(X).assign(label=y).to_sql('features_clf', SQLengine)
import sys
sys.path.append('..')
from seizurecast.models.pipeline import Pipeline, Config
from seizurecast.features.to_sql import SQLengine
import pandas as pd

pipeline = Pipeline(Config())
df = pd.read_sql_table('features_clf', SQLengine)
df.head()<jupyter_output><empty_output><jupyter_text>n_estimators<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from seizurecast.models.Result import Results

pipeline.X = df.iloc[:,1:17].to_numpy()
pipeline.y = df.iloc[:,17].to_numpy()
pipeline.results = Results()
pipeline.models = {}
pipeline.models['rf-n=40'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=3, random_state=0)
pipeline.models['rf-n=10'] = RandomForestClassifier(n_estimators=10, max_depth=None,
                                               min_samples_split=3, random_state=0)
pipeline.models['rf-n=5'] = RandomForestClassifier(n_estimators=5, max_depth=None,
                                               min_samples_split=3, random_state=0)
pipeline.pipe()
pipeline.results.plot_roc_curve()<jupyter_output>Collected 3068 data points
Recall	 Base: 1.0	 Test: 0.87	 Train: 1.0
Precis	 Base: 0.49	 Test: 0.93	 Train: 1.0
Roc	 Base: 0.5	 Test: 0.95	 Train: 1.0
Recall	 Base: 1.0	 Test: 0.85	 Train: 0.99
Precis	 Base: 0.49	 Test: 0.88	 Train: 1.0
Roc	 Base: 0.5	 Test: 0.93	 Train: 1.0
Recall	 Base: 1.0	 Test: 0.87	 Train: 0.98
Precis	 Base: 0.49	 Test: 0.8	 Train: 0.98
Roc	 Base: 0.5	 Test: 0.91	 Train: 1.0<jupyter_text>min_sample_split<jupyter_code>pipeline.results = Results()
pipeline.models = {}
pipeline.models['rf-min_split=3'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=3, random_state=0)
pipeline.models['rf-min_split=5'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=5, random_state=0)
pipeline.models['rf-min_split=10'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=10, random_state=0)
pipeline.pipe()
import numpy as np
pd.DataFrame({k: round(np.mean(v), 3) for (k, v) in pipeline.scores_CV.items()}, index=[0])
import numpy as np
pd.DataFrame({k: round(np.mean(v), 3) for (k, v) in pipeline.scores_CV.items()}, index=[0])
pipeline.results = Results()
pipeline.models = {}
pipeline.models['rf-max_depth=None'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=3, random_state=0)
pipeline.models['rf-max_depth=20'] = RandomForestClassifier(n_estimators=40, max_depth=20,
                                               min_samples_split=3, random_state=0)
pipeline.models['rf-max_depth=10'] = RandomForestClassifier(n_estimators=40, max_depth=5,
                                               min_samples_split=3, random_state=0)
pipeline.pipe()
import numpy as np
pd.DataFrame({k: round(np.mean(v), 3) for (k, v) in pipeline.scores_CV.items()}, index=[0])<jupyter_output>Collected 3068 data points
Recall	 Base: 1.0	 Test: 0.87	 Train: 1.0
Precis	 Base: 0.49	 Test: 0.93	 Train: 1.0
Roc	 Base: 0.5	 Test: 0.95	 Train: 1.0
Recall	 Base: 1.0	 Test: 0.85	 Train: 1.0
Precis	 Base: 0.49	 Test: 0.91	 Train: 1.0
Roc	 Base: 0.5	 Test: 0.95	 Train: 1.0
Recall	 Base: 1.0	 Test: 0.68	 Train: 0.71
Precis	 Base: 0.49	 Test: 0.84	 Train: 0.87
Roc	 Base: 0.5	 Test: 0.87	 Train: 0.91<jupyter_text>Feature importance<jupyter_code>def plot_feature_importance(forest):
    import matplotlib.pyplot as plt
    import numpy as np
    importances = forest.feature_importances_
    std = np.std([tree.feature_importances_ for tree in forest.estimators_],
                 axis=0)
    indices = np.argsort(importances)[::-1]

    num_of_features = len(indices)
    # Plot the impurity-based feature importances of the forest
    plt.figure(figsize=[5/15*num_of_features, 3])
    plt.title("Feature importances")
    plt.bar(range(len(importances)), importances[indices],
            color="r", yerr=std[indices], align="center")
    plt.xticks(range(len(importances)), indices)
    plt.xlim([-1, len(importances)])
    # plt.show()
    return(indices)
forest = pipeline.results.results['rf-max_depth=None'].model
indices = plot_feature_importance(forest)<jupyter_output><empty_output><jupyter_text>Feature  0 - 7 correspond to the amplitude of the Fourier transformed spectra for channel 0 - 7. The above figure showed these features are the most important ones in the random forest.     8 channels vs 2 channelsChoose channel 2 and channel 6<jupyter_code>pipeline.results = Results()
pipeline.models = {}
pipeline.models['8 channels'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=3, random_state=0)
pipeline.pipe()
results = pipeline.results

# 2channel
pipe2 = Pipeline(Config())
pipe2.X = pipeline.X[:,[2,6]]
pipe2.y = pipeline.y
pipe2.models = {}
pipe2.models['2 channels'] = RandomForestClassifier(n_estimators=40, max_depth=None,
                                               min_samples_split=3, random_state=0)

pipe2.pipe()
results.append('2 channels', pipe2.results.results['2 channels'])
# import numpy as np
# pd.DataFrame({k: round(np.mean(v), 3) for (k, v) in pipeline.scores_CV.items()}, index=[0])

results.plot_roc_curve()<jupyter_output>Collected 3068 data points
Recall	 Base: 1.0	 Test: 0.87	 Train: 1.0
Precis	 Base: 0.49	 Test: 0.93	 Train: 1.0
Roc	 Base: 0.5	 Test: 0.95	 Train: 1.0
Collected 3068 data points
Recall	 Base: 1.0	 Test: 0.78	 Train: 0.99
Precis	 Base: 0.49	 Test: 0.73	 Train: 1.0
Roc	 Base: 0.5	 Test: 0.81	 Train: 1.0<jupyter_text>Regression<jupyter_code>import sys
sys.path.append('..')
from seizurecast.models.pipeline_sql import Pipeline_sql, Config
conf = Config()
conf.len_pos = 180
conf.len_pre = 180
conf.sec_gap = 180
psql = Pipeline_sql(conf = conf)
psql.pipe()
psql.results.plot_roc_curve()


forest = psql.results.results['rf'].model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12190650779101742, 'fraction_numerical': 0.04720439963336388, 'mean_word_length': 3.49039780521262, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '257829', 'license': 'MIT', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2257, 'n_words': 502, 'path': 'notebooks/Model-selection.ipynb', 'repo_name': 'kusumikakd/Real_time_seizurecast', 'size': '6546'}