我想创建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'))
答案 0 :(得分:0)
首先,请不要像那样“缩短”您的导入。进口实际上很重要。
假设 train_test_split
是指 sklearn.model_selection.train_test_split
记录的 here,那么问题是:
X
和 Y
是字符串——也就是说,它们的值分别是“X.csv”和“Y.csv”。根据文档,允许的输出是:
<块引用>允许的输入是列表、numpy 数组、scipy-sparse 矩阵或 Pandas 数据帧。
例如,您必须使用 pandas
打开这些 CSV 文件。