{ "cells": [ { "cell_type": "code", "execution_count": 313, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatepricebedroomsbathroomssqft_livingsqft_lotfloorswaterfrontview...gradesqft_abovesqft_basementyr_builtyr_renovatedzipcodelatlongsqft_living15sqft_lot15
0712930052020141013T000000221900.031.00118056501.000...711800195509817847.5112-122.25713405650
1641410019220141209T000000538000.032.25257072422.000...72170400195119919812547.7210-122.31916907639
2563150040020150225T000000180000.021.00770100001.000...67700193309802847.7379-122.23327208062
3248720087520141209T000000604000.043.00196050001.000...71050910196509813647.5208-122.39313605000
4195440051020150218T000000510000.032.00168080801.000...816800198709807447.6168-122.04518007503
..................................................................
999532205926420140926T000000279000.021.001020470441.000...710200190419589804247.4206-122.155193012139
9996555750027020150209T000000262000.031.50170095791.000...71100600196209802347.3209-122.33817009628
9997916410012520140807T000000533000.041.00155047501.500...715500191909811747.6824-122.38913204750
9998737060004520150402T000000640000.031.75168081001.002...816800195009817747.7212-122.36418807750
9999859440006020140609T000000285000.032.251680351272.000...716800198709809247.3025-122.067182035166
\n", "

10000 rows × 21 columns

\n", "
" ], "text/plain": [ " id date price bedrooms bathrooms sqft_living \\\n", "0 7129300520 20141013T000000 221900.0 3 1.00 1180 \n", "1 6414100192 20141209T000000 538000.0 3 2.25 2570 \n", "2 5631500400 20150225T000000 180000.0 2 1.00 770 \n", "3 2487200875 20141209T000000 604000.0 4 3.00 1960 \n", "4 1954400510 20150218T000000 510000.0 3 2.00 1680 \n", "... ... ... ... ... ... ... \n", "9995 322059264 20140926T000000 279000.0 2 1.00 1020 \n", "9996 5557500270 20150209T000000 262000.0 3 1.50 1700 \n", "9997 9164100125 20140807T000000 533000.0 4 1.00 1550 \n", "9998 7370600045 20150402T000000 640000.0 3 1.75 1680 \n", "9999 8594400060 20140609T000000 285000.0 3 2.25 1680 \n", "\n", " sqft_lot floors waterfront view ... grade sqft_above \\\n", "0 5650 1.0 0 0 ... 7 1180 \n", "1 7242 2.0 0 0 ... 7 2170 \n", "2 10000 1.0 0 0 ... 6 770 \n", "3 5000 1.0 0 0 ... 7 1050 \n", "4 8080 1.0 0 0 ... 8 1680 \n", "... ... ... ... ... ... ... ... \n", "9995 47044 1.0 0 0 ... 7 1020 \n", "9996 9579 1.0 0 0 ... 7 1100 \n", "9997 4750 1.5 0 0 ... 7 1550 \n", "9998 8100 1.0 0 2 ... 8 1680 \n", "9999 35127 2.0 0 0 ... 7 1680 \n", "\n", " sqft_basement yr_built yr_renovated zipcode lat long \\\n", "0 0 1955 0 98178 47.5112 -122.257 \n", "1 400 1951 1991 98125 47.7210 -122.319 \n", "2 0 1933 0 98028 47.7379 -122.233 \n", "3 910 1965 0 98136 47.5208 -122.393 \n", "4 0 1987 0 98074 47.6168 -122.045 \n", "... ... ... ... ... ... ... \n", "9995 0 1904 1958 98042 47.4206 -122.155 \n", "9996 600 1962 0 98023 47.3209 -122.338 \n", "9997 0 1919 0 98117 47.6824 -122.389 \n", "9998 0 1950 0 98177 47.7212 -122.364 \n", "9999 0 1987 0 98092 47.3025 -122.067 \n", "\n", " sqft_living15 sqft_lot15 \n", "0 1340 5650 \n", "1 1690 7639 \n", "2 2720 8062 \n", "3 1360 5000 \n", "4 1800 7503 \n", "... ... ... \n", "9995 1930 12139 \n", "9996 1700 9628 \n", "9997 1320 4750 \n", "9998 1880 7750 \n", "9999 1820 35166 \n", "\n", "[10000 rows x 21 columns]" ] }, "execution_count": 313, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "from sklearn.model_selection import train_test_split\n", "from sklearn import set_config\n", "\n", "df = pd.read_csv(\"data/house_data.csv\", sep=\",\", nrows=10000)\n", "df.dropna()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Устраняем выбросы в колонке цены и добавляем колонку с категориями цены" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatepricebedroomsbathroomssqft_livingsqft_lotfloorswaterfrontview...sqft_abovesqft_basementyr_builtyr_renovatedzipcodelatlongsqft_living15sqft_lot15price_category
9980684070003620140728T000000497000.021.0077033251.000...7700191809812247.6102-122.2999604800middle
9981182406908320150429T000000835000.031.003060301661.000...30600195909802747.5656-122.093188019602high
9982183698024020141015T000000730000.042.75292045002.000...29200199909800647.5646-122.12429204505high
9983352890016020141001T000000655000.031.00137052501.000...1070300193909810947.6421-122.34824104200high
9984144280006020141120T000000205000.032.50187031182.000...18700199309803847.3739-122.05615803601low
9985872210003020150407T000000632750.042.00180048001.500...18000191809811247.6388-122.30219504800high
9986172304962420140512T000000330000.053.00210077151.000...1250850201309816847.4866-122.31921007959low
9987404040020020141007T000000527500.052.25253082502.000...25300196109800747.6117-122.13420208250middle
9988869139109020140508T000000716500.042.50329064652.000...32900200209807547.5981-121.97631005929high
9989785330219020141217T000000388500.042.50189053952.000...18900200609806547.5415-121.88320605395middle
9990326000070020140904T000000530000.031.75168077701.000...16800196709800547.6028-122.16718807770middle
9991512630051020150108T000000419000.032.50217045172.000...21700200209805947.4819-122.14026104770middle
9992719933037020150309T000000385000.031.75120073601.000...12000197809805247.6979-122.13012007500middle
9993185490024020140528T000000655000.042.50299056692.000...29900200309807447.6119-122.01131105058high
9994673870033520140701T0000001127312.542.753770109002.002...3070700192409814447.5849-122.29030005000very_high
999532205926420140926T000000279000.021.001020470441.000...10200190419589804247.4206-122.155193012139low
9996555750027020150209T000000262000.031.50170095791.000...1100600196209802347.3209-122.33817009628low
9997916410012520140807T000000533000.041.00155047501.500...15500191909811747.6824-122.38913204750middle
9998737060004520150402T000000640000.031.75168081001.002...16800195009817747.7212-122.36418807750high
9999859440006020140609T000000285000.032.251680351272.000...16800198709809247.3025-122.067182035166low
\n", "

20 rows × 22 columns

\n", "
" ], "text/plain": [ " id date price bedrooms bathrooms \\\n", "9980 6840700036 20140728T000000 497000.0 2 1.00 \n", "9981 1824069083 20150429T000000 835000.0 3 1.00 \n", "9982 1836980240 20141015T000000 730000.0 4 2.75 \n", "9983 3528900160 20141001T000000 655000.0 3 1.00 \n", "9984 1442800060 20141120T000000 205000.0 3 2.50 \n", "9985 8722100030 20150407T000000 632750.0 4 2.00 \n", "9986 1723049624 20140512T000000 330000.0 5 3.00 \n", "9987 4040400200 20141007T000000 527500.0 5 2.25 \n", "9988 8691391090 20140508T000000 716500.0 4 2.50 \n", "9989 7853302190 20141217T000000 388500.0 4 2.50 \n", "9990 3260000700 20140904T000000 530000.0 3 1.75 \n", "9991 5126300510 20150108T000000 419000.0 3 2.50 \n", "9992 7199330370 20150309T000000 385000.0 3 1.75 \n", "9993 1854900240 20140528T000000 655000.0 4 2.50 \n", "9994 6738700335 20140701T000000 1127312.5 4 2.75 \n", "9995 322059264 20140926T000000 279000.0 2 1.00 \n", "9996 5557500270 20150209T000000 262000.0 3 1.50 \n", "9997 9164100125 20140807T000000 533000.0 4 1.00 \n", "9998 7370600045 20150402T000000 640000.0 3 1.75 \n", "9999 8594400060 20140609T000000 285000.0 3 2.25 \n", "\n", " sqft_living sqft_lot floors waterfront view ... sqft_above \\\n", "9980 770 3325 1.0 0 0 ... 770 \n", "9981 3060 30166 1.0 0 0 ... 3060 \n", "9982 2920 4500 2.0 0 0 ... 2920 \n", "9983 1370 5250 1.0 0 0 ... 1070 \n", "9984 1870 3118 2.0 0 0 ... 1870 \n", "9985 1800 4800 1.5 0 0 ... 1800 \n", "9986 2100 7715 1.0 0 0 ... 1250 \n", "9987 2530 8250 2.0 0 0 ... 2530 \n", "9988 3290 6465 2.0 0 0 ... 3290 \n", "9989 1890 5395 2.0 0 0 ... 1890 \n", "9990 1680 7770 1.0 0 0 ... 1680 \n", "9991 2170 4517 2.0 0 0 ... 2170 \n", "9992 1200 7360 1.0 0 0 ... 1200 \n", "9993 2990 5669 2.0 0 0 ... 2990 \n", "9994 3770 10900 2.0 0 2 ... 3070 \n", "9995 1020 47044 1.0 0 0 ... 1020 \n", "9996 1700 9579 1.0 0 0 ... 1100 \n", "9997 1550 4750 1.5 0 0 ... 1550 \n", "9998 1680 8100 1.0 0 2 ... 1680 \n", "9999 1680 35127 2.0 0 0 ... 1680 \n", "\n", " sqft_basement yr_built yr_renovated zipcode lat long \\\n", "9980 0 1918 0 98122 47.6102 -122.299 \n", "9981 0 1959 0 98027 47.5656 -122.093 \n", "9982 0 1999 0 98006 47.5646 -122.124 \n", "9983 300 1939 0 98109 47.6421 -122.348 \n", "9984 0 1993 0 98038 47.3739 -122.056 \n", "9985 0 1918 0 98112 47.6388 -122.302 \n", "9986 850 2013 0 98168 47.4866 -122.319 \n", "9987 0 1961 0 98007 47.6117 -122.134 \n", "9988 0 2002 0 98075 47.5981 -121.976 \n", "9989 0 2006 0 98065 47.5415 -121.883 \n", "9990 0 1967 0 98005 47.6028 -122.167 \n", "9991 0 2002 0 98059 47.4819 -122.140 \n", "9992 0 1978 0 98052 47.6979 -122.130 \n", "9993 0 2003 0 98074 47.6119 -122.011 \n", "9994 700 1924 0 98144 47.5849 -122.290 \n", "9995 0 1904 1958 98042 47.4206 -122.155 \n", "9996 600 1962 0 98023 47.3209 -122.338 \n", "9997 0 1919 0 98117 47.6824 -122.389 \n", "9998 0 1950 0 98177 47.7212 -122.364 \n", "9999 0 1987 0 98092 47.3025 -122.067 \n", "\n", " sqft_living15 sqft_lot15 price_category \n", "9980 960 4800 middle \n", "9981 1880 19602 high \n", "9982 2920 4505 high \n", "9983 2410 4200 high \n", "9984 1580 3601 low \n", "9985 1950 4800 high \n", "9986 2100 7959 low \n", "9987 2020 8250 middle \n", "9988 3100 5929 high \n", "9989 2060 5395 middle \n", "9990 1880 7770 middle \n", "9991 2610 4770 middle \n", "9992 1200 7500 middle \n", "9993 3110 5058 high \n", "9994 3000 5000 very_high \n", "9995 1930 12139 low \n", "9996 1700 9628 low \n", "9997 1320 4750 middle \n", "9998 1880 7750 high \n", "9999 1820 35166 low \n", "\n", "[20 rows x 22 columns]" ] }, "execution_count": 314, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q1 = df['price'].quantile(0.25) # Находим 1-й квартиль (Q1)\n", "q3 = df['price'].quantile(0.75) # Находим 3-й квартиль (Q3)\n", "iqr = q3 - q1 # Вычисляем межквартильный размах (IQR)\n", "\n", "# Определяем границы для выбросов\n", "lower_bound = q1 - 1.5 * iqr # Нижняя граница\n", "upper_bound = q3 + 1.5 * iqr # Верхняя граница\n", "\n", "# Устраняем выбросы: заменяем значения ниже нижней границы на саму нижнюю границу, а выше верхней — на верхнюю\n", "df['price'] = df['price'].apply(lambda x: lower_bound if x < lower_bound else upper_bound if x > upper_bound else x)\n", "\n", "# Добавляем столбец с категорями цены\n", "df['price_category'] = pd.cut(df['price'], bins=[75000,338750,602750,866750,1130750], labels=['low','middle','high','very_high'], include_lowest=True)\n", "df.tail(20)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Бизнес-цели\n", "1. Прогноз класса цены недвижимости (Классификация)\n", "2. Оценка состояния недвижимости (Регрессия)\n", "\n", "### Определение достижимого уровня качества модели для первой задачи\n", "#### Разделение набора данных на обучающую и тестовые выборки (80/20) для задачи классификации (Целевой признак - price)" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'X_train'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatepricebedroomsbathroomssqft_livingsqft_lotfloorswaterfrontview...sqft_abovesqft_basementyr_builtyr_renovatedzipcodelatlongsqft_living15sqft_lot15price_category
9843326000034020140622T000000732600.042.50213073001.000...1230900196309800547.6050-122.16721307560high
9623982870205520140508T000000358000.021.5096018082.000...9600199309812247.6183-122.29812901668middle
3095343850062520140519T000000210000.031.001080210431.000...10800194209810647.5515-122.35713807620low
411242202909420140716T000000517534.021.008331439471.000...8330200609807047.3889-122.4821380143947middle
3060746290001520150108T000000387000.032.251760451332.000...17600198409806547.5124-121.866191051773middle
..................................................................
1750278772014020150407T000000416000.032.501790115421.000...1190600196909805947.5124-122.16017909131middle
2354619240040020140728T000000775000.042.50309071122.000...30900200109805247.7050-122.11830506000high
857229650003620150310T000000450000.042.752980132601.000...18001180197909805647.5152-122.197192010731middle
6181278731013020141212T000000289950.041.75209074161.000...10501040197009803147.4107-122.17917107527low
3141856730011020140604T000000485000.032.502340590581.000...23400198509803847.4052-122.028270037263middle
\n", "

8000 rows × 22 columns

\n", "
" ], "text/plain": [ " id date price bedrooms bathrooms sqft_living \\\n", "9843 3260000340 20140622T000000 732600.0 4 2.50 2130 \n", "9623 9828702055 20140508T000000 358000.0 2 1.50 960 \n", "3095 3438500625 20140519T000000 210000.0 3 1.00 1080 \n", "411 2422029094 20140716T000000 517534.0 2 1.00 833 \n", "3060 7462900015 20150108T000000 387000.0 3 2.25 1760 \n", "... ... ... ... ... ... ... \n", "1750 2787720140 20150407T000000 416000.0 3 2.50 1790 \n", "2354 6192400400 20140728T000000 775000.0 4 2.50 3090 \n", "857 2296500036 20150310T000000 450000.0 4 2.75 2980 \n", "6181 2787310130 20141212T000000 289950.0 4 1.75 2090 \n", "3141 8567300110 20140604T000000 485000.0 3 2.50 2340 \n", "\n", " sqft_lot floors waterfront view ... sqft_above sqft_basement \\\n", "9843 7300 1.0 0 0 ... 1230 900 \n", "9623 1808 2.0 0 0 ... 960 0 \n", "3095 21043 1.0 0 0 ... 1080 0 \n", "411 143947 1.0 0 0 ... 833 0 \n", "3060 45133 2.0 0 0 ... 1760 0 \n", "... ... ... ... ... ... ... ... \n", "1750 11542 1.0 0 0 ... 1190 600 \n", "2354 7112 2.0 0 0 ... 3090 0 \n", "857 13260 1.0 0 0 ... 1800 1180 \n", "6181 7416 1.0 0 0 ... 1050 1040 \n", "3141 59058 1.0 0 0 ... 2340 0 \n", "\n", " yr_built yr_renovated zipcode lat long sqft_living15 \\\n", "9843 1963 0 98005 47.6050 -122.167 2130 \n", "9623 1993 0 98122 47.6183 -122.298 1290 \n", "3095 1942 0 98106 47.5515 -122.357 1380 \n", "411 2006 0 98070 47.3889 -122.482 1380 \n", "3060 1984 0 98065 47.5124 -121.866 1910 \n", "... ... ... ... ... ... ... \n", "1750 1969 0 98059 47.5124 -122.160 1790 \n", "2354 2001 0 98052 47.7050 -122.118 3050 \n", "857 1979 0 98056 47.5152 -122.197 1920 \n", "6181 1970 0 98031 47.4107 -122.179 1710 \n", "3141 1985 0 98038 47.4052 -122.028 2700 \n", "\n", " sqft_lot15 price_category \n", "9843 7560 high \n", "9623 1668 middle \n", "3095 7620 low \n", "411 143947 middle \n", "3060 51773 middle \n", "... ... ... \n", "1750 9131 middle \n", "2354 6000 high \n", "857 10731 middle \n", "6181 7527 low \n", "3141 37263 middle \n", "\n", "[8000 rows x 22 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'y_train'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
price_category
9843high
9623middle
3095low
411middle
3060middle
......
1750middle
2354high
857middle
6181low
3141middle
\n", "

8000 rows × 1 columns

\n", "
" ], "text/plain": [ " price_category\n", "9843 high\n", "9623 middle\n", "3095 low\n", "411 middle\n", "3060 middle\n", "... ...\n", "1750 middle\n", "2354 high\n", "857 middle\n", "6181 low\n", "3141 middle\n", "\n", "[8000 rows x 1 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'X_test'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatepricebedroomsbathroomssqft_livingsqft_lotfloorswaterfrontview...sqft_abovesqft_basementyr_builtyr_renovatedzipcodelatlongsqft_living15sqft_lot15price_category
5341663290057420150225T000000595000.053.002980100641.000...16801300194009815547.7372-122.31615907800middle
4384242302924520140617T000000550000.031.752240782252.000...22400197609807047.4638-122.4842030202554middle
5795247337005020140604T000000327500.041.75165078001.000...16500196809805847.4507-122.139175010400low
4956952810498520141104T000000611000.021.00127051001.000...1100170190009811547.6771-122.32816703900high
7723397290002520150313T000000499000.061.75240075001.500...14001000197509815547.7661-122.31319807500middle
..................................................................
8517387660012020150422T000000265000.031.501780101961.000...1270510196709800147.3375-122.29113207875low
6914682160000520150403T000000710000.041.75212054001.000...10601060194109819947.6501-122.39520526000high
4499276760393120140818T000000469000.033.25137011943.000...13700200409810747.6718-122.38818002678middle
8651880240041120140619T000000249000.031.00105084981.000...10500195909803147.4043-122.20210508498low
4234545280073520140722T000000780000.042.502270134491.000...1310960197509804047.5416-122.232281013475high
\n", "

2000 rows × 22 columns

\n", "
" ], "text/plain": [ " id date price bedrooms bathrooms sqft_living \\\n", "5341 6632900574 20150225T000000 595000.0 5 3.00 2980 \n", "4384 2423029245 20140617T000000 550000.0 3 1.75 2240 \n", "5795 2473370050 20140604T000000 327500.0 4 1.75 1650 \n", "4956 9528104985 20141104T000000 611000.0 2 1.00 1270 \n", "7723 3972900025 20150313T000000 499000.0 6 1.75 2400 \n", "... ... ... ... ... ... ... \n", "8517 3876600120 20150422T000000 265000.0 3 1.50 1780 \n", "6914 6821600005 20150403T000000 710000.0 4 1.75 2120 \n", "4499 2767603931 20140818T000000 469000.0 3 3.25 1370 \n", "8651 8802400411 20140619T000000 249000.0 3 1.00 1050 \n", "4234 5452800735 20140722T000000 780000.0 4 2.50 2270 \n", "\n", " sqft_lot floors waterfront view ... sqft_above sqft_basement \\\n", "5341 10064 1.0 0 0 ... 1680 1300 \n", "4384 78225 2.0 0 0 ... 2240 0 \n", "5795 7800 1.0 0 0 ... 1650 0 \n", "4956 5100 1.0 0 0 ... 1100 170 \n", "7723 7500 1.5 0 0 ... 1400 1000 \n", "... ... ... ... ... ... ... ... \n", "8517 10196 1.0 0 0 ... 1270 510 \n", "6914 5400 1.0 0 0 ... 1060 1060 \n", "4499 1194 3.0 0 0 ... 1370 0 \n", "8651 8498 1.0 0 0 ... 1050 0 \n", "4234 13449 1.0 0 0 ... 1310 960 \n", "\n", " yr_built yr_renovated zipcode lat long sqft_living15 \\\n", "5341 1940 0 98155 47.7372 -122.316 1590 \n", "4384 1976 0 98070 47.4638 -122.484 2030 \n", "5795 1968 0 98058 47.4507 -122.139 1750 \n", "4956 1900 0 98115 47.6771 -122.328 1670 \n", "7723 1975 0 98155 47.7661 -122.313 1980 \n", "... ... ... ... ... ... ... \n", "8517 1967 0 98001 47.3375 -122.291 1320 \n", "6914 1941 0 98199 47.6501 -122.395 2052 \n", "4499 2004 0 98107 47.6718 -122.388 1800 \n", "8651 1959 0 98031 47.4043 -122.202 1050 \n", "4234 1975 0 98040 47.5416 -122.232 2810 \n", "\n", " sqft_lot15 price_category \n", "5341 7800 middle \n", "4384 202554 middle \n", "5795 10400 low \n", "4956 3900 high \n", "7723 7500 middle \n", "... ... ... \n", "8517 7875 low \n", "6914 6000 high \n", "4499 2678 middle \n", "8651 8498 low \n", "4234 13475 high \n", "\n", "[2000 rows x 22 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'y_test'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
price_category
5341middle
4384middle
5795low
4956high
7723middle
......
8517low
6914high
4499middle
8651low
4234high
\n", "

2000 rows × 1 columns

\n", "
" ], "text/plain": [ " price_category\n", "5341 middle\n", "4384 middle\n", "5795 low\n", "4956 high\n", "7723 middle\n", "... ...\n", "8517 low\n", "6914 high\n", "4499 middle\n", "8651 low\n", "4234 high\n", "\n", "[2000 rows x 1 columns]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from typing import Tuple\n", "import pandas as pd\n", "from pandas import DataFrame\n", "from sklearn.model_selection import train_test_split\n", "\n", "def split_stratified_into_train_val_test(\n", " df_input,\n", " stratify_colname=\"y\",\n", " frac_train=0.6,\n", " frac_val=0.15,\n", " frac_test=0.25,\n", " random_state=None,\n", ") -> Tuple[DataFrame, DataFrame, DataFrame, DataFrame, DataFrame, DataFrame]:\n", " \n", " if frac_train + frac_val + frac_test != 1.0:\n", " raise ValueError(\n", " \"fractions %f, %f, %f do not add up to 1.0\"\n", " % (frac_train, frac_val, frac_test)\n", " )\n", " if stratify_colname not in df_input.columns:\n", " raise ValueError(\"%s is not a column in the dataframe\" % (stratify_colname))\n", " X = df_input # Contains all columns.\n", " y = df_input[\n", " [stratify_colname]\n", " ] # Dataframe of just the column on which to stratify.\n", " # Split original dataframe into train and temp dataframes.\n", " df_train, df_temp, y_train, y_temp = train_test_split(\n", " X, y, stratify=y, test_size=(1.0 - frac_train), random_state=random_state\n", " )\n", " if frac_val <= 0:\n", " assert len(df_input) == len(df_train) + len(df_temp)\n", " return df_train, pd.DataFrame(), df_temp, y_train, pd.DataFrame(), y_temp\n", " # Split the temp dataframe into val and test dataframes.\n", " relative_frac_test = frac_test / (frac_val + frac_test)\n", " df_val, df_test, y_val, y_test = train_test_split(\n", " df_temp,\n", " y_temp,\n", " stratify=y_temp,\n", " test_size=relative_frac_test,\n", " random_state=random_state,\n", " )\n", " assert len(df_input) == len(df_train) + len(df_val) + len(df_test)\n", " return df_train, df_val, df_test, y_train, y_val, y_test\n", "\n", "X_train, X_val, X_test, y_train, y_val, y_test = split_stratified_into_train_val_test(\n", " df, stratify_colname=\"price_category\", frac_train=0.80, frac_val=0, frac_test=0.20, random_state=42\n", ")\n", "\n", "display(\"X_train\", X_train)\n", "display(\"y_train\", y_train)\n", "\n", "display(\"X_test\", X_test)\n", "display(\"y_test\", y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Формирование конвейера\n", "preprocessing_num -- конвейер для обработки числовых данных: заполнение пропущенных значений и стандартизация\n", "\n", "preprocessing_cat -- конвейер для обработки категориальных данных: заполнение пропущенных данных и унитарное кодирование\n", "\n", "features_preprocessing -- трансформер для предобработки признаков\n", "\n", "drop_columns -- трансформер для удаления колонок\n", "\n", "pipeline_end -- основной конвейер предобработки данных и конструирования признаков" ] }, { "cell_type": "code", "execution_count": 316, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idpricebedroomsbathroomssqft_livingsqft_lotfloorsconditiongradesqft_above...yr_renovatedzipcodelatlongsqft_living15sqft_lot15price_hprice_lprice_mprice_vh
0-0.4511030.9163810.7005590.5734160.081706-0.187493-0.8387390.839159-0.512647-0.638064...-0.2158-1.3499620.322540.3405930.223199-0.2105841.00.00.00.0
11.845014-0.589326-1.49426-0.72971-1.191326-0.3029991.120073-0.666734-0.512647-0.969739...-0.21580.8206560.417588-0.601419-1.022503-0.4219660.00.01.00.0
2-0.388708-1.184213-0.396851-1.381273-1.0607590.101544-0.838739-0.666734-1.369558-0.822328...-0.21580.523819-0.059795-1.025683-0.889035-0.2084310.01.00.00.0
3-0.744020.051922-1.49426-1.381273-1.329512.686416-0.838739-0.666734-2.22647-1.125749...-0.2158-0.144063-1.221808-1.924549-0.8890354.6824440.00.01.00.0
41.018038-0.47276-0.3968510.247635-0.3208770.6081961.120073-0.666734-0.5126470.013003...-0.2158-0.236825-0.3392212.505062-0.1030561.3756040.00.01.00.0
5-0.083826-0.492858-0.3968511.550761-0.701698-0.3146723.078884-0.6667340.344264-0.416947...-0.21580.4681620.987875-0.903438-0.844546-0.4368540.00.01.00.0
60.301277-0.953091-0.3968510.573416-0.712579-0.180574-0.838739-0.666734-0.512647-0.773191...-0.2158-0.886155-1.2939870.254302-0.666588-0.2059920.01.00.00.0
7-0.086798-1.148038-1.49426-1.381273-1.25661-0.232501-0.838739-0.666734-1.369558-1.043445...-0.21580.523819-0.249176-1.018493-1.600865-0.2966860.01.00.00.0
8-0.824567-1.148038-1.49426-1.381273-1.0934-0.151740.1406670.839159-0.512647-0.859181...-0.2158-1.387066-1.937882-0.60861-0.636929-0.1373970.01.00.00.0
91.647935-0.7621652.8953780.8991980.963036-0.186442-0.838739-0.6667340.3442640.037571...-0.2158-1.016021-1.783519-0.8962470.208369-0.1863320.01.00.00.0
10-1.159614-0.581287-1.49426-1.381273-1.321893-0.185096-0.8387390.839159-1.369558-1.11715...-0.2158-0.8304980.8377990.304638-0.355163-0.1307960.00.01.00.0
11-1.329183-0.681775-1.49426-1.381273-1.071639-0.200575-0.8387390.839159-0.512647-0.834612...-0.21581.0247311.226566-1.025683-0.444141-0.2024040.01.00.00.0
120.3778640.2869260.7005590.5734160.4190050.2563791.120073-0.6667340.3442640.848334...-0.2158-0.9232591.277306-0.1699630.742242-0.0717790.00.01.00.0
130.289882-0.88677-0.3968510.5734160.103467-0.143853-0.838739-0.6667340.344264-0.244967...-0.21582.045107-0.729417-0.428836-0.043737-0.1553350.01.00.00.0
141.6130490.282907-0.396851-0.0781470.103467-0.259422-0.838739-0.6667340.344264-0.822328...-0.21580.7278940.868529-1.2773660.223199-0.3383030.00.01.00.0
15-0.9628850.2851180.7005590.5734160.005542-0.183813-0.838739-0.6667340.344264-0.380094...-0.2158-0.4780041.1958370.786430.445646-0.1805920.00.01.00.0
161.722145-0.259726-0.396851-0.403928-0.571131-0.18865-0.8387390.839159-0.512647-0.269535...-0.2158-0.8119451.2229930.168011-0.666588-0.2130950.00.01.00.0
170.7405621.5892470.7005591.5507612.8780250.4668431.120073-0.6667342.0580872.052192...-0.2158-1.3499620.6048250.3405932.4624980.794340.00.00.01.0
18-1.555659-0.922945-0.396851-1.381273-0.799624-0.107784-0.838739-0.666734-0.512647-0.527505...-0.21581.4328811.536008-0.644564-0.978014-0.1833540.01.00.00.0
19-0.9537380.1422242.8953781.2249790.8868724.001461.120073-0.666734-0.5126470.713207...4.605736-0.663527-1.1353350.858340.5939441.6591690.00.01.00.0
\n", "

20 rows × 22 columns

\n", "
" ], "text/plain": [ " id price bedrooms bathrooms sqft_living sqft_lot floors \\\n", "0 -0.451103 0.916381 0.700559 0.573416 0.081706 -0.187493 -0.838739 \n", "1 1.845014 -0.589326 -1.49426 -0.72971 -1.191326 -0.302999 1.120073 \n", "2 -0.388708 -1.184213 -0.396851 -1.381273 -1.060759 0.101544 -0.838739 \n", "3 -0.74402 0.051922 -1.49426 -1.381273 -1.32951 2.686416 -0.838739 \n", "4 1.018038 -0.47276 -0.396851 0.247635 -0.320877 0.608196 1.120073 \n", "5 -0.083826 -0.492858 -0.396851 1.550761 -0.701698 -0.314672 3.078884 \n", "6 0.301277 -0.953091 -0.396851 0.573416 -0.712579 -0.180574 -0.838739 \n", "7 -0.086798 -1.148038 -1.49426 -1.381273 -1.25661 -0.232501 -0.838739 \n", "8 -0.824567 -1.148038 -1.49426 -1.381273 -1.0934 -0.15174 0.140667 \n", "9 1.647935 -0.762165 2.895378 0.899198 0.963036 -0.186442 -0.838739 \n", "10 -1.159614 -0.581287 -1.49426 -1.381273 -1.321893 -0.185096 -0.838739 \n", "11 -1.329183 -0.681775 -1.49426 -1.381273 -1.071639 -0.200575 -0.838739 \n", "12 0.377864 0.286926 0.700559 0.573416 0.419005 0.256379 1.120073 \n", "13 0.289882 -0.88677 -0.396851 0.573416 0.103467 -0.143853 -0.838739 \n", "14 1.613049 0.282907 -0.396851 -0.078147 0.103467 -0.259422 -0.838739 \n", "15 -0.962885 0.285118 0.700559 0.573416 0.005542 -0.183813 -0.838739 \n", "16 1.722145 -0.259726 -0.396851 -0.403928 -0.571131 -0.18865 -0.838739 \n", "17 0.740562 1.589247 0.700559 1.550761 2.878025 0.466843 1.120073 \n", "18 -1.555659 -0.922945 -0.396851 -1.381273 -0.799624 -0.107784 -0.838739 \n", "19 -0.953738 0.142224 2.895378 1.224979 0.886872 4.00146 1.120073 \n", "\n", " condition grade sqft_above ... yr_renovated zipcode lat \\\n", "0 0.839159 -0.512647 -0.638064 ... -0.2158 -1.349962 0.32254 \n", "1 -0.666734 -0.512647 -0.969739 ... -0.2158 0.820656 0.417588 \n", "2 -0.666734 -1.369558 -0.822328 ... -0.2158 0.523819 -0.059795 \n", "3 -0.666734 -2.22647 -1.125749 ... -0.2158 -0.144063 -1.221808 \n", "4 -0.666734 -0.512647 0.013003 ... -0.2158 -0.236825 -0.339221 \n", "5 -0.666734 0.344264 -0.416947 ... -0.2158 0.468162 0.987875 \n", "6 -0.666734 -0.512647 -0.773191 ... -0.2158 -0.886155 -1.293987 \n", "7 -0.666734 -1.369558 -1.043445 ... -0.2158 0.523819 -0.249176 \n", "8 0.839159 -0.512647 -0.859181 ... -0.2158 -1.387066 -1.937882 \n", "9 -0.666734 0.344264 0.037571 ... -0.2158 -1.016021 -1.783519 \n", "10 0.839159 -1.369558 -1.11715 ... -0.2158 -0.830498 0.837799 \n", "11 0.839159 -0.512647 -0.834612 ... -0.2158 1.024731 1.226566 \n", "12 -0.666734 0.344264 0.848334 ... -0.2158 -0.923259 1.277306 \n", "13 -0.666734 0.344264 -0.244967 ... -0.2158 2.045107 -0.729417 \n", "14 -0.666734 0.344264 -0.822328 ... -0.2158 0.727894 0.868529 \n", "15 -0.666734 0.344264 -0.380094 ... -0.2158 -0.478004 1.195837 \n", "16 0.839159 -0.512647 -0.269535 ... -0.2158 -0.811945 1.222993 \n", "17 -0.666734 2.058087 2.052192 ... -0.2158 -1.349962 0.604825 \n", "18 -0.666734 -0.512647 -0.527505 ... -0.2158 1.432881 1.536008 \n", "19 -0.666734 -0.512647 0.713207 ... 4.605736 -0.663527 -1.135335 \n", "\n", " long sqft_living15 sqft_lot15 price_h price_l price_m price_vh \n", "0 0.340593 0.223199 -0.210584 1.0 0.0 0.0 0.0 \n", "1 -0.601419 -1.022503 -0.421966 0.0 0.0 1.0 0.0 \n", "2 -1.025683 -0.889035 -0.208431 0.0 1.0 0.0 0.0 \n", "3 -1.924549 -0.889035 4.682444 0.0 0.0 1.0 0.0 \n", "4 2.505062 -0.103056 1.375604 0.0 0.0 1.0 0.0 \n", "5 -0.903438 -0.844546 -0.436854 0.0 0.0 1.0 0.0 \n", "6 0.254302 -0.666588 -0.205992 0.0 1.0 0.0 0.0 \n", "7 -1.018493 -1.600865 -0.296686 0.0 1.0 0.0 0.0 \n", "8 -0.60861 -0.636929 -0.137397 0.0 1.0 0.0 0.0 \n", "9 -0.896247 0.208369 -0.186332 0.0 1.0 0.0 0.0 \n", "10 0.304638 -0.355163 -0.130796 0.0 0.0 1.0 0.0 \n", "11 -1.025683 -0.444141 -0.202404 0.0 1.0 0.0 0.0 \n", "12 -0.169963 0.742242 -0.071779 0.0 0.0 1.0 0.0 \n", "13 -0.428836 -0.043737 -0.155335 0.0 1.0 0.0 0.0 \n", "14 -1.277366 0.223199 -0.338303 0.0 0.0 1.0 0.0 \n", "15 0.78643 0.445646 -0.180592 0.0 0.0 1.0 0.0 \n", "16 0.168011 -0.666588 -0.213095 0.0 0.0 1.0 0.0 \n", "17 0.340593 2.462498 0.79434 0.0 0.0 0.0 1.0 \n", "18 -0.644564 -0.978014 -0.183354 0.0 1.0 0.0 0.0 \n", "19 0.85834 0.593944 1.659169 0.0 0.0 1.0 0.0 \n", "\n", "[20 rows x 22 columns]" ] }, "execution_count": 316, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from sklearn.base import BaseEstimator, TransformerMixin\n", "from sklearn.compose import ColumnTransformer\n", "from sklearn.discriminant_analysis import StandardScaler\n", "from sklearn.impute import SimpleImputer\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import OneHotEncoder\n", "from sklearn.ensemble import RandomForestRegressor # Пример регрессионной модели\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.pipeline import make_pipeline\n", "\n", "random_state = 42\n", "\n", "# Указываем столбцы, которые нужно удалить и обрабатывать\n", "columns_to_drop = [\"date\", \"view\", \"waterfront\"]\n", "num_columns = [\n", " column\n", " for column in df.columns\n", " if column not in columns_to_drop and df[column].dtype != \"object\" and df[column].dtype != \"category\"\n", "]\n", "cat_columns = [\n", " column\n", " for column in df.columns\n", " if column not in columns_to_drop and df[column].dtype == \"object\" or df[column].dtype == \"category\"\n", "]\n", "\n", "# Определяем предобработку для численных данных\n", "num_imputer = SimpleImputer(strategy=\"median\")\n", "num_scaler = StandardScaler()\n", "preprocessing_num = Pipeline(\n", " [\n", " (\"imputer\", num_imputer),\n", " (\"scaler\", num_scaler),\n", " ]\n", ")\n", "\n", "# Определяем предобработку для категориальных данных\n", "cat_imputer = SimpleImputer(strategy=\"constant\")\n", "cat_encoder = OneHotEncoder(handle_unknown=\"ignore\", sparse_output=False)\n", "preprocessing_cat = Pipeline(\n", " [\n", " (\"imputer\", cat_imputer),\n", " (\"encoder\", cat_encoder),\n", " ]\n", ")\n", "\n", "features_preprocessing = ColumnTransformer(\n", " verbose_feature_names_out=False,\n", " transformers=[\n", " (\"prepocessing_num\", preprocessing_num, num_columns),\n", " (\"prepocessing_cat\", preprocessing_cat, cat_columns),\n", " (\"prepocessing_features\", cat_imputer, [\"price_category\"]),\n", " ],\n", " remainder=\"passthrough\"\n", ")\n", "\n", "drop_columns = ColumnTransformer(\n", " verbose_feature_names_out=False,\n", " transformers=[\n", " (\"drop_columns\", \"drop\", columns_to_drop),\n", " ],\n", " remainder=\"passthrough\",\n", ")\n", "\n", "features_postprocessing = ColumnTransformer(\n", " verbose_feature_names_out=False,\n", " transformers=[\n", " (\"prepocessing_cat\", preprocessing_cat, [\"price_category\"]),\n", " ],\n", " remainder=\"passthrough\",\n", ")\n", "\n", "pipeline_end = Pipeline(\n", " [\n", " (\"features_preprocessing\", features_preprocessing),\n", " (\"drop_columns\", drop_columns),\n", " (\"features_postprocessing\", features_postprocessing),\n", " ]\n", "\n", ")\n", "cols = ['price_h', 'price_l', 'price_m', 'price_vh']\n", "preprocessing_result = features_preprocessing.fit_transform(X_train)\n", "preprocessing_result = pd.DataFrame(preprocessing_result, columns=num_columns + cat_columns + cols + columns_to_drop)\n", "\n", "preprocessing_result = drop_columns.fit_transform(preprocessing_result)\n", "preprocessing_result = pd.DataFrame(preprocessing_result, columns=num_columns + cols + cat_columns)\n", "\n", "preprocessing_result = preprocessing_result.drop(columns=[\"price_category\"])\n", "preprocessing_result.head(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Формирование набора моделей для классификации¶\n", "logistic -- логистическая регрессия\n", "\n", "ridge -- гребневая регрессия\n", "\n", "decision_tree -- дерево решений\n", "\n", "knn -- k-ближайших соседей\n", "\n", "naive_bayes -- наивный Байесовский классификатор\n", "\n", "gradient_boosting -- метод градиентного бустинга (набор деревьев решений)\n", "\n", "random_forest -- метод случайного леса (набор деревьев решений)\n", "\n", "mlp -- многослойный персептрон (нейронная сеть)" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [], "source": [ "from sklearn import ensemble, linear_model, naive_bayes, neighbors, neural_network, tree\n", "\n", "class_models = {\n", " \"logistic\": {\"model\": linear_model.LogisticRegression()},\n", " # \"ridge\": {\"model\": linear_model.RidgeClassifierCV(cv=5, class_weight=\"balanced\")},\n", " \"ridge\": {\"model\": linear_model.LogisticRegression(penalty=\"l2\", class_weight=\"balanced\")},\n", " \"decision_tree\": {\n", " \"model\": tree.DecisionTreeClassifier(max_depth=7, random_state=random_state)\n", " },\n", " \"knn\": {\"model\": neighbors.KNeighborsClassifier(n_neighbors=7)},\n", " \"naive_bayes\": {\"model\": naive_bayes.GaussianNB()},\n", " \"gradient_boosting\": {\n", " \"model\": ensemble.GradientBoostingClassifier(n_estimators=210)\n", " },\n", " \"random_forest\": {\n", " \"model\": ensemble.RandomForestClassifier(\n", " max_depth=11, class_weight=\"balanced\", random_state=random_state\n", " )\n", " },\n", " \"mlp\": {\n", " \"model\": neural_network.MLPClassifier(\n", " hidden_layer_sizes=(7,),\n", " max_iter=500,\n", " early_stopping=True,\n", " random_state=random_state,\n", " )\n", " },\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Обучение моделей на обучающем наборе данных и оценка на тестовом" ] }, { "cell_type": "code", "execution_count": 320, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: logistic\n" ] }, { "ename": "ValueError", "evalue": "Specifying the columns using strings is only supported for dataframes.", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\utils\\_indexing.py:338\u001b[0m, in \u001b[0;36m_get_column_indices\u001b[1;34m(X, key)\u001b[0m\n\u001b[0;32m 337\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 338\u001b[0m all_columns \u001b[38;5;241m=\u001b[39m \u001b[43mX\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolumns\u001b[49m\n\u001b[0;32m 339\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m:\n", "\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'columns'", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[320], line 9\u001b[0m\n\u001b[0;32m 6\u001b[0m model \u001b[38;5;241m=\u001b[39m class_models[model_name][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m 8\u001b[0m model_pipeline \u001b[38;5;241m=\u001b[39m Pipeline([(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpipeline\u001b[39m\u001b[38;5;124m\"\u001b[39m, pipeline_end), (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m, model)])\n\u001b[1;32m----> 9\u001b[0m model_pipeline \u001b[38;5;241m=\u001b[39m \u001b[43mmodel_pipeline\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX_train\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my_train\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mravel\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 11\u001b[0m y_train_predict \u001b[38;5;241m=\u001b[39m model_pipeline\u001b[38;5;241m.\u001b[39mpredict(X_train)\n\u001b[0;32m 12\u001b[0m y_test_probs \u001b[38;5;241m=\u001b[39m model_pipeline\u001b[38;5;241m.\u001b[39mpredict_proba(X_test)[:, \u001b[38;5;241m1\u001b[39m]\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\base.py:1473\u001b[0m, in \u001b[0;36m_fit_context..decorator..wrapper\u001b[1;34m(estimator, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1466\u001b[0m estimator\u001b[38;5;241m.\u001b[39m_validate_params()\n\u001b[0;32m 1468\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m config_context(\n\u001b[0;32m 1469\u001b[0m skip_parameter_validation\u001b[38;5;241m=\u001b[39m(\n\u001b[0;32m 1470\u001b[0m prefer_skip_nested_validation \u001b[38;5;129;01mor\u001b[39;00m global_skip_validation\n\u001b[0;32m 1471\u001b[0m )\n\u001b[0;32m 1472\u001b[0m ):\n\u001b[1;32m-> 1473\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfit_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43mestimator\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\pipeline.py:469\u001b[0m, in \u001b[0;36mPipeline.fit\u001b[1;34m(self, X, y, **params)\u001b[0m\n\u001b[0;32m 426\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Fit the model.\u001b[39;00m\n\u001b[0;32m 427\u001b[0m \n\u001b[0;32m 428\u001b[0m \u001b[38;5;124;03mFit all the transformers one after the other and sequentially transform the\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 466\u001b[0m \u001b[38;5;124;03m Pipeline with fitted steps.\u001b[39;00m\n\u001b[0;32m 467\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 468\u001b[0m routed_params \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_method_params(method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit\u001b[39m\u001b[38;5;124m\"\u001b[39m, props\u001b[38;5;241m=\u001b[39mparams)\n\u001b[1;32m--> 469\u001b[0m Xt \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_fit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrouted_params\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 470\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m _print_elapsed_time(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPipeline\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_log_message(\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msteps) \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m)):\n\u001b[0;32m 471\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_final_estimator \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpassthrough\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\pipeline.py:406\u001b[0m, in \u001b[0;36mPipeline._fit\u001b[1;34m(self, X, y, routed_params)\u001b[0m\n\u001b[0;32m 404\u001b[0m cloned_transformer \u001b[38;5;241m=\u001b[39m clone(transformer)\n\u001b[0;32m 405\u001b[0m \u001b[38;5;66;03m# Fit or load from cache the current transformer\u001b[39;00m\n\u001b[1;32m--> 406\u001b[0m X, fitted_transformer \u001b[38;5;241m=\u001b[39m \u001b[43mfit_transform_one_cached\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 407\u001b[0m \u001b[43m \u001b[49m\u001b[43mcloned_transformer\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 408\u001b[0m \u001b[43m \u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 409\u001b[0m \u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 410\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 411\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessage_clsname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mPipeline\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 412\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessage\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_log_message\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstep_idx\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 413\u001b[0m \u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrouted_params\u001b[49m\u001b[43m[\u001b[49m\u001b[43mname\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 414\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 415\u001b[0m \u001b[38;5;66;03m# Replace the transformer of the step with the fitted\u001b[39;00m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;66;03m# transformer. This is necessary when loading the transformer\u001b[39;00m\n\u001b[0;32m 417\u001b[0m \u001b[38;5;66;03m# from the cache.\u001b[39;00m\n\u001b[0;32m 418\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msteps[step_idx] \u001b[38;5;241m=\u001b[39m (name, fitted_transformer)\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\joblib\\memory.py:312\u001b[0m, in \u001b[0;36mNotMemorizedFunc.__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 311\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m--> 312\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\pipeline.py:1310\u001b[0m, in \u001b[0;36m_fit_transform_one\u001b[1;34m(transformer, X, y, weight, message_clsname, message, params)\u001b[0m\n\u001b[0;32m 1308\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m _print_elapsed_time(message_clsname, message):\n\u001b[0;32m 1309\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(transformer, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit_transform\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m-> 1310\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mtransformer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit_transform\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfit_transform\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1311\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 1312\u001b[0m res \u001b[38;5;241m=\u001b[39m transformer\u001b[38;5;241m.\u001b[39mfit(X, y, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit\u001b[39m\u001b[38;5;124m\"\u001b[39m, {}))\u001b[38;5;241m.\u001b[39mtransform(\n\u001b[0;32m 1313\u001b[0m X, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtransform\u001b[39m\u001b[38;5;124m\"\u001b[39m, {})\n\u001b[0;32m 1314\u001b[0m )\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\base.py:1473\u001b[0m, in \u001b[0;36m_fit_context..decorator..wrapper\u001b[1;34m(estimator, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1466\u001b[0m estimator\u001b[38;5;241m.\u001b[39m_validate_params()\n\u001b[0;32m 1468\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m config_context(\n\u001b[0;32m 1469\u001b[0m skip_parameter_validation\u001b[38;5;241m=\u001b[39m(\n\u001b[0;32m 1470\u001b[0m prefer_skip_nested_validation \u001b[38;5;129;01mor\u001b[39;00m global_skip_validation\n\u001b[0;32m 1471\u001b[0m )\n\u001b[0;32m 1472\u001b[0m ):\n\u001b[1;32m-> 1473\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfit_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43mestimator\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\pipeline.py:533\u001b[0m, in \u001b[0;36mPipeline.fit_transform\u001b[1;34m(self, X, y, **params)\u001b[0m\n\u001b[0;32m 490\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Fit the model and transform with the final estimator.\u001b[39;00m\n\u001b[0;32m 491\u001b[0m \n\u001b[0;32m 492\u001b[0m \u001b[38;5;124;03mFit all the transformers one after the other and sequentially transform\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 530\u001b[0m \u001b[38;5;124;03m Transformed samples.\u001b[39;00m\n\u001b[0;32m 531\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 532\u001b[0m routed_params \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_method_params(method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit_transform\u001b[39m\u001b[38;5;124m\"\u001b[39m, props\u001b[38;5;241m=\u001b[39mparams)\n\u001b[1;32m--> 533\u001b[0m Xt \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_fit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrouted_params\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 535\u001b[0m last_step \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_final_estimator\n\u001b[0;32m 536\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m _print_elapsed_time(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPipeline\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_log_message(\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msteps) \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m)):\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\pipeline.py:406\u001b[0m, in \u001b[0;36mPipeline._fit\u001b[1;34m(self, X, y, routed_params)\u001b[0m\n\u001b[0;32m 404\u001b[0m cloned_transformer \u001b[38;5;241m=\u001b[39m clone(transformer)\n\u001b[0;32m 405\u001b[0m \u001b[38;5;66;03m# Fit or load from cache the current transformer\u001b[39;00m\n\u001b[1;32m--> 406\u001b[0m X, fitted_transformer \u001b[38;5;241m=\u001b[39m \u001b[43mfit_transform_one_cached\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 407\u001b[0m \u001b[43m \u001b[49m\u001b[43mcloned_transformer\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 408\u001b[0m \u001b[43m \u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 409\u001b[0m \u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 410\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 411\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessage_clsname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mPipeline\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 412\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessage\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_log_message\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstep_idx\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 413\u001b[0m \u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrouted_params\u001b[49m\u001b[43m[\u001b[49m\u001b[43mname\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 414\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 415\u001b[0m \u001b[38;5;66;03m# Replace the transformer of the step with the fitted\u001b[39;00m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;66;03m# transformer. This is necessary when loading the transformer\u001b[39;00m\n\u001b[0;32m 417\u001b[0m \u001b[38;5;66;03m# from the cache.\u001b[39;00m\n\u001b[0;32m 418\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msteps[step_idx] \u001b[38;5;241m=\u001b[39m (name, fitted_transformer)\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\joblib\\memory.py:312\u001b[0m, in \u001b[0;36mNotMemorizedFunc.__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 311\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m--> 312\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\pipeline.py:1310\u001b[0m, in \u001b[0;36m_fit_transform_one\u001b[1;34m(transformer, X, y, weight, message_clsname, message, params)\u001b[0m\n\u001b[0;32m 1308\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m _print_elapsed_time(message_clsname, message):\n\u001b[0;32m 1309\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(transformer, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit_transform\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m-> 1310\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mtransformer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit_transform\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfit_transform\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1311\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 1312\u001b[0m res \u001b[38;5;241m=\u001b[39m transformer\u001b[38;5;241m.\u001b[39mfit(X, y, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit\u001b[39m\u001b[38;5;124m\"\u001b[39m, {}))\u001b[38;5;241m.\u001b[39mtransform(\n\u001b[0;32m 1313\u001b[0m X, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtransform\u001b[39m\u001b[38;5;124m\"\u001b[39m, {})\n\u001b[0;32m 1314\u001b[0m )\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\utils\\_set_output.py:316\u001b[0m, in \u001b[0;36m_wrap_method_output..wrapped\u001b[1;34m(self, X, *args, **kwargs)\u001b[0m\n\u001b[0;32m 314\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(f)\n\u001b[0;32m 315\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mwrapped\u001b[39m(\u001b[38;5;28mself\u001b[39m, X, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m--> 316\u001b[0m data_to_wrap \u001b[38;5;241m=\u001b[39m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data_to_wrap, \u001b[38;5;28mtuple\u001b[39m):\n\u001b[0;32m 318\u001b[0m \u001b[38;5;66;03m# only wrap the first output for cross decomposition\u001b[39;00m\n\u001b[0;32m 319\u001b[0m return_tuple \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m 320\u001b[0m _wrap_data_with_container(method, data_to_wrap[\u001b[38;5;241m0\u001b[39m], X, \u001b[38;5;28mself\u001b[39m),\n\u001b[0;32m 321\u001b[0m \u001b[38;5;241m*\u001b[39mdata_to_wrap[\u001b[38;5;241m1\u001b[39m:],\n\u001b[0;32m 322\u001b[0m )\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\base.py:1473\u001b[0m, in \u001b[0;36m_fit_context..decorator..wrapper\u001b[1;34m(estimator, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1466\u001b[0m estimator\u001b[38;5;241m.\u001b[39m_validate_params()\n\u001b[0;32m 1468\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m config_context(\n\u001b[0;32m 1469\u001b[0m skip_parameter_validation\u001b[38;5;241m=\u001b[39m(\n\u001b[0;32m 1470\u001b[0m prefer_skip_nested_validation \u001b[38;5;129;01mor\u001b[39;00m global_skip_validation\n\u001b[0;32m 1471\u001b[0m )\n\u001b[0;32m 1472\u001b[0m ):\n\u001b[1;32m-> 1473\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfit_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43mestimator\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:968\u001b[0m, in \u001b[0;36mColumnTransformer.fit_transform\u001b[1;34m(self, X, y, **params)\u001b[0m\n\u001b[0;32m 965\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_transformers()\n\u001b[0;32m 966\u001b[0m n_samples \u001b[38;5;241m=\u001b[39m _num_samples(X)\n\u001b[1;32m--> 968\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_validate_column_callables\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 969\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_remainder(X)\n\u001b[0;32m 971\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _routing_enabled():\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:536\u001b[0m, in \u001b[0;36mColumnTransformer._validate_column_callables\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m 534\u001b[0m columns \u001b[38;5;241m=\u001b[39m columns(X)\n\u001b[0;32m 535\u001b[0m all_columns\u001b[38;5;241m.\u001b[39mappend(columns)\n\u001b[1;32m--> 536\u001b[0m transformer_to_input_indices[name] \u001b[38;5;241m=\u001b[39m \u001b[43m_get_column_indices\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolumns\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 538\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_columns \u001b[38;5;241m=\u001b[39m all_columns\n\u001b[0;32m 539\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_transformer_to_input_indices \u001b[38;5;241m=\u001b[39m transformer_to_input_indices\n", "File \u001b[1;32md:\\Study\\3 курс 5 семестр\\AIM\\AIM-PIbd-31-Yakovlev-M-G\\kernel\\Lib\\site-packages\\sklearn\\utils\\_indexing.py:340\u001b[0m, in \u001b[0;36m_get_column_indices\u001b[1;34m(X, key)\u001b[0m\n\u001b[0;32m 338\u001b[0m all_columns \u001b[38;5;241m=\u001b[39m X\u001b[38;5;241m.\u001b[39mcolumns\n\u001b[0;32m 339\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m:\n\u001b[1;32m--> 340\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 341\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSpecifying the columns using strings is only supported for dataframes.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 342\u001b[0m )\n\u001b[0;32m 343\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, \u001b[38;5;28mstr\u001b[39m):\n\u001b[0;32m 344\u001b[0m columns \u001b[38;5;241m=\u001b[39m [key]\n", "\u001b[1;31mValueError\u001b[0m: Specifying the columns using strings is only supported for dataframes." ] } ], "source": [ "import numpy as np\n", "from sklearn import metrics\n", "\n", "for model_name in class_models.keys():\n", " print(f\"Model: {model_name}\")\n", " model = class_models[model_name][\"model\"]\n", "\n", " model_pipeline = Pipeline([(\"pipeline\", pipeline_end), (\"model\", model)])\n", " model_pipeline = model_pipeline.fit(X_train, y_train.values.ravel())\n", "\n", " y_train_predict = model_pipeline.predict(X_train)\n", " y_test_probs = model_pipeline.predict_proba(X_test)[:, 1]\n", " y_test_predict = np.where(y_test_probs > 0.5, 1, 0)\n", "\n", " class_models[model_name][\"pipeline\"] = model_pipeline\n", " class_models[model_name][\"probs\"] = y_test_probs\n", " class_models[model_name][\"preds\"] = y_test_predict\n", "\n", " class_models[model_name][\"Precision_train\"] = metrics.precision_score(\n", " y_train, y_train_predict\n", " )\n", " class_models[model_name][\"Precision_test\"] = metrics.precision_score(\n", " y_test, y_test_predict\n", " )\n", " class_models[model_name][\"Recall_train\"] = metrics.recall_score(\n", " y_train, y_train_predict\n", " )\n", " class_models[model_name][\"Recall_test\"] = metrics.recall_score(\n", " y_test, y_test_predict\n", " )\n", " class_models[model_name][\"Accuracy_train\"] = metrics.accuracy_score(\n", " y_train, y_train_predict\n", " )\n", " class_models[model_name][\"Accuracy_test\"] = metrics.accuracy_score(\n", " y_test, y_test_predict\n", " )\n", " class_models[model_name][\"ROC_AUC_test\"] = metrics.roc_auc_score(\n", " y_test, y_test_probs\n", " )\n", " class_models[model_name][\"F1_train\"] = metrics.f1_score(y_train, y_train_predict)\n", " class_models[model_name][\"F1_test\"] = metrics.f1_score(y_test, y_test_predict)\n", " class_models[model_name][\"MCC_test\"] = metrics.matthews_corrcoef(\n", " y_test, y_test_predict\n", " )\n", " class_models[model_name][\"Cohen_kappa_test\"] = metrics.cohen_kappa_score(\n", " y_test, y_test_predict\n", " )\n", " class_models[model_name][\"Confusion_matrix\"] = metrics.confusion_matrix(\n", " y_test, y_test_predict\n", " )" ] } ], "metadata": { "kernelspec": { "display_name": "kernel", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.5" } }, "nbformat": 4, "nbformat_minor": 2 }