from matplotlib.colors import ListedColormap
from sklearn import cross_validation, datasets, linear_model, metrics

import numpy as np

%pylab inline

"""
Explanation: Sklearn
sklearn.liner_model
linear_model:
* RidgeClassifier
* SGDClassifier
* SGDRegressor
* LinearRegression
* LogisticRegression
* Lasso
* etc
документация: http://scikit-learn.org/stable/modules/classes.html#module-sklearn.linear_model
примеры: http://scikit-learn.org/stable/modules/linear_model.html#linear-model
End of explanation
"""


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)

train_data, test_data, train_labels, test_labels = cross_validation.train_test_split(blobs[0], blobs[1], 
                                                                                    test_size = 0.3,
                                                                                    random_state = 1)

"""
Explanation: Генерация данных
End of explanation
"""


#создание объекта - классификатора
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_ 

"""
Explanation: Линейная классификация
RidgeClassifier
End of explanation
"""


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)

"""
Explanation: LogisticRegression
End of explanation
"""


ridge_scoring = cross_validation.cross_val_score(ridge_classifier, blobs[0], blobs[1], scoring = 'accuracy', cv = 10)

lr_scoring = cross_validation.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())

"""
Explanation: Оценка качества по cross-validation
cross_val_score
End of explanation
"""


scorer = metrics.make_scorer(metrics.accuracy_score)

cv_strategy = cross_validation.StratifiedShuffleSplit(blobs[1], n_iter = 20 , test_size = 0.3, random_state = 2)

ridge_scoring = cross_validation.cross_val_score(ridge_classifier, blobs[0], blobs[1], scoring = scorer, cv = cv_strategy)

lr_scoring = cross_validation.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())

"""
Explanation: cross_val_score с заданными scorer и cv_strategy
End of explanation
"""


