Advertisement
akashtadwai

random_forest_classifier

Jul 15th, 2021
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. def RandomForestFuction():
  2.   rf = RandomForestClassifier(random_state = 42)
  3.   n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
  4.   max_features = ['auto', 'sqrt']      # Number of features to consider at every split
  5.   max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]  # Maximum number of levels in tree
  6.   max_depth.append(None)
  7.   min_samples_split = [2, 5, 10]       # Minimum number of samples required to split a node
  8.   min_samples_leaf = [1, 2, 4]         # Minimum number of samples required at each leaf node
  9.   bootstrap = [True, False]            # Method of selecting samples for training each tree
  10.   # Create the random grid
  11.   random_grid = {'n_estimators': n_estimators,
  12.                  'max_features': max_features,
  13.                  'max_depth': max_depth,
  14.                  'min_samples_split': min_samples_split,
  15.                  'min_samples_leaf': min_samples_leaf,
  16.                  'bootstrap': bootstrap}
  17.  
  18.   rf = RandomForestClassifier()       # Number of trees in random forest
  19.   # Random search of parameters, using 3 fold cross validation,
  20.   # Search across 100 different combinations, and use all available cores
  21.   rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=42, n_jobs = -1)
  22.   rf_random.fit(train, y_train)       # Fit the random search model
  23.   print(rf_random.best_params_)       # Printing the params
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement