根据其他可用列创建3个分类模型来预测类别

问题描述 投票:-1回答:1

我有三种类型的类(stetosa,versicolor,virginica),还有其他4列,如sepal_length,sepal_width,petal_length,petal_width,大约有150行,每行都填充有自己的信息(所以那里没有什么是空的)。我需要根据其他列预测类的类型。这是我尝试过的:

import numpy as np
import pandas as pd
df = pd.read_csv("data.csv")
X=df[["sepal_length","sepal_width","petal_length","petal_width"]]
y=df["class"]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.1)

from sklearn.linear_model import LinearRegression
clf=LinearRegression()
clf.fit(y_train, X_train)
clf.predict(y_test)

标记为已答复此问题的文本:ValueError:无法将字符串转换为float:'virginica'我需要通过培训和测试来做到这一点。

python pandas machine-learning prediction modeling
1个回答
0
投票

您需要对数据进行编码。换句话说,将每个类别转换为数字(整数或浮点数)。

映射如下类别:

mapping={'setosa':0,'versicolor':1,'virginica':2}
y.map(mapping)

训练模型后,结果为0,1 or 2。将其转换回去,您将获得预测。

而且,如果要预测一个类,则必须更改模型。 LinearRegression()是一个数值预测变量,它只能预测数值。

尝试改用SVCLogisticRegression或任何其他分类模型。

© www.soinside.com 2019 - 2024. All rights reserved.