<jupyter_start><jupyter_text>Sklearn  sklearn.grid_search документация: http://scikit-learn.org/stable/modules/grid_search.html<jupyter_code>import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy.stats as sts
import seaborn as sns

sns.set()
sns.set_style("whitegrid")

color_palette = sns.color_palette('deep') + sns.color_palette('husl', 6) + sns.color_palette('bright') + sns.color_palette('pastel')

%matplotlib inline
sns.palplot(color_palette)
from sklearn import datasets, linear_model, metrics, model_selection as mdsel<jupyter_output><empty_output><jupyter_text>Генерация датасета<jupyter_code>iris = datasets.load_iris()
train_data, test_data, train_labels, test_labels = mdsel.train_test_split(iris.data, iris.target, 
                                                                                     test_size = 0.3,random_state = 0)<jupyter_output><empty_output><jupyter_text>Задание модели<jupyter_code>classifier = linear_model.SGDClassifier(random_state = 0)<jupyter_output><empty_output><jupyter_text>Генерация сетки<jupyter_code>classifier.get_params().keys()
parameters_grid = {
    'loss' : ['hinge', 'log', 'squared_hinge', 'squared_loss'],
    'penalty' : ['l1', 'l2'],
    'n_iter' : range(5,10),
    'alpha' : np.linspace(0.0001, 0.001, num = 5),
}
cv = mdsel.StratifiedShuffleSplit(test_size = 0.2, random_state = 0)
cv<jupyter_output><empty_output><jupyter_text>Подбор параметров и оценка качества  Grid search<jupyter_code>grid_cv = mdsel.GridSearchCV(classifier, parameters_grid, scoring='accuracy', cv=cv, n_jobs=3)
%%time

grid_cv.fit(train_data, train_labels)
grid_cv.best_estimator_
print(grid_cv.best_score_)
print(grid_cv.best_params_)
grid_cv.grid_scores_[:10]<jupyter_output><empty_output><jupyter_text>Randomized grid search<jupyter_code>randomized_grid_cv = mdsel.RandomizedSearchCV(
    classifier, parameters_grid, scoring='accuracy', cv=cv, n_iter=20, random_state=0
)
%%time
randomized_grid_cv.fit(train_data, train_labels)
print(randomized_grid_cv.best_score_)
print(randomized_grid_cv.best_params_)<jupyter_output>0.8142857142857143
{'penalty': 'l1', 'n_iter': 9, 'loss': 'log', 'alpha': 0.00055}
{'fraction_non_alphanumeric': 0.12243969048702777, 'fraction_numerical': 0.024578971324533454, 'mean_word_length': 6.254125412541254, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '72693', 'license': 'MIT', 'n_tokens_mistral': 868, 'n_tokens_neox': 845, 'n_words': 124, 'path': '02_supervised_learning/12_w3p_sklearn.grid_search.ipynb', 'repo_name': 'pkuderov/mlda-course', 'size': '2197'}