I am building a multiple model and i am getting results with 7 models accuracy, i need those results with a proper json format.
My multiple model building code will be like this
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 selectionfor 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 dictionarykfold_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 datasetfor key in sorted_model.keys():print(key)break# if conditionif(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)
I am getting a json results for this sorted multiple model accuracy will be like this
"sorted_model_results": {"LogisticRegression": 0.801307365,"LinearDiscriminantAnalysis": 0.7919713349,"SVC": 0.7490145069,"KNeighborsClassifier": 0.7576049658,"DecisionTreeClassifier": 0.7200680011,"RandomForestClassifier": 0.7775861347,"GaussianNB": 0.7521913796}
But, my expected output have to be like this,
[{"model": [{"model_name": "LogisticRegression","model_accuracy": 80.131},{"model_name": "LinearDiscriminantAnalysis","model_accuracy": 80.131}]}
]
i need the json results like above format. how to change my code to get the json results like this