【问题标题】:Classification Problem: iterate different K for KNN分类问题:为 KNN 迭代不同的 K
【发布时间】:2020-10-27 05:36:35
【问题描述】:

我目前正在研究一个分类问题(推文情绪分析),我想在下面的分类器列表中包含一个用于不同 K 值 (KNN) 的 for 循环。

我知道我可以选择:
KNeighborsClassifier(3), KNeighborsClassifier(5)... 但我正在尝试使用 for 循环来实现相当优雅的解决方案。

不幸的是,尝试创建一个空的 list 并向其中添加不同的 K 值,然后将其包含在 classifiers = [] 列表中无法正常工作。你有什么好的建议吗?

我的代码:

classifiers = [
    KNeighborsClassifier(3),
    LogisticRegression(),
    SVC(kernel = "rbf", C = 0.025, probability = True),
    NuSVC(probability = True),
    DecisionTreeClassifier(),
    RandomForestClassifier(),
    GradientBoostingClassifier(),
    MultinomialNB(),
    BernoulliNB()] 

for clf in classifiers:
    clf.fit(train_tf, y_train)
    name = clf.__class__.__name__
        
    expectation = y_train
    train_prediction = clf.predict(train_tf)
    acc = accuracy_score(expectation, train_prediction)   
    pre = precision_score(expectation, train_prediction)
    rec = recall_score(expectation, train_prediction)
    f1 = f1_score(expectation, train_prediction)


    fig, ax = plt.subplots(1,2, figsize=(14,4))
    plt.suptitle(f'{name} \n', fontsize = 18)
    plt.subplots_adjust(top = 0.8)
    skplt.metrics.plot_confusion_matrix(expectation, train_prediction, ax=ax[0])
    skplt.metrics.plot_confusion_matrix(expectation, train_prediction, normalize=True, ax = ax[1])
    plt.show()
    
    print(f"for the {name} we receive the following values:")
    print("Accuracy: {:.3%}".format(acc))
    print('Precision score: {:.3%}'.format(pre))
    print('Recall score: {:.3%}'.format(rec))
    print('F1 score: {:.3%}'.format(f1))

如果您需要更多信息,请告诉我 :) 提前非常感谢您!

【问题讨论】:

  • 非常感谢您的回复。我稍后会尝试,如果有任何其他问题,我会回来。

标签: python machine-learning classification sentiment-analysis


【解决方案1】:

这样的事情可能会起作用,您基本上只是冻结 KNeighbors 的迭代,直到所有邻居值都用完。

classifiers = [
    KNeighborsClassifier(3),
    LogisticRegression(),
    SVC(kernel = "rbf", C = 0.025, probability = True),
    NuSVC(probability = True),
    DecisionTreeClassifier(),
    RandomForestClassifier(),
    GradientBoostingClassifier(),
    MultinomialNB(),
    BernoulliNB()] 
n_neighbors = [3, 5, 6, 10, 15]  # or whatever
class_iter = iter(classifiers)
clf = next(class_iter)
while True:
    try:
        if isinstance(clf, KNeighborsClassifier) and any(n_neighbors):
            neighbor_val = n_neighbors.pop()
            clf.set_params(n_neighbors=neighbor_val)
        else:
            clf = next(class_iter)
        #rest of code here

        clf.fit(train_tf, y_train)
        name = clf.__class__.__name__
            
        expectation = y_train
        train_prediction = clf.predict(train_tf)
        acc = accuracy_score(expectation, train_prediction)   
        pre = precision_score(expectation, train_prediction)
        rec = recall_score(expectation, train_prediction)
        f1 = f1_score(expectation, train_prediction)


        fig, ax = plt.subplots(1,2, figsize=(14,4))
        plt.suptitle(f'{name} \n', fontsize = 18)
        plt.subplots_adjust(top = 0.8)
        skplt.metrics.plot_confusion_matrix(expectation, train_prediction, ax=ax[0])
        skplt.metrics.plot_confusion_matrix(expectation, train_prediction, normalize=True, ax = ax[1])
        plt.show()
        
        print(f"for the {name} we receive the following values:")
        print("Accuracy: {:.3%}".format(acc))
        print('Precision score: {:.3%}'.format(pre))
        print('Recall score: {:.3%}'.format(rec))
        print('F1 score: {:.3%}'.format(f1))

    except StopIteration:
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-26
    • 2014-05-24
    • 1970-01-01
    • 2017-04-23
    • 2013-02-05
    • 2016-03-22
    • 2021-12-23
    • 2015-03-18
    相关资源
    最近更新 更多