<jupyter_start><jupyter_text>Random Forest Classifier  Get dataset<jupyter_code>from finlab.ml import fundamental_features
dataset = fundamental_features()
dataset = dataset.dropna(thresh=int(len(dataset)*0.5), axis=1).dropna(how='any')<jupyter_output><empty_output><jupyter_text>Add labels<jupyter_code>features = dataset.columns

import finlab.ml as ml

ml.add_profit_prediction(dataset)
dataset = dataset.dropna()
dataset.head()<jupyter_output><empty_output><jupyter_text>Split Train Test dataset<jupyter_code>select = dataset.index.get_level_values('date') < '2017'
dataset_train = dataset[select]
dataset_test = dataset[~select]<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from sklearn.ensemble import RandomForestClassifier

cf = RandomForestClassifier(200)
cf.fit(dataset_train[features], dataset_train['return'] > 1)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>import pandas as pd
prediction = cf.predict(dataset_test[features])<jupyter_output><empty_output><jupyter_text>Backtest<jupyter_code>import matplotlib.pyplot as plt
plt.style.use("ggplot")

%matplotlib inline

returns1 = dataset_test['return'][prediction == True]
dates = returns1.index.get_level_values('date')
returns1.groupby(dates).mean().cumprod().plot()

returns2 = dataset_test['return'][prediction == False]
dates = returns2.index.get_level_values('date')
returns2.groupby(dates).mean().cumprod().plot()<jupyter_output><empty_output><jupyter_text>Feature Importance<jupyter_code>importance = pd.Series(cf.feature_importances_, index=features).sort_values(ascending=False)
importance<jupyter_output><empty_output><jupyter_text>New Strategy using feature importance<jupyter_code>from finlab.data import Data
data = Data()
close = data.get('收盤價')
sma = close.rolling(60, min_periods=10).mean()
bias = close / sma

ml.add_feature(dataset, 'bias', bias)
dataset.head()
items = list(importance.index[:20])

def select(df):
    rank = df[items].rank(pct=True).sum(axis=1)
    return df[rank > rank.quantile(0.9)]['return'][df['bias'] > 1].mean()

dates = dataset.index.get_level_values('date')
dataset.groupby(dates).apply(select).cumprod().plot()
dataset['return'].groupby(dates).mean().cumprod().plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1436832740213523, 'fraction_numerical': 0.01201067615658363, 'mean_word_length': 10.078817733990148, 'pattern_counts': {'":': 0, '<': 34, '<?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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '327801', 'license': 'Apache-2.0', 'n_tokens_mistral': 799, 'n_tokens_neox': 782, 'n_words': 102, 'path': 'finlab/u14_random_forest.ipynb', 'repo_name': 'DandyWei/finlab_course_ml', 'size': '2248'}