其他分享
首页 > 其他分享> > 带有Scikit-Learn的Google云ML引发:“ dict”对象没有属性“ lower”

带有Scikit-Learn的Google云ML引发:“ dict”对象没有属性“ lower”

作者:互联网

我使用以下教程在Google Cloud中使用我的Scikit学习情感分析模型:
https://cloud.google.com/ml-engine/docs/scikit/quickstart

我的模型定义如下:

import csv
import os
from collections import defaultdict
import sys
import re
import numpy as np
import random
import math

import sklearn.datasets
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
from sklearn import metrics
from sklearn.model_selection import GridSearchCV
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score

def build_data_cv(datafile, trait_number):
    """
    Loads data
    """

    with open(datafile, "rb") as csvf:
        csvreader = csv.reader(csvf,delimiter=',',quotechar='"')
        data = []
        target = np.array([])

        for index, line in enumerate(csvreader):

            # escape header
            if index < 1:
                continue

            document = unicode(line[1], errors='replace')

            data.append(document)

            index_of_trait = trait_number + 2

            if line[index_of_trait].lower()=='y':
                target = np.append(target, 1.0)
            else:
                target = np.append(target, 0.0)

        dataset = sklearn.datasets.base.Bunch(data=data, target=target)
        dataset.target_names = ["positive", "negative"]

        return dataset

# main program
if __name__=="__main__":
    current_directory = os.getcwd() + "/"
    data_file = current_directory + "essays.csv"

    class_labels = ['EXT','NEU','AGR','CON','OPN']

    for index, selected_trait in enumerate(class_labels):
        print selected_trait

        dataset = build_data_cv(data_file, index)

        X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.2, random_state=0)

        clf = Pipeline([('vect', CountVectorizer()),
                      ('tfidf', TfidfTransformer()),
                      ('clf', SGDClassifier(loss='hinge', penalty='l2',
                                            alpha=1e-3, random_state=42,
                                            max_iter=5, tol=None)),
        ])

        # clf.fit(X_train, y_train)
        parameters = {'vect__ngram_range': [(1, 1), (1, 2)],
               'tfidf__use_idf': (True, False),
               'clf__alpha': (1e-2, 1e-3),
        }
        gs_clf = GridSearchCV(clf, parameters, n_jobs=-1)

        # fit the model
        gs_clf.fit(X_train, y_train)

        # simple test score
        # print clf.score(X_test, y_test)

        # 10-fold cross-validation score
        scores = cross_val_score(gs_clf, dataset.data, dataset.target, cv=10)
        print("Accuracy: %0.4f (+/- %0.4f)" % (scores.mean(), scores.std() * 2))

        # Export the classifier to a file
        joblib.dump(gs_clf, 'svm_gs_'+selected_trait+'.joblib')

        print "______________________"

输入文件位于此处:
https://github.com/novinfard/profiler-sentiment-analysis/blob/master/model/input_dataset/essays.csv

当我想使用以下shell命令使用gcloud ml-engine local predict测试我的模型时:

    MODEL_DIR="gs://MY_BUCKET/"
    
    INPUT_FILE="input.json"
    
    FRAMEWORK="SCIKIT_LEARN"

   gcloud ml-engine local predict --model-dir=$MODEL_DIR \
   --json-instances $INPUT_FILE \
    
   --framework $FRAMEWORK

它引起以下错误:

  File "/Users/XXX/google-cloud-sdk/lib/third_party/ml_sdk/cloud/ml/prediction/frameworks/sk_xg_prediction_lib.py", line 57, in predict
    "Exception during sklearn prediction: " + str(e))
cloud.ml.prediction.prediction_utils.PredictionError: Failed to run the provided model: Exception during sklearn prediction: 'dict' object has no attribute 'lower' (Error code: 2)

input.json定义如下:

{"instances": [["the quick brown fox"],["another test"]]}

问题是什么,如何解决?

解决方法:

正如评论中所讨论的,包含JSON_INSTANCES的文件在每行上都需要有一个JSON.

在这种情况下

"the quick brown fox"
"another test"

看起来很奇怪,但这是有效的JSON.

标签:gcloud,python,scikit-learn,machine-learning
来源: https://codeday.me/bug/20191011/1891496.html