{"id":318982,"date":"2021-03-03T21:00:08","date_gmt":"2021-03-03T21:00:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318982"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318982","title":{"rendered":"\u041c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u0435 \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (Multi-Class Target Encoding)"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p><strong>\u0427\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0441 TargetEncoder \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 category_encoders?<\/strong><\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <a href=\"https:\/\/habr.com\/ru\/post\/544666\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u043b\u043e\u0441\u044c, \u043a\u0430\u043a \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"http:\/\/contrib.scikit-learn.org\/category_encoders\/index.html\" rel=\"noopener noreferrer nofollow\">category_encoders<\/a> \u0434\u0430\u0435\u0442 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0430 \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0438\u0437\u0443\u0447\u0438\u043c \u0442\u0435\u043e\u0440\u0438\u044e \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u0433\u043e \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<p><strong>1.&nbsp;&nbsp; \u041a\u043e\u0433\u0434\u0430 \u043e\u0448\u0438\u0431\u0430\u0435\u0442\u0441\u044f TargetEncoder?<\/strong><\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435. \u0426\u0432\u0435\u0442 &#8212; \u044d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0430 \u0446\u0435\u043b\u044c &#8212; \u044d\u0442\u043e\u2026 \u0446\u0435\u043b\u044c. \u041d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c &#8212; \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0446\u0432\u0435\u0442 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Target.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/236\/0ed\/4a4\/2360ed4a4d707577489101ac26722a57.png\" width=\"181\" height=\"328\"><figcaption><\/figcaption><\/figure>\n<p> \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044b\u0447\u043d\u0443\u044e \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u0443\u044e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443.<\/p>\n<pre><code class=\"python\">import category_encoders as ce ce.TargetEncoder(smoothing=0).fit_transform(df.Color,df.Target)<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3f8\/74d\/31d\/3f874d31d008da7a8b1c9116b7f8e9fc.png\" width=\"108\" height=\"329\"><figcaption><\/figcaption><\/figure>\n<p> \u0425\u043c\u043c\u2026 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c, \u043d\u0435 \u0442\u0430\u043a \u043b\u0438? \u0412\u0441\u0435 \u0446\u0432\u0435\u0442\u0430 \u0431\u044b\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b \u043d\u0430 1. \u041f\u043e\u0447\u0435\u043c\u0443? \u0422\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e TargetEncoder \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0446\u0435\u043b\u0435\u0432\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0446\u0432\u0435\u0442\u0430, \u0430 \u043d\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>\u0425\u043e\u0442\u044f TargetEncoder \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u0430\u044f \u0446\u0435\u043b\u044c, \u0438\u043c\u0435\u044e\u0449\u0430\u044f 0 \u0438 1, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0431\u043e\u0439 \u0432 \u0434\u0432\u0443\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445:<\/p>\n<ul>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0446\u0435\u043b\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u0430\u044f, \u043d\u043e \u043d\u0435 0\/1 (\u0445\u043e\u0442\u044f \u0431\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 1 \u0438 2).&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0446\u0435\u043b\u044c &#8212; \u043c\u0443\u043b\u044c\u0442\u0438\u043a\u043b\u0430\u0441\u0441, \u043a\u0430\u043a \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u0436\u0435 \u0434\u0435\u043b\u0430\u0442\u044c?!<\/p>\n<p><strong>\u0422\u0435\u043e\u0440\u0438\u044f<\/strong><\/p>\n<p>\u0412 <a href=\"https:\/\/dl.acm.org\/doi\/10.1145\/507533.507538\" rel=\"noopener noreferrer nofollow\">\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0435 <\/a><em>Daniele Micci-Barreca<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0432\u043e\u0434\u0438\u0442 \u0441\u0440\u0435\u0434\u043d\u0435-\u0446\u0435\u043b\u0435\u0432\u0443\u044e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u043e \u043c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u044b\u0435 \u0446\u0435\u043b\u0435\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0438\u043c\u0435\u0435\u043c n \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0422\u0435\u043e\u0440\u0438\u044f \u0433\u043b\u0430\u0441\u0438\u0442, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. \u042d\u0442\u043e \u0434\u0430\u0441\u0442 n \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0446\u0435\u043b\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u043e\u043b\u044c\u043a\u043e n-1 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0431\u0443\u0434\u0443\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b, \u0442\u0430\u043a \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u043d\u043e\u0432\u044b\u0445 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u043a, \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u0437\u0430 \u0440\u0430\u0437.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong><\/p>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>\u0428\u0430\u0433 1: \u0411\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438.<\/p>\n<pre><code class=\"python\">enc=ce.OneHotEncoder().fit(df.Target.astype(str)) y_onehot=enc.transform(df.Target.astype(str)) y_onehot<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1b6\/fe3\/6b8\/1b6fe36b83eac3cc1bb9a7fab9bc89f5.png\" width=\"544\" height=\"343\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0441\u0442\u043e\u043b\u0431\u0435\u0446 Target_1 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043b\u0438\u0431\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f 0 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 Target. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1 \u0435\u0441\u043b\u0438 \u0432 Target \u0435\u0441\u0442\u044c 0, \u043b\u0438\u0431\u043e 0 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435. \u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 Target_2 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0438\u043b\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 1 \u0432 Target.<\/p>\n<p>\u0428\u0430\u0433 2: \u041a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0446\u0432\u0435\u0442, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439.<\/p>\n<pre><code class=\"python\">class_names = y_onehot.columns  for class_ in class_names:       enc = ce.TargetEncoder(smoothing = 0)     print(enc.fit_transform(X,y_onehot[class_]))<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/794\/0c7\/acc\/7940c7acca7840952904cf71d08aa9f5.png\" width=\"503\" height=\"339\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 0<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d85\/93e\/527\/d8593e527edced50d1d22c8e32a05b85.png\" width=\"508\" height=\"344\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 1<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fde\/09e\/aa2\/fde09eaa20bcecbeb72648519d780cef.png\" width=\"498\" height=\"341\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 2<\/p>\n<p>\u0428\u0430\u0433 3: \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438, \u043a\u0440\u043e\u043c\u0435 \u0446\u0432\u0435\u0442\u0430, \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0448\u0430\u0433\u0438 1 \u0438 2 \u0434\u043b\u044f \u043d\u0438\u0445.<\/p>\n<p>\u0413\u043e\u0442\u043e\u0432\u043e!<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/45a\/de0\/142\/45ade01426c5d5ccbf7824e477bc5981.png\" width=\"667\" height=\"264\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u044f\u0441\u043d\u043e\u0441\u0442\u0438 \u044f \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b \u0437\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u0442\u0440\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 Color_Target. \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0442\u043e \u0441\u043b\u044b\u0448\u0430\u043b\u0438, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0431\u0435\u0437 \u043f\u043e\u0442\u0435\u0440\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u043e\u043b\u0431\u0435\u0446 Color_Target_3 (\u043b\u0438\u0431\u043e \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439) \u0431\u0435\u0437 \u043f\u043e\u0442\u0435\u0440\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<p><strong>\u041f\u0440\u0430\u043a\u0442\u0438\u043a\u0430<\/strong><\/p>\n<p>\u0412\u044b \u0437\u0434\u0435\u0441\u044c \u0437\u0430 \u043a\u043e\u0434\u043e\u043c, \u043d\u0435 \u0442\u0430\u043a \u043b\u0438?!<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043c\u0435\u0442\u043a\u0438 \u0442\u0438\u043f\u0430 Series. \u0424\u0443\u043d\u043a\u0446\u0438\u044f df \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043a\u0430\u043a \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435, \u0442\u0430\u043a \u0438 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<pre><code class=\"python\">def target_encode_multiclass(X,y): #X,y are pandas df and series      y=y.astype(str)  #convert to string to onehot encode     enc=ce.OneHotEncoder().fit(y)     y_onehot=enc.transform(y)       class_names=y_onehot.columns  #names of onehot encoded columns      X_obj=X.select_dtypes('object') #separate categorical columns     X=X.select_dtypes(exclude='object')      for class_ in class_names:          enc=ce.TargetEncoder()         enc.fit(X_obj,y_onehot[class_]) #convert all categorical         temp=enc.transform(X_obj)       #columns for class_         temp.columns=[str(x)+'_'+str(class_) for x in temp.columns]         X=pd.concat([X,temp],axis=1)    #add to original dataset      return X <\/code><\/pre>\n<p><strong>\u0420\u0435\u0437\u044e\u043c\u0435<\/strong><\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0441 TargetEncoder \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 category_encoder, \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043b, \u0447\u0442\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e \u0446\u0435\u043b\u0435\u0432\u043e\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0443\u043b\u044c\u0442\u0438\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0441\u0451 \u044d\u0442\u043e \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e.<\/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\/545380\/\"> https:\/\/habr.com\/ru\/post\/545380\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p><strong>\u0427\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0441 TargetEncoder \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 category_encoders?<\/strong><\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <a href=\"https:\/\/habr.com\/ru\/post\/544666\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u043b\u043e\u0441\u044c, \u043a\u0430\u043a \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"http:\/\/contrib.scikit-learn.org\/category_encoders\/index.html\" rel=\"noopener noreferrer nofollow\">category_encoders<\/a> \u0434\u0430\u0435\u0442 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0430 \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0438\u0437\u0443\u0447\u0438\u043c \u0442\u0435\u043e\u0440\u0438\u044e \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u0433\u043e \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<p><strong>1.&nbsp;&nbsp; \u041a\u043e\u0433\u0434\u0430 \u043e\u0448\u0438\u0431\u0430\u0435\u0442\u0441\u044f TargetEncoder?<\/strong><\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435. \u0426\u0432\u0435\u0442 &#8212; \u044d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0430 \u0446\u0435\u043b\u044c &#8212; \u044d\u0442\u043e\u2026 \u0446\u0435\u043b\u044c. \u041d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c &#8212; \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0446\u0432\u0435\u0442 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Target.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p> \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044b\u0447\u043d\u0443\u044e \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u0443\u044e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443.<\/p>\n<pre><code class=\"python\">import category_encoders as ce ce.TargetEncoder(smoothing=0).fit_transform(df.Color,df.Target)<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p> \u0425\u043c\u043c\u2026 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c, \u043d\u0435 \u0442\u0430\u043a \u043b\u0438? \u0412\u0441\u0435 \u0446\u0432\u0435\u0442\u0430 \u0431\u044b\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b \u043d\u0430 1. \u041f\u043e\u0447\u0435\u043c\u0443? \u0422\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e TargetEncoder \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0446\u0435\u043b\u0435\u0432\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0446\u0432\u0435\u0442\u0430, \u0430 \u043d\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>\u0425\u043e\u0442\u044f TargetEncoder \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u0430\u044f \u0446\u0435\u043b\u044c, \u0438\u043c\u0435\u044e\u0449\u0430\u044f 0 \u0438 1, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0431\u043e\u0439 \u0432 \u0434\u0432\u0443\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445:<\/p>\n<ul>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0446\u0435\u043b\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u0430\u044f, \u043d\u043e \u043d\u0435 0\/1 (\u0445\u043e\u0442\u044f \u0431\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 1 \u0438 2).&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0446\u0435\u043b\u044c &#8212; \u043c\u0443\u043b\u044c\u0442\u0438\u043a\u043b\u0430\u0441\u0441, \u043a\u0430\u043a \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u0436\u0435 \u0434\u0435\u043b\u0430\u0442\u044c?!<\/p>\n<p><strong>\u0422\u0435\u043e\u0440\u0438\u044f<\/strong><\/p>\n<p>\u0412 <a href=\"https:\/\/dl.acm.org\/doi\/10.1145\/507533.507538\" rel=\"noopener noreferrer nofollow\">\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0435 <\/a><em>Daniele Micci-Barreca<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0432\u043e\u0434\u0438\u0442 \u0441\u0440\u0435\u0434\u043d\u0435-\u0446\u0435\u043b\u0435\u0432\u0443\u044e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u043e \u043c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u044b\u0435 \u0446\u0435\u043b\u0435\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0438\u043c\u0435\u0435\u043c n \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0422\u0435\u043e\u0440\u0438\u044f \u0433\u043b\u0430\u0441\u0438\u0442, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. \u042d\u0442\u043e \u0434\u0430\u0441\u0442 n \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0446\u0435\u043b\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u043e\u043b\u044c\u043a\u043e n-1 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0431\u0443\u0434\u0443\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b, \u0442\u0430\u043a \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0446\u0435\u043b\u0435-\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u043d\u043e\u0432\u044b\u0445 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u043a, \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u0437\u0430 \u0440\u0430\u0437.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong><\/p>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>\u0428\u0430\u0433 1: \u0411\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u0443\u043b\u044c\u0442\u0438-\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438.<\/p>\n<pre><code class=\"python\">enc=ce.OneHotEncoder().fit(df.Target.astype(str)) y_onehot=enc.transform(df.Target.astype(str)) y_onehot<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0441\u0442\u043e\u043b\u0431\u0435\u0446 Target_1 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043b\u0438\u0431\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f 0 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 Target. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1 \u0435\u0441\u043b\u0438 \u0432 Target \u0435\u0441\u0442\u044c 0, \u043b\u0438\u0431\u043e 0 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435. \u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 Target_2 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0438\u043b\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 1 \u0432 Target.<\/p>\n<p>\u0428\u0430\u0433 2: \u041a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0446\u0432\u0435\u0442, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439.<\/p>\n<pre><code class=\"python\">class_names = y_onehot.columns  for class_ in class_names:       enc = ce.TargetEncoder(smoothing = 0)     print(enc.fit_transform(X,y_onehot[class_]))<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 0<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 1<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 2<\/p>\n<p>\u0428\u0430\u0433 3: \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438, \u043a\u0440\u043e\u043c\u0435 \u0446\u0432\u0435\u0442\u0430, \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0448\u0430\u0433\u0438 1 \u0438 2 \u0434\u043b\u044f \u043d\u0438\u0445.<\/p>\n<p>\u0413\u043e\u0442\u043e\u0432\u043e!<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u044f\u0441\u043d\u043e\u0441\u0442\u0438 \u044f \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b \u0437\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u0442\u0440\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 Color_Target. \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0442\u043e \u0441\u043b\u044b\u0448\u0430\u043b\u0438, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0431\u0435\u0437 \u043f\u043e\u0442\u0435\u0440\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u043e\u043b\u0431\u0435\u0446 Color_Target_3 (\u043b\u0438\u0431\u043e \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439) \u0431\u0435\u0437 \u043f\u043e\u0442\u0435\u0440\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<p><strong>\u041f\u0440\u0430\u043a\u0442\u0438\u043a\u0430<\/strong><\/p>\n<p>\u0412\u044b \u0437\u0434\u0435\u0441\u044c \u0437\u0430 \u043a\u043e\u0434\u043e\u043c, \u043d\u0435 \u0442\u0430\u043a \u043b\u0438?!<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043c\u0435\u0442\u043a\u0438 \u0442\u0438\u043f\u0430 Series. \u0424\u0443\u043d\u043a\u0446\u0438\u044f df \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043a\u0430\u043a \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435, \u0442\u0430\u043a \u0438 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<pre><code class=\"python\">def target_encode_multiclass(X,y): #X,y are pandas df and series      y=y.astype(str)  #convert to string to onehot encode     enc=ce.OneHotEncoder().fit(y)     y_onehot=enc.transform(y)       class_names=y_onehot.columns  #names of onehot encoded columns      X_obj=X.select_dtypes('object') #separate categorical columns     X=X.select_dtypes(exclude='object')      for class_ in class_names:          enc=ce.TargetEncoder()         enc.fit(X_obj,y_onehot[class_]) #convert all categorical         temp=enc.transform(X_obj)       #columns for class_         temp.columns=[str(x)+'_'+str(class_) for x in temp.columns]         X=pd.concat([X,temp],axis=1)    #add to original dataset      return X <\/code><\/pre>\n<p><strong>\u0420\u0435\u0437\u044e\u043c\u0435<\/strong><\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0441 TargetEncoder \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 category_encoder, \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043b, \u0447\u0442\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e \u0446\u0435\u043b\u0435\u0432\u043e\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0443\u043b\u044c\u0442\u0438\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0441\u0451 \u044d\u0442\u043e \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e.<\/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\/545380\/\"> https:\/\/habr.com\/ru\/post\/545380\/<\/a><br \/><\/br><\/br><\/div>\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-318982","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318982","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=318982"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318982\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}