gpt4 book ai didi

python - 评分系统 - 输入特征

转载 作者:行者123 更新时间:2023-11-30 09:47:34 25 4
gpt4 key购买 nike

我正在研究评分系统(毕业项目)。我对数据进行了预处理,然后对数据使用 TfidfVectorizer 并使用 LinearSVC 来拟合模型。

系统如下,有265个定义,任意长度;但总的来说,它们的形状为 (265, 8581 )所以当我尝试输入一些新的随机句子来预测它时,我收到此消息

Error Message

如果您愿意,您可以查看所使用的代码(完整和长);

使用的代码;

def normalize(df):
lst = []
for x in range(len(df)):
text = re.sub(r"[,.'!?]",'', df[x])
lst.append(text)
filtered_sentence = ' '.join(lst)
return filtered_sentence


def stopWordRemove(df):
stop = stopwords.words("english")
needed_words = []
for x in range(len(df)):

words = word_tokenize(df)
for word in words:
if word not in stop:
needed_words.append(word)
return needed_words


def prepareDataSets(df):
sentences = []
for index, d in df.iterrows():
Definitions = stopWordRemove(d['Definitions'].lower())
Definitions_normalized = normalize(Definitions)
if d['Results'] == 'F':
sentences.append([Definitions, 'false'])
else:
sentences.append([Definitions, 'true'])
df_sentences = DataFrame(sentences, columns=['Definitions', 'Results'])
for x in range(len(df_sentences)):
df_sentences['Definitions'][x] = ' '.join(df_sentences['Definitions'][x])
return df_sentences

def featureExtraction(data):
vectorizer = TfidfVectorizer(min_df=10, max_df=0.75, ngram_range=(1,3))
tfidf_data = vectorizer.fit_transform(data)
return tfidf_data

def learning(clf, X, Y):
X_train, X_test, Y_train, Y_test = \
cross_validation.train_test_split(X,Y, test_size=.2,random_state=43)
classifier = clf()
classifier.fit(X_train, Y_train)
predict = cross_validation.cross_val_predict(classifier, X_test, Y_test, cv=5)
scores = cross_validation.cross_val_score(classifier, X_test, Y_test, cv=5)
print(scores)
print ("Accuracy of %s: %0.2f(+/- %0.2f)" % (classifier, scores.mean(), scores.std() *2))
print (classification_report(Y_test, predict))

然后我运行这些脚本:之后我得到了提到的错误

test = LinearSVC()
data, target = preprocessed_df['Definitions'], preprocessed_df['Results']
tfidf_data = featureExtraction(data)
X_train, X_test, Y_train, Y_test = \
cross_validation.train_test_split(tfidf_data,target, test_size=.2,random_state=43)
test.fit(tfidf_data, target)
predict = cross_validation.cross_val_predict(test, X_test, Y_test, cv=10)
scores = cross_validation.cross_val_score(test, X_test, Y_test, cv=10)
print(scores)
print ("Accuracy of %s: %0.2f(+/- %0.2f)" % (test, scores.mean(), scores.std() *2))
print (classification_report(Y_test, predict))
Xnew = ["machine learning is playing games in home"]
tvect = TfidfVectorizer(min_df=1, max_df=1.0, ngram_range=(1,3))
X_test= tvect.fit_transform(Xnew)
ynew = test.predict(X_test)

最佳答案

您永远不会在测试时调用 fit_transform(),仅调用 transform() 并使用与训练数据相同的向量化器。

这样做:

def featureExtraction(data):
vectorizer = TfidfVectorizer(min_df=10, max_df=0.75, ngram_range=(1,3))
tfidf_data = vectorizer.fit_transform(data)

# Here I am returning the vectorizer as well, which was used to generate the training data
return vectorizer, tfidf_data
...
...
tfidf_vectorizer, tfidf_data = featureExtraction(data)
...
...

# Now using the same vectorizer on test data
X_test= tfidf_vectorizer.transform(Xnew)
...

在您的代码中,您使用的是新的 TfidfVectorizer,它显然不知道训练数据,也不知道训练数据有 8581 个特征。

测试数据的准备方式应始终与准备训练数据的方式相同。否则,即使没有出现错误,结果也是错误的,并且模型将不会像实际场景中那样执行。

请参阅我的其他答案,解释不同特征预处理技术的类似情况:

我会将此问题标记为其中一个问题的重复项,但看到您完全使用新的矢量化器并且有不同的方法来转换训练数据,我回答了这个问题。从下次开始,请先搜索问题并尝试了解类似场景中发生的情况,然后再发布问题。

关于python - 评分系统 - 输入特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com