Issue
I am building a logistic Regression model using Sci-kit Learn. My data is made up mainly of float and int types except for the date column which is of datetime64[ns](Its type was first object, then I converted it using
df['date'] = pd.to_datetime(df['date'],infer_datetime_format=True)
I did split my data to train and test and when trying to fit the model using logr.fit(X,Y)
I get the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-18dd45102c66> in <module>
----> 1 logr.fit(X,Y)
/opt/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py in fit(self, X, y, sample_weight)
1342 X, y = self._validate_data(X, y, accept_sparse='csr', dtype=_dtype,
1343 order="C",
-> 1344 accept_large_sparse=solver != 'liblinear')
1345 check_classification_targets(y)
1346 self.classes_ = np.unique(y)
/opt/anaconda3/lib/python3.7/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
430 y = check_array(y, **check_y_params)
431 else:
--> 432 X, y = check_X_y(X, y, **check_params)
433 out = X, y
434
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
71 FutureWarning)
72 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73 return f(**kwargs)
74 return inner_f
75
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
801 ensure_min_samples=ensure_min_samples,
802 ensure_min_features=ensure_min_features,
--> 803 estimator=estimator)
804 if multi_output:
805 y = check_array(y, accept_sparse='csr', force_all_finite=True,
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
71 FutureWarning)
72 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73 return f(**kwargs)
74 return inner_f
75
/opt/anaconda3/lib/python3.7/site-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, estimator)
532
533 if all(isinstance(dtype, np.dtype) for dtype in dtypes_orig):
--> 534 dtype_orig = np.result_type(*dtypes_orig)
535
536 if dtype_numeric:
<__array_function__ internals> in result_type(*args, **kwargs)
TypeError: invalid type promotion.
I am not able to understand what this error is pointing to. However from research, I found that it could be related to the date type but found nothing in the error that points out to date in particular. Any idea?
Solution
After converting the date using:
df['date'] = pd.to_datetime(df['date'],infer_datetime_format=True)
I applied the following statement:
df['date']=df['date'].apply(lambda x: x.toordinal())
And it worked perfectly.
Answered By - Sal_H
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.