<jupyter_start><jupyter_text>2 - Intro в Scikit-learn  Цели- Основные понятия:        + Модели    + Кластеризация, Классификация, Регрессия    + Fit, Predict, Evaluate    + Accuracy, confusion matrix    + Overfitting, training/test data, and crossvalidation    - API:        + model.fit()    + model.predict()    + accuracy_score()    + confusion_matrix()    + train_test_split()    + cross_val_score()  What is a model?<jupyter_code>from IPython.display import Image
Image("images/ml-model.png", width=500)<jupyter_output><empty_output><jupyter_text>http://phdp.github.io/posts/2013-07-05-dtl.html  What kind of algorithms do we have?<jupyter_code>Image("images/ml-1.png", width=800)<jupyter_output><empty_output><jupyter_text>3 шага моделирования: fit, predict and evaluate (metrics)  - Clustering  Data<jupyter_code>from sklearn.datasets import make_blobs
X, y = make_blobs()<jupyter_output><empty_output><jupyter_text>Algorithm k-means (метод k-средних) — наиболее популярный метод кластеризации. Он стремится минимизировать суммарное квадратичное отклонение точек кластеров от центров этих кластеров![](https://i.stack.imgur.com/UWcwJ.png)<jupyter_code>from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html<jupyter_code>kmeans = KMeans(n_clusters=3)<jupyter_output><empty_output><jupyter_text>Fit<jupyter_code>kmeans.fit(X)<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>kmeans.predict(X)
cluster_labels = kmeans.predict(X)<jupyter_output><empty_output><jupyter_text>Evaluate<jupyter_code>from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>accuracy_score - показывает точное совпадение тренировочных и предсказаных значений> y_pred = [0, 2, 1, 3]> y_true = [0, 1, 2, 3]> accuracy_score(y_true, y_pred)>0.5http://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html<jupyter_code>accuracy_score(y, cluster_labels)<jupyter_output><empty_output><jupyter_text>Accuracy<jupyter_code>Image("images/accuracy.png", width=300)<jupyter_output><empty_output><jupyter_text>Упражнение: используя iris data set попытайтесь его класифицировать и попробуйте разные метрики   Data<jupyter_code>from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target<jupyter_output><empty_output><jupyter_text>Выберите алгоритм класификации<jupyter_code>#from sklearn import svm<jupyter_output><empty_output><jupyter_text>Другие алгоритмы: http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.htmlexample-classification-plot-classifier-comparison-py  Тренировка модели<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>Оцените точность<jupyter_code># Your code here<jupyter_output><empty_output><jupyter_text>Evaluating models<jupyter_code>Image("images/training-data.png", width=500)
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4)
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier()
model.fit(X_train, y_train)
model.score(X_train, y_train)
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Crossvalidation<jupyter_code>Image("images/crossvalidation.png", width=500)
from sklearn.cross_validation import cross_val_score
cross_val_score(model, X, y, cv=5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11036036036036036, 'fraction_numerical': 0.01126126126126126, 'mean_word_length': 8.869444444444444, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 73, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '616757', 'license': 'MIT', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1401, 'n_words': 185, 'path': '2-sklearn.ipynb', 'repo_name': 'woters/ds101', 'size': '3552'}