<class'ValueError'>,ValueError(“无法将字符串转换为float:'X'”),

2024-05-23 15:02:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建flask应用程序,但出现以下错误

(<class 'ValueError'>, ValueError("could not convert string to float: 'X'"), <traceback object at 0x0000022B617C0280>)

这是我的app.py,我的app.py目录中有X.csv和Y.csv文件

    imports..

app = Flask(__name__)

X = 'X.csv'
Y = 'Y.csv'

# Splitting the dataset into the Training set and Test set
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

# Scaling data
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

# Oversample data
smk = SMOTETomek()
# Training data
x_train, y_train = smk.fit_sample(x_train, y_train)
# Testing data
x_test, y_test = smk.fit_sample(x_test, y_test)

# Fitting RandomForestClassifier to the Training set
rfr = RandomForestClassifier()
rfr.fit(x_train, x_train)

# Predicting the Test set results
y_pred = rfr.predict(x_test)

# Saving model to disk
pickle.dump(rfr, open('model.pkl', 'wb'))

# Loading model to compare the results
model = pickle.load(open('model.pkl', 'rb'))

Tags: csvthetotestappdatamodeltraining
1条回答
网友
1楼 · 发布于 2024-05-23 15:02:44

首先,请不要像那样“缩短”你的进口。进口实际上很重要

假设train_test_split是指sklearn.model_selection.train_test_split文档化here,那么问题在于:

  • XY是字符串-也就是说,它们的值分别是“X.csv”和“Y.csv”

根据文件,允许的输出为:

Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.

例如,必须使用pandas打开这些CSV文件

相关问题 更多 >