Value error in linear regression

Could you please help me with this error-:
I’m getting this error while using linear regression-

ValueError Traceback (most recent call last)
in
----> 1 modelfit(alg1, train, test, predictors, target, IDcol, ‘alg1.csv’)
2 coef1 = pd.Series(alg1.coef_, predictors).sort_values()
3 coef1.plot(kind=‘bar’, title=‘Model Coefficients’)

in modelfit(alg, dtrain, dtest, predictors, target, IDcol, filename)
2 def modelfit(alg, dtrain, dtest, predictors, target, IDcol, filename):
3 #Fit the algorithm on the data
----> 4 alg.fit(dtrain[predictors], dtrain[target])

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in fit(self, X, y, sample_weight)
456 n_jobs_ = self.n_jobs
457 X, y = check_X_y(X, y, accept_sparse=[‘csr’, ‘csc’, ‘coo’],
–> 458 y_numeric=True, multi_output=True)
459
460 if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:

~\Anaconda3\lib\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, warn_on_dtype, estimator)
754 ensure_min_features=ensure_min_features,
755 warn_on_dtype=warn_on_dtype,
–> 756 estimator=estimator)
757 if multi_output:
758 y = check_array(y, ‘csr’, force_all_finite=True, ensure_2d=False,

~\Anaconda3\lib\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, warn_on_dtype, estimator)
565 # make sure we actually converted to numeric:
566 if dtype_numeric and array.dtype.kind == “O”:
–> 567 array = array.astype(np.float64)
568 if not allow_nd and array.ndim >= 3:
569 raise ValueError(“Found array with dim %d. %s expected <= 2.”

ValueError: could not convert string to float: ‘Low Fat’

Hi @anushka21,

You have some categorical values in your dataset and before training the model you need to convert these values into numbers. You can use either of the two methods - One hot encoding and Lable Encoding.

Refer this Article :

2 Likes

Thank you for the help.I tried…it’s working

© Copyright 2013-2019 Analytics Vidhya