Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python LightGBMError:不支持功能名称中的特殊JSON字符-相同的代码在jupyter中工作,但不支持';我不在Spyder工作_Python_Jupyter_Spyder_Lightgbm - Fatal编程技术网

Python LightGBMError:不支持功能名称中的特殊JSON字符-相同的代码在jupyter中工作,但不支持';我不在Spyder工作

Python LightGBMError:不支持功能名称中的特殊JSON字符-相同的代码在jupyter中工作,但不支持';我不在Spyder工作,python,jupyter,spyder,lightgbm,Python,Jupyter,Spyder,Lightgbm,我有以下代码: most_important = features_importance_chi(importance_score_tresh, df_user.drop(columns = 'CHURN'),churn) X = df_user.drop(columns = 'CHURN') churn[churn==2] = 1 y = churn # handle undersample problem X,y = handle_

我有以下代码:

    most_important = features_importance_chi(importance_score_tresh, 
    df_user.drop(columns = 'CHURN'),churn)
    X = df_user.drop(columns = 'CHURN')
    churn[churn==2] = 1
    y = churn

    # handle undersample problem
    X,y = handle_undersampe(X,y)

    # train the model

    X=X.loc[:,X.columns.isin(most_important)].values
    y=y.values

    parameters = {
    'application': 'binary',
    'objective': 'binary',
    'metric': 'auc',
    'is_unbalance': 'true',
    'boosting': 'gbdt',
    'num_leaves': 31,
    'feature_fraction': 0.5,
    'bagging_fraction': 0.5,
    'bagging_freq': 20,
    'learning_rate': 0.05,
    'verbose': 0
    }

    # split data
    x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

    train_data = lightgbm.Dataset(x_train, label=y_train)
    test_data = lightgbm.Dataset(x_test, label=y_test)
    model = lightgbm.train(parameters,
                       train_data,
                       valid_sets=[train_data, test_data], 
                       **feature_name=most_important,**
                       num_boost_round=5000,
                       early_stopping_rounds=100) 
和返回最重要参数的函数

def features_importance_chi(importance_score_tresh, X, Y):
    model = ExtraTreesClassifier(n_estimators=10)
    model.fit(X,Y.values.ravel())
    feature_list = pd.Series(model.feature_importances_,
                             index=X.columns)
    feature_list = feature_list[feature_list > importance_score_tresh]
    feature_list = feature_list.index.values.tolist()
    return feature_list
有趣的是Spyder中的代码返回以下错误

LightGBMError:不支持功能名称中的特殊JSON字符。

但在朱皮特很好用。我能够打印最重要功能的列表


你知道这个错误的原因吗?

你知道吗,这个消息经常出现在LGBMClassifier()模型上,即LGBM。 一旦你上传了熊猫的数据,并且你的头有问题,只要把这一行放在开头就行了:

import re
df = df.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '', x))

我认为您忘记了一些关于数据帧列的代码,以便将数据用于两个数据集
(训练+测试)
。确保您不只是在测试集上使用它