首页 > 解决方案 > 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环境中工作。

标签: pythonpandasdataframecross-validation

解决方案


使用模型名称和模型值创建一个字典,并将模型名称用作文件名:

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))

推荐阅读