我正在腌制一个模型以备后用。然后加载模型并在其上运行predict_proba
。我得到ValueError: X has 1 features per sample; expecting 319
。不知道我是否正确转换
import csv, pickle
from sklearn import svm
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.calibration import CalibratedClassifierCV
import numpy as np
import operator
train_data = []
train_labels = []
test_lables = []
test_lables.append("nah")
with open('training_file', 'r') as f:
reader = csv.reader(f, dialect='excel', delimiter='\t')
for row in reader:
train_data.append(row[0])
train_labels.append(row[1])
lables = []
for item in train_labels:
if item in lables:
continue
else:
lables.append(item)
def linear_svc(train_data, train_labels):
vectorizer = TfidfVectorizer()
train_vectors = vectorizer.fit_transform(train_data)
classifier_linear = svm.LinearSVC()
clf = CalibratedClassifierCV(classifier_linear)
clf.fit(train_vectors, train_labels)
with open('test', 'wb') as fi:
pickle.dump(clf, fi)
def run_classifier():
vectorizer = TfidfVectorizer()
test_vectors = vectorizer.fit_transform(test_lables)
with open('test', 'rb') as fi:
clf = pickle.load(fi)
prediction_linear = clf.predict_proba(test_vectors)
return prediction_linear
#linear_svc(train_data, train_labels)
sorted_intent_probability = run_classifier()
print(sorted_intent_probability)
我首先调用
linear_svc()
方法。模型被腌制。然后,我呼叫run_classifier()
。我在这里做错了什么?另外,当我结合这两种方法时,它可以正常工作:def linear_svc(train_data, train_labels, test_lables):
vectorizer = TfidfVectorizer()
train_vectors = vectorizer.fit_transform(train_data)
test_vectors = vectorizer.transform(test_lables)
classifier_linear = svm.LinearSVC()
clf = CalibratedClassifierCV(classifier_linear)
clf.fit(train_vectors, train_labels)
prediction_linear = clf.predict_proba(test_vectors)
return prediction_linear
我是否也需要腌制矢量化器,以后再使用?
最佳答案
我有问题。当我创建TfidfVectorizer()
的新实例时,我没有使用与培训相同的功能。我做了以下更改
linear_svc_model = clf.fit(train_vectors, train_labels)
model_object = []
model_object.append(linear_svc_model)
model_object.append(vectorizer)
然后腌制这个模型对象。然后在使用未腌制的分类器和矢量化器时,在训练字符串上使用相同的方法。有效。