Skip to content

Instantly share code, notes, and snippets.

@SherazKhan
Created April 25, 2022 01:27
Show Gist options
  • Save SherazKhan/f1d11d7e024c4c44059124d51e4f03b8 to your computer and use it in GitHub Desktop.
Save SherazKhan/f1d11d7e024c4c44059124d51e4f03b8 to your computer and use it in GitHub Desktop.
from imblearn.ensemble import BalancedRandomForestClassifier
from sklearn.datasets import make_classification
from imblearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn import metrics
X, y = make_classification(n_samples=1000, n_classes=3,
n_informative=4, weights=[0.2, 0.3, 0.5],
random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size=0.2)
clf = BalancedRandomForestClassifier()
pipeline = Pipeline([ ('rf', clf)])
hyper_params = {'rf__max_depth': 20, 'rf__max_features': 2, 'rf__n_estimators': 1000}
pipeline.set_params(**hyper_params)
model = pipeline.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = metrics.accuracy_score(y_true=y_test, y_pred=y_pred)
# store accuracy in results
print('---------------------')
print('| Accuracy |')
print('---------------------')
print('\n {}\n\n'.format(accuracy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment