<jupyter_start><jupyter_text>Sequence classification with Neural Networks: a primer Part 2: Advnced Tree modelIn this notebook we will try to give our tree model a boost by using values from previous timesteps as separate features.<jupyter_code>import altair as alt
import pandas as pd

import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)

from tmdprimer.datagen import Dataset
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
data_trees = []

def get_windowed_data(dataset: Dataset, window_size: int):
    # use the same as we use for CNN
    tfds = dataset.to_cnn_tfds(2, batch_size=1)
    # I'm not a numpy ninja so let's just append to arrays
    X = []
    y = []
    for elem in tfds:
        X.append(elem[0][0].numpy().ravel(order='C'))
        y.append(elem[1].numpy().ravel(order='C')[0])
    return X, y

for outlier_prob in (0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0):
    for window_size in (1, 2, 5):
        dataset = Dataset.generate(train_outlier_prob=outlier_prob)
        X, y = get_windowed_data(dataset, window_size=2)

        clf = RandomForestClassifier(n_estimators=10, class_weight="balanced")
        clf.fit(X, y)
        dataset_test = Dataset.generate(train_outlier_prob=outlier_prob, n_samples=20)
        X_test, y_test = get_windowed_data(dataset_test, window_size=2)
        y_pred = clf.predict(X_test)
        acc = accuracy_score(y_test, y_pred)
        data_trees.append({'outlier_prob': outlier_prob, 'accuracy': acc, 'window_size': window_size})
df_trees = pd.DataFrame(data_trees)
alt.Chart(df_trees).mark_line().encode(x='outlier_prob', y='accuracy', color='window_size:N')<jupyter_output><empty_output><jupyter_text>Didn't change anything<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
data_trees = []

for outlier_prob in (0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0):
    for window_size in (1, 2, 5):
        dataset = Dataset.generate(train_outlier_prob=outlier_prob)
        X, y = get_windowed_data(dataset, window_size=2)

        clf = RandomForestClassifier(n_estimators=10, class_weight="balanced")
        clf.fit(X, y)
        dataset_test = Dataset.generate(train_outlier_prob=outlier_prob, n_samples=20)
        X_test, y_test = get_windowed_data(dataset_test, window_size=2)
        y_pred = clf.predict(X_test)
        acc = accuracy_score(y_test, y_pred)
        data_trees.append({'outlier_prob': outlier_prob, 'accuracy': acc, 'window_size': window_size})
df_trees = pd.DataFrame(data_trees)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11771217712177122, 'fraction_numerical': 0.02841328413284133, 'mean_word_length': 4.253875968992248, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '257075', 'license': 'MIT', 'n_tokens_mistral': 1054, 'n_tokens_neox': 981, 'n_words': 220, 'path': 'tmdprimer/Tree model advanced.ipynb', 'repo_name': 'dragoon/lstm-primer', 'size': '2710'}