{"id":262946,"date":"2015-08-05T12:46:05","date_gmt":"2015-08-05T08:46:05","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=262946"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=262946","title":{"rendered":"\u041a\u0430\u043a \u0443\u0437\u043d\u0430\u0442\u044c \u0433\u043e\u0434 \u0432\u044b\u043f\u0443\u0441\u043a\u0430 \u043f\u0435\u0441\u043d\u0438 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u0430\u0443\u0434\u0438\u043e \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a?"},"content":{"rendered":"<p>             \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u043a\u0443\u0440\u0441 <a href=\"https:\/\/www.edx.org\/course\/scalable-machine-learning-uc-berkeleyx-cs190-1x\">Scalable Machine Learning<\/a> \u043f\u043e Apache Spark, \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 MLlib \u0434\u043b\u044f \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. \u041a\u0443\u0440\u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043b \u0438\u0437 \u0432\u0438\u0434\u0435\u043e\u043b\u0435\u043a\u0446\u0438\u0439 \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u043d\u0438\u0439. \u041b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0430 PySpark, \u0430 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u043c\u043d\u0435 \u0447\u0430\u0449\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043e scala, \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0435\u0440\u0435\u0440\u0435\u0448\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043b\u0430\u0431\u044b \u043d\u0430 \u044d\u0442\u043e\u043c \u044f\u0437\u044b\u043a\u0435, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u043b\u0443\u0447\u0448\u0435 \u0443\u0441\u0432\u043e\u0438\u0442\u044c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b. \u0411\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0442\u043b\u0438\u0447\u0438\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043d\u0435\u0442, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e PySpark \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <a href=\"http:\/\/docs.scipy.org\/doc\/numpy\/reference\/index.html\">NumPy<\/a>, \u0430 \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u043e scala \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <a href=\"https:\/\/github.com\/scalanlp\/breeze\">Breeze<\/a>.<\/p>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043e\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u043b\u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0430\u043b\u0433\u0435\u0431\u0440\u044b \u0432 NumPy \u0438 \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0441 apache spark \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u043e\u0441\u044c \u0441 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u043e\u043d\u0430 \u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u0430 \u043d\u0438\u0436\u0435.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0414\u0430\u043d\u043d\u0430\u044f \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043d\u0430\u0446\u0435\u043b\u0435\u043d\u0430 \u043d\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 (\u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441 \u0443\u0447\u0438\u0442\u0435\u043b\u0435\u043c). \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 <a href=\"http:\/\/labrosa.ee.columbia.edu\/millionsong\/\">Million Song Dataset<\/a> (<a href=\"https:\/\/drive.google.com\/file\/d\/0B7o28oVJ-RWhMkJ0VkZlVjg0UEE\/view?pli=1\">\u0441\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0435<\/a>). \u041d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0438 \u043b\u0443\u0447\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0433\u043e\u0434 \u043f\u0435\u0441\u043d\u0438 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u0430\u0443\u0434\u0438\u043e \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438. <\/p>\n<p>  \u0414\u0430\u043d\u043d\u0430\u044f \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u0430\u043f\u044b:<br \/>  1. \u0427\u0442\u0435\u043d\u0438\u0435, \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<br \/>  2. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u043e\u0446\u0435\u043d\u043a\u0430 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438<br \/>  3. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438, \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u043e\u0446\u0435\u043d\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<br \/>  4. \u041d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e MLlib, \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432<br \/>  5. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438<\/p>\n<h4>1. \u0427\u0442\u0435\u043d\u0438\u0435, \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>  \u0421\u044b\u0440\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 <a href=\"https:\/\/drive.google.com\/file\/d\/0B7o28oVJ-RWhMkJ0VkZlVjg0UEE\/edit?pli=1\">\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c \u0444\u0430\u0439\u043b\u0435<\/a> \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u0432\u0438\u0434\u0430:  <\/p>\n<pre><code class=\"scala\">2001.0,0.884123733793,0.610454259079,0.600498416968,0.474669212493,0.247232680947,0.357306088914,0.344136412234,0.339641227335,0.600858840135,0.425704689024,0.60491501652,0.419193351817 <\/code><\/pre>\n<p>  \u041a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043c\u0435\u0442\u043a\u0438 (\u0433\u043e\u0434 \u0432\u044b\u043f\u0443\u0441\u043a\u0430 \u043f\u0435\u0441\u043d\u0438), \u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0430\u0443\u0434\u0438\u043e-\u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u0440\u0430\u0437\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u044f\u0442\u043e\u0439.<\/p>\n<p>  \u041f\u0435\u0440\u0432\u043e\u0435, \u0441 \u0447\u0435\u0433\u043e \u043c\u044b \u043d\u0430\u0447\u043d\u0451\u043c: \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0432\u0438\u0434\u0435 RDD:  <\/p>\n<pre><code class=\"scala\">  val sc = new SparkContext(&quot;local[*]&quot;, &quot;week3&quot;)   val rawData: RDD[String] = sc.textFile(&quot;millionsong.txt&quot;) <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c:<\/p>\n<pre><code class=\"scala\">  val numPoints = rawData.count()   println(numPoints)   &gt; 6724 <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u043d\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0438\u0445, \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0432 \u0442\u0435\u043f\u043b\u043e\u0432\u0443\u044e \u043a\u0430\u0440\u0442\u0443 \u0445\u043e\u0442\u044f \u0431\u044b \u0434\u043b\u044f \u0447\u0430\u0441\u0442\u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0439. \u0422.\u043a. \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1, \u0442\u043e \u043f\u0443\u0441\u0442\u044c \u0447\u0435\u043c \u0431\u043b\u0438\u0436\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a \u0435\u0434\u0438\u043d\u0438\u0446\u0435 \u0442\u0435\u043c \u0442\u0435\u043c\u043d\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0442\u0435\u043d\u043e\u043a \u0441\u0435\u0440\u043e\u0433\u043e, \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/221\/763\/0db\/2217630db88d485a8a5f291de68bc075.png\"\/><\/p>\n<p>  \u0412 MLlib \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0430\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430 (\u0442\u043e\u0447\u043d\u0435\u0435 \u0435\u0451 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b) \u0434\u043e\u043b\u0436\u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 <a href=\"http:\/\/spark.apache.org\/docs\/latest\/api\/scala\/#org.apache.spark.mllib.regression.LabeledPoint\">LabeledPoint<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043c\u0435\u0442\u043a\u0438 (label) \u0438 \u043d\u0430\u0431\u043e\u0440\u0430 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a (features), \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u0435. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0433\u043e\u0434, \u0430 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438- \u0430\u0443\u0434\u0438\u043e- \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<p>  \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 labeledPoint.  <\/p>\n<pre><code class=\"scala\">  import org.apache.spark.mllib.regression.LabeledPoint   import org.apache.spark.mllib.linalg.Vectors    def parsePoint(line: String) = {     val sp = line.split(',')     LabeledPoint(sp.head.toDouble, Vectors.dense(sp.tail.map(_.toDouble)))   } <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u0432\u0435\u0434\u0451\u043c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:<\/p>\n<pre><code class=\"scala\">  val parsedDataInit: RDD[LabeledPoint] = rawData.map(parsePoint) <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0437\u0443\u0447\u0438\u043c \u043c\u0435\u0442\u043a\u0438 \u0438 \u043d\u0430\u0439\u0434\u0451\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0433\u043e\u0434\u043e\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435; \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0433\u043e\u0434.  <\/p>\n<pre><code class=\"scala\">  val onlyLabels = parsedDataInit.map(_.label)   val minYear = onlyLabels.min()   val maxYear = onlyLabels.max()   println(s&quot;max: $maxYear, min: $minYear&quot;)   &gt; max: 2011.0, min: 1922.0 <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043c\u0435\u0442\u043a\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u043c\u0435\u0436\u0434\u0443 1922 \u0438 2011 \u0433.\u0433. \u0412 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u043b\u0438\u0441\u044c \u0441 \u043d\u0443\u043b\u044f. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 RDD, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u0441\u043c\u0435\u0449\u0451\u043d\u043d\u044b\u043c\u0438 \u043d\u0430 1922.0.  <\/p>\n<pre><code class=\"scala\">  val parsedData = parsedDataInit.map(x =&gt; LabeledPoint(x.label - minYear, x.features)) <\/code><\/pre>\n<p>  \u041c\u044b \u043f\u043e\u0447\u0442\u0438 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0432\u0441\u0451, \u0447\u0442\u043e \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c- \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 3 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430:    <\/p>\n<ul>\n<li><b>\u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0430\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430<\/b> \u2014 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043d\u0435\u0439 \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0443\u0447\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c<\/li>\n<li><b>\u043f\u0440\u043e\u0432\u0435\u0440\u043e\u0447\u043d\u0430\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430<\/b> \u2014 \u043d\u0430 \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/li>\n<li><b>\u0442\u0435\u0441\u0442\u043e\u0432\u0430\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430<\/b> \u2014 \u044d\u0442\u043e\u0442 \u043d\u0430\u0431\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043f\u0440\u0430\u0432\u0434\u0438\u0432\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043b\u0443\u0447\u0448\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c<\/li>\n<\/ul>\n<p>  \u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0432 spark \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <a href=\"http:\/\/spark.apache.org\/docs\/latest\/api\/scala\/#org.apache.spark.rdd.RDD\">randomSplit<\/a>:  <\/p>\n<pre><code class=\"scala\">  val weights = Array(0.8, 0.1, 0.1)   val seed = 42   val splitData: Array[RDD[LabeledPoint]] = parsedData.randomSplit(weights, seed).map(_.cache())   val parsedTrainData = splitData(0)   val parsedValData = splitData(1)   val parsedTestData = splitData(2)   val nTrain = parsedTrainData.count()   val nVal = parsedValData.count()   val nTest = parsedTestData.count()    println(nTrain, nVal, nTest, nTrain + nVal + nTest)   println(parsedData.count())   &gt;(5402,622,700,6724)   &gt; 6724 <\/code><\/pre>\n<p>   <\/p>\n<h4>2. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u043e\u0446\u0435\u043d\u043a\u0430 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438<\/h4>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c. \u0412 \u0441\u0430\u043c\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0441\u0440\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u0433\u043e\u0434\u0430, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u044d\u0442\u0443 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443:  <\/p>\n<pre><code class=\"scala\">  val averageTrainYear = parsedTrainData.map(_.label).mean()   println(averageTrainYear)   &gt; 53.68659755646054 <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0446\u0435\u043d\u043a\u0438 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0440\u0435\u043d\u044c \u0438\u0437 \u0441\u0440\u0435\u0434\u043d\u0435\u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438 (<a href=\"http:\/\/statistica.ru\/glossary\/general\/srednekvadraticheskaya-oshibka\/\">RMSE<\/a>)<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/469\/6ca\/b11\/4696cab11f3c4c74990277f2c891d8df.png\"\/>.<\/p>\n<p>  \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 y<sub>i<\/sub> \u2014 \u044d\u0442\u043e \u043c\u0435\u0442\u043a\u0430-\u0433\u043e\u0434, \u0430 y<sub>i<\/sub> <sub>\u0441 \u043a\u0440\u044b\u0448\u0435\u0447\u043a\u043e\u0439<\/sub> \u2014 \u044d\u0442\u043e \u0433\u043e\u0434, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0449\u0443\u044e \u043a\u0432\u0430\u0434\u0440\u0430\u0442 \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u0438 label \u0438 prediction \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c RMSE:  <\/p>\n<pre><code class=\"scala\">  def squaredError(label: Double, prediction: Double) = math.pow(label - prediction, 2)    def calcRMSE(labelsAndPreds: RDD[(Double, Double)]) =     math.sqrt(labelsAndPreds.map(x =&gt; squaredError(x._1, x._2)).sum() \/ labelsAndPreds.count()) <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0442\u044c RMSE \u043d\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043d\u043e\u0432\u044b\u0435 RDD \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0438 \u0441 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u044b\u043c\u0438 \u0438 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u043d\u044b\u043c\u0438 \u0433\u043e\u0434\u0430\u043c\u0438.  <\/p>\n<pre><code class=\"scala\">  val labelsAndPredsTrain = parsedTrainData.map(x =&gt; x.label -&gt; averageTrainYear)   val rmseTrainBase = calcRMSE(labelsAndPredsTrain)    val labelsAndPredsVal = parsedValData.map(x =&gt; x.label -&gt; averageTrainYear)   val rmseValBase = calcRMSE(labelsAndPredsVal)    val labelsAndPredsTest = parsedTestData.map(x =&gt; x.label -&gt; averageTrainYear)   val rmseTestBase = calcRMSE(labelsAndPredsTest)    &gt; Base model: \tBaseline Train RMSE = 21,496 \tBaseline Validation RMSE = 21,100 \tBaseline Test RMSE = 21,100 <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 RMSE \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u0442\u043e\u043b\u043a\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 (\u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043b\u0435\u0442)<\/p>\n<h4>3. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438, \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u043e\u0446\u0435\u043d\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<\/h4>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0435\u0451 \u043e\u0446\u0435\u043d\u043a\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e.<\/p>\n<p>  \u041d\u0430\u043f\u043e\u043c\u043d\u0438\u043c \u0444\u043e\u0440\u043c\u0443\u043b\u0443 \u0448\u0430\u0433\u0430 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430 \u0434\u043b\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438:<\/p>\n<p>  w<sub>i+1<\/sub> = w<sub>i<\/sub> \u2014 \u03b1<sub>i<\/sub> * \u2211(w<sub>i<\/sub><sup>T<\/sup> * x<sub>j<\/sub> \u2014 y<sub>j<\/sub>) * x<sub>j<\/sub>.<\/p>\n<p>  \u0413\u0434\u0435 i \u044d\u0442\u043e \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0430 j \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f (w<sup>T<\/sup> * x \u2014 y) * x:  <\/p>\n<pre><code class=\"scala\">  import breeze.linalg.{ DenseVector =&gt; BreezeDenseVector }    def gradientSummand(weights: BreezeDenseVector[Double], lp: LabeledPoint): BreezeDenseVector[Double] = {     val x = BreezeDenseVector(lp.features.toArray)     val y = lp.label     val wx: Double = weights.t * x     (wx - y) * x   } <\/code><\/pre>\n<p>  \u0418\u0434\u0451\u043c \u0434\u0430\u043b\u044c\u0448\u0435: \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0430 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u2014 \u043a\u043e\u0440\u0442\u0435\u0436 (\u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435).  <\/p>\n<pre><code class=\"scala\">  def getLabeledPrediction(weights: BreezeDenseVector[Double], observation: LabeledPoint): (Double, Double) = {     val x = BreezeDenseVector(observation.features.toArray)     val y = observation.label     val prediction: Double = weights.t * x     (y, prediction)   } <\/code><\/pre>\n<p>  \u041d\u0430\u0441\u0442\u0430\u043b\u043e \u0441\u0430\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0413\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430:<\/p>\n<pre><code class=\"scala\">  def linregGradientDescent(trainData: RDD[LabeledPoint], numIters: Int) = {     \/\/ \u0420\u0430\u0437\u043c\u0435\u0440 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0438     val n = trainData.count()     \/\/ \u041a\u043e\u043b-\u0432\u043e \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a     val d = trainData.first().features.size     \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u0435\u0441\u043e\u0432 (\u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 d \u043d\u0443\u043b\u0435\u0439)     val w = BreezeDenseVector.zeros[Double](d)     val alpha = 1.0     val errorTrain = BreezeDenseVector.zeros[Double](numIters)     for (i &lt;- 0 until numIters) {       val labelsAndPredsTrain = trainData.map(x =&gt; getLabeledPrediction(w, x))       errorTrain(i) = calcRMSE(labelsAndPredsTrain)       val gradient: BreezeDenseVector[Double] = trainData.map(x =&gt; gradientSummand(w, x)).reduce((dv1, dv2) =&gt; dv1 + dv2)       val alpha_i = alpha \/ ( n * math.sqrt(i+1) )       w -= alpha_i * gradient     }     (w, errorTrain)   } <\/code><\/pre>\n<p>  \u041d\u0443 \u0432\u043e\u0442 \u0432\u0441\u0451 \u0433\u043e\u0442\u043e\u0432\u043e! \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 \u043d\u0430 \u0432\u0441\u0435\u0439 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0438 \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0435\u0451 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u0447\u043d\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435:  <\/p>\n<pre><code class=\"scala\">  val numIters = 50   val (weightsCustom, errorTrainCustom) = linregGradientDescent(parsedTrainData, numIters = numIters)   val labelsAndPreds = parsedValData.map(lp =&gt; getLabeledPrediction(weightsCustom, lp))   val rmseValLRCustom = calcRMSE(labelsAndPreds)   &gt; Custom Linear regression algorithm \tWeights = DenseVector(22.429559050840954, 20.57848949803509, -0.38069311013701224, 8.286892767462648, 5.725188813272974, -4.547779089973534, 15.494323154076804, 3.7888305696294986, 10.205862111138764, 5.877745123091225, 11.13061973187094, 3.6849419014146703) \tBaseline = 21,100 \tCustomLR = 18,341 <\/code><\/pre>\n<p>  \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0441\u0447\u0438\u0442\u0430\u043b\u0438 RMSE \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c, \u043a\u0430\u043a \u0432\u0451\u043b \u0441\u0435\u0431\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c: \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u0434\u0432\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0430. \u041f\u0435\u0440\u0432\u044b\u0439- \u044d\u0442\u043e \u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0442 \u043d\u043e\u043c\u0435\u0440\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 50 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439-\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043a\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 44 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/048\/f3d\/447\/048f3d4477924952a2fc62d1a38d3c3f.png\"\/><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/a92\/469\/ad2\/a92469ad278f4c9c8fd9a005dd126cc7.png\"\/><\/p>\n<p>  \u0423\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043c\u044b \u044f\u0432\u043d\u043e \u0434\u043e\u0431\u0438\u043b\u0438\u0441\u044c: \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448\u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u0430\u044e\u0442\u0441\u044f \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043d\u0435 \u043d\u0430 21 \u0433\u043e\u0434, \u0430 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043d\u0430 18. \u041d\u0430\u0434\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043e 500, \u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c 16,403.<\/p>\n<h4>4. \u041d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e MLlib, \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432<\/h4>\n<p>  \u041d\u0443 \u0447\u0442\u043e \u0436\u0435, \u0442\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0443\u0447\u0448\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0439 \u0447\u043b\u0435\u043d (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, intercept) \u0438 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u044e. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c LinearRegressionWithSGD, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0442\u043e\u0442 \u0436\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u0447\u0442\u043e \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u043c\u044b, \u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c, \u0442\u0430\u043a\u0438\u043c, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043a\u0430\u043a \u0441\u0442\u043e\u0445\u0430\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u044b\u0439 \u0441\u043f\u0443\u0441\u043a, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0433\u043e \u0447\u043b\u0435\u043d\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 L1 \u0438 L2 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<pre><code class=\"scala\">  import org.apache.spark.mllib.regression.RidgeRegressionWithSGD    def tuneModel(numIterations: Int = 500, stepSize: Double = 1.0, regParam: Double = 1e-1) = {     val model = new RidgeRegressionWithSGD()     model.optimizer       .setStepSize(stepSize)       .setNumIterations(numIterations)       .setRegParam(regParam)     model.setIntercept(true)   } <\/code><\/pre>\n<p>  \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 RidgeRegressionWithSGD, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0441 \u0443\u0436\u0435 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u0438 L2, \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 LinearRegressionWithSGD, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u0434\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u0447\u043a\u0443 <br \/>  <code>model.optimizer.setUpdater(new SquaredL2Updater)<\/code><\/p>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:  <\/p>\n<pre><code class=\"scala\">  val ridgeModel = tuneModel(numIterations = 500, stepSize = 1.0, regParam = 1e-1).run(parsedTrainData)   val weightsRidge = ridgeModel.weights   val interceptRidge = ridgeModel.intercept   val rmseValRidge = calcError(ridgeModel, parsedValData)    &gt; RidgeRegressionWithSGD \tweights = [16.49707401343799,15.090143596016913,-0.33882414671890876,6.21399054657087,3.9399959912276765,-3.326873353072514,11.219894673155363,2.605614334247694,7.368594539065305,4.352602095699236,7.936294197372791,2.597979499976716] \tintercept = 13.40151357030002    &gt; RidgeRegressionWithSGD validation RMSE \tBaseline = 21,100 \tCustomLR = 18,341 \tRidgeLR = 18,924 <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0441 \u043d\u0430\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 (\u0430 \u0435\u0441\u043b\u0438 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0438 \u0440\u0430\u0432\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 16.4, \u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0435 \u0430\u0445\u0442\u0438 \u043a\u0430\u043a\u043e\u0439), \u043d\u043e, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u0440\u043e\u0431\u0443\u044f \u0440\u0430\u0437\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u044c 1e-10, 1e-5 \u0438 1.  <\/p>\n<pre><code class=\"scala\">  var bestRMSE = rmseValRidge   var bestRegParam = 1.0   var bestModel = ridgeModel    for (regParam &lt;- Array(1e-10, 1e-5, 1)) {     val model = tuneModel(regParam = regParam).run(parsedTrainData)     val rmseVal = calcError(model, parsedValData)     println(f&quot;regParam = $regParam%.0e, RMSE = $rmseVal%.3f&quot;)      if (rmseVal &lt; bestRMSE) {       bestRMSE = rmseVal       bestRegParam = regParam       bestModel = model     }   }    &gt; regParam = 1e-10, RMSE = 16,292      regParam = 1e-05, RMSE = 16,292      regParam = 1e+00, RMSE = 23,179    &gt; Best model \tweights = [29.417184432817606,31.186800575308965,-17.37215928935944,8.288971260120093,5.111705048981693,-22.61199371516979,25.231243109503467,-4.989933439427709,6.709806469376133,-0.08589332350175394,9.958713420381914,-6.252319073419468] \tintercept = 16.170147238736043 \tregParam = 1.0E-10    &gt; Grid validation RMSE \tBaseline = 21,100 \tLR0 = 18,341 \tLR1 = 18,924 \tLRGrid = 16,292 <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043a\u0430\u043a \u0432\u0435\u0434\u0451\u0442 \u0441\u0435\u0431\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0435\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0448\u0430\u0433\u0430 (alpha) \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u044c\u043c\u0451\u043c \u0442\u043e\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0430\u043b \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0448\u0430\u0433 \u043f\u0443\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 1e-5, 10, \u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 500 \u0438 5.<\/p>\n<pre><code class=\"scala\">  for (alpha &lt;- Array(1e-5, 10)) {     for (numIter &lt;- Array(5, 500)) {       val model = tuneModel(numIterations = numIter, stepSize = alpha, regParam = bestRegParam).run(parsedTrainData)       val rmseVal = calcError(model, parsedValData)       println(f&quot;alpha = $alpha%.0e, numIters = $numIter, RMSE = $rmseVal%.3f&quot;)     }   }    &gt; alpha = 1e-05, numIters = 5,   RMSE = 56,912     alpha = 1e-05, numIters = 500, RMSE = 56,835     alpha = 1e+01, numIters = 5,   RMSE = 351471078,741     alpha = 1e+01, numIters = 500, RMSE = 3696615731186115000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,000 <\/code><\/pre>\n<p>  \u0418\u0442\u0430\u043a, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e \u043f\u0440\u0438 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u043c \u0448\u0430\u0433\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0447\u0435\u043d\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0430\u0435\u0442\u0441\u044f \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443, \u0430 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u0438\u0442\u0441\u044f, \u043f\u0440\u0438\u0442\u043e\u043c \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e.<\/p>\n<h4>5. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438<\/h4>\n<p>  \u0414\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u0431\u044b\u043b\u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0431\u044b \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u0442\u043e\u043b\u044c\u043a\u043e 2 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 x<sub>1<\/sub> \u0438 x<sub>2<\/sub>, \u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b\u0430 \u0431\u044b \u0441\u043e\u0431\u043e\u0439:<\/p>\n<p>  w<sub>0<\/sub> + w<sub>1<\/sub> * x<sub>1<\/sub> + w<sub>2<\/sub> * x<sub>2<\/sub>.<\/p>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043d\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438: \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043f\u043e\u0441\u043b\u043e\u0436\u043d\u0435\u0435:<\/p>\n<p>  w<sub>0<\/sub> + w<sub>1<\/sub>x<sub>1<\/sub> + w<sub>2<\/sub>x<sub>2<\/sub> + w<sub>3<\/sub>x<sub>1<\/sub>x<sub>2<\/sub> + w<sub>4<\/sub>x<sub>2<\/sub>x<sub>1<\/sub> + w<sub>5<\/sub>x<sub>1<\/sub><sup>2<\/sup> + w<sub>6<\/sub>x<sub>2<\/sub><sup>2<\/sup><\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0430\u043a \u0438 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u043c; \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b \u043f\u043e\u043f\u0430\u0440\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0430\u043b\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0430 \u0431\u044b \u043d\u043e\u0432\u044b\u0439 \u043d\u0430\u0431\u043e\u0440, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0451\u043d\u043d\u044b\u0439 \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c.  <\/p>\n<pre><code class=\"scala\">  def twoWayInteractions(lp: LabeledPoint) = {     val features: List[Double] = lp.features.toArray.toList     val two = (for (x &lt;- features; y &lt;- features) yield (x,y)).map(x =&gt; x._1 * x._2)     val inter = two ::: features     LabeledPoint(lp.label, Vectors.dense(inter.toArray))   } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u0432\u044b\u0448\u0435 \u043a \u043d\u0430\u0448\u0438\u043c \u043d\u0430\u0431\u043e\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445:  <\/p>\n<pre><code class=\"scala\">  val trainDataInteract = parsedTrainData.map(twoWayInteractions)   val valDataInteract = parsedValData.map(twoWayInteractions)   val testDataInteract = parsedTestData.map(twoWayInteractions) <\/code><\/pre>\n<p>  \u041d\u0443 \u0430 \u0434\u0430\u043b\u0435\u0435 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442.\u00a0\u043a. \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0440\u0430\u043d\u0435\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u043d\u0435 \u0434\u0430\u0442\u044c \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041d\u043e \u043c\u044b \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0437\u0434\u0435\u0441\u044c \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0430 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435.  <\/p>\n<pre><code class=\"scala\">  val modelInteract = tuneModel(regParam = bestRegParam).run(trainDataInteract)   val rmseValLRInteract = calcError(modelInteract, valDataInteract)    &gt; Interact validation RMSE \tBaseline = 21,100     LR0 = 18,341 \tLR1 = 18,924 \tLRGrid = 16,292 \tLRInteract = 15,225 <\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u043e\u0446\u0435\u043d\u0438\u043c \u043d\u0430\u0448\u0443 \u043d\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u043b\u0443\u0447\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0448\u0430 \u043e\u0446\u0435\u043d\u043a\u0430 \u0434\u0430\u0451\u0442 \u043d\u0435\u0441\u043c\u0435\u0449\u0451\u043d\u043d\u0443\u044e \u043e\u0446\u0435\u043d\u043a\u0443 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430 \u043d\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043b\u0443\u0447\u0448\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439.  <\/p>\n<pre><code class=\"scala\">  &gt; Test validation RMSE \tBaseline = 21,100 \tLRInteract = 15,905 <\/code><\/pre>\n<p>  \u041d\u0443 \u0432\u043e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u0432\u0441\u0451, \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043e\u0446\u0435\u043d\u043a\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043d\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0447\u0435\u043c \u0442\u0430 \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u043b\u0438.             <\/p>\n<div class=\"clear\"><\/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=\"http:\/\/habrahabr.ru\/post\/264193\/\"> http:\/\/habrahabr.ru\/post\/264193\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>             \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u043a\u0443\u0440\u0441 <a href=\"https:\/\/www.edx.org\/course\/scalable-machine-learning-uc-berkeleyx-cs190-1x\">Scalable Machine Learning<\/a> \u043f\u043e Apache Spark, \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 MLlib \u0434\u043b\u044f \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. \u041a\u0443\u0440\u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043b \u0438\u0437 \u0432\u0438\u0434\u0435\u043e\u043b\u0435\u043a\u0446\u0438\u0439 \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u043d\u0438\u0439. \u041b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0430 PySpark, \u0430 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u043c\u043d\u0435 \u0447\u0430\u0449\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043e scala, \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0435\u0440\u0435\u0440\u0435\u0448\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043b\u0430\u0431\u044b \u043d\u0430 \u044d\u0442\u043e\u043c \u044f\u0437\u044b\u043a\u0435, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u043b\u0443\u0447\u0448\u0435 \u0443\u0441\u0432\u043e\u0438\u0442\u044c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b. \u0411\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0442\u043b\u0438\u0447\u0438\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043d\u0435\u0442, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e PySpark \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <a href=\"http:\/\/docs.scipy.org\/doc\/numpy\/reference\/index.html\">NumPy<\/a>, \u0430 \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u043e scala \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <a href=\"https:\/\/github.com\/scalanlp\/breeze\">Breeze<\/a>.<\/p>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043e\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u043b\u0438 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0430\u043b\u0433\u0435\u0431\u0440\u044b \u0432 NumPy \u0438 \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0441 apache spark \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u043e\u0441\u044c \u0441 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u043e\u043d\u0430 \u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u0430 \u043d\u0438\u0436\u0435.  <\/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-262946","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262946","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=262946"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262946\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}