信息特征代码无法工作

我想在SciKit Learn中为二元朴素贝叶斯实现一个最具信息量的特征函数。我使用的是Python3。

首先,我知道已经有人问过如何为SciKit的多项式朴素贝叶斯实现某种“信息特征”函数的问题。然而,我尝试了这些回答但没有成功——所以我想可能是SciKit更新了,或者是我做错了什么。我使用的是tobigue在这里提供的一个函数的答案。

from nltk.corpus import stopwordsimport numpy as npfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.pipeline import Pipelinefrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.feature_extraction.text import TfidfTransformerfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.model_selection import GridSearchCVfrom sklearn.model_selection import train_test_split#数组包含一个(标题, 来源)元组列表,其中有两个来源。我想将每个标题分类为属于给定的来源。 array = [('toyota showcases humanoid that mirrors user', 'drudge'), ('virginia again delays vote certification after error in ballot distribution', 'npr'), ("do doctors need to use computers? one physician's case highlights the quandary", 'npr'), ('office sex summons', 'drudge'), ('launch calibrated to avoid military response?', 'drudge'), ('snl skewers alum al franken, trump sons', 'npr'), ('mulvaney shows up for work at consumer watchdog group, as leadership feud deepens', 'npr'), ('indonesia tries to evacuate 100,000 people away from erupting volcano on bali', 'npr'), ('downing street blasts', 'drudge'), ('stocks soar more; records smashed', 'drudge'), ('aid begins to filter back into yemen, as saudi-led blockade eases', 'npr'), ('just look at these fancy port-a-potties', 'npr'), ('nyt turns to twitter activism to thwart', 'drudge'), ('uncertainty reigns in battle for virginia house of delegates', 'npr'), ('u.s. reverses its decision to close palestinian office in d.c.', 'npr'), ("'i don't believe in science,' says flat-earther set to launch himself in own rocket", 'npr'), ("bosnian war chief 'dies' after being filmed 'drinking poison' at the hague", 'drudge'), ('federal judge blocks new texas anti-abortion law', 'npr'), ('gm unveils driverless cars, aiming to lead pack', 'drudge'), ('in japan, a growing scandal over companies faking product-quality data', 'npr')]#我想将每个标题分类为属于给定的来源。 def scikit_naivebayes(data_array):    headlines = [element[0] for element in data_array]    sources = [element[1] for element in data_array]    text_clf = Pipeline([('vect', CountVectorizer(stop_words='english')), ('tfidf', TfidfTransformer()),('clf', MultinomialNB())])    cf1 = text_clf.fit(headlines, sources)    train(cf1,headlines,sources)    #在CountVectorizer和分类器上调用most_informative_features函数    show_most_informative_features(CountVectorizer, cf1)def train(classifier, X, y):    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=33)    classifier.fit(X_train, y_train)    print ("Accuracy: {}".format(classifier.score(X_test, y_test)))#tobigue的代码: def show_most_informative_features(vectorizer, clf, n=20):    feature_names = vectorizer.get_feature_names()    coefs_with_fns = sorted(zip(clf.coef_[0], feature_names))    top = zip(coefs_with_fns[:n], coefs_with_fns[:-(n + 1):-1])    for (coef_1, fn_1), (coef_2, fn_2) in top:    print ("\t%.4f\t%-15s\t\t%.4f\t%-15s" % (coef_1, fn_1, coef_2, fn_2))def main():    scikit_naivebayes(array)main()#错误: # File "file_path_here", line 34, in program_name# feature_names = vectorizer.get_feature_names()# TypeError: get_feature_names() missing 1 required positional argument: 'self'

回答:

在调用vectorizer.get_feature_names()之前,您需要先对CountVectorizer进行拟合。在您的代码中,您只是用类CountVectorizer调用了另一个函数,这不会产生任何结果。

您应该独立于您的管道,创建一个使用CountVectorizer的向量化器,然后对您的文本调用fit,然后使用已经提供的函数,尽管您应该根据您的问题进一步调整它。

您应该很容易理解,您使用的函数需要一个实例化的对象,而不仅仅是一个类。如果您不明白,请告诉我。

编辑

coef_是一个仅由估计器(即分类器)可访问的属性(并不是所有的)。Pipeline是一个sklearn对象,用于组合不同的步骤以便为分类器提供数据。通常,一个词袋管道由一个特征提取器和一个分类器组成(这里是逻辑回归):

pipeline = Pipeline([('vectorizer', CountVectorizer(args)),('classifier', LogisticRegression()])

所以,在您的情况下,您应该避免使用管道(我建议您从这里开始),或者使用管道的get_params()方法来访问分类器。

我建议您对文本进行拟合转换,然后将转换后的结果输入到逻辑回归或朴素贝叶斯分类器中,然后调用您拥有的函数:

vectorizer = CountVectorizer(stop_words='english')X = vectorizer.fit_transform(headlines, sources)naive_bayes = MultinomialNB()naive_bayes.fit(X, sources)show_most_informative_features(vectorizer, naive_bayes)

先尝试这个,如果它工作了,您将更好地理解如何然后使用管道。请注意,您的管道不应该工作,因为您结合了两个特征提取器,最后一步应该是估计器。如果您想堆叠两个特征提取器,您需要查看FeatureUnion

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注