在 Python 中将 SVM 拟合到 pandas 数据框上

发布于 2025-01-14 05:10:18 字数 780 浏览 0 评论 0原文

我有一个名为 dataset 的 pandas 数据框,并将其前两列导出为 X ,将名为“Class”的最后一列导出为 y code>:

X = dataset.drop('Class', axis=1)
y = dataset['Class']

然后使用以下代码行,我尝试使用这些数据来拟合 SVM:

from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

之后,使用下面的两行,我尝试训练 SVM,但出现以下错误。

SupportVectorClassModel = SVC()
SupportVectorClassModel.fit(X_train,y_train)

SVM错误

如何解决此问题?

I have a pandas data frame named dataset and I exported the first two columns of it as X and the last column which is named "Class" as y:

X = dataset.drop('Class', axis=1)
y = dataset['Class']

Then using the following lines of code, I tried to fit SVM using these data:

from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

After that, using the two lines below, I tried to train the SVM but I got the following error.

SupportVectorClassModel = SVC()
SupportVectorClassModel.fit(X_train,y_train)

The SVM Error

How can I fix this issue?

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

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

发布评论

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

评论(1

小嗷兮 2025-01-21 05:10:18

尝试将您的数据框转换为列表。然后将数据帧拆分为 X 和 Y 后拆分并适合模型

,使用 values 函数或 tolist 函数将其转换为列表。 IE

X = dataset.drop('Class', axis=1)
y = dataset['Class']
# assuming X has multiple columns and y only one column
X = X.values
y = y['Class'].tolist()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

try to convert your dataframe into the list. then split and fit into model

after you split the dataframe into X and Y, convert it into list by using values function or tolist function. i.e

X = dataset.drop('Class', axis=1)
y = dataset['Class']
# assuming X has multiple columns and y only one column
X = X.values
y = y['Class'].tolist()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

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