夜与深巷
2022-05-26 18:58
采纳率: 100%
浏览 175

关于#takes 1 positional argument but 2 were given #的问题,如何解决?(语言-python)

问题
报错TypeError: init() takes 1 positional argument but 2 positional arguments (and 2 keyword-only arguments) were given,不知道为什么会出现这样的报错

File "E:/py的作业/情感判定.py", line 112, in
commentCls.fit(train_x, train_y, 0.98)
File "E:/py的作业/情感判定.py", line 74, in fit
self.vectorizer = CountVectorizer(max_df, stop_words=stopwords, ngram_range=(1, 3)).fit(list_text)
TypeError: init() takes 1 positional argument but 2 positional arguments (and 2 keyword-only arguments) were given


class CommentClassifier:
    def __init__(self, classifier_type, vector_type):
        self.classifier_type = classifier_type  # 分类器类型:支持向量机或贝叶斯分类
        self.vector_type = vector_type  # 文本向量化模型:0\1模型,TF模型,TF-IDF模型

    def fit(self, train_x, train_y, max_df):

        list_text = list(train_x)

        # 向量化方法:0 - 0/1,1 - TF,2 - TF-IDF
        if self.vector_type == 0:
            self.vectorizer = CountVectorizer(max_df, stop_words=stopwords, ngram_range=(1, 3)).fit(list_text)
        elif self.vector_type == 1:
            self.vectorizer = TfidfVectorizer(max_df, stop_words=stopwords, ngram_range=(1, 3), use_idf=False).fit(list_text)
        else:
            self.vectorizer = TfidfVectorizer(max_df, stop_words=stopwords, ngram_range=(1, 3)).fit(list_text)

        self.array_trainx = self.vectorizer.transform(list_text)
        self.array_trainy = train_y

        # 分类模型选择:1 - SVC,2 - LinearSVC,3 - SGDClassifier,三种SVM模型
        if self.classifier_type == 1:
            self.model = SVC(kernel='linear', gamma=10 ** -5, C=1).fit(self.array_trainx, self.array_trainy)
        elif self.classifier_type == 2:
            self.model = LinearSVC().fit(self.array_trainx, self.array_trainy)
        else:
            self.model = SGDClassifier().fit(self.array_trainx, self.array_trainy)

    def predict_value(self, test_x):
        list_text = list(test_x)
        self.array_testx = self.vectorizer.transform(list_text)
        array_predict = self.model.predict(self.array_testx)
        return array_predict

    def predict_proba(self, test_x):
        list_text = list(test_x)
        self.array_testx = self.vectorizer.transform(list_text)
        array_score = self.model.predict_proba(self.array_testx)
        return array_score

train_x, test_x, train_y, test_y = train_test_split(data_cutted['Comment'].ravel().astype('U'), data_cutted['Class'].ravel(),test_size=0.2, random_state=4)

classifier_list = [1,2,3]
vector_list = [0,1,2]

for classifier_type in classifier_list:
    for vector_type in vector_list:
        commentCls = CommentClassifier(classifier_type, vector_type)
        #max_df 设置为0.98
        commentCls.fit(train_x, train_y, 0.98)
        if classifier_type == 0:
            value_result = commentCls.predict_value(test_x)
            proba_result = commentCls.predict_proba(test_x)
            print(classifier_type,vector_type)
            print('classification report')
            print(metrics.classification_report(test_y, value_result, labels=[-1, 0, 1]))
            print('confusion matrix')
            print(metrics.confusion_matrix(test_y, value_result, labels=[-1, 0, 1]))
        else:
            value_result = commentCls.predict_value(test_x)
            print(classifier_type,vector_type)
            print('classification report')
            print(metrics.classification_report(test_y, value_result, labels=[-1, 0, 1]))
            print('confusion matrix')
            print(metrics.confusion_matrix(test_y, value_result, labels=[-1, 0, 1]))

2条回答 默认 最新

相关推荐 更多相似问题