Invalid Type Promotion in Linear Regression using Scikitlearn

Viewed 95

Context of the problem and question

I'm trying to use linear regression from scikit learn on a dataset related to covid19 data. Why am I getting the 'invalid type promotion' error?

My level

I'm completely new to Machine learning and data science. I only know python and pandas so I'm currently expanding my knowledge in machine learning. I don't know a lot of theory about which data types machine learning algorithms can handle and why missing values are a problem, etc.

Overview of the data

<class 'pandas.core.frame.DataFrame'>
Int64Index: 2768 entries, 14421 to 98025
Data columns (total 10 columns):
 #   Column                 Non-Null Count  Dtype         
---  ------                 --------------  -----         
 0   date                   2768 non-null   datetime64[ns]
 1   location               2768 non-null   object        
 2   new_deaths             2768 non-null   float64       
 3   female_smokers         2768 non-null   float64       
 4   male_smokers           2768 non-null   float64       
 5   population             2768 non-null   float64       
 6   people_vaccinated      2768 non-null   float64       
 7   cardiovasc_death_rate  2768 non-null   float64       
 8   aged_65_older          2768 non-null   float64       
 9   gdp_per_capita         2768 non-null   float64       
dtypes: datetime64[ns](1), float64(8), object(1)

  • There are no missing values (I've replaced missing values with '0's)

The code and output

reg = linear_model.LinearRegression()
x =['date', 'people_vaccinated', 'population']
y = 'new_deaths'
reg.fit(df[x], df[y])

Output

TypeError                                 Traceback (most recent call last)
<ipython-input-65-74f8f535dda9> in <module>()
      2 x =['date', 'people_vaccinated', 'population']
      3 y = 'new_deaths'
----> 4 reg.fit(df[x], df[y])

2 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    473 
    474         if all(isinstance(dtype, np.dtype) for dtype in dtypes_orig):
--> 475             dtype_orig = np.result_type(*dtypes_orig)
    476 
    477     if dtype_numeric:

<__array_function__ internals> in result_type(*args, **kwargs)

TypeError: invalid type promotion
1 Answers

The error is because of the date column in your input train data i.e., x. Since date is not a particular variable value that fits into linear regression line y = mx+c, you need to convert the date into numeric type like below:

x['date'] = x['date'].map(dt.datetime.toordinal)

Also I wonder why you need date when you are trying to fit a regression line. date is generally used in the case of time-series forecasting that too mainly for plotting purpose. The model is not trained with datetime kind of data.

Related