python - Errno 36:文件名太长:“testLogisticRegression”
问题描述
我有一个进行分类的功能。导出数据时遇到问题。这是堆栈跟踪:
[Errno 36] File name too long: "testLogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n intercept_scaling=1, l1_ratio=None, max_iter=100,\n multi_class='warn', n_jobs=None, penalty='l2',\n random_state=None, solver='warn', tol=0.0001, verbose=0,\n warm_start=False).xlsx"
我的代码:
def classifieur(X, y):
X = matrix(X)
model_l = LinearSVC()
model_m = MultinomialNB()
model_lr = LogisticRegression()
model_r = RandomForestClassifier()
model_k = KNeighborsClassifier()
models = [model_l, model_m, model_lr, model_r, model_k]
cv_splitter = KFold(n_splits=10, shuffle=False, random_state=None)
for model in models:
y_pred = cross_val_predict(model, X, y, cv=cv_splitter)
print("Model: {}".format(model))
print("Accuracy: {}".format(accuracy_score(y, y_pred)))
# export
res = pd.DataFrame()
res['Expected Output'] = y
res['Predicted Output'] = y_pred
print(output.head())
res.to_excel("test{}.xlsx".format(model))
classifieur(X, y)
该功能有效。它只是造成问题的文件的名称。我在linux环境中工作。
解决方案
使用模型名称和模型值创建一个字典,并将模型名称用作文件名:
def classifieur(X, y):
X = matrix(X)
model_l = LinearSVC()
model_m = MultinomialNB()
model_lr = LogisticRegression()
model_r = RandomForestClassifier()
model_k = KNeighborsClassifier()
models = {'model_l': model_l, 'model_m': model_m, 'model_lr': model_lr, 'model_r': model_r, 'model_k': model_k}
cv_splitter = KFold(n_splits=10, shuffle=False, random_state=None)
for model_name, model in models.items():
y_pred = cross_val_predict(model, X, y, cv=cv_splitter)
print("Model: {}".format(model))
print("Accuracy: {}".format(accuracy_score(y, y_pred)))
# export
res = pd.DataFrame()
res['Expected Output'] = y
res['Predicted Output'] = y_pred
print(output.head())
res.to_excel("test{}.xlsx".format(model_name))
推荐阅读
- javascript - 在样式组件道具上使用反向选择器模式时不起作用?
- html - 如何使用 JScript 从 HTML 网页自动化 Excel?
- java - API 调用似乎在 React 中不起作用
- reactjs - 如何更改 Material-UI 线性进度条不确定的动画速度?
- android - 将项目导入 Android Studio。同步因“未知原因”而失败。有什么潜在的原因吗?
- java - 如何解决错误“找不到符号”和“')'预期”?
- python - TensorFlow 图形数学
- mod-wsgi - Apache 配置:“客户端被服务器配置拒绝”
- javascript - 尝试使用 sweetalert2 从 javascript 上传时出现“未定义的数组键‘文件’”
- javascript - 使用 Javascript 在共享驱动器 (Google Drive API) 中创建文件夹