{"id":165283,"date":"2013-01-21T22:02:04","date_gmt":"2013-01-21T18:02:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=165283"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=165283","title":{"rendered":"<span class=\"post_title\">Data Mining: \u041f\u0435\u0440\u0432\u0438\u0447\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0421\u0423\u0411\u0414. \u0427\u0430\u0441\u0442\u044c 3 (\u0421\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b)<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0435\u0440\u0438\u044f \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0430\u043d\u0430\u043b\u0438\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0430 \u0438\u0437 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430 \u0441\u043f\u043e\u0440\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 Kaggle. \u0425\u043e\u0442\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0435, \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043f\u043e\u043b\u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u044b \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f <a href=\"http:\/\/habrahabr.ru\/post\/165001\/\">\u0427\u0430\u0441\u0442\u044c 1<\/a> \u0438 <a href=\"http:\/\/habrahabr.ru\/post\/165281\/\">\u0427\u0430\u0441\u0442\u0438 2<\/a> \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<br \/>  titanik_test_3 \u0438 titanik_train_3. <br \/>  <a name=\"habracut\"><\/a><br \/>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u043e\u043b\u0435\u0439 \u0443 \u043d\u0438\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0435 \u2014 survived, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u043e\u0442 \u043a\u043e\u0434 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u044b titanik_train_3  <\/p>\n<pre><code class=\"sql\">CREATE TABLE titanik_train_3 (   id bigint,   survived integer,   pclass integer,   name character varying(255),   sex character varying(255),   age double precision,   sibsp integer,   parch integer,   ticket character varying(255),   fare double precision,   cabin character varying(255),   embarked character varying(255),   cabin_cnt integer,   cabin_type text,   ticket_type text,   ticket_number text,   cabin_people_cnt integer ) <\/code><\/pre>\n<p>  \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u0441\u0438\u043c\u0432\u043e\u043b\u044c\u043d\u043e-\u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u2014 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c. \u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u0432 \u044d\u0442\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0432 \u0442\u043e\u043c \u0436\u0435 \u0432\u0438\u0434\u0435 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0438 \u0431\u044b\u043b\u0438, \u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u044c\u043d\u044b\u0435 \u2014 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c. <br \/>  \u0412\u0430\u0436\u043d\u0435\u0439\u0448\u0438\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0435 \u043f\u043e\u043a\u0440\u044b\u0442\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041f\u043e\u0442\u043e\u043c\u0443, \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435(\u0445\u043e\u0442\u044f \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043c\u043e\u0436\u043d\u043e \u0438 \u0440\u0430\u043d\u0435\u0435) \u0441\u043b\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043e\u0434\u043d\u0443. \u0410 \u0432 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0435\u0435 \u043f\u043e\u043b\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c NULL. <br \/>  \u0423\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0447\u0442\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u0442\u044c \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430\u0436\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0431\u044b\u0442\u044c \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e. \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 UNION.  <\/p>\n<pre><code class=\"sql\">select a.* into titanik_full_1 from ( select * from  titanik_train_3 union select  id, NULL::integer as survived,   pclass, &quot;name&quot;,  sex ,  age ,  sibsp ,  parch, ticket,fare,cabin,embarked,cabin_cnt,cabin_type,ticket_type,ticket_number,   cabin_people_cnt  from  titanik_test_3 ) as a; <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0434\u043d\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u0438 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445. <br \/>  \u0423\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u043f\u043e\u043b\u044f, \u043a\u0440\u043e\u043c\u0435 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0445:  <\/p>\n<pre><code class=\"sql\"> select a.* into titanik_full_2 from (  select id, survived, pclass::float, age::float, sibsp::float, parch::float, fare::float, cabin_cnt::float, CAST(ticket_number as float) as ticket_number, cabin_people_cnt::float  from titanik_full_1 where ticket_number != ''   union  select id, survived, pclass, age, sibsp, parch, fare, cabin_cnt, 0 as ticket_number, cabin_people_cnt  from titanik_full_1 where ticket_number = '' ) as a; <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 titanik_full_2, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<pre><code class=\"sql\">CREATE TABLE titanik_full_2 (   id bigint,   survived integer,   pclass integer,   age double precision,   sibsp integer,   parch integer,   fare double precision,   cabin_cnt integer,   ticket_number integer,   cabin_people_cnt bigint ) <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e \u043f\u043e\u043b\u044e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u0442\u043e, \u043b\u0438\u0431\u043e \u0438\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. \u0422\u0430\u043a\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0441\u0432\u043e\u0434\u043d\u044b\u043c\u0438(pivot tables), \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043d\u0435 \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e, \u043f\u043e\u043b\u044f-values \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043b\u0438\u0431\u043e 0 \u043b\u0438\u0431\u043e 1. \u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u044d\u0442\u043e \u0442\u043e\u0431\u043e\u0440\u0430\u0436\u0435\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435:<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/cbd\/5bc\/816\/cbd5bc8166a75053f873cf03443fbd45.png\"\/><br \/>  \u0422.\u0435. \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0442\u0430\u043b\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0432\u0441\u0435 \u044d\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c. \u041d\u043e \u043b\u0443\u0447\u0448\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 PL\/PGSQL, \u043a\u043e\u0442\u043e\u0440\u0430\u044f\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044f.  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION sparse_matrix_generator( tablename_source character varying,  tablename_dest character varying,  field_name character varying) RETURNS integer AS $$ DECLARE   pgst_object  REFCURSOR;   unival character varying; BEGIN   OPEN pgst_object FOR EXECUTE 'select distinct '||field_name ||' from '||tablename_source ||' where ' || field_name ||' NOTNULL';     LOOP       FETCH pgst_object INTO unival;       EXIT WHEN NOT FOUND;       EXECUTE 'ALTER TABLE '|| tablename_dest ||' ADD COLUMN &quot;'|| field_name||unival ||'&quot; smallint NOT NULL DEFAULT 0';       EXECUTE 'UPDATE '||tablename_dest||' SET &quot;'||field_name||unival|| '&quot;= 1 FROM ' ||tablename_source||       ' WHERE '||tablename_dest||'.id = '||tablename_source||'.id and '||field_name||' = '''||unival||'''';     END LOOP;   RETURN 0; END; $$ LANGUAGE 'plpgsql'; <\/code><\/pre>\n<p>  \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"sql\">select sparse_matrix_generator('titanik_full_1', 'titanik_full_2', 'cabin_type'); select sparse_matrix_generator('titanik_full_1', 'titanik_full_2', 'ticket_type'); select sparse_matrix_generator('titanik_full_1', 'titanik_full_2', 'embarked'); select sparse_matrix_generator('titanik_full_1', 'titanik_full_2', 'sex'); <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0443 \u043d\u0430\u0441 \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0441\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0436\u0435\u043d\u043d\u0430\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c\u044e \u0432 58 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u043d\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u0438 \u043e\u0442\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0438 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u043f\u043e \u043f\u043e\u043b\u044e survived.<br \/>  \u0415\u0441\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043d\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0414\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0435\u0441\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0432\u044b\u0431\u043e\u0440\u043a\u0435. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0445, \u043c\u0438\u043d\u0438\u043c\u0430\u043a\u0441\u043d\u044b\u043c \u043d\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c. \u0421\u0443\u0442\u044c \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u0442\u0430\u043a\u043e\u0432\u0430: \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0431\u0443\u0434\u0435\u0442 0, \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c: 1, \u0430 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u043e\u043b\u043e\u0436\u0435\u043d\u043e \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION minmax_normalizer(tablename_source character varying, field_name character varying)   RETURNS integer AS $BODY$ DECLARE   pgst_object  REFCURSOR;   maxval float;   minval float;   C RECORD; BEGIN   EXECUTE 'select min(&quot;'||field_name ||'&quot;) as minval, max(&quot;'||field_name ||'&quot;) as maxval from '|| tablename_source INTO C;   maxval := C.maxval;   minval := C.minval;   EXECUTE 'UPDATE '||tablename_source||' SET &quot;'||field_name||'&quot;=(&quot;'||field_name||'&quot;-$1)\/($2-$1)' USING minval, maxval;   RETURN 0; END; $BODY$   LANGUAGE plpgsql VOLATILE   COST 100;  <\/code><\/pre>\n<p>  \u0418 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0435\u0435 \u043a \u043f\u043e\u043b\u044f\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u043e \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c:  <\/p>\n<pre><code class=\"sql\">select minmax_normalizer('titanik_full_2', 'pclass'); select minmax_normalizer('titanik_full_2', 'age'); select minmax_normalizer('titanik_full_2', 'sibsp'); select minmax_normalizer('titanik_full_2', 'parch'); select minmax_normalizer('titanik_full_2', 'fare'); select minmax_normalizer('titanik_full_2', 'cabin_cnt'); select minmax_normalizer('titanik_full_2', 'ticket_number'); select minmax_normalizer('titanik_full_2', 'cabin_people_cnt');  <\/code><\/pre>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 \u043d\u0443\u043b\u044f \u0434\u043e \u0435\u0434\u0438\u043d\u0438\u0446\u044b.<br \/>  \u0412\u044b\u0431\u0435\u0440\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0438 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443:  <\/p>\n<pre><code class=\"sql\">select * into titanik_test_final from  titanik_full_2 where survived isnull; alter table titanik_test_final drop column survived;  <\/code><\/pre>\n<p>  \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e:  <\/p>\n<pre><code class=\"sql\">select * into titanik_train_final from  titanik_full_2 where survived notnull; <\/code><\/pre>\n<p>  \u0434\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e\u0439.<br \/>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0435\u0441\u0442\u044c \u043f\u0443\u0441\u0442\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0418\u0445 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u0440\u0435\u0434\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439:  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION null_normalizer(tablename_source character varying)   RETURNS integer AS $BODY$ DECLARE   pgst_object  REFCURSOR;   fieldval character varying;   count_null integer;   field_avg float; BEGIN   OPEN pgst_object FOR EXECUTE 'select column_name from information_schema.columns where'||   ' table_name='''||tablename_source||'''';     LOOP       FETCH pgst_object INTO fieldval;       EXIT WHEN NOT FOUND;       count_null := 0;       EXECUTE 'select count(id) from '||tablename_source||' where &quot;'||fieldval||'&quot; isnull' into count_null;       IF count_null &gt; 0 THEN \t  raise notice 'field: %', fieldval; \t  EXECUTE 'select avg('||fieldval||') from '||tablename_source INTO field_avg;  \t  EXECUTE 'UPDATE '||tablename_source||' set '||fieldval||'= $1 where '||fieldval||' isnull' using field_avg;       END IF;           END LOOP;   RETURN 0; END; $BODY$   LANGUAGE plpgsql VOLATILE   COST 100; <\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0438\u043c\u0435\u043d\u0430 \u043f\u043e\u043b\u0435\u0439 \u0434\u043b\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043f\u043e\u043b\u0435, \u0438 \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0443\u043b\u044f \u2014 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0441\u0442\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043d\u0430 \u0441\u0440\u0435\u0434\u043d\u0435\u0435.<br \/>  \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<pre><code class=\"sql\">select null_normalizer('titanik_test_final'); select null_normalizer('titanik_train_final'); <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0438 \u0440\u0430\u0437\u0440\u0435\u0436\u0435\u043d\u043d\u0430\u044f:  <\/p>\n<pre><code class=\"sql\">CREATE TABLE titanik_test_final (   id bigint,   pclass double precision,   age double precision,   sibsp double precision,   parch double precision,   fare double precision,   cabin_cnt double precision,   ticket_number double precision,   cabin_people_cnt double precision,   &quot;cabin_typeF&quot; smallint,   &quot;cabin_typeB&quot; smallint,   &quot;cabin_typeG&quot; smallint,   &quot;cabin_typeC&quot; smallint,   &quot;cabin_typeT&quot; smallint,   &quot;cabin_typeD&quot; smallint,   &quot;cabin_typeE&quot; smallint,   &quot;cabin_typeA&quot; smallint,   &quot;ticket_typeSW\/PP&quot; smallint,   &quot;ticket_typeC&quot; smallint,   &quot;ticket_typePC&quot; smallint,   &quot;ticket_typeAQ\/3.&quot; smallint,   &quot;ticket_typeSC\/A.3&quot; smallint,   &quot;ticket_typeS.O.C.&quot; smallint,   &quot;ticket_typeS.O.\/P.P.&quot; smallint,   &quot;ticket_typeSC\/AH&quot; smallint,   &quot;ticket_typeSOTON\/O2&quot; smallint,   &quot;ticket_typeC.A.&quot; smallint,   &quot;ticket_typeW\/C&quot; smallint,   &quot;ticket_typeS.C.\/A.4.&quot; smallint,   &quot;ticket_typeFa&quot; smallint,   &quot;ticket_typeLP&quot; smallint,   &quot;ticket_typeSCO\/W&quot; smallint,   &quot;ticket_typeF.C.&quot; smallint,   &quot;ticket_typeA.5.&quot; smallint,   &quot;ticket_typeSC\/AH Basle&quot; smallint,   &quot;ticket_typeSC\/A4&quot; smallint,   &quot;ticket_typeS.C.\/PARIS&quot; smallint,   &quot;ticket_typeS.O.P.&quot; smallint,   &quot;ticket_typeLINE&quot; smallint,   &quot;ticket_typeSO\/C&quot; smallint,   &quot;ticket_typeP\/PP&quot; smallint,   &quot;ticket_typeAQ\/4&quot; smallint,   &quot;ticket_typeSC&quot; smallint,   &quot;ticket_typeW.E.P.&quot; smallint,   &quot;ticket_typeSOTON\/O.Q.&quot; smallint,   &quot;ticket_typeA\/4&quot; smallint,   &quot;ticket_typeSC\/PARIS&quot; smallint,   &quot;ticket_typeA. 2.&quot; smallint,   &quot;ticket_typeF.C.C.&quot; smallint,   &quot;ticket_typeS.P.&quot; smallint,   &quot;ticket_typePP&quot; smallint,   &quot;ticket_typeC.A.\/SOTON&quot; smallint,   &quot;embarkedC&quot; smallint,   &quot;embarkedQ&quot; smallint,   &quot;embarkedS&quot; smallint,   sexfemale smallint,   sexmale smallint ) <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:  <\/p>\n<pre><code class=\"sql\">COPY titanik_train_final to '\/tmp\/titanik_train_final.csv'; COPY titanik_test_final to '\/tmp\/titanik_test_final.csv'; <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u0430\u043d\u043d\u044b\u0435 \u0433\u043e\u0442\u043e\u0432\u044b. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438.<br \/>  \u0414\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438, \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u0440\u0435\u0436\u0435\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u044d\u043d\u043a\u043e\u0434\u0435\u0440\u043e\u043c, \u043b\u0438\u0431\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c PCA. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438. \u041f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u044d\u043d\u043a\u043e\u0434\u0435\u0440 \u0438 \u0440\u0435\u0448\u0430\u044e\u0449\u0438\u0439 \u043b\u0435\u0441 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0432 \u0442\u0443\u0440\u043d\u0438\u0440\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435.    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/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=\"http:\/\/habrahabr.ru\/post\/165283\/\"> http:\/\/habrahabr.ru\/post\/165283\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0435\u0440\u0438\u044f \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0430\u043d\u0430\u043b\u0438\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0430 \u0438\u0437 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430 \u0441\u043f\u043e\u0440\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 Kaggle. \u0425\u043e\u0442\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0435, \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043f\u043e\u043b\u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u044b \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f <a href=\"http:\/\/habrahabr.ru\/post\/165001\/\">\u0427\u0430\u0441\u0442\u044c 1<\/a> \u0438 <a href=\"http:\/\/habrahabr.ru\/post\/165281\/\">\u0427\u0430\u0441\u0442\u0438 2<\/a> \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<br \/>  titanik_test_3 \u0438 titanik_train_3.   <\/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-165283","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/165283","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=165283"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/165283\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}