How to convert string to float . data is in csv error is - ValueError: could not convert string to float: 'unknown'

data_science

#1

how to resolve this error Building a decision tree

ValueError Traceback (most recent call last)
in ()
1 from sklearn.tree import DecisionTreeClassifier
2 classifier = DecisionTreeClassifier()
----> 3 classifier.fit(X_train, y_train)

E:\anacondaff\lib\site-packages\sklearn\tree\tree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
788 sample_weight=sample_weight,
789 check_input=check_input,
–> 790 X_idx_sorted=X_idx_sorted)
791 return self
792

E:\anacondaff\lib\site-packages\sklearn\tree\tree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
114 random_state = check_random_state(self.random_state)
115 if check_input:
–> 116 X = check_array(X, dtype=DTYPE, accept_sparse=“csc”)
117 y = check_array(y, ensure_2d=False, dtype=None)
118 if issparse(X):

E:\anacondaff\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
431 force_all_finite)
432 else:
–> 433 array = np.array(array, dtype=dtype, order=order, copy=copy)
434
435 if ensure_2d:

ValueError: could not convert string to float: ‘unknown’


#2

Hi @urvashi51,

A decision tree cannot handle categorical variables. Here is an article you can refer to understand how to handle categorical variables :