elena-sharova / isolationforest Goto Github PK
View Code? Open in Web Editor NEWtesting scikit-learn Isolation Forest
testing scikit-learn Isolation Forest
I have a problem regarding section 4 of notebook (4. Setting Parameters of Isolation Forest - n_estimators
) I want to print the optimized number of trees which is n_estimators
as follows:
#import libraries
from sklearn import datasets
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import recall_score, silhouette_score, roc_curve, roc_auc_score, f1_score, precision_recall_curve, auc
from sklearn.model_selection import GridSearchCV
# The part of notebook related to section 4 (4. Setting Parameters of Isolation Forest - n_estimators)
scoring = {'AUC': 'roc_auc', 'Recall': make_scorer(recall_score, pos_label=-1)}
gs = GridSearchCV(IsolationForest(max_samples=0.25, random_state=11, contamination = 0.15, n_jobs=-1),
param_grid={'n_estimators': range(20, 230, 30)},
scoring=scoring, refit='Recall', return_train_score=True, cv=3, verbose=1, n_jobs=-1)
gs.fit(X_train_sf, y_train_sf)
results = gs.cv_results_
#Frame the results via pandas
pd.DataFrame(results)
#print the best n_estimators based on recall metric
max_recall_n_estimators = int(pd.DataFrame(results)[["param_n_estimators", "mean_train_Recall"]].loc[pd.DataFrame(results)["mean_train_Recall"] == pd.DataFrame(results)["mean_train_Recall"].max()]["param_n_estimators"])
max_recall_n_estimators
visually I need to access the marked part on plot which is the optimum value on train-set and test-set based on recall metric:
and I get the error which its Traceback is following:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-61-95626cf7130a> in <module>
----> 1 max_recall_n_estimators = int(pd.DataFrame(results)[["param_n_estimators", "mean_train_Recall"]].loc[pd.DataFrame(results)["mean_train_Recall"] == pd.DataFrame(results)["mean_train_Recall"].max()]["param_n_estimators"])
2 max_recall_n_estimators = max_recall_n_estimators.astype(int)
/opt/anaconda3/envs/.../lib/python3.8/site-packages/pandas/core/series.py in wrapper(self)
137 if len(self) == 1:
138 return converter(self.iloc[0])
--> 139 raise TypeError(f"cannot convert the series to {converter}")
140
141 wrapper.__name__ = f"__{converter.__name__}__"
TypeError: cannot convert the series to <class 'int'>
I already checked this answer1 & answer2 via .astype(int)
but I couldn't figure it out how I can return/print max_recall_n_estimators
from results of GridSearchCV
elegantly.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.