CoolFace
Apppublic

asadsheikh/cpp-project

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
main.ipynb734 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "markdown",5   "metadata": {},6   "source": [7    "## **Car Price Predictor**"8   ]9  },10  {11   "cell_type": "code",12   "execution_count": 31,13   "metadata": {},14   "outputs": [],15   "source": [16    "import pandas as pd \n",17    "import numpy as np"18   ]19  },20  {21   "cell_type": "code",22   "execution_count": 32,23   "metadata": {},24   "outputs": [25    {26     "data": {27      "text/html": [28       "<div>\n",29       "<style scoped>\n",30       "    .dataframe tbody tr th:only-of-type {\n",31       "        vertical-align: middle;\n",32       "    }\n",33       "\n",34       "    .dataframe tbody tr th {\n",35       "        vertical-align: top;\n",36       "    }\n",37       "\n",38       "    .dataframe thead th {\n",39       "        text-align: right;\n",40       "    }\n",41       "</style>\n",42       "<table border=\"1\" class=\"dataframe\">\n",43       "  <thead>\n",44       "    <tr style=\"text-align: right;\">\n",45       "      <th></th>\n",46       "      <th>name</th>\n",47       "      <th>company</th>\n",48       "      <th>year</th>\n",49       "      <th>Price</th>\n",50       "      <th>kms_driven</th>\n",51       "      <th>fuel_type</th>\n",52       "    </tr>\n",53       "  </thead>\n",54       "  <tbody>\n",55       "    <tr>\n",56       "      <th>0</th>\n",57       "      <td>Hyundai Santro Xing XO eRLX Euro III</td>\n",58       "      <td>Hyundai</td>\n",59       "      <td>2007</td>\n",60       "      <td>80,000</td>\n",61       "      <td>45,000 kms</td>\n",62       "      <td>Petrol</td>\n",63       "    </tr>\n",64       "    <tr>\n",65       "      <th>1</th>\n",66       "      <td>Mahindra Jeep CL550 MDI</td>\n",67       "      <td>Mahindra</td>\n",68       "      <td>2006</td>\n",69       "      <td>4,25,000</td>\n",70       "      <td>40 kms</td>\n",71       "      <td>Diesel</td>\n",72       "    </tr>\n",73       "    <tr>\n",74       "      <th>2</th>\n",75       "      <td>Maruti Suzuki Alto 800 Vxi</td>\n",76       "      <td>Maruti</td>\n",77       "      <td>2018</td>\n",78       "      <td>Ask For Price</td>\n",79       "      <td>22,000 kms</td>\n",80       "      <td>Petrol</td>\n",81       "    </tr>\n",82       "    <tr>\n",83       "      <th>3</th>\n",84       "      <td>Hyundai Grand i10 Magna 1.2 Kappa VTVT</td>\n",85       "      <td>Hyundai</td>\n",86       "      <td>2014</td>\n",87       "      <td>3,25,000</td>\n",88       "      <td>28,000 kms</td>\n",89       "      <td>Petrol</td>\n",90       "    </tr>\n",91       "    <tr>\n",92       "      <th>4</th>\n",93       "      <td>Ford EcoSport Titanium 1.5L TDCi</td>\n",94       "      <td>Ford</td>\n",95       "      <td>2014</td>\n",96       "      <td>5,75,000</td>\n",97       "      <td>36,000 kms</td>\n",98       "      <td>Diesel</td>\n",99       "    </tr>\n",100       "  </tbody>\n",101       "</table>\n",102       "</div>"103      ],104      "text/plain": [105       "                                     name   company  year          Price  \\\n",106       "0    Hyundai Santro Xing XO eRLX Euro III   Hyundai  2007         80,000   \n",107       "1                 Mahindra Jeep CL550 MDI  Mahindra  2006       4,25,000   \n",108       "2              Maruti Suzuki Alto 800 Vxi    Maruti  2018  Ask For Price   \n",109       "3  Hyundai Grand i10 Magna 1.2 Kappa VTVT   Hyundai  2014       3,25,000   \n",110       "4        Ford EcoSport Titanium 1.5L TDCi      Ford  2014       5,75,000   \n",111       "\n",112       "   kms_driven fuel_type  \n",113       "0  45,000 kms    Petrol  \n",114       "1      40 kms    Diesel  \n",115       "2  22,000 kms    Petrol  \n",116       "3  28,000 kms    Petrol  \n",117       "4  36,000 kms    Diesel  "118      ]119     },120     "execution_count": 32,121     "metadata": {},122     "output_type": "execute_result"123    }124   ],125   "source": [126    "df = pd.read_csv('quikr_car.csv')\n",127    "df.head()"128   ]129  },130  {131   "cell_type": "code",132   "execution_count": 33,133   "metadata": {},134   "outputs": [135    {136     "data": {137      "text/plain": [138       "(892, 6)"139      ]140     },141     "execution_count": 33,142     "metadata": {},143     "output_type": "execute_result"144    }145   ],146   "source": [147    "df.shape"148   ]149  },150  {151   "cell_type": "code",152   "execution_count": 34,153   "metadata": {},154   "outputs": [155    {156     "name": "stdout",157     "output_type": "stream",158     "text": [159      "<class 'pandas.core.frame.DataFrame'>\n",160      "RangeIndex: 892 entries, 0 to 891\n",161      "Data columns (total 6 columns):\n",162      " #   Column      Non-Null Count  Dtype \n",163      "---  ------      --------------  ----- \n",164      " 0   name        892 non-null    object\n",165      " 1   company     892 non-null    object\n",166      " 2   year        892 non-null    object\n",167      " 3   Price       892 non-null    object\n",168      " 4   kms_driven  840 non-null    object\n",169      " 5   fuel_type   837 non-null    object\n",170      "dtypes: object(6)\n",171      "memory usage: 41.9+ KB\n"172     ]173    }174   ],175   "source": [176    "df.info()"177   ]178  },179  {180   "cell_type": "markdown",181   "metadata": {},182   "source": [183    "### **Quality Issues** \n",184    "- Year has many non numeric values.\n",185    "- Year is in object data type.\n",186    "- Price has 35 Ask for Price values. \n",187    "- Price is in object data type.\n",188    "- Price has one outlier. \n",189    "- Kilometers Driven is in object data type and has one Petrol Entry.\n",190    "- Fuel Type has nan values."191   ]192  },193  {194   "cell_type": "markdown",195   "metadata": {},196   "source": [197    "### **Cleaning**"198   ]199  },200  {201   "cell_type": "code",202   "execution_count": 35,203   "metadata": {},204   "outputs": [],205   "source": [206    "df = df[df['year'].str.isnumeric()]"207   ]208  },209  {210   "cell_type": "code",211   "execution_count": 36,212   "metadata": {},213   "outputs": [],214   "source": [215    "df['year'] = df['year'].astype(int)"216   ]217  },218  {219   "cell_type": "code",220   "execution_count": 37,221   "metadata": {},222   "outputs": [],223   "source": [224    "df = df[df['Price'] != 'Ask For Price']"225   ]226  },227  {228   "cell_type": "code",229   "execution_count": 38,230   "metadata": {},231   "outputs": [],232   "source": [233    "df['Price'] = df[\"Price\"].str.replace(',','').astype(int)"234   ]235  },236  {237   "cell_type": "code",238   "execution_count": 39,239   "metadata": {},240   "outputs": [],241   "source": [242    "df = df[df['Price'] < 6e6]"243   ]244  },245  {246   "cell_type": "code",247   "execution_count": 40,248   "metadata": {},249   "outputs": [],250   "source": [251    "df = df[df['kms_driven'] != 'Petrol']"252   ]253  },254  {255   "cell_type": "code",256   "execution_count": 41,257   "metadata": {},258   "outputs": [],259   "source": [260    "df['kms_driven'] = df['kms_driven'].str.split(' ').str.get(0).str.replace(\",\",\"\").astype(int)"261   ]262  },263  {264   "cell_type": "code",265   "execution_count": 42,266   "metadata": {},267   "outputs": [],268   "source": [269    "df = df[~df['fuel_type'].isna()]"270   ]271  },272  {273   "cell_type": "code",274   "execution_count": 43,275   "metadata": {},276   "outputs": [],277   "source": [278    "df['name'] = df['name'].str.split(' ').str.slice(0,3).str.join(' ')"279   ]280  },281  {282   "cell_type": "code",283   "execution_count": 44,284   "metadata": {},285   "outputs": [286    {287     "data": {288      "text/html": [289       "<div>\n",290       "<style scoped>\n",291       "    .dataframe tbody tr th:only-of-type {\n",292       "        vertical-align: middle;\n",293       "    }\n",294       "\n",295       "    .dataframe tbody tr th {\n",296       "        vertical-align: top;\n",297       "    }\n",298       "\n",299       "    .dataframe thead th {\n",300       "        text-align: right;\n",301       "    }\n",302       "</style>\n",303       "<table border=\"1\" class=\"dataframe\">\n",304       "  <thead>\n",305       "    <tr style=\"text-align: right;\">\n",306       "      <th></th>\n",307       "      <th>year</th>\n",308       "      <th>Price</th>\n",309       "      <th>kms_driven</th>\n",310       "    </tr>\n",311       "  </thead>\n",312       "  <tbody>\n",313       "    <tr>\n",314       "      <th>count</th>\n",315       "      <td>815.000000</td>\n",316       "      <td>8.150000e+02</td>\n",317       "      <td>815.000000</td>\n",318       "    </tr>\n",319       "    <tr>\n",320       "      <th>mean</th>\n",321       "      <td>2012.442945</td>\n",322       "      <td>4.017933e+05</td>\n",323       "      <td>46277.096933</td>\n",324       "    </tr>\n",325       "    <tr>\n",326       "      <th>std</th>\n",327       "      <td>4.005079</td>\n",328       "      <td>3.815888e+05</td>\n",329       "      <td>34318.459638</td>\n",330       "    </tr>\n",331       "    <tr>\n",332       "      <th>min</th>\n",333       "      <td>1995.000000</td>\n",334       "      <td>3.000000e+04</td>\n",335       "      <td>0.000000</td>\n",336       "    </tr>\n",337       "    <tr>\n",338       "      <th>25%</th>\n",339       "      <td>2010.000000</td>\n",340       "      <td>1.750000e+05</td>\n",341       "      <td>27000.000000</td>\n",342       "    </tr>\n",343       "    <tr>\n",344       "      <th>50%</th>\n",345       "      <td>2013.000000</td>\n",346       "      <td>2.999990e+05</td>\n",347       "      <td>41000.000000</td>\n",348       "    </tr>\n",349       "    <tr>\n",350       "      <th>75%</th>\n",351       "      <td>2015.000000</td>\n",352       "      <td>4.900000e+05</td>\n",353       "      <td>56879.000000</td>\n",354       "    </tr>\n",355       "    <tr>\n",356       "      <th>max</th>\n",357       "      <td>2019.000000</td>\n",358       "      <td>3.100000e+06</td>\n",359       "      <td>400000.000000</td>\n",360       "    </tr>\n",361       "  </tbody>\n",362       "</table>\n",363       "</div>"364      ],365      "text/plain": [366       "              year         Price     kms_driven\n",367       "count   815.000000  8.150000e+02     815.000000\n",368       "mean   2012.442945  4.017933e+05   46277.096933\n",369       "std       4.005079  3.815888e+05   34318.459638\n",370       "min    1995.000000  3.000000e+04       0.000000\n",371       "25%    2010.000000  1.750000e+05   27000.000000\n",372       "50%    2013.000000  2.999990e+05   41000.000000\n",373       "75%    2015.000000  4.900000e+05   56879.000000\n",374       "max    2019.000000  3.100000e+06  400000.000000"375      ]376     },377     "execution_count": 44,378     "metadata": {},379     "output_type": "execute_result"380    }381   ],382   "source": [383    "df.describe()"384   ]385  },386  {387   "cell_type": "code",388   "execution_count": 45,389   "metadata": {},390   "outputs": [],391   "source": [392    "df = df.reset_index(drop=True)"393   ]394  },395  {396   "cell_type": "code",397   "execution_count": 46,398   "metadata": {},399   "outputs": [],400   "source": [401    "df.to_csv('clean_quikr_car.csv')"402   ]403  },404  {405   "cell_type": "code",406   "execution_count": 89,407   "metadata": {},408   "outputs": [409    {410     "data": {411      "text/plain": [412       "name\n",413       "Maruti Suzuki Swift            51\n",414       "Maruti Suzuki Alto             42\n",415       "Maruti Suzuki Wagon            28\n",416       "Maruti Suzuki Ertiga           16\n",417       "Hyundai Santro Xing            15\n",418       "                               ..\n",419       "Mercedes Benz A                 1\n",420       "Tata Manza ELAN                 1\n",421       "Volkswagen Polo Comfortline     1\n",422       "Nissan Sunny                    1\n",423       "Tata Zest XM                    1\n",424       "Name: count, Length: 254, dtype: int64"425      ]426     },427     "execution_count": 89,428     "metadata": {},429     "output_type": "execute_result"430    }431   ],432   "source": [433    "df['name'].value_counts()"434   ]435  },436  {437   "cell_type": "markdown",438   "metadata": {},439   "source": [440    "### **Model**"441   ]442  },443  {444   "cell_type": "code",445   "execution_count": 48,446   "metadata": {},447   "outputs": [],448   "source": [449    "X = df.drop(columns=['Price'])\n",450    "y = df['Price']"451   ]452  },453  {454   "cell_type": "code",455   "execution_count": 49,456   "metadata": {},457   "outputs": [],458   "source": [459    "from sklearn.model_selection import train_test_split\n",460    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=124)"461   ]462  },463  {464   "cell_type": "code",465   "execution_count": 50,466   "metadata": {},467   "outputs": [468    {469     "data": {470      "text/html": [471       "<div>\n",472       "<style scoped>\n",473       "    .dataframe tbody tr th:only-of-type {\n",474       "        vertical-align: middle;\n",475       "    }\n",476       "\n",477       "    .dataframe tbody tr th {\n",478       "        vertical-align: top;\n",479       "    }\n",480       "\n",481       "    .dataframe thead th {\n",482       "        text-align: right;\n",483       "    }\n",484       "</style>\n",485       "<table border=\"1\" class=\"dataframe\">\n",486       "  <thead>\n",487       "    <tr style=\"text-align: right;\">\n",488       "      <th></th>\n",489       "      <th>name</th>\n",490       "      <th>company</th>\n",491       "      <th>year</th>\n",492       "      <th>kms_driven</th>\n",493       "      <th>fuel_type</th>\n",494       "    </tr>\n",495       "  </thead>\n",496       "  <tbody>\n",497       "    <tr>\n",498       "      <th>467</th>\n",499       "      <td>Maruti Suzuki Vitara</td>\n",500       "      <td>Maruti</td>\n",501       "      <td>2017</td>\n",502       "      <td>36000</td>\n",503       "      <td>Diesel</td>\n",504       "    </tr>\n",505       "    <tr>\n",506       "      <th>212</th>\n",507       "      <td>Maruti Suzuki Alto</td>\n",508       "      <td>Maruti</td>\n",509       "      <td>2015</td>\n",510       "      <td>5000</td>\n",511       "      <td>Petrol</td>\n",512       "    </tr>\n",513       "    <tr>\n",514       "      <th>685</th>\n",515       "      <td>Hyundai Santro</td>\n",516       "      <td>Hyundai</td>\n",517       "      <td>2003</td>\n",518       "      <td>51000</td>\n",519       "      <td>Petrol</td>\n",520       "    </tr>\n",521       "    <tr>\n",522       "      <th>538</th>\n",523       "      <td>Maruti Suzuki Alto</td>\n",524       "      <td>Maruti</td>\n",525       "      <td>2019</td>\n",526       "      <td>9800</td>\n",527       "      <td>Petrol</td>\n",528       "    </tr>\n",529       "    <tr>\n",530       "      <th>786</th>\n",531       "      <td>Hyundai Eon</td>\n",532       "      <td>Hyundai</td>\n",533       "      <td>2018</td>\n",534       "      <td>25000</td>\n",535       "      <td>Petrol</td>\n",536       "    </tr>\n",537       "    <tr>\n",538       "      <th>...</th>\n",539       "      <td>...</td>\n",540       "      <td>...</td>\n",541       "      <td>...</td>\n",542       "      <td>...</td>\n",543       "      <td>...</td>\n",544       "    </tr>\n",545       "    <tr>\n",546       "      <th>681</th>\n",547       "      <td>Hyundai Santro AE</td>\n",548       "      <td>Hyundai</td>\n",549       "      <td>2011</td>\n",550       "      <td>45000</td>\n",551       "      <td>Petrol</td>\n",552       "    </tr>\n",553       "    <tr>\n",554       "      <th>135</th>\n",555       "      <td>Toyota Corolla Altis</td>\n",556       "      <td>Toyota</td>\n",557       "      <td>2012</td>\n",558       "      <td>59000</td>\n",559       "      <td>Petrol</td>\n",560       "    </tr>\n",561       "    <tr>\n",562       "      <th>17</th>\n",563       "      <td>Maruti Suzuki Alto</td>\n",564       "      <td>Maruti</td>\n",565       "      <td>2014</td>\n",566       "      <td>35550</td>\n",567       "      <td>Petrol</td>\n",568       "    </tr>\n",569       "    <tr>\n",570       "      <th>668</th>\n",571       "      <td>Maruti Suzuki Swift</td>\n",572       "      <td>Maruti</td>\n",573       "      <td>2014</td>\n",574       "      <td>11523</td>\n",575       "      <td>Petrol</td>\n",576       "    </tr>\n",577       "    <tr>\n",578       "      <th>462</th>\n",579       "      <td>Maruti Suzuki 800</td>\n",580       "      <td>Maruti</td>\n",581       "      <td>2001</td>\n",582       "      <td>81876</td>\n",583       "      <td>Petrol</td>\n",584       "    </tr>\n",585       "  </tbody>\n",586       "</table>\n",587       "<p>652 rows × 5 columns</p>\n",588       "</div>"589      ],590      "text/plain": [591       "                     name  company  year  kms_driven fuel_type\n",592       "467  Maruti Suzuki Vitara   Maruti  2017       36000    Diesel\n",593       "212    Maruti Suzuki Alto   Maruti  2015        5000    Petrol\n",594       "685        Hyundai Santro  Hyundai  2003       51000    Petrol\n",595       "538    Maruti Suzuki Alto   Maruti  2019        9800    Petrol\n",596       "786           Hyundai Eon  Hyundai  2018       25000    Petrol\n",597       "..                    ...      ...   ...         ...       ...\n",598       "681     Hyundai Santro AE  Hyundai  2011       45000    Petrol\n",599       "135  Toyota Corolla Altis   Toyota  2012       59000    Petrol\n",600       "17     Maruti Suzuki Alto   Maruti  2014       35550    Petrol\n",601       "668   Maruti Suzuki Swift   Maruti  2014       11523    Petrol\n",602       "462     Maruti Suzuki 800   Maruti  2001       81876    Petrol\n",603       "\n",604       "[652 rows x 5 columns]"605      ]606     },607     "execution_count": 50,608     "metadata": {},609     "output_type": "execute_result"610    }611   ],612   "source": [613    "X_train"614   ]615  },616  {617   "cell_type": "code",618   "execution_count": 72,619   "metadata": {},620   "outputs": [],621   "source": [622    "from sklearn.linear_model import LinearRegression \n",623    "from sklearn.metrics import r2_score\n",624    "from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder\n",625    "from sklearn.compose import make_column_transformer\n",626    "from sklearn.pipeline import make_pipeline"627   ]628  },629  {630   "cell_type": "code",631   "execution_count": 84,632   "metadata": {},633   "outputs": [634    {635     "data": {636      "text/plain": [637       "0.834097589979544"638      ]639     },640     "execution_count": 84,641     "metadata": {},642     "output_type": "execute_result"643    }644   ],645   "source": [646    "ohe = OneHotEncoder()\n",647    "ohe.fit(X[['name', 'company', 'fuel_type']])\n",648    "\n",649    "transformer = make_column_transformer(\n",650    "    (OneHotEncoder(categories=ohe.categories_), ['name', 'company', 'fuel_type']),\n",651    "      remainder='passthrough')\n",652    "\n",653    "lr = LinearRegression()\n",654    "\n",655    "pipe = make_pipeline(transformer, lr)\n",656    "\n",657    "pipe.fit(X_train, y_train)\n",658    "\n",659    "y_pred = pipe.predict(X_test)\n",660    "\n",661    "r2_score(y_pred, y_test)"662   ]663  },664  {665   "cell_type": "code",666   "execution_count": 86,667   "metadata": {},668   "outputs": [],669   "source": [670    "import pickle"671   ]672  },673  {674   "cell_type": "code",675   "execution_count": 87,676   "metadata": {},677   "outputs": [],678   "source": [679    "pickle.dump(pipe, open('pipe.pkl', 'wb'))"680   ]681  },682  {683   "cell_type": "code",684   "execution_count": 32,685   "metadata": {},686   "outputs": [],687   "source": [688    "pickle.dump(df, open('df.pkl', 'wb'))"689   ]690  },691  {692   "cell_type": "code",693   "execution_count": 88,694   "metadata": {},695   "outputs": [696    {697     "data": {698      "text/plain": [699       "array([438777.10574272])"700      ]701     },702     "execution_count": 88,703     "metadata": {},704     "output_type": "execute_result"705    }706   ],707   "source": [708    "pipe.predict(pd.DataFrame([['Maruti Suzuki Swift', 'Maruti', 2019, 100, 'Petrol']], columns=['name','company','year','kms_driven','fuel_type']))"709   ]710  }711 ],712 "metadata": {713  "kernelspec": {714   "display_name": "myenv",715   "language": "python",716   "name": "python3"717  },718  "language_info": {719   "codemirror_mode": {720    "name": "ipython",721    "version": 3722   },723   "file_extension": ".py",724   "mimetype": "text/x-python",725   "name": "python",726   "nbconvert_exporter": "python",727   "pygments_lexer": "ipython3",728   "version": "3.12.5"729  }730 },731 "nbformat": 4,732 "nbformat_minor": 2733}734