{"id":324867,"date":"2021-06-14T09:00:12","date_gmt":"2021-06-14T09:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=324867"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=324867","title":{"rendered":"Mushrooms (Machine Learning)"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0433\u0440\u0438\u0431\u0430\u0445, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0436\u0435\u043c \u0438\u0445 \u0441\u044a\u0435\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044e \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e \u0433\u0440\u0438\u0431\u0430\u0445 \u0441 Kaggle (\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c) \u0441&nbsp;<a href=\"https:\/\/www.kaggle.com\/uciml\/mushroom-classification\" rel=\"noopener noreferrer nofollow\">https:\/\/www.kaggle.com\/uciml\/mushroom-classification<\/a>, 2 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0443 \u043a \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0412\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u043d\u044b \u043d\u0430&nbsp;<a href=\"https:\/\/colab.research.google.com\/notebooks\/intro.ipynb\" rel=\"noopener noreferrer nofollow\">https:\/\/colab.research.google.com\/notebooks\/intro.ipynb<\/a><\/p>\n<pre><code class=\"python\"># \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043ae\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 import pandas as pd  # \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043b\u0435\u0441\u0430 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f confusion_matrix: from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV from sklearn.metrics import confusion_matrix  # \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u0439: import matplotlib.pyplot as plt import seaborn as sns  # \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043d\u0430\u0448 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c mushrooms = pd.read_csv('\/content\/mushrooms.csv')  #\u041f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 mushrooms.head() # \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0432\u043d\u0438\u0437\u0443:<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a00\/df4\/abc\/a00df4abc18b614a96a9867f93c7bd63.png\" width=\"1874\" height=\"297\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\">#\u041a\u0440\u0430\u0442\u043a\u0430\u044f \u0441\u0432\u043e\u0434\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 mushrooms.info()<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0f2\/e25\/7da\/0f2e257da9093cbeeae05d8e1d4cd1ec.png\" width=\"530\" height=\"631\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\">#\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 mushrooms.shape  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445 LabelEncoder \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0438\u0445 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u043b\u0438 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0447\u0438\u0441\u043b\u0430 (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434 heatmap) # \u0415\u0441\u043b\u0438 \u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c, \u043f\u0440\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u0443 \u043d\u0430\u0441 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0435\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f from sklearn.preprocessing import LabelEncoder le=LabelEncoder() for i in mushrooms.columns:     mushrooms[i]=le.fit_transform(mushrooms[i])  # \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 mushrooms.head()<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f91\/6f9\/75f\/f916f975f0ee61d85031efd437b07cb4.png\" width=\"1875\" height=\"339\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\"># \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044e \u043d\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e heatmap fig = plt.figure(figsize=(18, 14)) sns.heatmap(mushrooms.corr(), annot = True, vmin=-1, vmax=1, center= 0, cmap= 'coolwarm', linewidths=3, linecolor='black') fig.tight_layout() plt.show()<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1d3\/22f\/596\/1d322f596f6c8df3fb2b5d562499cac8.png\" width=\"1197\" height=\"1000\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u0440\u0440\u0435\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f: \u0421\u0438\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (veil-color,gill-spacing) = +0.9 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (ring-type,bruises) = +0.69 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (ring-type,gill-color) = +0.63 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (spore-print-color,gill-size) = +0.62 \u041e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u0440\u0440\u0435\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (stalk-root,spore-print-color) = -0.54 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (population,gill-spacing) = -0.53 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (gill-color,class) = -0.53 \u0415\u0441\u043b\u0438 \u0432 \u043d\u0430\u0448\u0435\u043c \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0442\u0435\u0441\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u0440\u0440\u0435\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0442\u043e\u0447\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. \u0412 \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0443\u0447\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0443, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044f, \u0447\u0442\u043e \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a \u043d\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u0438.  <\/p>\n<pre><code class=\"python\"># \u041e\u0442\u0431\u0440\u043e\u0441\u0438\u043c \u043a\u043e\u043b\u043e\u043d\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c. X = mushrooms.drop(['class'], axis=1) # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c. y = mushrooms['class']  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c RandomForestClassifier. rf = RandomForestClassifier(random_state=0)  # \u0417\u0430\u0434\u0430\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u043e\u0434\u0435\u043b\u0438, \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043b\u0435\u0441\u0430 \u0437\u0430\u0434\u0430\u0435\u043c \u0442\u0430\u043a #{'n_estimators': range(10, 51, 10), 'max_depth': range(1, 13, 2), #             'min_samples_leaf': range(1,8), 'min_samples_split': range(2,10,2)} parameters = {'n_estimators': [10], 'max_depth': [7],               'min_samples_leaf': [1], 'min_samples_split': [2]}  # \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 Random forest \u043c\u043e\u0434\u0435\u043b\u0435\u0439 GridSearchCV. GridSearchCV_clf = GridSearchCV(rf, parameters, cv=3, n_jobs=-1) GridSearchCV_clf.fit(X, y)  # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0438 \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u0441 \u043d\u0438\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043e\u0431\u0443\u0447\u0430\u0435\u043c\u043e\u0441\u0442\u0438 best_clf = GridSearchCV_clf.best_params_  # \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. best_clf<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/741\/b17\/eca\/741b17ecaaa7ce4a7101115904b0e06a.png\" width=\"789\" height=\"425\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\"># \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 confusion matrix (\u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a) \u043f\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f\u043c, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0448\u0430\u0433\u0435 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c \u043e\u0442\u0432\u0435\u0442\u0430\u043c \u0441 \u043d\u043e\u0432\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430. y_true = pd.read_csv ('\/content\/testing_y_mush.csv') sns.heatmap(confusion_matrix(y_true, predictions), annot=True, cmap=\"Blues\") plt.show()<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e20\/1e8\/455\/e201e8455ee8d0dde8c90804c107f627.png\" width=\"665\" height=\"393\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u043d\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0438 3, \u0447\u0442\u043e \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u043d\u0438\u0437\u043a\u0438\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u0441\u0442\u0440\u0435\u043c\u044f\u0449\u0435\u043c\u0441\u044f \u043a 0.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0435\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0448\u0435\u043c \u0434\u0444<\/p>\n<pre><code class=\"python\"># \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438  from sklearn.metrics import accuracy_score mr = accuracy_score(y_true, predictions)   #\u0414\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c\u0430 from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)  #\u041b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044f #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.linear_model import LogisticRegression lr = LogisticRegression(max_iter = 10000) lr.fit(x_train,y_train)  #\u0421\u0442\u0440\u043e\u0438\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = lr.predict(x_test) cm = confusion_matrix(y_test,y_pred)   #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 log_reg = accuracy_score(y_test,y_pred)   #K \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u0441\u043e\u0441\u0435\u0434\u0435\u0439 #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski',p = 2) knn.fit(x_train,y_train)  #\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = knn.predict(x_test) cm = confusion_matrix(y_test,y_pred)   #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 from sklearn.metrics import accuracy_score knn_1 = accuracy_score(y_test,y_pred)   #\u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439 #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.tree import DecisionTreeClassifier dt = DecisionTreeClassifier(criterion = 'entropy') dt.fit(x_train,y_train)  #\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = dt.predict(x_test) cm = confusion_matrix(y_test,y_pred)  #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 from sklearn.metrics import accuracy_score dt_1 = accuracy_score(y_test,y_pred)   #\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.naive_bayes import GaussianNB nb = GaussianNB() nb.fit(x_train,y_train)  #\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = nb.predict(x_test) cm = confusion_matrix(y_test,y_pred)   #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 from sklearn.metrics import accuracy_score nb_1 = accuracy_score(y_test,y_pred)   #\u041e\u0441\u0443\u0449\u0435\u0432\u0441\u0442\u043b\u044f\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0435\u0439 plt.figure(figsize= (16,12)) ac = [log_reg,knn_1,nb_1,dt_1,mr] name = ['\u041b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044f','\u041a \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u0441\u043e\u0441\u0435\u0434\u0435\u0439','\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440','\u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439', '\u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f'] sns.barplot(x = ac,y = name,palette='colorblind') plt.title(\"\u0413\u0440\u0430\u0444\u0438\u043a \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0435\u0439\", fontsize=20, fontweight=\"bold\")<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a8c\/46b\/ff6\/a8c46bff6322f318a39e1836f1cbfb7c.png\" width=\"1354\" height=\"783\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439.  <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/562640\/\"> https:\/\/habr.com\/ru\/post\/562640\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0433\u0440\u0438\u0431\u0430\u0445, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0436\u0435\u043c \u0438\u0445 \u0441\u044a\u0435\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044e \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e \u0433\u0440\u0438\u0431\u0430\u0445 \u0441 Kaggle (\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c) \u0441&nbsp;<a href=\"https:\/\/www.kaggle.com\/uciml\/mushroom-classification\" rel=\"noopener noreferrer nofollow\">https:\/\/www.kaggle.com\/uciml\/mushroom-classification<\/a>, 2 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0443 \u043a \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0412\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u043d\u044b \u043d\u0430&nbsp;<a href=\"https:\/\/colab.research.google.com\/notebooks\/intro.ipynb\" rel=\"noopener noreferrer nofollow\">https:\/\/colab.research.google.com\/notebooks\/intro.ipynb<\/a><\/p>\n<pre><code class=\"python\"># \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043ae\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 import pandas as pd  # \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043b\u0435\u0441\u0430 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f confusion_matrix: from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV from sklearn.metrics import confusion_matrix  # \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u0439: import matplotlib.pyplot as plt import seaborn as sns  # \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043d\u0430\u0448 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c mushrooms = pd.read_csv('\/content\/mushrooms.csv')  #\u041f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 mushrooms.head() # \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0432\u043d\u0438\u0437\u0443:<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\">#\u041a\u0440\u0430\u0442\u043a\u0430\u044f \u0441\u0432\u043e\u0434\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 mushrooms.info()<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\">#\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 mushrooms.shape  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445 LabelEncoder \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0438\u0445 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u043b\u0438 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0447\u0438\u0441\u043b\u0430 (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434 heatmap) # \u0415\u0441\u043b\u0438 \u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c, \u043f\u0440\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u0443 \u043d\u0430\u0441 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0435\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f from sklearn.preprocessing import LabelEncoder le=LabelEncoder() for i in mushrooms.columns:     mushrooms[i]=le.fit_transform(mushrooms[i])  # \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 mushrooms.head()<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\"># \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044e \u043d\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e heatmap fig = plt.figure(figsize=(18, 14)) sns.heatmap(mushrooms.corr(), annot = True, vmin=-1, vmax=1, center= 0, cmap= 'coolwarm', linewidths=3, linecolor='black') fig.tight_layout() plt.show()<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u0440\u0440\u0435\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f: \u0421\u0438\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (veil-color,gill-spacing) = +0.9 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (ring-type,bruises) = +0.69 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (ring-type,gill-color) = +0.63 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (spore-print-color,gill-size) = +0.62 \u041e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u0440\u0440\u0435\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (stalk-root,spore-print-color) = -0.54 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (population,gill-spacing) = -0.53 \u0421\u0440\u0435\u0434\u043d\u044f\u044f \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u044f (gill-color,class) = -0.53 \u0415\u0441\u043b\u0438 \u0432 \u043d\u0430\u0448\u0435\u043c \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0442\u0435\u0441\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u0440\u0440\u0435\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0442\u043e\u0447\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. \u0412 \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0443\u0447\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0443, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044f, \u0447\u0442\u043e \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a \u043d\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043a\u043e\u0440\u0440\u0435\u043b\u044f\u0446\u0438\u0438.  <\/p>\n<pre><code class=\"python\"># \u041e\u0442\u0431\u0440\u043e\u0441\u0438\u043c \u043a\u043e\u043b\u043e\u043d\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c. X = mushrooms.drop(['class'], axis=1) # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c. y = mushrooms['class']  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c RandomForestClassifier. rf = RandomForestClassifier(random_state=0)  # \u0417\u0430\u0434\u0430\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u043e\u0434\u0435\u043b\u0438, \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043b\u0435\u0441\u0430 \u0437\u0430\u0434\u0430\u0435\u043c \u0442\u0430\u043a #{'n_estimators': range(10, 51, 10), 'max_depth': range(1, 13, 2), #             'min_samples_leaf': range(1,8), 'min_samples_split': range(2,10,2)} parameters = {'n_estimators': [10], 'max_depth': [7],               'min_samples_leaf': [1], 'min_samples_split': [2]}  # \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 Random forest \u043c\u043e\u0434\u0435\u043b\u0435\u0439 GridSearchCV. GridSearchCV_clf = GridSearchCV(rf, parameters, cv=3, n_jobs=-1) GridSearchCV_clf.fit(X, y)  # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0438 \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u0441 \u043d\u0438\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043e\u0431\u0443\u0447\u0430\u0435\u043c\u043e\u0441\u0442\u0438 best_clf = GridSearchCV_clf.best_params_  # \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. best_clf<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\"># \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 confusion matrix (\u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a) \u043f\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f\u043c, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0448\u0430\u0433\u0435 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c \u043e\u0442\u0432\u0435\u0442\u0430\u043c \u0441 \u043d\u043e\u0432\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430. y_true = pd.read_csv ('\/content\/testing_y_mush.csv') sns.heatmap(confusion_matrix(y_true, predictions), annot=True, cmap=\"Blues\") plt.show()<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u043d\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0438 3, \u0447\u0442\u043e \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u043d\u0438\u0437\u043a\u0438\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u0441\u0442\u0440\u0435\u043c\u044f\u0449\u0435\u043c\u0441\u044f \u043a 0.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0435\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0448\u0435\u043c \u0434\u0444<\/p>\n<pre><code class=\"python\"># \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438  from sklearn.metrics import accuracy_score mr = accuracy_score(y_true, predictions)   #\u0414\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c\u0430 from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)  #\u041b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044f #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.linear_model import LogisticRegression lr = LogisticRegression(max_iter = 10000) lr.fit(x_train,y_train)  #\u0421\u0442\u0440\u043e\u0438\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = lr.predict(x_test) cm = confusion_matrix(y_test,y_pred)   #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 log_reg = accuracy_score(y_test,y_pred)   #K \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u0441\u043e\u0441\u0435\u0434\u0435\u0439 #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski',p = 2) knn.fit(x_train,y_train)  #\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = knn.predict(x_test) cm = confusion_matrix(y_test,y_pred)   #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 from sklearn.metrics import accuracy_score knn_1 = accuracy_score(y_test,y_pred)   #\u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439 #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.tree import DecisionTreeClassifier dt = DecisionTreeClassifier(criterion = 'entropy') dt.fit(x_train,y_train)  #\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = dt.predict(x_test) cm = confusion_matrix(y_test,y_pred)  #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 from sklearn.metrics import accuracy_score dt_1 = accuracy_score(y_test,y_pred)   #\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 #\u0422\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c from sklearn.naive_bayes import GaussianNB nb = GaussianNB() nb.fit(x_train,y_train)  #\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043e\u0448\u0438\u0431\u043e\u043a from sklearn.metrics import confusion_matrix,classification_report y_pred = nb.predict(x_test) cm = confusion_matrix(y_test,y_pred)   #\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 from sklearn.metrics import accuracy_score nb_1 = accuracy_score(y_test,y_pred)   #\u041e\u0441\u0443\u0449\u0435\u0432\u0441\u0442\u043b\u044f\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0435\u0439 plt.figure(figsize= (16,12)) ac = [log_reg,knn_1,nb_1,dt_1,mr] name = ['\u041b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044f','\u041a \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u0441\u043e\u0441\u0435\u0434\u0435\u0439','\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440','\u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439', '\u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f'] sns.barplot(x = ac,y = name,palette='colorblind') plt.title(\"\u0413\u0440\u0430\u0444\u0438\u043a \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0435\u0439\", fontsize=20, fontweight=\"bold\")<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439.  <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/562640\/\"> https:\/\/habr.com\/ru\/post\/562640\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-324867","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/324867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324867"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/324867\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}