import sklearn from sklearn.datasets import load_breast_cancer from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, classification_report from sklearn.model_selection import train_test_split data = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split( data.data, data.target, test_size=0.2, random_state=42, stratify=data.target, ) model = RandomForestClassifier( n_estimators=200, max_depth=5, random_state=42, ) model.fit(X_train, y_train) predictions = model.predict(X_test) accuracy = accuracy_score(y_test, predictions) top_features = sorted( zip(data.feature_names, model.feature_importances_), key=lambda item: item[1], reverse=True, )[:5] print(f"sklearn version: {sklearn.__version__}") print(f"train rows: {X_train.shape[0]}") print(f"test rows: {X_test.shape[0]}") print(f"classes: {data.target_names.tolist()}") print(f"trees: {len(model.estimators_)}") print(f"held-out accuracy: {accuracy:.3f}") print(f"first predictions: {data.target_names[predictions[:5]].tolist()}") print("top feature importances:") for feature, importance in top_features: print(f" {feature}: {importance:.3f}") print("classification report:") print(classification_report(y_test, predictions, target_names=data.target_names))