{"id":311050,"date":"2020-10-06T15:00:37","date_gmt":"2020-10-06T15:00:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=311050"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=311050","title":{"rendered":"\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432 PySpark ML \u043d\u0430 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0440\u0436\u0430\u0432\u044b\u0445 \u0447\u0430\u0439\u043d\u0438\u043a\u043e\u0432"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/522296\/\">\u0410 \u0442\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u0448\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 PySpark ML? \u041d\u0435\u0442? \u0422\u043e\u0433\u0434\u0430 \u0442\u0435\u0431\u0435 \u0441\u0440\u043e\u0447\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043a \u043d\u0430\u043c.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/avatars.mds.yandex.net\/get-verba\/787013\/2a000001609d4b513d3d5964bb2b5cc4e81a\/cattouchretcr\" alt=\"image\"\/><br \/>  <a name=\"habracut\"><\/a><br \/>  \u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0425\u043e\u0447\u0443 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0434\u043d\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0443\u044e, \u043d\u043e, \u043a \u043d\u0435\u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0449\u0443\u044e\u0441\u044f \u0442\u0435\u043c\u0443 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Spark: \u043a\u0430\u043a \u043e\u0431\u0443\u0447\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432 PySpark ML \u043d\u0430 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u043c\u0438 \u0438 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c\u0438)? \u0416\u0435\u043b\u0430\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u0431\u044b\u043b\u043e \u0432\u044b\u0437\u0432\u0430\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0434\u043d\u0435\u0439 \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0441 \u043a\u043e\u0434\u043e\u043c, \u0432\u0435\u0434\u044c \u0432 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0435 \u043e\u0442 Spark \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435 \u0442\u043e \u0447\u0442\u043e \u0441 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0441 \u043e\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u043c, \u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c\u0438 \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043c \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u0432 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 PySpark \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u0438 \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a \u0432\u0441\u0451 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u0447\u0435\u043c \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0438 \u0441 \u0432\u0430\u043c\u0438. \u0422\u0430\u043a \u0447\u0442\u043e \u043f\u043e\u043b\u043d\u044b\u0439 \u0432\u043f\u0435\u0440\u0451\u0434, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<p>  \u041f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b, \u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043b\u044e\u0431\u043e\u0439 \u0443\u0432\u0430\u0436\u0430\u044e\u0449\u0438\u0439 \u0441\u0435\u0431\u044f \u00ab\u0440\u0436\u0430\u0432\u044b\u0439 \u0447\u0430\u0439\u043d\u0438\u043a\u00bb, \u043a\u0430\u043a, \u0432\u043f\u0440\u043e\u0447\u0435\u043c, \u0438 \u044f \u043d\u0435\u0434\u0430\u0432\u043d\u043e, \u0432\u0441\u0451 \u043f\u043e\u043d\u044f\u043b:<\/p>\n<pre><code class=\"python\">#\u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 from pyspark.context import SparkContext from pyspark.sql.session import SparkSession from pyspark.ml import Pipeline from pyspark.ml.feature import HashingTF, Tokenizer from pyspark.sql.functions import UserDefinedFunction from pyspark.sql.types import * from pyspark.ml import Pipeline from pyspark.ml.feature import StringIndexer, VectorIndexer from pyspark.ml.evaluation import MulticlassClassificationEvaluator import pyspark.sql.functions as sf from pyspark.ml.feature import OneHotEncoder, StringIndexer, VectorAssembler from pyspark.ml import Pipeline from pyspark.ml.regression import GBTRegressor #other types of regression models #\u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u0438\u0434\u044b \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 #from pyspark.ml.regression import LinearRegression #from pyspark.ml.regression import RandomForestRegressor #from pyspark.ml.regression import GeneralizedLinearRegression #from pyspark.ml.regression import DecisionTreeRegressor from pyspark.ml.feature import VectorIndexer from pyspark.ml.evaluation import RegressionEvaluator<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c (\u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439) \u0441\u043f\u0430\u0440\u043a\u043e\u0432\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438 \u0441\u043f\u0430\u0440\u043a\u043e\u0432\u0441\u043a\u0443\u044e \u0441\u0435\u0441\u0441\u0438\u044e \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0432\u0441\u0451 \u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0432\u044b\u0432\u0435\u0434\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043f\u0430\u0440\u043a\u043e\u0432\u0441\u043a\u043e\u0439 \u0441\u0435\u0441\u0441\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430\u043c\u0438 \u0432 Spark:<\/p>\n<pre><code class=\"python\">#\u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043f\u0430\u0440\u043a \u0441\u0435\u0441\u0441\u0435\u044e sc = SparkContext('local') spark = SparkSession(sc) spark<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fc\/uh\/b3\/fcuhb3qtma_urlmlf8xrhtm98ik.png\"\/><\/p>\n<p>  \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0435\u0441\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0438\u0445. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0430\u0442\u0430\u0441\u0435\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0432\u0437\u044f\u0442 \u0441 \u0441\u0430\u0439\u0442\u0430 \u0441\u043e\u0440\u0435\u0432\u043d\u043e\u0432\u0430\u043d\u0438\u0439 \u043f\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e Kaggle: <br \/>  <a href=\"https:\/\/www.kaggle.com\/unitednations\/international-greenhouse-gas-emissions\">https:\/\/www.kaggle.com\/unitednations\/international-greenhouse-gas-emissions<\/a><br \/>  \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0441\u043b\u0435 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 path_csv \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 .csv \u0438 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0446\u0438\u0438:<\/p>\n<ul>\n<li> header: \u0435\u0441\u043b\u0438 \u0432 \u043d\u0430\u0448\u0435\u043c \u0444\u0430\u0439\u043b\u0435 \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c, \u0442\u043e \u0441\u0442\u0430\u0432\u0438\u043c \u00abtrue\u00bb<\/li>\n<li> delimiter: \u0441\u0442\u0430\u0432\u0438\u043c \u0437\u043d\u0430\u043a, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c, \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u044d\u0442\u043e &quot;,&quot; \u0438\u043b\u0438 &quot;;&quot;<\/li>\n<li> inferSchema: \u0435\u0441\u043b\u0438 \u00abtrue\u00bb, \u0442\u043e PySpark \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442 \u0442\u0438\u043f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u0438\u043d\u0430\u0447\u0435 \u0432\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e <\/li>\n<\/ul>\n<p>  <\/p>\n<pre><code class=\"python\">#\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 .csv \u0438\u0437 path_csv path_csv = 'greenhouse_gas_inventory_data_data.csv' data = spark.read.format(&quot;csv&quot;)\\         .option(&quot;header&quot;, &quot;true&quot;)\\         .option(&quot;delimiter&quot;, &quot;,&quot;)\\         .option(&quot;inferSchema&quot;, &quot;true&quot;)\\         .load(path_csv)<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0441 \u043a\u0430\u043a\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0434\u0435\u043b\u043e, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0445 \u0441\u0442\u0440\u043e\u043a:<\/p>\n<pre><code class=\"python\">#\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0447\u0430\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 data.show()<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/b5\/8s\/5k\/b58s5kf3jwh4ik3rhbh2fjrbalq.png\"\/><br \/>  \u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0441 \u0432\u0441\u0435\u0433\u043e \u0441\u0442\u0440\u043e\u043a \u0432 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435:  <\/p>\n<pre><code class=\"python\">#\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445 data.select('year').count()<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/m-\/ze\/_w\/m-ze_wagu5dplmom4xdvojgli34.png\"\/><\/p>\n<p>  \u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0442\u0438\u043f\u044b \u043d\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u043a\u0430\u043a \u043c\u044b \u043f\u043e\u043c\u043d\u0438\u043c, \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0441\u0438\u043b\u0438 PySpark \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e option(\u00abinferSchema\u00bb, \u00abtrue\u00bb): <\/p>\n<pre><code class=\"python\">#\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0442\u0438\u043f\u044b \u0432\u0441\u0435\u0445 \u043d\u0430\u0448\u0438\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a data.printSchema()<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yz\/3j\/hb\/yz3jhbqqeepkk_aug-9dk5aqtj8.png\"\/><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u0443 \u0431\u043b\u044e\u0434\u0443 \u2014 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c\u0438 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445. <b>Spark \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u043c \u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0438 \u0441 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c\u0438 \u0442\u043e\u0436\u0435 \u2014 \u0432\u0435\u043a\u0442\u043e\u0440,<\/b> \u0447\u0442\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443\u2026 \u041d\u043e \u043c\u044b \u043d\u0435 \u0441\u0434\u0430\u0451\u043c\u0441\u044f, \u0438 \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432 PySpark \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Pipeline, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u043a\u0438\u0439 \u043f\u043b\u0430\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f stages):<\/p>\n<ol>\n<li> \u0448\u0430\u0433 label_stringIdx: \u043c\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 value, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u0432 \u0441\u043f\u0430\u0440\u043a\u043e\u0432\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443-\u0432\u0435\u043a\u0442\u043e\u0440 \u0438 \u043f\u0435\u0440\u0435\u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0430 label \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c handleInvalid = &#8216;keep&#8217;, \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439, \u0447\u0442\u043e \u043d\u0430\u0448\u0430 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 null<\/li>\n<li> \u0448\u0430\u0433 stringIndexer: \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438 \u0432 \u0441\u043f\u0430\u0440\u043a\u043e\u0432\u0441\u043a\u0438\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438<\/li>\n<li> \u0448\u0430\u0433 encoder: \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 (\u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435) \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u043c\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e<\/li>\n<li> \u0448\u0430\u0433 assembler: \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432 Spark, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043a\u043e\u043b\u043e\u043d\u043a\u0438 \u0441 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u0434\u0438\u043d \u0432\u0435\u043a\u0442\u043e\u0440, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e VectorAssembler(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0451\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 <b>\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445<\/b> (\u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0447\u0438\u0441\u043b\u0430 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0448\u0430\u0433\u0435) \u043a\u043e\u043b\u043e\u043d\u043e\u043a (assemblerInputs) \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438 \u0432 \u043e\u0434\u0438\u043d \u0432\u0435\u043a\u0442\u043e\u0440 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u00abfeatures\u00bb<\/li>\n<li> \u0448\u0430\u0433 gbt: \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 \u0438\u0437 PySpark ML \u0432\u044b\u0431\u0440\u0430\u043d GBTRegressor, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0431\u0443\u0441\u0442\u0438\u043d\u0433 \u043d\u0430\u0448\u0435 \u0432\u0441\u0451<\/li>\n<\/ol>\n<p>  <\/p>\n<pre><code class=\"python\">#value - \u044d\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430\u044f \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f - \u043c\u0435\u0442\u043a\u0430 stages = [] label_stringIdx = StringIndexer(inputCol = 'value', outputCol = 'label', handleInvalid = 'keep') stages += [label_stringIdx]  #depend on categorical columns: country and types of emission #\u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0430\u044c\u043d\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a: \u0441\u0442\u0440\u0430\u043d\u044b \u0438 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 \u0437\u0430\u0433\u0440\u044f\u0437\u043d\u0435\u043d\u0438\u044f categoricalColumns = ['country_or_area', 'category'] for categoricalCol in categoricalColumns:     #\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u043c\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e     stringIndexer = StringIndexer(inputCol = categoricalCol,                                   outputCol = categoricalCol + 'Index',                                   handleInvalid = 'keep')     encoder = OneHotEncoder(inputCol=stringIndexer.getOutputCol(),                             outputCol=categoricalCol + &quot;classVec&quot;)     stages += [stringIndexer, encoder]  #\u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0438: \u0433\u043e\u0434\u0430 numericCols = ['year'] assemblerInputs = [c + &quot;classVec&quot; for c in categoricalColumns] + numericCols #\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0432 \u0432\u0435\u043a\u0442\u043e\u0440-\u043a\u043e\u043b\u043e\u043d\u043a\u0443 - \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438 assembler = VectorAssembler(inputCols=assemblerInputs, outputCol=&quot;features&quot;) stages += [assembler]<\/code><\/pre>\n<p>  \u0420\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u043d\u0430\u0448 \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u043d\u0430 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u0443\u044e \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0432 \u043b\u044e\u0431\u0438\u043c\u043e\u043c \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0438 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0438 70% \u043a 30% \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u043d\u0430\u0447\u043d\u0451\u043c \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u043e\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u0443\u0441\u0442\u0438\u043d\u0433\u0430 (GBTRegressor), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0435\u043a\u0442\u043e\u0440 \u00ablabel\u00bb \u043f\u043e \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c, \u0440\u0430\u043d\u0435\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0451\u043d\u043d\u044b\u043c \u0432 \u043e\u0434\u0438\u043d \u0432\u0435\u043a\u0442\u043e\u0440 \u00abfeatures\u00bb \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e \u0438\u0442\u0435\u0440\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u0438 maxIter=10:<\/p>\n<pre><code class=\"python\">#\u0434\u0435\u043b\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0443\u044e \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0438 (30% \u0442\u0435\u0441\u0442\u043e\u0432\u0430\u044f) (trainingData, testData) = data.randomSplit([0.7, 0.3])  #\u0442\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c (\u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u043e\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u0443\u0441\u0442\u0438\u043d\u0433\u0430) gbt = GBTRegressor(labelCol=&quot;label&quot;, featuresCol=&quot;features&quot;, maxIter=10) stages += [gbt]  # \u0437\u0430\u0434\u0430\u0435\u043c \u043f\u043b\u0430\u043d stages \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438  pipeline = Pipeline(stages=stages)<\/code><\/pre>\n<p>  \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0443 \u043f\u043b\u0430\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0438 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0434\u0430\u0442\u0430\u0441\u0435\u0442:<\/p>\n<pre><code class=\"python\"># \u0442\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c model = pipeline.fit(trainingData)  # \u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 predictions = model.transform(testData)<\/code><\/pre>\n<p>  \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0433\u043b\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">#\u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c pipeline.write().overwrite().save('model\/gbtregr_model')<\/code><\/pre>\n<p>  \u0418 \u0435\u0441\u043b\u0438 \u0432\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0432\u043d\u043e\u0432\u044c \u043d\u0430\u0447\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439, \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435:<\/p>\n<pre><code class=\"python\">#\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f load_model = pipeline.read().load('model\/gbtregr_model')<\/code><\/pre>\n<p>  \u0418\u0442\u0430\u043a, \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438, \u043a\u0430\u043a \u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Python, PySpark, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c\u0438 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0440\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e \u0432 \u0432\u0430\u0448\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u044f\u0445\u2026<\/p><\/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\/522296\/\"> https:\/\/habr.com\/ru\/post\/522296\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/522296\/\">\u0410 \u0442\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u0448\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 PySpark ML? \u041d\u0435\u0442? \u0422\u043e\u0433\u0434\u0430 \u0442\u0435\u0431\u0435 \u0441\u0440\u043e\u0447\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043a \u043d\u0430\u043c.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/avatars.mds.yandex.net\/get-verba\/787013\/2a000001609d4b513d3d5964bb2b5cc4e81a\/cattouchretcr\" alt=\"image\"\/>  <\/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-311050","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311050","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=311050"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311050\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}