{"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":"markdown","source":"![car](https://di-uploads-pod9.dealerinspire.com/lynnesautomotivegroup/uploads/2021/02/lynnes-auto-group-used-cars.jpg)","metadata":{}},{"cell_type":"markdown","source":"# **Introduction**\n**One of the biggest challenges of an auto dealership purchasing a used car at an auto auction is the risk of that the vehicle might have serious issues that prevent it from being sold to customers. The auto community calls these unfortunate purchases \"kicks\".**\n\n**Kicked cars often result when there are tampered odometers, mechanical issues the dealer is not able to address, issues with getting the vehicle title from the seller, or some other unforeseen problem. Kick cars can be very costly to dealers after transportation cost, throw-away repair work, and market losses in reselling the vehicle.**","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-09T06:29:40.371863Z","iopub.execute_input":"2022-08-09T06:29:40.372667Z","iopub.status.idle":"2022-08-09T06:29:40.407046Z","shell.execute_reply.started":"2022-08-09T06:29:40.372564Z","shell.execute_reply":"2022-08-09T06:29:40.406001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/DontGetKicked/training.csv\")\ndf_t=pd.read_csv(\"../input/DontGetKicked/test.csv\")\ndf_s=pd.read_csv(\"../input/DontGetKicked/example_entry.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:40.409009Z","iopub.execute_input":"2022-08-09T06:29:40.409676Z","iopub.status.idle":"2022-08-09T06:29:41.258124Z","shell.execute_reply.started":"2022-08-09T06:29:40.409642Z","shell.execute_reply":"2022-08-09T06:29:41.257061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:41.259825Z","iopub.execute_input":"2022-08-09T06:29:41.260470Z","iopub.status.idle":"2022-08-09T06:29:42.437073Z","shell.execute_reply.started":"2022-08-09T06:29:41.260433Z","shell.execute_reply":"2022-08-09T06:29:42.436101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:42.440067Z","iopub.execute_input":"2022-08-09T06:29:42.440801Z","iopub.status.idle":"2022-08-09T06:29:42.482803Z","shell.execute_reply.started":"2022-08-09T06:29:42.440755Z","shell.execute_reply":"2022-08-09T06:29:42.481787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:42.484165Z","iopub.execute_input":"2022-08-09T06:29:42.484586Z","iopub.status.idle":"2022-08-09T06:29:42.557662Z","shell.execute_reply.started":"2022-08-09T06:29:42.484543Z","shell.execute_reply":"2022-08-09T06:29:42.556422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Visualization**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nx=df[\"IsBadBuy\"].value_counts()\nmylabel=[\"Not BadBuy(0)\",\"BadBuy(1)\"]\ncolors=['#f4acb7','#9d8189']\nplt.pie(x,labels=mylabel,autopct=\"%1.1f%%\",startangle=15,shadow=True,colors=colors)\nplt.axis(\"equal\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:42.559074Z","iopub.execute_input":"2022-08-09T06:29:42.559414Z","iopub.status.idle":"2022-08-09T06:29:42.788514Z","shell.execute_reply.started":"2022-08-09T06:29:42.559381Z","shell.execute_reply":"2022-08-09T06:29:42.787092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nhue_color={0:'#012a4a',1:'#2c7da0'}\nax=sns.countplot(data=df,x='Auction',hue='IsBadBuy',palette=hue_color)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:44:38.177292Z","iopub.execute_input":"2022-08-09T06:44:38.178263Z","iopub.status.idle":"2022-08-09T06:44:38.464860Z","shell.execute_reply.started":"2022-08-09T06:44:38.178170Z","shell.execute_reply":"2022-08-09T06:44:38.463460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nhue_color={0:'#012a4a',1:'#ef233c'}\nax=sns.countplot(data=df,x='VehYear',hue='IsBadBuy',palette=hue_color)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:49:28.946623Z","iopub.execute_input":"2022-08-09T06:49:28.947708Z","iopub.status.idle":"2022-08-09T06:49:29.252025Z","shell.execute_reply.started":"2022-08-09T06:49:28.947665Z","shell.execute_reply":"2022-08-09T06:49:29.251048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nhue_color={0:'#8D99AE',1:'#ef233c'}\nax=sns.countplot(data=df,x='Transmission',hue='IsBadBuy',palette=hue_color)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:48:44.379278Z","iopub.execute_input":"2022-08-09T06:48:44.379756Z","iopub.status.idle":"2022-08-09T06:48:44.654331Z","shell.execute_reply.started":"2022-08-09T06:48:44.379703Z","shell.execute_reply":"2022-08-09T06:48:44.653334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nhue_color={0:'#E63946',1:'#9d8189'}\nax=sns.countplot(data=df,x='Nationality',hue='IsBadBuy',palette=hue_color)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:48:18.438121Z","iopub.execute_input":"2022-08-09T06:48:18.438548Z","iopub.status.idle":"2022-08-09T06:48:18.717044Z","shell.execute_reply.started":"2022-08-09T06:48:18.438514Z","shell.execute_reply":"2022-08-09T06:48:18.715777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Cleaning**","metadata":{}},{"cell_type":"code","source":"train=df.drop(['PRIMEUNIT','AUCGUART','WheelTypeID','WheelType','Trim','MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice','MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice','MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice','MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice','PurchDate',\"Model\",\"SubModel\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:42.792647Z","iopub.execute_input":"2022-08-09T06:29:42.794176Z","iopub.status.idle":"2022-08-09T06:29:42.812242Z","shell.execute_reply.started":"2022-08-09T06:29:42.794115Z","shell.execute_reply":"2022-08-09T06:29:42.810952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:42.813614Z","iopub.execute_input":"2022-08-09T06:29:42.814121Z","iopub.status.idle":"2022-08-09T06:29:42.839340Z","shell.execute_reply.started":"2022-08-09T06:29:42.814082Z","shell.execute_reply":"2022-08-09T06:29:42.838095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Auction'].replace({'MANHEIM':0,'OTHER':1,'ADESA':2},inplace=True)\ntrain['Color'].replace({'SILVER':0,'WHITE':1,'BLUE':2,'GREY':3,'BLACK':4,'RED':5,'GOLD':6,'GREEN':7,'MAROON':8,'BEIGE':9,'BROWN':10,'ORANGE':11,'PURPLE':12,'YELLOW':13,'OTHER':14,'NOT AVAIL':15},inplace=True)\ntrain['Transmission'].replace({'AUTO':0,'MANUAL':1,'Manual':2},inplace=True)\ntrain['Nationality'].replace({'AMERICAN':0,'OTHER ASIAN':1,'TOP LINE ASIAN':2,'OTHER':3},inplace=True)\ntrain['Size'].replace({'MEDIUM':0,'LARGE':1,'MEDIUM SUV':2,'COMPACT':3,'VAN':4,'LARGE TRUCK':5,'SMALL SUV':6,'SPECIALTY':7,'CROSSOVER':8,'LARGE SUV':9,'SMALL TRUCK':10,'SPORTS':11},inplace=True)\ntrain['TopThreeAmericanName'].replace({'GM':0,'CHRYSLER':1,'FORD':2,'OTHER':3},inplace=True)\ntrain['Make'].replace({'CHEVROLET':0,'DODGE':1,'FORD':2,'CHRYSLER':3,'PONTIAC':4,'KIA':5,'SATURN':6,'NISSAN':7,'HYUNDAI':8,'JEEP':9,'SUZUKI':10,'TOYOTA':11,'MITSUBISHI':12,'MAZDA':13,'MERCURY':14,'BUICK':15,'GMC':16,'HONDA':17,'OLDSMOBILE':18,'VOLKSWAGEN':19,'ISUZU':20,'SCION':21,'LINCOLN':22,'INFINITI':23,'VOLVO':24,'CADILLAC':25,'ACURA':26,'LEXUS':27,'SUBARU':28,'MINI':29,'PLYMOUTH':30,'TOYOTA SCION':31,'HUMMER':32},inplace=True)\ntrain['VNST'].replace({'TX':0,'FL':1,'CA':2,'NC':3,'AZ':4,'CO':5,'SC':6,'OK':7,'GA':8,'TN':9,'VA':10,'MD':11,'UT':12,'PA':13,'OH':14,'MO':15,'AL':16,'NV':17,'IA':18,'MS':19,'IN':20,'IL':21,'LA':22,'NJ':23,'WV':24,'NM':25,'KY':26,'OR':27,'ID':28,'WA':29,'NH':30,'AR':31,'MN':32,'NE':33,'MA':34,'MI':35,'NY':36},inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:42.840824Z","iopub.execute_input":"2022-08-09T06:29:42.841859Z","iopub.status.idle":"2022-08-09T06:29:43.315006Z","shell.execute_reply.started":"2022-08-09T06:29:42.841813Z","shell.execute_reply":"2022-08-09T06:29:43.313602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.319130Z","iopub.execute_input":"2022-08-09T06:29:43.319844Z","iopub.status.idle":"2022-08-09T06:29:43.364456Z","shell.execute_reply.started":"2022-08-09T06:29:43.319774Z","shell.execute_reply":"2022-08-09T06:29:43.363358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.365964Z","iopub.execute_input":"2022-08-09T06:29:43.370396Z","iopub.status.idle":"2022-08-09T06:29:43.383951Z","shell.execute_reply.started":"2022-08-09T06:29:43.370355Z","shell.execute_reply":"2022-08-09T06:29:43.382679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=df_t.drop(['PRIMEUNIT','AUCGUART','WheelTypeID','WheelType','Trim','MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice','MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice','MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice','MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice','PurchDate',\"Model\",\"SubModel\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.386294Z","iopub.execute_input":"2022-08-09T06:29:43.387046Z","iopub.status.idle":"2022-08-09T06:29:43.398607Z","shell.execute_reply.started":"2022-08-09T06:29:43.386994Z","shell.execute_reply":"2022-08-09T06:29:43.397574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Color']=test['Color'].fillna(\"SILVER\")\ntest['Transmission']=test['Transmission'].fillna(\"AUTO\")\ntest['Nationality']=test['Nationality'].fillna(\"AMERICAN\")\ntest['Size']=test['Size'].fillna(\"MEDIUM\")\ntest['TopThreeAmericanName']=test['TopThreeAmericanName'].fillna(\"GM\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.402184Z","iopub.execute_input":"2022-08-09T06:29:43.402671Z","iopub.status.idle":"2022-08-09T06:29:43.430244Z","shell.execute_reply.started":"2022-08-09T06:29:43.402624Z","shell.execute_reply":"2022-08-09T06:29:43.429281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Auction'].replace({'MANHEIM':0,'OTHER':1,'ADESA':2},inplace=True)\ntest['Color'].replace({'SILVER':0,'WHITE':1,'BLUE':2,'GREY':3,'BLACK':4,'RED':5,'GOLD':6,'GREEN':7,'MAROON':8,'BEIGE':9,'BROWN':10,'ORANGE':11,'PURPLE':12,'YELLOW':13,'OTHER':14,'NOT AVAIL':15,'PINK':16},inplace=True)\ntest['Transmission'].replace({'AUTO':0,'MANUAL':1,'Manual':2},inplace=True)\ntest['Nationality'].replace({'AMERICAN':0,'OTHER ASIAN':1,'TOP LINE ASIAN':2,'OTHER':3},inplace=True)\ntest['Size'].replace({'MEDIUM':0,'LARGE':1,'MEDIUM SUV':2,'COMPACT':3,'VAN':4,'LARGE TRUCK':5,'SMALL SUV':6,'SPECIALTY':7,'CROSSOVER':8,'LARGE SUV':9,'SMALL TRUCK':10,'SPORTS':11},inplace=True)\ntest['TopThreeAmericanName'].replace({'GM':0,'CHRYSLER':1,'FORD':2,'OTHER':3},inplace=True)\ntest['Make'].replace({'CHEVROLET':0,'DODGE':1,'FORD':2,'CHRYSLER':3,'PONTIAC':4,'KIA':5,'SATURN':6,'NISSAN':7,'HYUNDAI':8,'JEEP':9,'SUZUKI':10,'TOYOTA':11,'MITSUBISHI':12,'MAZDA':13,'MERCURY':14,'BUICK':15,'GMC':16,'HONDA':17,'OLDSMOBILE':18,'VOLKSWAGEN':19,'ISUZU':20,'SCION':21,'LINCOLN':22,'INFINITI':23,'VOLVO':24,'CADILLAC':25,'ACURA':26,'LEXUS':27,'SUBARU':28,'MINI':29,'PLYMOUTH':30,'TOYOTA SCION':31,'HUMMER':32},inplace=True)\ntest['VNST'].replace({'TX':0,'FL':1,'CA':2,'NC':3,'AZ':4,'CO':5,'SC':6,'OK':7,'GA':8,'TN':9,'VA':10,'MD':11,'UT':12,'PA':13,'OH':14,'MO':15,'AL':16,'NV':17,'IA':18,'MS':19,'IN':20,'IL':21,'LA':22,'NJ':23,'WV':24,'NM':25,'KY':26,'OR':27,'ID':28,'WA':29,'NH':30,'AR':31,'MN':32,'NE':33,'MA':34,'MI':35,'NY':36,'WI':37},inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.431358Z","iopub.execute_input":"2022-08-09T06:29:43.432336Z","iopub.status.idle":"2022-08-09T06:29:43.745371Z","shell.execute_reply.started":"2022-08-09T06:29:43.432297Z","shell.execute_reply":"2022-08-09T06:29:43.744179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Selection**","metadata":{}},{"cell_type":"code","source":"X=train.drop(['IsBadBuy'],axis='columns')\ny=train['IsBadBuy']","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.746843Z","iopub.execute_input":"2022-08-09T06:29:43.747206Z","iopub.status.idle":"2022-08-09T06:29:43.760021Z","shell.execute_reply.started":"2022-08-09T06:29:43.747173Z","shell.execute_reply":"2022-08-09T06:29:43.758764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:43.761623Z","iopub.execute_input":"2022-08-09T06:29:43.762619Z","iopub.status.idle":"2022-08-09T06:29:44.028055Z","shell.execute_reply.started":"2022-08-09T06:29:43.762582Z","shell.execute_reply":"2022-08-09T06:29:44.026709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nmodel_1=LogisticRegression()\nmodel_1.fit(X_train,y_train)\npredictions_1=model_1.predict(X_test)\nprint(accuracy_score(y_test, predictions_1))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:44.029502Z","iopub.execute_input":"2022-08-09T06:29:44.029841Z","iopub.status.idle":"2022-08-09T06:29:44.808225Z","shell.execute_reply.started":"2022-08-09T06:29:44.029810Z","shell.execute_reply":"2022-08-09T06:29:44.807018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import tree\nmodel_2=tree.DecisionTreeClassifier()\nmodel_2.fit(X_train,y_train)\npredictions_2=model_2.predict(X_test)\nprint(accuracy_score(y_test, predictions_2))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:44.809960Z","iopub.execute_input":"2022-08-09T06:29:44.811068Z","iopub.status.idle":"2022-08-09T06:29:45.841667Z","shell.execute_reply.started":"2022-08-09T06:29:44.811025Z","shell.execute_reply":"2022-08-09T06:29:45.840413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodel_3=RandomForestClassifier()\nmodel_3.fit(X_train,y_train)\npredictions_3=model_3.predict(X_test)\nprint(accuracy_score(y_test, predictions_3))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:45.843562Z","iopub.execute_input":"2022-08-09T06:29:45.844410Z","iopub.status.idle":"2022-08-09T06:30:01.647817Z","shell.execute_reply.started":"2022-08-09T06:29:45.844365Z","shell.execute_reply":"2022-08-09T06:30:01.646683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Train Data**","metadata":{}},{"cell_type":"code","source":"model_1.fit(X,y)\npredictions_final=model_1.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:01.649222Z","iopub.execute_input":"2022-08-09T06:30:01.649575Z","iopub.status.idle":"2022-08-09T06:30:02.310116Z","shell.execute_reply.started":"2022-08-09T06:30:01.649542Z","shell.execute_reply":"2022-08-09T06:30:02.308679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame({\"RefId\": df_s[\"RefId\"],\"IsBadBuy\":predictions_final})","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:02.311794Z","iopub.execute_input":"2022-08-09T06:30:02.312595Z","iopub.status.idle":"2022-08-09T06:30:02.322772Z","shell.execute_reply.started":"2022-08-09T06:30:02.312545Z","shell.execute_reply":"2022-08-09T06:30:02.321192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:30:02.326012Z","iopub.execute_input":"2022-08-09T06:30:02.327447Z","iopub.status.idle":"2022-08-09T06:30:02.346884Z","shell.execute_reply.started":"2022-08-09T06:30:02.327375Z","shell.execute_reply":"2022-08-09T06:30:02.345592Z"},"trusted":true},"execution_count":null,"outputs":[]}]}