Linear Regression in Python 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:


One-Hot encoding for categorical data
Arbitrary numbers for ordinal data
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


to

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']})
print(pd.get_dummies(data))


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'))


Result:

  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