如何在Python中拟合两个列表?

发布于 2025-02-10 23:34:06 字数 215 浏览 1 评论 0原文

我想与Sklearn一起使用这两个列表,但最后说:无法将字符串转换为float ...您能帮我吗?

 from sklearn import tree
 x = ['BMW', '20000miles', '2010']
 y = ['12000']
 clf = tree.DecisionTreeClassifier()
 clf = clf.fit(x, y)

I want to fit these two lists with sklearn but at the end it say : could not convert string to float... can you help me with that?

 from sklearn import tree
 x = ['BMW', '20000miles', '2010']
 y = ['12000']
 clf = tree.DecisionTreeClassifier()
 clf = clf.fit(x, y)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-02-17 23:34:06

许多事情。
从文档中:

X{array-like, sparse matrix} of shape (n_samples, n_features)
The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)
The target values (class labels) as integers or strings.

您的输入应为形状(n_samples,n_features)。您有1个具有3个功能的样本吗?我想这没关系,但是拟合1个样本并没有太多意义。

但是您的模型无法解释“ BMW”,它预计会有浮动。因此,如果您有3种类型的汽车,宝马,奥迪,梅赛德斯,将它们转换为一个数字,即1,2,3代表它们。

A number of things.
From the documentation:

X{array-like, sparse matrix} of shape (n_samples, n_features)
The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)
The target values (class labels) as integers or strings.

Your input to fit should be an array of shape (n_samples, n_features). Do you have 1 sample with 3 features? I suppose that is ok, but fitting 1 sample doesn't make much sense.

But your model can't interpret "BMW", it expects a float. So if you have 3 types of cars, BMW, AUDI, MERCEDES, convert them to a number, i.e. 1,2,3 to represent them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文