使用python的多模型精度json结果格式

时间:2018-04-06 12:04:52

标签: python json model

我正在构建一个多模型,我得到了7个模型精度的结果,我需要使用适当的json格式的结果。

我的多个模型构建代码将是这样的

seed = 7

"prepare models"

models = []
models.append(('LogisticRegression', LogisticRegression()))
models.append(('LinearDiscriminantAnalysis', LinearDiscriminantAnalysis()))
models.append(('KNeighborsClassifier', KNeighborsClassifier()))
models.append(('DecisionTreeClassifier', DecisionTreeClassifier()))
models.append(('GaussianNB', GaussianNB()))
models.append(('RandomForestClassifier',RandomForestClassifier()))
models.append(('SVC', SVC()))

"evaluate each model in turn"

results = []
names = []
kfold_result = {}
scoring = 'accuracy'

# Kfold model selection

for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    cv_results = model_selection.cross_val_score(model, train[features_train],train["Churn"], cv=kfold, scoring=scoring)
    results.append(cv_results)
    names.append('"%s"' %name)
    # Appending result in new dictionary
    kfold_result[name] = cv_results.mean()
    model_results = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
#print(model_results)

# For testing im just printing the dictionary values

#print(kfold_result)
#type(kfold_result)

from collections import OrderedDict
sorted_model = OrderedDict(sorted(kfold_result.items(), key = lambda x:x[1], reverse = True))
#    print(sorted_model)
#    type(sorted_model)

    # make predictions on validation dataset

for key in sorted_model.keys():
    print(key)
    break

# if condition

if(key == "SVC"):
    prediction_model = SVC()
elif(key == "RandomForestClassifier"):
    prediction_model = RandomForestClassifier()
elif(key == "GaussianNB"):
    prediction_model = GaussianNB()
elif(key == "DecisionTreeClassifier"):
    prediction_model = DecisionTreeClassifier()
elif(key == "KNeighborsClassifier"):
    prediction_model = KNeighborsClassifier()
elif(key == "LinearDiscriminantAnalysis"):
    prediction_model = LinearDiscriminantAnalysis()
elif(key == "LogisticRegression"):
    prediction_model = LogisticRegression()

prediction_model.fit(train[features_train], train["Churn"])
predictions = prediction_model.predict(test[features_test])
Model_accuracy = accuracy_score(test["Churn"], predictions)

我得到一个json结果,这个排序的多个模型的准确性将是这样的

"sorted_model_results": {
            "LogisticRegression": 0.801307365,
            "LinearDiscriminantAnalysis": 0.7919713349,
            "SVC": 0.7490145069,
            "KNeighborsClassifier": 0.7576049658,
            "DecisionTreeClassifier": 0.7200680011,
            "RandomForestClassifier": 0.7775861347,
            "GaussianNB": 0.7521913796
        }

但是,我的预期输出必须是这样的,

[
    {
        "model": [
            {
                "model_name": "LogisticRegression",
                "model_accuracy": 80.131
            },
            {
                "model_name": "LinearDiscriminantAnalysis",
                "model_accuracy": 80.131
            }
        ]
    }
]

我需要像以上格式的json结果。如何更改我的代码以获得像这样的json结果

1 个答案:

答案 0 :(得分:0)

from collections import OrderedDict
    sorted_model = dict(OrderedDict(sorted(kfold_result.items(), key = lambda x:x[1], reverse = True)))

    s = pd.Series(sorted_model)

    a = pd.DataFrame(s).reset_index()

    sorted_models = a.rename(columns={'index':'model_name', 0 : 'model_accuracy'})

我通过将dict转换为系列和数据帧得到了预期的输出,然后我重命名了dataframe的列名。最后我将结果转换为json。

我的输出,

[
    {
        "model": [
            {
                "model_name": "LogisticRegression",
                "model_accuracy": 80.131
            },
            {
                "model_name": "LinearDiscriminantAnalysis",
                "model_accuracy": 80.131
            }
        ]
    }
]