Below is the dataset for which I am trying to implement Linear regression in python.

   age     sex     bmi  children smoker     region      charges
0   19  female  27.900         0    yes  southwest  16884.92400
1   18    male  33.770         1     no  southeast   1725.55230
2   28    male  33.000         3     no  southeast   4449.46200
3   33    male  22.705         0     no  northwest  21984.47061
4   32    male  28.880         0     no  northwest   3866.85520

I am confused what to do with the columns children, smoker, sex as they are of type "object".

Data columns (total 7 columns):
age         1338 non-null int64
sex         1338 non-null object
bmi         1338 non-null float64
children    1338 non-null int64
smoker      1338 non-null object
region      1338 non-null object
charges     1338 non-null float64

Do I have to convert this to numeric before building my model ? Please provide your suggestions.

Thank you.


Yes, you will have to convert everything to numeric. That requires thinking about what these attributes represent accordingly you can use either the below 3 options.

There are three options:

  1. One-Hot encoding for categorical data
  2. Arbitrary numbers for ordinal data
  3. Use something like group means for categorical data (e. g. mean prices for city districts).

You have to be careful to not infuse information you do not have in the application case.

I'm expanding on option 1 and 3, if you want to know about option 2 you can go through the links attached at last.

One hot encoding

If you have categorical data, you can create dummy variables with 0/1 values for each possible value. Similarly you could implement for children, smoker.

E. g.

id  Sex
0   Male
1   Feamle


id   Male   Female 
0      1       0    
1      0       1   

This can easily be done with pandas:

import pandas as pd

data = pd.DataFrame({'Sex': ['Male', 'Female']})

will result in:

        Sex_Male  Sex_Female 
0           1            0         
1           0            1        

Using categorical data for groupby operations

This is an additional usecase but in your case it is not necessary to use this but if you feel so, you can try implementing this as well

You could use the mean for each category over past (known events).

Say you have a DataFrame with the last known mean prices for cities:

prices = pd.DataFrame({
    'city': ['A', 'A', 'A', 'B', 'B', 'C'],
    'price': [1, 1, 1, 2, 2, 3],
mean_price = prices.groupby('city').mean()
data = pd.DataFrame({'city': ['A', 'B', 'C', 'A', 'B', 'A']})

print(data.merge(mean_price, on='city', how='left'))


  city  price
0    A      1
1    B      2
2    C      3
3    A      1
4    B      2
5    A      1

For better understanding you can go through this Link-1, Link-2

  • $\begingroup$ If you are looking for any additional information let me know, would expand on it. If you got what you are looking for, you can accept the answer. Thanks in Advance. $\endgroup$ – Toros91 May 10 '18 at 6:08

First notice that the column "children" is already numeric, so you don't need to do anything.

The columns "Sex" and "Smoker" are categorical and yes you need to change them to numeric form. This is because many ML algorithms like Linear Regression require data in numeric form, they can't operate directly on text data. You can check more answers here-


How to convert categorical to numeric form?

This has been answered by Toros91, you have get_dummies function in pandas which create dummy columns for each category of categorical feature in your data. So for your case,

>>> pd.get_dummies(df, columns=['sex','smoker'])
age    bmi     children   region      charges   sex_female  sex_male  smoker_no  smoker_yes    
0  19  27.900     0      southwest  16884.92400      1         0           0           1    
1  18  33.770     1      southeast   1725.55230      0         1           1           0    
2  28  33.000     3      southeast   4449.46200      0         1           1           0    
3  33  22.705     0      northwest  21984.47061      0         1           1           0    
4  32  28.880     0      northwest   3866.85520      0         1           1           0    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.