baseline_rcv1.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:NVDM-For-Document-Classification 作者: cryanzpj 项目源码 文件源码
def benchmark(clf, name = ""):
    print('_' * 80)
    print("Training: ")
    print(clf)
    t0 = time()
    clf.fit(X_train, y_train)
    train_time = time() - t0
    print("train time: %0.3fs" % train_time)

    t0 = time()
    if name == "kNN":
        pred = clf.predict(X_test[:200][:])
        score = metrics.f1_score(y_test[:200][:], pred, average=average_option)
    else:
        pred = clf.predict(X_test)
        score = metrics.f1_score(y_test, pred, average=average_option)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    print("f1-score:   %0.3f" % score)

    if hasattr(clf, 'coef_'):
        print("dimensionality: %d" % clf.coef_.shape[1])
        print("density: %f" % density(clf.coef_))

        if opts.print_top10 and feature_names is not None:
            print("top 10 keywords per class:")
            for i, category in enumerate(categories):
                top10 = np.argsort(clf.coef_[i])[-10:]
                print(trim("%s: %s"
                      % (category, " ".join(feature_names[top10]))))
        print()

    if opts.print_cm:
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))
    print()
    clf_descr = str(clf).split('(')[1].split("=")[-1]
    return clf_descr, score, train_time, test_time
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号