信息特征代码无法工作

我想在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

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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