{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.pipeline import Pipeline\nimport time \nfrom sklearn.metrics import  accuracy_score, roc_auc_score, roc_curve, confusion_matrix\nimport random\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom imblearn.over_sampling  import RandomOverSampler\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:07.243637Z","iopub.execute_input":"2022-09-28T12:36:07.244324Z","iopub.status.idle":"2022-09-28T12:36:08.344094Z","shell.execute_reply.started":"2022-09-28T12:36:07.244282Z","shell.execute_reply":"2022-09-28T12:36:08.343044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:13.375313Z","iopub.execute_input":"2022-09-28T12:36:13.375716Z","iopub.status.idle":"2022-09-28T12:36:13.391584Z","shell.execute_reply.started":"2022-09-28T12:36:13.375684Z","shell.execute_reply":"2022-09-28T12:36:13.390822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1 = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_2019_complete.csv')\ndata1","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:20.907343Z","iopub.execute_input":"2022-09-28T12:36:20.908338Z","iopub.status.idle":"2022-09-28T12:36:40.795520Z","shell.execute_reply.started":"2022-09-28T12:36:20.908293Z","shell.execute_reply":"2022-09-28T12:36:40.794422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## we see the data is more than 5 million record and my processor have  tender heart i want not break his heart 😂😂","metadata":{}},{"cell_type":"markdown","source":"## we have problem the columns names in data is missed so we try to treat this problem","metadata":{}},{"cell_type":"code","source":"List_of_columns_names=[    \"track_id\"   ,\"race_date\"   ,\"race_number\" ,  \"program_number\", \"trakus_index\" \n                      , \"latitude\",\"longitude\" ,\"distance_id\",\"course_type\",\"track_condition\",\"run_up_distance\"\n\n                       ,\"race_type\",\"purse\",\"post_time\",\"weight_carried\",\"jockey\", \"odds\",\"position_at_finish\"]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:40.797793Z","iopub.execute_input":"2022-09-28T12:36:40.798134Z","iopub.status.idle":"2022-09-28T12:36:40.803257Z","shell.execute_reply.started":"2022-09-28T12:36:40.798103Z","shell.execute_reply":"2022-09-28T12:36:40.802145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:40.805016Z","iopub.execute_input":"2022-09-28T12:36:40.805685Z","iopub.status.idle":"2022-09-28T12:36:40.816983Z","shell.execute_reply.started":"2022-09-28T12:36:40.805638Z","shell.execute_reply":"2022-09-28T12:36:40.815877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(List_of_columns_names,len(List_of_columns_names))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:40.820285Z","iopub.execute_input":"2022-09-28T12:36:40.820824Z","iopub.status.idle":"2022-09-28T12:36:40.828208Z","shell.execute_reply.started":"2022-09-28T12:36:40.820759Z","shell.execute_reply":"2022-09-28T12:36:40.827205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1.columns=List_of_columns_names\ndata1.head().style.background_gradient(cmap = \"crest_r\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:36:40.829406Z","iopub.execute_input":"2022-09-28T12:36:40.829831Z","iopub.status.idle":"2022-09-28T12:36:40.927322Z","shell.execute_reply.started":"2022-09-28T12:36:40.829789Z","shell.execute_reply":"2022-09-28T12:36:40.926282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## as we seen the is very big so we try to take random sample to make essay especially when make Data Viz and Model and pred.\n## so i will  take about 2.8% from this data","metadata":{}},{"cell_type":"code","source":"data2=data1.sample(150000)\ndata2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:09.420312Z","iopub.execute_input":"2022-09-28T12:38:09.420965Z","iopub.status.idle":"2022-09-28T12:38:09.893306Z","shell.execute_reply.started":"2022-09-28T12:38:09.420912Z","shell.execute_reply":"2022-09-28T12:38:09.892359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(150000/5225000)*100","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:11.688274Z","iopub.execute_input":"2022-09-28T12:38:11.689436Z","iopub.status.idle":"2022-09-28T12:38:11.697702Z","shell.execute_reply.started":"2022-09-28T12:38:11.689373Z","shell.execute_reply":"2022-09-28T12:38:11.696413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=0\nfor i in data1.columns:\n    if data1[i].dtype in   [\"object\"]:\n        print(f'{i},\"coulmn_value_count\"'+f\"num_of_coulmn=\\t{x}\"+\"\\t\"f\"num_of_items_in_each_coulmn\\t{len(data1[i].value_counts())})\\n\")\n        print(f\"\\n{data1[i].value_counts(normalize=True).mul(100).to_frame()}\")\n        x=x+1\n        print(\"*******************\")\n        \n    else:\n        continue;\n        ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:12.496201Z","iopub.execute_input":"2022-09-28T12:38:12.496592Z","iopub.status.idle":"2022-09-28T12:38:16.647717Z","shell.execute_reply.started":"2022-09-28T12:38:12.496560Z","shell.execute_reply":"2022-09-28T12:38:16.646562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=0\nfor i in data2.columns:\n    if data2[i].dtype in   [\"object\"]:\n        print(f'{i},\"coulmn_value_count\"'+f\"num_of_coulmn=\\t{x}\"+\"\\t\"f\"num_of_items_in_each_coulmn\\t{len(data2[i].value_counts())})\\n\")\n        print(f\"\\n{data2[i].value_counts(normalize=True).mul(100).to_frame()}\")\n        x=x+1\n        print(\"*******************\")\n        \n    else:\n        continue;","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:16.651423Z","iopub.execute_input":"2022-09-28T12:38:16.651829Z","iopub.status.idle":"2022-09-28T12:38:16.819375Z","shell.execute_reply.started":"2022-09-28T12:38:16.651794Z","shell.execute_reply":"2022-09-28T12:38:16.818194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## I make this code to make sure that this sample is representative the majority of data","metadata":{}},{"cell_type":"code","source":"data2.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:16.821853Z","iopub.execute_input":"2022-09-28T12:38:16.822842Z","iopub.status.idle":"2022-09-28T12:38:16.887354Z","shell.execute_reply.started":"2022-09-28T12:38:16.822799Z","shell.execute_reply":"2022-09-28T12:38:16.886324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# we can see the data don't have any NaN values ## we have the object columns and int and float and date","metadata":{}},{"cell_type":"code","source":"data2.describe()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:16.889326Z","iopub.execute_input":"2022-09-28T12:38:16.889641Z","iopub.status.idle":"2022-09-28T12:38:16.998521Z","shell.execute_reply.started":"2022-09-28T12:38:16.889612Z","shell.execute_reply":"2022-09-28T12:38:16.997316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## as we see the data don't have any extreme outlier except odds and purse","metadata":{}},{"cell_type":"code","source":"data2[\"purse\"].min()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:17.112304Z","iopub.execute_input":"2022-09-28T12:38:17.113031Z","iopub.status.idle":"2022-09-28T12:38:17.121443Z","shell.execute_reply.started":"2022-09-28T12:38:17.112978Z","shell.execute_reply":"2022-09-28T12:38:17.120390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[\"purse\"].quantile(.9620)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:27.211639Z","iopub.execute_input":"2022-09-28T12:38:27.212286Z","iopub.status.idle":"2022-09-28T12:38:27.223680Z","shell.execute_reply.started":"2022-09-28T12:38:27.212253Z","shell.execute_reply":"2022-09-28T12:38:27.222850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outlierdata=data2[data2[\"purse\"]>data2[\"purse\"].quantile(.962)]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-09-28T12:38:33.809688Z","iopub.execute_input":"2022-09-28T12:38:33.810485Z","iopub.status.idle":"2022-09-28T12:38:33.824160Z","shell.execute_reply.started":"2022-09-28T12:38:33.810444Z","shell.execute_reply":"2022-09-28T12:38:33.823213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[data2[\"purse\"]>data2[\"purse\"].quantile(.9621)][\"purse\"]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:36.256742Z","iopub.execute_input":"2022-09-28T12:38:36.257252Z","iopub.status.idle":"2022-09-28T12:38:36.278528Z","shell.execute_reply.started":"2022-09-28T12:38:36.257208Z","shell.execute_reply":"2022-09-28T12:38:36.277346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2.loc[data2[\"purse\"]>data2[\"purse\"].quantile(.9621),[\"purse\"]]=250000","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:40.177431Z","iopub.execute_input":"2022-09-28T12:38:40.177885Z","iopub.status.idle":"2022-09-28T12:38:40.194936Z","shell.execute_reply.started":"2022-09-28T12:38:40.177847Z","shell.execute_reply":"2022-09-28T12:38:40.193665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[data2[\"purse\"]>data2[\"purse\"].quantile(.9621)][\"purse\"].values","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:42.180748Z","iopub.execute_input":"2022-09-28T12:38:42.182370Z","iopub.status.idle":"2022-09-28T12:38:42.196554Z","shell.execute_reply.started":"2022-09-28T12:38:42.182301Z","shell.execute_reply":"2022-09-28T12:38:42.195553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[\"purse\"].quantile(1)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:46.601046Z","iopub.execute_input":"2022-09-28T12:38:46.601453Z","iopub.status.idle":"2022-09-28T12:38:46.613445Z","shell.execute_reply.started":"2022-09-28T12:38:46.601421Z","shell.execute_reply":"2022-09-28T12:38:46.612028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[\"purse\"].max()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:48.591033Z","iopub.execute_input":"2022-09-28T12:38:48.591438Z","iopub.status.idle":"2022-09-28T12:38:48.597947Z","shell.execute_reply.started":"2022-09-28T12:38:48.591404Z","shell.execute_reply":"2022-09-28T12:38:48.597062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2.odds.quantile(.95)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:52.265957Z","iopub.execute_input":"2022-09-28T12:38:52.266679Z","iopub.status.idle":"2022-09-28T12:38:52.281327Z","shell.execute_reply.started":"2022-09-28T12:38:52.266641Z","shell.execute_reply":"2022-09-28T12:38:52.279695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2.loc[data2[\"odds\"]>data2[\"odds\"].quantile(.95),[\"odds\"]]=5975","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:53.781216Z","iopub.execute_input":"2022-09-28T12:38:53.781880Z","iopub.status.idle":"2022-09-28T12:38:53.795197Z","shell.execute_reply.started":"2022-09-28T12:38:53.781833Z","shell.execute_reply":"2022-09-28T12:38:53.794025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2[\"odds\"].max()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:54.521195Z","iopub.execute_input":"2022-09-28T12:38:54.522238Z","iopub.status.idle":"2022-09-28T12:38:54.529627Z","shell.execute_reply.started":"2022-09-28T12:38:54.522197Z","shell.execute_reply":"2022-09-28T12:38:54.528260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## there are two columns outlier \n## 1- so i try to find balance between two things one of them is take biggest limit from data and two of them that the biggest values dont be very extream about mean and .963 is best case\n## notes it is very difficult like find balance between your work and your love 😞😞\n## 2- we note the odds columns is probability of win in race but there non logical values the 90% of data about 0 to 39% but the values after that\n## is non logical there are some values bigger than 100%\n## this dont occur except pay a bribe to persons and devices which puts odds  😂😂\n## the maximum limit odds is logical is 59# because if there are values bigger than that i choose this horse and jockey","metadata":{}},{"cell_type":"code","source":"data2.describe()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:56.828416Z","iopub.execute_input":"2022-09-28T12:38:56.829031Z","iopub.status.idle":"2022-09-28T12:38:56.933035Z","shell.execute_reply.started":"2022-09-28T12:38:56.828995Z","shell.execute_reply":"2022-09-28T12:38:56.932078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2.describe(include=\"object\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:57.509909Z","iopub.execute_input":"2022-09-28T12:38:57.510592Z","iopub.status.idle":"2022-09-28T12:38:57.664376Z","shell.execute_reply.started":"2022-09-28T12:38:57.510554Z","shell.execute_reply":"2022-09-28T12:38:57.663271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## we note the most frequent in object columns i feel lazy to write it","metadata":{}},{"cell_type":"code","source":"data3=data2.copy()\ndata3","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:38:59.956622Z","iopub.execute_input":"2022-09-28T12:38:59.957892Z","iopub.status.idle":"2022-09-28T12:38:59.997416Z","shell.execute_reply.started":"2022-09-28T12:38:59.957833Z","shell.execute_reply":"2022-09-28T12:38:59.996272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data3.jockey.value_counts(normalize=True).head(60)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:06.307081Z","iopub.execute_input":"2022-09-28T12:39:06.307500Z","iopub.status.idle":"2022-09-28T12:39:06.329310Z","shell.execute_reply.started":"2022-09-28T12:39:06.307468Z","shell.execute_reply":"2022-09-28T12:39:06.328494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## we can note that the jockey column include about 173 items and ## this is problem in Data Viz and Data pred. and i don't it is not\n## a good idea to drop it by holistic method so i think to make to every jockey number representative his freq","metadata":{}},{"cell_type":"code","source":"er=[]\nt=0","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:10.179117Z","iopub.execute_input":"2022-09-28T12:39:10.180229Z","iopub.status.idle":"2022-09-28T12:39:10.186010Z","shell.execute_reply.started":"2022-09-28T12:39:10.180176Z","shell.execute_reply":"2022-09-28T12:39:10.184243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"er=[]\nfor i in data3.jockey:\n    if i in [\"Manuel Franco\"]:\n        i=.8\n        er.append(i)\n    elif i in [\"Dylan Davis\"]:\n        i=.7\n        er.append(i)\n\n    elif i in [\"Jose Lezcano\"]:\n        i=.6\n        er.append(i)\n\n    elif i in [\"Junior Alvarado\" ,\"Irad Ortiz Jr. \" ]:\n        i=.5\n        er.append(i)\n\n    elif i in [\"Eric Cancel\",\"Eric Cancel\",\"Jose L. Ortiz \",\"Kendrick Carmouch\",\"Joel Rosario\",\n            \"Luis Saez\",]:\n        i=.4\n        er.append(i)\n\n    elif i in [\"Javier Castellano\",\"Benjamin Hernandez\",\"Rajiv Maragh\"]:\n        \n        i=.3\n        er.append(i)\n\n    elif i in [\"John R. Velazquez\",\"Luis R. Reyes \",\"Joey R. Martinez\",\"Reylu Gutierrez\"]:\n        i=.2\n        er.append(i)\n    else :\n    \n        t= random.randint(0,1)\n        i=t/10\n        er.append(i)\n\n\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:12.276055Z","iopub.execute_input":"2022-09-28T12:39:12.276443Z","iopub.status.idle":"2022-09-28T12:39:12.484354Z","shell.execute_reply.started":"2022-09-28T12:39:12.276412Z","shell.execute_reply":"2022-09-28T12:39:12.483346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(er)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:13.501596Z","iopub.execute_input":"2022-09-28T12:39:13.502010Z","iopub.status.idle":"2022-09-28T12:39:13.508757Z","shell.execute_reply.started":"2022-09-28T12:39:13.501976Z","shell.execute_reply":"2022-09-28T12:39:13.507947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data3[\"jockey_alternative\"]=er\ndata3","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:15.404347Z","iopub.execute_input":"2022-09-28T12:39:15.405024Z","iopub.status.idle":"2022-09-28T12:39:15.455459Z","shell.execute_reply.started":"2022-09-28T12:39:15.404984Z","shell.execute_reply":"2022-09-28T12:39:15.454626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data4=data3.drop(columns=[\"jockey\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:20.827403Z","iopub.execute_input":"2022-09-28T12:39:20.828445Z","iopub.status.idle":"2022-09-28T12:39:20.848289Z","shell.execute_reply.started":"2022-09-28T12:39:20.828403Z","shell.execute_reply":"2022-09-28T12:39:20.847091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data4","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:21.702285Z","iopub.execute_input":"2022-09-28T12:39:21.702699Z","iopub.status.idle":"2022-09-28T12:39:21.737203Z","shell.execute_reply.started":"2022-09-28T12:39:21.702662Z","shell.execute_reply":"2022-09-28T12:39:21.735867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data4[\"race_date\"]=pd.to_datetime(data4['race_date'])","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:35.971179Z","iopub.execute_input":"2022-09-28T12:39:35.971602Z","iopub.status.idle":"2022-09-28T12:39:36.017799Z","shell.execute_reply.started":"2022-09-28T12:39:35.971565Z","shell.execute_reply":"2022-09-28T12:39:36.016441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data4['Day'] = data4.race_date.dt.day\ndata4['Month'] = data4.race_date.dt.month\ndata4['Year'] = data4.race_date.dt.year\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:36.030410Z","iopub.execute_input":"2022-09-28T12:39:36.030858Z","iopub.status.idle":"2022-09-28T12:39:36.091319Z","shell.execute_reply.started":"2022-09-28T12:39:36.030822Z","shell.execute_reply":"2022-09-28T12:39:36.090226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5=data4.drop(columns=[\"race_date\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:20.119239Z","iopub.execute_input":"2022-09-28T12:40:20.119647Z","iopub.status.idle":"2022-09-28T12:40:20.136456Z","shell.execute_reply.started":"2022-09-28T12:40:20.119613Z","shell.execute_reply":"2022-09-28T12:40:20.135257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:23.983083Z","iopub.execute_input":"2022-09-28T12:40:23.984168Z","iopub.status.idle":"2022-09-28T12:40:24.016923Z","shell.execute_reply.started":"2022-09-28T12:40:23.984121Z","shell.execute_reply":"2022-09-28T12:40:24.016078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#data5.to_csv(\"F1.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:26.657078Z","iopub.execute_input":"2022-09-28T12:40:26.657903Z","iopub.status.idle":"2022-09-28T12:40:26.662902Z","shell.execute_reply.started":"2022-09-28T12:40:26.657859Z","shell.execute_reply":"2022-09-28T12:40:26.661629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:27.547435Z","iopub.execute_input":"2022-09-28T12:40:27.547877Z","iopub.status.idle":"2022-09-28T12:40:27.598030Z","shell.execute_reply.started":"2022-09-28T12:40:27.547840Z","shell.execute_reply":"2022-09-28T12:40:27.597032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data5=pd.read_csv(\"F1.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:39:51.176129Z","iopub.execute_input":"2022-09-28T12:39:51.176554Z","iopub.status.idle":"2022-09-28T12:39:51.181556Z","shell.execute_reply.started":"2022-09-28T12:39:51.176517Z","shell.execute_reply":"2022-09-28T12:39:51.180416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ## we try to reduce size of data to to speed up processing","metadata":{}},{"cell_type":"code","source":"num_col=[x for x in data5.columns if (data5[x].dtype) in [ \"float64\",\"int64\"]  ]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:41.993148Z","iopub.execute_input":"2022-09-28T12:40:41.994183Z","iopub.status.idle":"2022-09-28T12:40:42.000076Z","shell.execute_reply.started":"2022-09-28T12:40:41.994138Z","shell.execute_reply":"2022-09-28T12:40:41.998567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_col","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:44.166346Z","iopub.execute_input":"2022-09-28T12:40:44.166741Z","iopub.status.idle":"2022-09-28T12:40:44.174289Z","shell.execute_reply.started":"2022-09-28T12:40:44.166710Z","shell.execute_reply":"2022-09-28T12:40:44.173021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in num_col:\n    if i in [\"latitude\",\"longitude\"]:\n         data5[i]=data5[i].astype(\"float32\")\n    elif i in [\"jockey_alternative\"]:\n         data5[i]=data5[i].astype(\"float16\")\n    elif i in [\"purse\"]:\n        data5[i]=data5[i].astype(\"float32\")\n\n    else:\n        data5[i]=data5[i].astype(\"int16\")\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:57.109954Z","iopub.execute_input":"2022-09-28T12:40:57.110749Z","iopub.status.idle":"2022-09-28T12:40:57.146380Z","shell.execute_reply.started":"2022-09-28T12:40:57.110700Z","shell.execute_reply":"2022-09-28T12:40:57.145024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:40:59.697087Z","iopub.execute_input":"2022-09-28T12:40:59.697504Z","iopub.status.idle":"2022-09-28T12:40:59.752919Z","shell.execute_reply.started":"2022-09-28T12:40:59.697468Z","shell.execute_reply":"2022-09-28T12:40:59.751718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5.purse.max()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:41:05.853575Z","iopub.execute_input":"2022-09-28T12:41:05.854324Z","iopub.status.idle":"2022-09-28T12:41:05.862181Z","shell.execute_reply.started":"2022-09-28T12:41:05.854278Z","shell.execute_reply":"2022-09-28T12:41:05.860719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"cat_col=[x for x in data5.columns if x not in num_col]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:41:19.620643Z","iopub.execute_input":"2022-09-28T12:41:19.621470Z","iopub.status.idle":"2022-09-28T12:41:19.626628Z","shell.execute_reply.started":"2022-09-28T12:41:19.621431Z","shell.execute_reply":"2022-09-28T12:41:19.625703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5[cat_col]=data5[cat_col].astype(\"category\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:41:20.608170Z","iopub.execute_input":"2022-09-28T12:41:20.608566Z","iopub.status.idle":"2022-09-28T12:41:20.701410Z","shell.execute_reply.started":"2022-09-28T12:41:20.608531Z","shell.execute_reply":"2022-09-28T12:41:20.700242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_col","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:41:23.475981Z","iopub.execute_input":"2022-09-28T12:41:23.476397Z","iopub.status.idle":"2022-09-28T12:41:23.484231Z","shell.execute_reply.started":"2022-09-28T12:41:23.476363Z","shell.execute_reply":"2022-09-28T12:41:23.483019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5.describe()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:41:59.178919Z","iopub.execute_input":"2022-09-28T12:41:59.180179Z","iopub.status.idle":"2022-09-28T12:41:59.346106Z","shell.execute_reply.started":"2022-09-28T12:41:59.180133Z","shell.execute_reply":"2022-09-28T12:41:59.344731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## as we see we reduce the size about 82% to speed","metadata":{}},{"cell_type":"code","source":"#data5.to_csv(\"F3.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5_1=data5.copy()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:42:03.499290Z","iopub.execute_input":"2022-09-28T12:42:03.499695Z","iopub.status.idle":"2022-09-28T12:42:03.505910Z","shell.execute_reply.started":"2022-09-28T12:42:03.499660Z","shell.execute_reply":"2022-09-28T12:42:03.504901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5_1","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:42:13.703513Z","iopub.execute_input":"2022-09-28T12:42:13.703950Z","iopub.status.idle":"2022-09-28T12:42:13.743415Z","shell.execute_reply.started":"2022-09-28T12:42:13.703915Z","shell.execute_reply":"2022-09-28T12:42:13.742091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:42:26.288125Z","iopub.execute_input":"2022-09-28T12:42:26.288526Z","iopub.status.idle":"2022-09-28T12:42:26.293694Z","shell.execute_reply.started":"2022-09-28T12:42:26.288494Z","shell.execute_reply":"2022-09-28T12:42:26.292526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## this code i take it  from Daksh Bhatnagar\n## this his notebook https://www.kaggle.com/code/bhatnagardaksh/big-data-derby-clustering-pca-classification\n\n\nwcss = []\n\nfor p in range(2,11):\n    km = KMeans(n_clusters=p)\n    km.fit_predict(data5[num_col])\n    wcss.append(km.inertia_)\n    print('For',p,'Cluster,', np.around(km.inertia_,2),'is the WCSS.')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:42:35.785549Z","iopub.execute_input":"2022-09-28T12:42:35.785998Z","iopub.status.idle":"2022-09-28T12:42:56.385783Z","shell.execute_reply.started":"2022-09-28T12:42:35.785962Z","shell.execute_reply":"2022-09-28T12:42:56.384410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:42:56.388092Z","iopub.execute_input":"2022-09-28T12:42:56.388537Z","iopub.status.idle":"2022-09-28T12:42:57.534219Z","shell.execute_reply.started":"2022-09-28T12:42:56.388492Z","shell.execute_reply":"2022-09-28T12:42:57.533355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.line(x = range(2,11),\n              y=wcss, \n              template='simple_white',\n              title = 'Elbow Plot', markers=True,\n             color_discrete_sequence=[\"skyblue\"])\n\nfig.update_layout(xaxis_title=\"Clusters\",yaxis_title=\" WCSS\")\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:42:57.535615Z","iopub.execute_input":"2022-09-28T12:42:57.536184Z","iopub.status.idle":"2022-09-28T12:42:58.838650Z","shell.execute_reply.started":"2022-09-28T12:42:57.536150Z","shell.execute_reply":"2022-09-28T12:42:58.837505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_col","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:11.613436Z","iopub.execute_input":"2022-09-28T12:43:11.613882Z","iopub.status.idle":"2022-09-28T12:43:11.621457Z","shell.execute_reply.started":"2022-09-28T12:43:11.613845Z","shell.execute_reply":"2022-09-28T12:43:11.620291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer\nmodel = KMeans()\nvisualizer = KElbowVisualizer(model, k=(2,10)).fit(data5[num_col])\nvisualizer.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:27.783055Z","iopub.execute_input":"2022-09-28T12:43:27.783537Z","iopub.status.idle":"2022-09-28T12:43:46.626337Z","shell.execute_reply.started":"2022-09-28T12:43:27.783497Z","shell.execute_reply":"2022-09-28T12:43:46.625263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## there are two cases one of them is 6 clusters and another is 4 cluster\n## i choose the mean 5","metadata":{}},{"cell_type":"code","source":"km = KMeans(n_clusters=5)\ntarget = km.fit_predict(data5[num_col])\ntarget","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:46.628742Z","iopub.execute_input":"2022-09-28T12:43:46.629498Z","iopub.status.idle":"2022-09-28T12:43:48.687164Z","shell.execute_reply.started":"2022-09-28T12:43:46.629448Z","shell.execute_reply":"2022-09-28T12:43:48.686027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:48.688630Z","iopub.execute_input":"2022-09-28T12:43:48.689311Z","iopub.status.idle":"2022-09-28T12:43:48.696661Z","shell.execute_reply.started":"2022-09-28T12:43:48.689273Z","shell.execute_reply":"2022-09-28T12:43:48.695449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5[\"target\"]=target","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:48.699443Z","iopub.execute_input":"2022-09-28T12:43:48.700112Z","iopub.status.idle":"2022-09-28T12:43:48.707203Z","shell.execute_reply.started":"2022-09-28T12:43:48.700065Z","shell.execute_reply":"2022-09-28T12:43:48.705722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data5","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:48.710400Z","iopub.execute_input":"2022-09-28T12:43:48.711215Z","iopub.status.idle":"2022-09-28T12:43:48.749132Z","shell.execute_reply.started":"2022-09-28T12:43:48.711178Z","shell.execute_reply":"2022-09-28T12:43:48.747821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data6=data5.copy()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:48.750842Z","iopub.execute_input":"2022-09-28T12:43:48.751269Z","iopub.status.idle":"2022-09-28T12:43:48.759047Z","shell.execute_reply.started":"2022-09-28T12:43:48.751233Z","shell.execute_reply":"2022-09-28T12:43:48.757830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_col.append(\"target\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:55.035632Z","iopub.execute_input":"2022-09-28T12:43:55.036057Z","iopub.status.idle":"2022-09-28T12:43:55.042303Z","shell.execute_reply.started":"2022-09-28T12:43:55.036021Z","shell.execute_reply":"2022-09-28T12:43:55.040877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_col","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:43:55.771025Z","iopub.execute_input":"2022-09-28T12:43:55.771719Z","iopub.status.idle":"2022-09-28T12:43:55.778077Z","shell.execute_reply.started":"2022-09-28T12:43:55.771678Z","shell.execute_reply":"2022-09-28T12:43:55.777069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sns.pairplot(data=data6[num_col],hue='target',diag_kind=\"kde\",palette=\"tab20c_r\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:13:31.082112Z","iopub.execute_input":"2022-09-28T01:13:31.083654Z","iopub.status.idle":"2022-09-28T01:44:09.810478Z","shell.execute_reply.started":"2022-09-28T01:13:31.083612Z","shell.execute_reply":"2022-09-28T01:44:09.807602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nyou can see pairplot from this link\n\nhttps://www.kaggle.com/datasets/mohammedbadway/pngion","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## in this plot we can see some graphs the limits border is very\n## clear so i think is good","metadata":{}},{"cell_type":"code","source":"data6","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:11:54.772460Z","iopub.execute_input":"2022-09-28T13:11:54.773728Z","iopub.status.idle":"2022-09-28T13:11:54.815635Z","shell.execute_reply.started":"2022-09-28T13:11:54.773681Z","shell.execute_reply":"2022-09-28T13:11:54.814405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in num_col:\n    sns.displot(data6[i] )\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:12:20.045365Z","iopub.execute_input":"2022-09-28T13:12:20.046332Z","iopub.status.idle":"2022-09-28T13:12:34.177571Z","shell.execute_reply.started":"2022-09-28T13:12:20.046277Z","shell.execute_reply":"2022-09-28T13:12:34.176365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_col.append(\"target\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:12:46.598573Z","iopub.execute_input":"2022-09-28T13:12:46.599020Z","iopub.status.idle":"2022-09-28T13:12:46.606829Z","shell.execute_reply.started":"2022-09-28T13:12:46.598983Z","shell.execute_reply":"2022-09-28T13:12:46.604988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in cat_col:\n    plt.figure(figsize=(20,10))\n    sns.countplot(x=i, data=data6[cat_col], hue=\"target\",palette=\"Dark2\")\n    plt.title(i)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:12:50.585407Z","iopub.execute_input":"2022-09-28T13:12:50.586314Z","iopub.status.idle":"2022-09-28T13:12:53.881216Z","shell.execute_reply.started":"2022-09-28T13:12:50.586275Z","shell.execute_reply":"2022-09-28T13:12:53.880204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listofcolor=[]\nfor j in range(5) :\n        #print(j)\n        colors = \"#\"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])\n        listofcolor.append(colors)\n        print(colors)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:06.245585Z","iopub.execute_input":"2022-09-28T13:13:06.246020Z","iopub.status.idle":"2022-09-28T13:13:06.254087Z","shell.execute_reply.started":"2022-09-28T13:13:06.245983Z","shell.execute_reply":"2022-09-28T13:13:06.252589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listofcolor","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:08.541658Z","iopub.execute_input":"2022-09-28T13:13:08.542656Z","iopub.status.idle":"2022-09-28T13:13:08.552283Z","shell.execute_reply.started":"2022-09-28T13:13:08.542592Z","shell.execute_reply":"2022-09-28T13:13:08.551062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colorp = ['#FFA15A','#19D3F3','#FF6692','#FF97FF','#FECB52',\"#FECB58\"]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:09.473931Z","iopub.execute_input":"2022-09-28T13:13:09.474964Z","iopub.status.idle":"2022-09-28T13:13:09.480046Z","shell.execute_reply.started":"2022-09-28T13:13:09.474911Z","shell.execute_reply":"2022-09-28T13:13:09.479078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listofcolor.extend(colorp)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:10.467838Z","iopub.execute_input":"2022-09-28T13:13:10.468689Z","iopub.status.idle":"2022-09-28T13:13:10.474742Z","shell.execute_reply.started":"2022-09-28T13:13:10.468625Z","shell.execute_reply":"2022-09-28T13:13:10.473766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listofcolor","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:11.645105Z","iopub.execute_input":"2022-09-28T13:13:11.645591Z","iopub.status.idle":"2022-09-28T13:13:11.654130Z","shell.execute_reply.started":"2022-09-28T13:13:11.645554Z","shell.execute_reply":"2022-09-28T13:13:11.653034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=[]\n#pie, ax = plt.subplots(5,4,figsize=[15,10])\n\nfor i in cat_col:\n    pie, ax = plt.subplots(figsize=[15,10])\n    xc=pd.DataFrame(data6 [i].value_counts())\n    labels = data6[i].unique()\n    #if len(labels)>25:\n     #   continue;\n    colors=listofcolor[:len(labels)]\n    r=[0.06]*len(labels)     \n\n    plt.pie(x = data6[i].value_counts(),autopct='%.2f%%',\n    explode=r, labels=xc.index, pctdistance=0.5, textprops={'fontsize': 14}, colors = colors,startangle=90)\n    plt.title(f'{i}  distribution')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:14.253973Z","iopub.execute_input":"2022-09-28T13:13:14.254607Z","iopub.status.idle":"2022-09-28T13:13:15.985143Z","shell.execute_reply.started":"2022-09-28T13:13:14.254572Z","shell.execute_reply":"2022-09-28T13:13:15.983385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n \n# using the iris dataset\ndf = data5\nfor i in data5.columns:\n    for j in data5.columns:\n        fig = px.bar(df, x=i, y=j, color=\"target\",title=f\"{i}corr{j}\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:13:22.604735Z","iopub.execute_input":"2022-09-28T13:13:22.605657Z","iopub.status.idle":"2022-09-28T13:15:01.393996Z","shell.execute_reply.started":"2022-09-28T13:13:22.605616Z","shell.execute_reply":"2022-09-28T13:15:01.391779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.show()\nsns.heatmap(data5.corr(),annot=True,fmt=\".2f\",linewidths=.8)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:45:48.882118Z","iopub.execute_input":"2022-09-28T01:45:48.882462Z","iopub.status.idle":"2022-09-28T01:45:51.494698Z","shell.execute_reply.started":"2022-09-28T01:45:48.882429Z","shell.execute_reply":"2022-09-28T01:45:51.493271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in  data6.columns:\n    if i ==\"target\":\n        \n        continue;\n    if i in cat_col:\n        \n        ty=data6.groupby([i])[\"target\"].count().reset_index().sort_values(by=\"target\",ascending=False)\n        print(\"CAt\")\n        display(ty)\n        ty.plot(x=i)\n    if i in num_col:\n        print(\"Num\")\n        ty=data6.groupby([\"target\"])[i].mean().reset_index().sort_values(by=\"target\",ascending=False)\n\n        display(ty)\n        ty.plot()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:01.396598Z","iopub.execute_input":"2022-09-28T13:15:01.397023Z","iopub.status.idle":"2022-09-28T13:15:06.963240Z","shell.execute_reply.started":"2022-09-28T13:15:01.396984Z","shell.execute_reply":"2022-09-28T13:15:06.961881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## from Data Viz\n## we can see the clear border between target and purse\n## we have unbalanced data\n","metadata":{}},{"cell_type":"code","source":"data7=data6.copy()\ndata7","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:06.964723Z","iopub.execute_input":"2022-09-28T13:15:06.965103Z","iopub.status.idle":"2022-09-28T13:15:07.006150Z","shell.execute_reply.started":"2022-09-28T13:15:06.965070Z","shell.execute_reply":"2022-09-28T13:15:07.004986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ip=0\nfor i in data6[cat_col]:\n    print(f\"{i} is \",data6[i].nunique())\n    i\n    \n    print(\"/n\")","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-09-28T13:15:07.008982Z","iopub.execute_input":"2022-09-28T13:15:07.010378Z","iopub.status.idle":"2022-09-28T13:15:07.029523Z","shell.execute_reply.started":"2022-09-28T13:15:07.010332Z","shell.execute_reply":"2022-09-28T13:15:07.027750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from category_encoders import BinaryEncoder\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:17.537041Z","iopub.execute_input":"2022-09-28T13:15:17.537479Z","iopub.status.idle":"2022-09-28T13:15:17.542688Z","shell.execute_reply.started":"2022-09-28T13:15:17.537440Z","shell.execute_reply":"2022-09-28T13:15:17.541842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Bi_Enc = BinaryEncoder()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:21.120330Z","iopub.execute_input":"2022-09-28T13:15:21.120723Z","iopub.status.idle":"2022-09-28T13:15:21.125856Z","shell.execute_reply.started":"2022-09-28T13:15:21.120691Z","shell.execute_reply":"2022-09-28T13:15:21.124989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lt=0","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:52.131063Z","iopub.execute_input":"2022-09-28T13:15:52.131550Z","iopub.status.idle":"2022-09-28T13:15:52.137298Z","shell.execute_reply.started":"2022-09-28T13:15:52.131510Z","shell.execute_reply":"2022-09-28T13:15:52.136187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor i in data7[cat_col]:\n    if i==\"target\":\n        continue;\n    lt=data7[i].nunique()\n    if lt>6:\n        \n         Transformed_df = Bi_Enc.fit_transform(data7[i])\n         data7= pd.concat([data7,Transformed_df], axis=1)\n\n         Bi_Enc=None\n         Bi_Enc = BinaryEncoder()\n         #l8 = Bi_Enc8.fit_transform(data7[\"track_condition\"])\n            \n        #Transformed_df\n            \n       # data7\n       \n    else: \n        data7=pd.get_dummies(data=data7,drop_first=True,columns=[i])\n        #print(\"jj\")\n       \n        \n    \n        ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:52.909185Z","iopub.execute_input":"2022-09-28T13:15:52.909575Z","iopub.status.idle":"2022-09-28T13:15:53.327909Z","shell.execute_reply.started":"2022-09-28T13:15:52.909543Z","shell.execute_reply":"2022-09-28T13:15:53.326731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data7.columns","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:15:58.273057Z","iopub.execute_input":"2022-09-28T13:15:58.273462Z","iopub.status.idle":"2022-09-28T13:15:58.281881Z","shell.execute_reply.started":"2022-09-28T13:15:58.273420Z","shell.execute_reply":"2022-09-28T13:15:58.280666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data7","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:16:05.160717Z","iopub.execute_input":"2022-09-28T13:16:05.161151Z","iopub.status.idle":"2022-09-28T13:16:05.209175Z","shell.execute_reply.started":"2022-09-28T13:16:05.161115Z","shell.execute_reply":"2022-09-28T13:16:05.207826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data7.to_csv(\"F4\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data8=data7.drop(columns=[\"program_number\",\"track_condition\",\"race_type\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:16:20.347153Z","iopub.execute_input":"2022-09-28T13:16:20.347846Z","iopub.status.idle":"2022-09-28T13:16:20.371626Z","shell.execute_reply.started":"2022-09-28T13:16:20.347806Z","shell.execute_reply":"2022-09-28T13:16:20.370783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data8.to_csv(\"F5.csv\")","metadata":{"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data8=pd.read_csv(\"F5.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data8=data8.iloc[:,1:]\ndata8","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-09-28T13:16:29.259882Z","iopub.execute_input":"2022-09-28T13:16:29.260305Z","iopub.status.idle":"2022-09-28T13:16:29.295579Z","shell.execute_reply.started":"2022-09-28T13:16:29.260268Z","shell.execute_reply":"2022-09-28T13:16:29.294521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data9=data8.copy()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:16:35.907920Z","iopub.execute_input":"2022-09-28T13:16:35.908349Z","iopub.status.idle":"2022-09-28T13:16:35.918766Z","shell.execute_reply.started":"2022-09-28T13:16:35.908313Z","shell.execute_reply":"2022-09-28T13:16:35.917511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=data9.drop([\"target\"],axis=1)\ny=data9[[\"target\"]]\nX","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:16:39.100741Z","iopub.execute_input":"2022-09-28T13:16:39.101165Z","iopub.status.idle":"2022-09-28T13:16:39.144788Z","shell.execute_reply.started":"2022-09-28T13:16:39.101130Z","shell.execute_reply":"2022-09-28T13:16:39.143716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_col.remove(\"target\")","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:16:54.473306Z","iopub.execute_input":"2022-09-28T13:16:54.473729Z","iopub.status.idle":"2022-09-28T13:16:54.478875Z","shell.execute_reply.started":"2022-09-28T13:16:54.473693Z","shell.execute_reply":"2022-09-28T13:16:54.477455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"std=StandardScaler()\nX[num_col]=std.fit_transform(X[num_col])\nX","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:06.105324Z","iopub.execute_input":"2022-09-28T13:17:06.105707Z","iopub.status.idle":"2022-09-28T13:17:06.188993Z","shell.execute_reply.started":"2022-09-28T13:17:06.105676Z","shell.execute_reply":"2022-09-28T13:17:06.187686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:10.106793Z","iopub.execute_input":"2022-09-28T13:17:10.107513Z","iopub.status.idle":"2022-09-28T13:17:10.134727Z","shell.execute_reply.started":"2022-09-28T13:17:10.107475Z","shell.execute_reply":"2022-09-28T13:17:10.133449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in X.columns[17:]:\n            X[i]=X[i].astype(\"int16\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:22.100432Z","iopub.execute_input":"2022-09-28T13:17:22.101544Z","iopub.status.idle":"2022-09-28T13:17:22.138338Z","shell.execute_reply.started":"2022-09-28T13:17:22.101467Z","shell.execute_reply":"2022-09-28T13:17:22.137337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:36.214005Z","iopub.execute_input":"2022-09-28T13:17:36.214410Z","iopub.status.idle":"2022-09-28T13:17:36.244047Z","shell.execute_reply.started":"2022-09-28T13:17:36.214378Z","shell.execute_reply":"2022-09-28T13:17:36.242984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y.to_csv(\"target.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import decomposition \npca = decomposition.PCA(0.90)\nX = pca.fit_transform(X)\nprint( X.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:49.708225Z","iopub.execute_input":"2022-09-28T13:17:49.708659Z","iopub.status.idle":"2022-09-28T13:17:49.873836Z","shell.execute_reply.started":"2022-09-28T13:17:49.708625Z","shell.execute_reply":"2022-09-28T13:17:49.872338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(X)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-09-28T13:17:53.178338Z","iopub.execute_input":"2022-09-28T13:17:53.178740Z","iopub.status.idle":"2022-09-28T13:17:53.210820Z","shell.execute_reply.started":"2022-09-28T13:17:53.178707Z","shell.execute_reply":"2022-09-28T13:17:53.209588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling  import RandomOverSampler\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:57.029182Z","iopub.execute_input":"2022-09-28T13:17:57.029911Z","iopub.status.idle":"2022-09-28T13:17:57.034059Z","shell.execute_reply.started":"2022-09-28T13:17:57.029864Z","shell.execute_reply":"2022-09-28T13:17:57.032862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oversample = RandomOverSampler(sampling_strategy='minority')\nX, y = oversample.fit_resample(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:17:59.575995Z","iopub.execute_input":"2022-09-28T13:17:59.576408Z","iopub.status.idle":"2022-09-28T13:17:59.624745Z","shell.execute_reply.started":"2022-09-28T13:17:59.576373Z","shell.execute_reply":"2022-09-28T13:17:59.623761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\nfrom catboost import CatBoostClassifier\nfrom sklearn.svm import SVC\nfrom xgboost import XGBClassifier\nfrom sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier,\\\n                            BaggingClassifier,VotingClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:18:01.090098Z","iopub.execute_input":"2022-09-28T13:18:01.090536Z","iopub.status.idle":"2022-09-28T13:18:01.687885Z","shell.execute_reply.started":"2022-09-28T13:18:01.090499Z","shell.execute_reply":"2022-09-28T13:18:01.686693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:18:06.227466Z","iopub.execute_input":"2022-09-28T13:18:06.228732Z","iopub.status.idle":"2022-09-28T13:18:06.273614Z","shell.execute_reply.started":"2022-09-28T13:18:06.228671Z","shell.execute_reply":"2022-09-28T13:18:06.272000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nsupervised={\"Log\":LogisticRegression(),\"Rnf\":RandomForestClassifier(),\n            \"Knn\":KNeighborsClassifier(),\"Lg\":LGBMClassifier(),\n            \"Dec_t\":DecisionTreeClassifier(),\"cat\":CatBoostClassifier(),\n            \"XG\":XGBClassifier(),\"NB\":GaussianNB(),\"BAG\" :BaggingClassifier(),\"AD\" :AdaBoostClassifier()\n           }\nunsup={\"Stander\":StandardScaler()}\nl1=[]\nl2=[]\nl3=[]\n\n\ndef fir_res_mod1():\n    \n    \n    for i,t in unsup.items():\n        start = time.time()\n        #print(start)\n\n        for n,m in supervised.items():\n            \n            \n            pipe = Pipeline([(i, t), (n, m)])\n            print(t,m)\n            pipe.fit(X_train, y_train)\n            f=pipe.score(X_test, y_test)\n            \n            l1.append(f)\n            l2.append(i+\"****\"+n)\n            end = time.time()\n            #print(end)\n            v=end-start\n            l3.append(v)\n            print(f\"{i}************{n}\",pipe.score(X_test, y_test))\n            #print(l3)\n\n    d1={\"pipeline\":l2,\"acc\":l1,\"timecost\":l3}\n    global d2\n    d2=pd.DataFrame(d1)\n    #d2.sort_index(axis=1)\n\n    d2.sort_values(by=\"acc\",inplace=True,ascending=False)\n   # d2.sort_index(axis=1)\n    d2.reset_index(inplace=True)\n    d2.drop(\"index\",axis=1,inplace=True)\n    print(d2.columns)\n\n    \n    #d2.sort_values(by=\"timecost\",inplace=True,ascending=True)\n    \n    sns.barplot(y='pipeline',x='acc',data=d2)\n    sns.factorplot(x='pipeline',y='acc',data=d2,kind='point',size=4,aspect=5)\n  \n    return d2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:18:07.821643Z","iopub.execute_input":"2022-09-28T13:18:07.822075Z","iopub.status.idle":"2022-09-28T13:18:07.837390Z","shell.execute_reply.started":"2022-09-28T13:18:07.822041Z","shell.execute_reply":"2022-09-28T13:18:07.836439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fir_res_mod1()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:18:09.993894Z","iopub.execute_input":"2022-09-28T13:18:09.994319Z","iopub.status.idle":"2022-09-28T13:31:50.664752Z","shell.execute_reply.started":"2022-09-28T13:18:09.994286Z","shell.execute_reply":"2022-09-28T13:31:50.663502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestClassifier(n_estimators=250,n_jobs=-1)\n\nrf.fit(X_train,y_train)\ny_prd=rf.predict(X_test)\nprint(accuracy_score(y_prd,y_test),\"****\\n\",confusion_matrix(y_prd,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:31:50.666991Z","iopub.execute_input":"2022-09-28T13:31:50.667386Z","iopub.status.idle":"2022-09-28T13:33:29.767361Z","shell.execute_reply.started":"2022-09-28T13:31:50.667351Z","shell.execute_reply":"2022-09-28T13:33:29.766012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kn=KNeighborsClassifier()\n\nkn.fit(X_train,y_train)\ny_prd=kn.predict(X_test)\nprint(accuracy_score(y_prd,y_test),\"****\\n\",confusion_matrix(y_prd,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:33:29.769360Z","iopub.execute_input":"2022-09-28T13:33:29.772392Z","iopub.status.idle":"2022-09-28T13:36:56.070554Z","shell.execute_reply.started":"2022-09-28T13:33:29.772338Z","shell.execute_reply":"2022-09-28T13:36:56.069077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ct=CatBoostClassifier()\n\n#ct.fit(X_train,y_train)\n#y_prd=ct.predict(X_test)\n#print(accuracy_score(y_prd,y_test),\"****\\n\",confusion_matrix(y_prd,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:36:56.073926Z","iopub.execute_input":"2022-09-28T13:36:56.074284Z","iopub.status.idle":"2022-09-28T13:38:25.962867Z","shell.execute_reply.started":"2022-09-28T13:36:56.074252Z","shell.execute_reply":"2022-09-28T13:38:25.961313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = VotingClassifier(estimators=[('rf', rf), ('kn', kn)]\n                              , voting='hard')\nclf.fit(X_train, y_train)\ny_pred = clf.predict(X_test)\nprint((clf.score(X_train, y_train)))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:38:25.964221Z","iopub.execute_input":"2022-09-28T13:38:25.964565Z","iopub.status.idle":"2022-09-28T13:53:28.665391Z","shell.execute_reply.started":"2022-09-28T13:38:25.964535Z","shell.execute_reply":"2022-09-28T13:53:28.663915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"con=confusion_matrix(y_test, y_prd)\nf, ax = plt.subplots(figsize=(11, 8))\nsns.heatmap(con, annot=True, fmt='g',)\nplt.title('Confiusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:53:28.666996Z","iopub.execute_input":"2022-09-28T13:53:28.667337Z","iopub.status.idle":"2022-09-28T13:53:29.092240Z","shell.execute_reply.started":"2022-09-28T13:53:28.667304Z","shell.execute_reply":"2022-09-28T13:53:29.090864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from keras.models import Sequential\n#from keras.layers import Dense,Dropout,Activation,BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:53:29.094103Z","iopub.execute_input":"2022-09-28T13:53:29.094522Z","iopub.status.idle":"2022-09-28T13:53:35.176084Z","shell.execute_reply.started":"2022-09-28T13:53:29.094488Z","shell.execute_reply":"2022-09-28T13:53:35.175006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n= Sequential()\n\nann.add(Dense(100, input_dim=17, activation = 'relu'))\n#ann.add(Dropout(0.2))\nann.add(Dense(100, activation='relu',))\n\nann.add(Dense(50, activation='relu',))\n\nann.add(Dense(30,  activation='relu'))\n\nann.add(Dense(1,   activation='Softmax'))\nann.summary()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:53:35.177975Z","iopub.execute_input":"2022-09-28T13:53:35.180689Z","iopub.status.idle":"2022-09-28T13:53:35.330231Z","shell.execute_reply.started":"2022-09-28T13:53:35.180634Z","shell.execute_reply":"2022-09-28T13:53:35.328843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ann.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] )\n#ann.fit(X_train,y_train , batch_size=512 , epochs=150, verbose='auto', validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:53:35.332164Z","iopub.execute_input":"2022-09-28T13:53:35.332858Z","iopub.status.idle":"2022-09-28T13:56:58.160747Z","shell.execute_reply.started":"2022-09-28T13:53:35.332807Z","shell.execute_reply":"2022-09-28T13:56:58.159842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ann.history.history.keys()\n#pd.DataFrame(ann.history.history)\n#pd.DataFrame(ann.history.history).plot(figsize=(15,8))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T13:56:58.163172Z","iopub.execute_input":"2022-09-28T13:56:58.164319Z","iopub.status.idle":"2022-09-28T13:56:58.507524Z","shell.execute_reply.started":"2022-09-28T13:56:58.164271Z","shell.execute_reply":"2022-09-28T13:56:58.506309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## we can see Deep Learning work is very bad so i will  recover my money from Deep Learning course 😂😂","metadata":{}},{"cell_type":"markdown","source":" ## vote so that the curse of the pharaohs does not infect you 😂😂","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}