from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pyplot as plt
from sklearn import tree
import dtreeviz
import pandas as pd
iris = load_iris()
data = pd.DataFrame(iris.data)
X = data.iloc[:,1:4]
y = data.iloc[:,0]
fn=["sepal width","petal length","petal width"]
cn ="sepal length"
clf = RandomForestRegressor(max_depth=4,random_state=2024,min_samples_leaf=10,n_estimators=10,oob_score=True )
clf.fit(X, y)
model = clf.estimators_[0]
no_dup = list(set(clf.estimators_samples_[0]))
X_in = X.loc[no_dup]
y_in = y.loc[no_dup]
viz_model = dtreeviz.model(model,
X_train = X_in,
y_train = y_in,
feature_names=fn,
target_name=cn)
v = viz_model.view(x=X.iloc[0,:]) # render as SVG into internal object
v.show()
v = viz_model.view(x=[3.5,1.4,0.2])