Kinev. 2022-10-17 17:38 采纳率: 75%
浏览 53
已结题

Python机器学习基于决策树的鸢尾花分类预测

问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
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'
  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-17 18:08
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月1日
  • 已采纳回答 10月24日
  • 创建了问题 10月17日

悬赏问题

  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)