问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
import pandas as pd
df = pd.read_csv('C:/Users/HUAWEI/Desktop/iris.csv',index_col=0)
df.head()
import matplotlib.pyplot as plt
df.hist(figsize=(10,8))
plt.show()
X = df.loc[:,df.columns!='classes']
y = df.loc[:,df.columns=='classes']
from sklearn.model_selection import train_test_split
X_tr,X_ts,y_tr,y_ts = train_test_split(X,y)
X_tr.shape,X_ts.shape
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
dtc = dtc.fit(X_tr,y_tr)
运行结果及报错内容
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16372/2573424984.py in <module>
1 from sklearn.tree import DecisionTreeClassifier
2 dtc = DecisionTreeClassifier()
----> 3 dtc = dtc.fit(X_tr,y_tr)
D:\ANACONDA\lib\site-packages\sklearn\tree\_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
935 """
936
--> 937 super().fit(
938 X,
939 y,
D:\ANACONDA\lib\site-packages\sklearn\tree\_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
163 check_X_params = dict(dtype=DTYPE, accept_sparse="csc")
164 check_y_params = dict(ensure_2d=False, dtype=None)
--> 165 X, y = self._validate_data(
166 X, y, validate_separately=(check_X_params, check_y_params)
167 )
D:\ANACONDA\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
576 # :(
577 check_X_params, check_y_params = validate_separately
--> 578 X = check_array(X, **check_X_params)
579 y = check_array(y, **check_y_params)
580 else:
D:\ANACONDA\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, estimator)
744 array = array.astype(dtype, casting="unsafe", copy=False)
745 else:
--> 746 array = np.asarray(array, order=order, dtype=dtype)
747 except ComplexWarning as complex_warning:
748 raise ValueError(
D:\ANACONDA\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order, like)
100 return _asarray_with_like(a, dtype=dtype, order=order, like=like)
101
--> 102 return array(a, dtype, copy=False, order=order)
103
104
D:\ANACONDA\lib\site-packages\pandas\core\generic.py in __array__(self, dtype)
1991
1992 def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
-> 1993 return np.asarray(self._values, dtype=dtype)
1994
1995 def __array_wrap__(
D:\ANACONDA\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order, like)
100 return _asarray_with_like(a, dtype=dtype, order=order, like=like)
101
--> 102 return array(a, dtype, copy=False, order=order)
103
104
ValueError: could not convert string to float: 'setosa'