Skip to content

Commit

Permalink
Merge pull request #150 from MAhmadUzair/master
Browse files Browse the repository at this point in the history
Code Improvement in READ.md
  • Loading branch information
rodrigo-arenas authored May 7, 2024
2 parents b7ea518 + 8d5243e commit 1e9740b
Showing 1 changed file with 59 additions and 44 deletions.
103 changes: 59 additions & 44 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,50 +111,65 @@ Example: Hyperparameters Tuning

.. code-block:: python
from sklearn_genetic import GASearchCV
from sklearn_genetic.space import Continuous, Categorical, Integer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
data = load_digits()
n_samples = len(data.images)
X = data.images.reshape((n_samples, -1))
y = data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
clf = RandomForestClassifier()
param_grid = {'min_weight_fraction_leaf': Continuous(0.01, 0.5, distribution='log-uniform'),
'bootstrap': Categorical([True, False]),
'max_depth': Integer(2, 30),
'max_leaf_nodes': Integer(2, 35),
'n_estimators': Integer(100, 300)}
cv = StratifiedKFold(n_splits=3, shuffle=True)
evolved_estimator = GASearchCV(estimator=clf,
cv=cv,
scoring='accuracy',
population_size=20,
generations=35,
param_grid=param_grid,
n_jobs=-1,
verbose=True,
keep_top_k=4)
# Train and optimize the estimator
evolved_estimator.fit(X_train, y_train)
# Best parameters found
print(evolved_estimator.best_params_)
# Use the model fitted with the best parameters
y_predict_ga = evolved_estimator.predict(X_test)
print(accuracy_score(y_test, y_predict_ga))
# Saved metadata for further analysis
print("Stats achieved in each generation: ", evolved_estimator.history)
print("Best k solutions: ", evolved_estimator.hof)
# Import necessary libraries
from sklearn_genetic import GASearchCV
from sklearn_genetic.space import Continuous, Categorical, Integer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
# Load the dataset
data = load_digits()
n_samples = len(data.images)
X = data.images.reshape((n_samples, -1))
y = data['target']
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
# Define the RandomForestClassifier
clf = RandomForestClassifier()
# Define the parameter grid for GASearchCV
param_grid = {
'min_weight_fraction_leaf': Continuous(0.01, 0.5, distribution='log-uniform'),
'bootstrap': Categorical([True, False]),
'max_depth': Integer(2, 30),
'max_leaf_nodes': Integer(2, 35),
'n_estimators': Integer(100, 300)
}
# Configure cross-validation
cv = StratifiedKFold(n_splits=3, shuffle=True)
# Initialize and configure GASearchCV
evolved_estimator = GASearchCV(
estimator=clf,
cv=cv,
scoring='accuracy',
population_size=20,
generations=35,
param_grid=param_grid,
n_jobs=-1,
verbose=True,
keep_top_k=4
)
# Train and optimize the estimator
evolved_estimator.fit(X_train, y_train)
# Display best parameters found
print("Best parameters:", evolved_estimator.best_params_)
# Use the model fitted with the best parameters to make predictions
y_predict_ga = evolved_estimator.predict(X_test)
print("Test accuracy:", accuracy_score(y_test, y_predict_ga))
# Display additional information about the optimization process
print("Stats achieved in each generation:", evolved_estimator.history)
print("Best k solutions:", evolved_estimator.hof)
Example: Feature Selection
Expand Down

0 comments on commit 1e9740b

Please sign in to comment.