<jupyter_start><jupyter_text>Sklearn  sklearn.liner_model **linear_model:*** RidgeClassifier* SGDClassifier* SGDRegressor* LinearRegression* LogisticRegression* Lasso* etc документация: http://scikit-learn.org/stable/modules/classes.htmlmodule-sklearn.linear_modelпримеры: http://scikit-learn.org/stable/modules/linear_model.htmllinear-model<jupyter_code>from matplotlib.colors import ListedColormap
from sklearn import model_selection
from sklearn import datasets, linear_model, metrics

import numpy as np
%pylab inline<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>Генерация данных<jupyter_code>blobs = datasets.make_blobs(centers = 2, cluster_std = 5.5, random_state=1)
colors = ListedColormap(['red', 'blue'])

pylab.figure(figsize(8, 8))
pylab.scatter([x[0] for x in blobs[0]], [x[1] for x in blobs[0]], c=blobs[1], cmap=colors)
# hold-out
train_data, test_data, train_labels, test_labels = model_selection.train_test_split(blobs[0], blobs[1], 
                                                                                    test_size = 0.3,
                                                                                    random_state = 1)<jupyter_output><empty_output><jupyter_text>Линейная классификация  RidgeClassifier<jupyter_code>#создание объекта - классификатора
ridge_classifier = linear_model.RidgeClassifier(random_state = 1)
#обучение классификатора
ridge_classifier.fit(train_data, train_labels)
#применение обученного классификатора
ridge_predictions = ridge_classifier.predict(test_data)
print(test_labels)
print(ridge_predictions)
#оценка качества классификации
metrics.accuracy_score(test_labels, ridge_predictions)
ridge_classifier.coef_
ridge_classifier.intercept_<jupyter_output><empty_output><jupyter_text>LogisticRegression<jupyter_code>log_regressor = linear_model.LogisticRegression(random_state = 1)
log_regressor.fit(train_data, train_labels)
lr_predictions = log_regressor.predict(test_data)
lr_proba_predictions = log_regressor.predict_proba(test_data)
print(test_labels)
print(lr_predictions)
print(lr_proba_predictions)
print(metrics.accuracy_score(test_labels, lr_predictions))
print(metrics.accuracy_score(test_labels, ridge_predictions))<jupyter_output>0.8666666666666667<jupyter_text>Оценка качества по cross-validation  cross_val_score<jupyter_code>ridge_scoring = model_selection.cross_val_score(ridge_classifier, blobs[0], blobs[1], scoring = 'accuracy', cv = 10)
lr_scoring = model_selection.cross_val_score(log_regressor, blobs[0], blobs[1], scoring = 'accuracy', cv = 10)
lr_scoring
print('Ridge mean:{}, max:{}, min:{}, std:{}'.format(ridge_scoring.mean(), ridge_scoring.max(), 
                                                     ridge_scoring.min(), ridge_scoring.std()))
print('Log mean:{}, max:{}, min:{}, std:{}'.format(lr_scoring.mean(), lr_scoring.max(), 
                                                   lr_scoring.min(), lr_scoring.std()))<jupyter_output>Log mean:0.8700000000000001, max:1.0, min:0.7, std:0.10049875621120892<jupyter_text>cross_val_score с заданными scorer и cv_strategy<jupyter_code>scorer = metrics.make_scorer(metrics.accuracy_score)
cv_strategy = model_selection.StratifiedShuffleSplit(n_splits=20 , test_size = 0.3, random_state = 2).get_n_splits(blobs[1])
ridge_scoring = model_selection.cross_val_score(ridge_classifier, blobs[0], blobs[1], scoring = scorer, cv = cv_strategy)
lr_scoring = model_selection.cross_val_score(log_regressor, blobs[0], blobs[1], scoring = scorer, cv = cv_strategy)
print('Ridge mean:{}, max:{}, min:{}, std:{}'.format(ridge_scoring.mean(), ridge_scoring.max(), 
                                                     ridge_scoring.min(), ridge_scoring.std()))
print('Log mean:{}, max:{}, min:{}, std:{}'.format(lr_scoring.mean(), lr_scoring.max(), 
                                                   lr_scoring.min(), lr_scoring.std()))<jupyter_output>Log mean:0.8625, max:1.0, min:0.5, std:0.1608894548025652
{'fraction_non_alphanumeric': 0.1168471417778897, 'fraction_numerical': 0.029715436917652983, 'mean_word_length': 4.674285714285714, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '152279', 'license': 'MIT', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1365, 'n_words': 201, 'path': 'supervised_training/2_week/.ipynb_checkpoints/sklearn.linear_model_part1-checkpoint.ipynb', 'repo_name': 'rvg77/ML_Coursera', 'size': '3971'}