<jupyter_start><jupyter_text>Demo of visualizing iRF interaction prevalence<jupyter_code>import irf
from sklearn.datasets import load_boston, load_breast_cancer
from irf.ensemble import (wrf, 
                          wrf_reg)
from irf.utils import (get_prevalent_interactions, 
                       visualize_impurity_decrease, 
                       visualize_prevalent_interactions)
import numpy as np<jupyter_output><empty_output><jupyter_text>load data<jupyter_code>#load_data
data = load_boston()
#data = load_breast_cancer()
X = data['data']
y = data['target']<jupyter_output><empty_output><jupyter_text>traing weighted RF<jupyter_code>#train_model
rf = wrf_reg(min_impurity_decrease=0.01)
weight = np.ones((X.shape[1],))
weight = weight / np.sum(weight)
rf.fit(X, y, feature_weight=weight, K = 3, keep_record=False)
#visualize feature importance of rf
print(rf.feature_importances_)<jupyter_output>[0.03944525 0.00050898 0.00419495 0.00047362 0.02077369 0.43525535
 0.01113178 0.06336516 0.00163973 0.01238247 0.01501942 0.00775546
 0.38805414]<jupyter_text>- Observation: feature 5 and 12 are most useful.<jupyter_code>#visualize the impurity decrease distribution at all inner nodes
visualize_impurity_decrease(rf, yscale='log', xscale="linear", bins=40)<jupyter_output><empty_output><jupyter_text>- Observation: there are two modes, one around 15, the other around 40.<jupyter_code>#preprocess compute the prevalence of the rf model
prevalence = get_prevalent_interactions(rf, impurity_decrease_threshold=9, min_support=10, signed=True)
#visualize prevalence of the interactions from signed irf
visualize_prevalent_interactions(prevalence)<jupyter_output><empty_output><jupyter_text>- Observation: There is a order-two interaction that is close to the limit.<jupyter_code>#visualize interactions sorted by prevalence
sorted(list(prevalence.items()), key= lambda x: -x[1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09342379958246347, 'fraction_numerical': 0.07045929018789145, 'mean_word_length': 6.316793893129771, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '525750', 'license': 'Apache-2.0', 'n_tokens_mistral': 766, 'n_tokens_neox': 644, 'n_words': 143, 'path': 'I-Random-Forest/02_iRF_visualize_interaction_prevalence.ipynb', 'repo_name': 'raja21068/Research-on-data-mining-of-permission-induced-risk-for-android-IoT-devices', 'size': '1916'}