<jupyter_start><jupyter_text>Scikit-learn Comparison<jupyter_code>from sklearn_genetic import GASearchCV
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn_genetic.space import Categorical, Continuous
import scipy.stats as stats
from sklearn.utils.fixes import loguniform
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
import numpy as np
import warnings
warnings.filterwarnings("ignore")
data = load_digits() 
label_names = data['target_names'] 
y = data['target']
X = data['data'] 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
clf = SGDClassifier(loss='hinge',fit_intercept=True)<jupyter_output><empty_output><jupyter_text>1. Random Search<jupyter_code>param_dist = {'average': [True, False],
              'l1_ratio': stats.uniform(0, 1),
              'alpha': loguniform(1e-4, 1e0)}
n_iter_search = 30
random_search = RandomizedSearchCV(clf, param_distributions=param_dist,
                                   n_iter=n_iter_search,n_jobs=-1)
random_search.fit(X_train,y_train)
accuracy_score(y_test, random_search.predict(X_test))
random_search.best_params_<jupyter_output><empty_output><jupyter_text>2. Grid Search<jupyter_code>param_grid = {'average': [True, False],
              'l1_ratio': np.linspace(0, 1, num=10),
              'alpha': np.power(10, np.arange(-4, 1, dtype=float))}
grid_search = GridSearchCV(clf, param_grid=param_grid,n_jobs=-1)
grid_search.fit(X_train,y_train)
accuracy_score(y_test, grid_search.predict(X_test))
grid_search.best_params_<jupyter_output><empty_output><jupyter_text>3. Genetic Algorithm<jupyter_code>param_grid = {'l1_ratio': Continuous(0,1),
              'alpha': Continuous(1e-4,1),
              'average': Categorical([True, False])}

evolved_estimator = GASearchCV(clf,
                    cv=3,
                    scoring='accuracy',
                    param_grid=param_grid,
                    population_size=10,
                    generations=8,
                    tournament_size=3,
                    elitism=True,
                    verbose=True)
evolved_estimator.fit(X_train,y_train)
y_predicy_ga = evolved_estimator.predict(X_test)
accuracy_score(y_test,y_predicy_ga)
evolved_estimator.best_params<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10572872788542544, 'fraction_numerical': 0.016427969671440605, 'mean_word_length': 4.208333333333333, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '111558', 'license': 'MIT', 'n_tokens_mistral': 840, 'n_tokens_neox': 809, 'n_words': 127, 'path': 'docs/notebooks/sklearn_comparison.ipynb', 'repo_name': 'Raul9595/Sklearn-genetic-opt', 'size': '2374'}