• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python extmath.density函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sklearn.utils.extmath.density函数的典型用法代码示例。如果您正苦于以下问题:Python density函数的具体用法?Python density怎么用?Python density使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了density函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_density

def test_density():
    rng = np.random.RandomState(0)
    X = rng.randint(10, size=(10, 5))
    X[1, 2] = 0
    X[5, 3] = 0
    X_csr = sparse.csr_matrix(X)
    X_csc = sparse.csc_matrix(X)
    X_coo = sparse.coo_matrix(X)
    X_lil = sparse.lil_matrix(X)

    for X_ in (X_csr, X_csc, X_coo, X_lil):
        assert_equal(density(X_), density(X))
开发者ID:93sam,项目名称:scikit-learn,代码行数:12,代码来源:test_extmath.py


示例2: getReport

    def getReport(self,save = 1, get_top_words = 0):       # returns report
        report = ""
        if get_top_words == 1:
            if hasattr(self.mlModel, 'coef_'):
                    report += "Dimensionality: " + str(self.mlModel.coef_.shape[1])
                    report += "\nDensity: " +  str(density(self.mlModel.coef_))

                    rank = np.argsort(self.mlModel.coef_[0])
                    top10 = rank[-20:]
                    bottom10 = rank[:20]
                    report += "\n\nTop 10 keywords: "
                    report += "\nPositive: " + (" ".join(self.feature_names[top10]))
                    report += "\nNegative: " + (" ".join(self.feature_names[bottom10]))

        score = metrics.accuracy_score(self.y_test, self.y_pred)
        report += "\n\nAccuracy: " + str(score)
        report += "\nClassification report: "
        report += "\n\n" + str(metrics.classification_report(self.y_test, self.y_pred,target_names=["Negative","Positive"]))
        report += "\nConfusion matrix: "
        report += "\n\n" + str(metrics.confusion_matrix(self.y_test, self.y_pred)) + "\n\n"

        if save == 1:
            with open(self.model_path + "report.txt", "w") as text_file:
                text_file.write(report)

        return report
开发者ID:tpsatish95,项目名称:Universal-MultiDomain-Sentiment-Classifier,代码行数:26,代码来源:learner.py


示例3: _classify

def _classify(clf, cluster_data, X_train, y_train, X_test, feature_names,
             categories, c_params):
    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()
    cluster_data.cluster_of_posts = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

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

        if 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 c_params.is_report_printed:
        print("classification report:")

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, train_time, test_time
开发者ID:lpritchett,项目名称:team-amzn-1,代码行数:33,代码来源:classification.py


示例4: report_accuracy

def report_accuracy(model, categories, test_target, predicted):
    score = metrics.f1_score(test_target, predicted)
    print "f1-score: {:.3f}".format(score)

    clf = model.named_steps['clf']
    if hasattr(clf, 'coef_'):
        coef = model.named_steps['clf'].coef_
        print "dimensionality: {}".format(coef.shape[1])
        print "density: {}".format(density(coef))

        print "top 15 keywords per class:"
        feature_names = np.asarray(model.named_steps['vect'].get_feature_names())
        for i, category in enumerate(categories):
            topkw = np.argsort(coef[i])[-15:]
            keywords = '\n\t'.join(textwrap.wrap(
                ", ".join(feature_names[topkw])
            ))
            print "{}: {}".format(category, keywords)
        print

    print "classification report:"
    print metrics.classification_report(test_target, predicted,
                                        target_names=categories)

    print "confusion matrix:"
    print metrics.confusion_matrix(test_target, predicted)
    print
开发者ID:rolando-archive,项目名称:yatiri,代码行数:27,代码来源:run_classifier.py


示例5: benchmark_features_selection

def benchmark_features_selection(clf,name):
    print('_' * 80)
    print("Training: ")
    print(clf)
    t0 = time()
    rfecv = RFECV(estimator=clf, step=1, cv=StratifiedKFold(y_train, 2),
              scoring='accuracy')
    rfecv.fit(X_train, y_train)
    train_time = time() - t0
    print("train time: %0.3fs" % train_time)

    print(name+"Optimal number of features : %d" % rfecv.n_features_)

# Plot number of features VS. cross-validation scores
    plt.figure()
    plt.xlabel("Number of features selected")
    plt.ylabel("Cross validation score (nb of correct classifications)")    
    plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
    plt.show()

 
    t0 = time()
    pred = rfecv.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

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

    print("Saving data to database:")
    save_results_data(cursor, name, testing_identifiant_produit_list, pred)
    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr,train_time,test_time
开发者ID:sduprey,项目名称:PYTHON_WEB,代码行数:35,代码来源:launching_recursive_feature_selection_production_sparse_on_uniformly_restrained_data.py


示例6: benchmark

def benchmark(clf):
    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()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.f1_score(y_test, pred)
    print("f1-score:   %0.3f" % score)

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

      

    
    print("classification report:")
    print(metrics.classification_report(y_test, pred,
                                            target_names=categories))

    
    print("confusion matrix:")
    print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:afshinrahimi,项目名称:textylon,代码行数:35,代码来源:Copy+of+dsm.py


示例7: benchmark

def benchmark(clf):
    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()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print "test time:  %0.3fs" % test_time

    score = metric(y_test, pred)
    print "MAE:  %0.3f" % score

    if hasattr(clf, 'alpha_'):
        print "Alpha", clf.alpha_

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

            print
    except Exception as ex:
        print ex


    print
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:ageek,项目名称:kaggle-job-salary,代码行数:33,代码来源:test_notext.py


示例8: benchmark

def benchmark(clf, clf_name):
    print('_' * 80)
    print("Training: ")
    print(clf)
    t0 = time()
    clf.fit(x_train_std, y_train)
    train_time = time() - t0
    print("train time: %0.3fs" % train_time)

    t0 = time()
    pred = clf.predict(x_test_std)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.accuracy_score(y_test, pred)
    print("accuracy:   %0.3f" % score)

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

    print("classification report:")
    print(metrics.classification_report(y_test, pred,
                                        target_names=["not helpful", "helpful"]))
    print("confusion matrix:")
    print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    save_confusion_matrix(confusion_matrix(y_test, pred), pred, clf_name)
    return clf_descr, score, train_time, test_time
开发者ID:jacobmbr,项目名称:psam-5600-machine-learning,代码行数:31,代码来源:helpfulness.py


示例9: benchmark

    def benchmark(self, clf):
        print('_' * 80)
        print("Training: ")
        print(clf)
        t0 = time.time()
        clf.fit(self.X_train, self.y_train)
        train_time = time.time() - t0
        print("train time: %0.3fs" % train_time)

        t0 = time.time()
        pred = clf.predict(self.X_test)
        test_time = time.time() - t0
        print("test time:  %0.3fs" % test_time)

        score = metrics.accuracy_score(self.y_test, pred)
        print("accuracy:   %0.3f" % score)

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

        
            print("top 10 keywords per class:")
            for i, label in enumerate(self.target_names):
                top10 = np.argsort(clf.coef_[i])[-10:]
                print(self.trim("%s: %s" % (label, " ".join(self.feature_names[top10]))))
        print()

        print("confusion matrix:")
        print(metrics.confusion_matrix(self.y_test, pred))

        print()
        clf_descr = str(clf).split('(')[0]
        return clf_descr, score, train_time, test_time
开发者ID:vinidixit,项目名称:codes,代码行数:34,代码来源:ClassifierBenchmarker.py


示例10: benchmark

def benchmark(clf):
    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()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.f1_score(y_test, pred)
    print("f1-score:   %0.3f" % score)

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

        if feature_names is not None:
            print("top 10 keywords per class:")
        print()

    if True:
        print("confusion matrix:")
        cm = metrics.confusion_matrix(y_test, pred)
        
    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:samijaber,项目名称:scenic-routing,代码行数:33,代码来源:classifier.py


示例11: benchmark

def benchmark(clf,X_train,y_train,X_test,y_test):
    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()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.accuracy_score(y_test, pred)
    print("accuracy:   %0.3f" % score)

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

    print("confusion matrix:")
    print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:LopezGG,项目名称:SwitchBoardDialogClassifcation,代码行数:28,代码来源:NgramsBasedClassification.py


示例12: benchmark

    def benchmark(clf):
        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()
        pred = clf.predict(X_test)
        test_time = time() - t0
        print("test time:  %0.3fs" % test_time)
    
        score = metrics.f1_score(y_test, pred)
        accscore = metrics.accuracy_score(y_test, pred)
        print ("pred count is %d" %len(pred))
        print ('accuracy score:     %0.3f' % accscore)
        print("f1-score:   %0.3f" % score)
    
        if hasattr(clf, 'coef_'):
            print("dimensionality: %d" % clf.coef_.shape[1])
            print("density: %f" % density(clf.coef_))
    
          
    
        
        print("classification report:")
        print(metrics.classification_report(y_test, pred,
                                                target_names=categories))
    
        
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))
        
        print("confidence for unlabeled data:")
        #compute absolute confidence for each unlabeled sample in each class
        confidences = np.abs(clf.decision_function(X_unlabeled))
        #average abs(confidence) over all classes for each unlabeled sample (if there is more than 2 classes)
        if(len(categories) > 2):
            confidences = np.average(confidences, axix=1)
        
        print confidences
        sorted_confidences = np.argsort(confidences)
        question_samples = []
        #select top k low confidence unlabeled samples
        low_confidence_samples = sorted_confidences[0:NUM_QUESTIONS]
        #select top k high confidence unlabeled samples
        high_confidence_samples = sorted_confidences[-NUM_QUESTIONS:]

        question_samples.extend(low_confidence_samples.tolist())
        question_samples.extend(high_confidence_samples.tolist())

        
        print()
        clf_descr = str(clf).split('(')[0]
        return clf_descr, score, train_time, test_time, question_samples
开发者ID:afshinrahimi,项目名称:activelearning,代码行数:57,代码来源:activelearner.py


示例13: benchmark

def benchmark(clf):
    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()
    pred = clf.predict(X_test)
    predictions = clf.predict_proba(X_test)
    fin_predict = []
    for i in xrange(0,len(predictions)):
        x = np.argpartition(predictions[i],-5)[-5:]
        x = clf.classes_[x]
        fin_predict.append([bunch.target_names[e] for e in x])
    
    our_accuracies.append(final_accuracy(fin_predict))
    print(our_accuracies[-1])
    # print("------------predictions------------")
    # print(pred)
    # print("-------------------------")
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.accuracy_score(y_test, pred)
    print("accuracy:   %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_report:
        print("classification report:")
        print(metrics.classification_report(y_test, pred,
                                            target_names=categories))

    if opts.print_cm:
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:rohitsakala,项目名称:SMAI_Project,代码行数:53,代码来源:TED-Supervised.py


示例14: benchmark

def benchmark(clf):
    print('_' * 80)
    print("Training: ")
    print(clf)
    t0 = time()
    # FIXME: use X_train.toarray() instead. if it didn't work use y_train.toarray() too :D
    #y_train.toarray()
    #X_train.toarray()
    #clf.fit(X_train.toarray(), y_train)
    #clf.fit(X_train, y_train.toarray())
    clf.fit(X_train, y_train)
    train_time = time() - t0
    print("train time: %0.3fs" % train_time)

    t0 = time()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.accuracy_score(y_test, pred)
    print("accuracy:   %0.3f" % score)
    score = metrics.precision_score(y_test, pred, average='weighted', pos_label=None)
    print("precision:   %0.3f" % score)
    score = metrics.recall_score(y_test, pred, average='weighted', pos_label=None)
    print("recall:   %0.3f" % score)
    score = metrics.f1_score(y_test, pred, average='weighted', pos_label=None)
    print("f1:   %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_report:
        print("classification report:")
        #print(metrics.classification_report(y_test, pred,
        #                                    target_names=categories))

    if opts.print_cm:
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:Mitra00,项目名称:scikit_learn_BinaryClass,代码行数:52,代码来源:document_classification_20newsgroups.py


示例15: benchmark

def benchmark(clf):
    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()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print "test time:  %0.3fs" % test_time
    score = metrics.f1_score(y_test, pred)
    print "f1-score:   %0.3f" % score

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

        if opts.print_top10:
            print "top 10 keywords per class:"
            for i, category in enumerate(categories):
                import pdb;pdb.set_trace()
                if len(clf.coef_.shape) == 1:
                    top10 = np.argsort(clf.coef_[i])[-10:]
                else:
                    top10 = np.argsort(clf.coef_[0][i])[-10:]
                print trim("%s: %s" % (
                    category, " ".join(np.array(feature_names)[top10])))
        print
    pos_hits = []
    for i in range(len(pred)):
        if pred[i] == 1:
            pos_hits.append(y_test[i])
    #print float(sum(pos_hits))/len(pos_hits)
    #print len(pos_hits)

    if opts.print_report:
        print "classification report:"
        print metrics.classification_report(y_test, pred,
                                            target_names=map(str,categories))

    if opts.print_cm:
        print "confusion matrix:"
        print metrics.confusion_matrix(y_test, pred)

    print
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:CTCL,项目名称:candidate_classifier,代码行数:50,代码来源:classify_ex_fb.py


示例16: benchmark

    def benchmark(self, clf):
        print_topX = self.print_topX
        print_report = self.print_report
        print_cm = self.print_cm
        X_train = self.X_train
        y_train = self.y_train
        X_test = self.X_test
        y_test = self.y_test
        feature_names = self.feature_names
        categories = ["1"]

        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()
        pred = clf.predict(X_test)
        test_time = time() - t0
        print("test time:  %0.3fs" % test_time)

        score = metrics.f1_score(y_test, pred)
        print("f1-score:   %0.3f" % score)

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

            if print_topX:
                print("top 10 keywords per class:")
                for i, category in enumerate(categories):
                    topX = np.argsort(clf.coef_[i])[-print_topX:]
                    print(trim("%s: %s" % (category, " ".join(feature_names[topX]))))
            print()

        if print_report:
            print("classification report:")
            print(classification_report(y_test, pred))
            # target_names=categories))

        if print_cm:
            print("confusion matrix:")
            print(metrics.confusion_matrix(y_test, pred))

        print()
        clf_descr = str(clf).split("(")[0]
        return clf_descr, score, train_time, test_time, clf, pred
开发者ID:jacobboy,项目名称:spring14,代码行数:50,代码来源:classes.py


示例17: benchmark

def benchmark(clf):
    needsDense=[RandomForestClassifier, AdaBoostClassifier, Pipeline]
    print('_' * 80)
    print("Training: ")
    print(clf)
    t0 = time()
    if type(clf) in needsDense:
        clf.fit(X_train.todense(), y_train)
    else:
        clf.fit(X_train, y_train)
    train_time = time() - t0
    print("train time: %0.3fs" % train_time)

    t0 = time()
    if type(clf) in needsDense:
        pred = clf.predict(X_test.todense())
    else:
        pred = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.f1_score(y_test, pred)
    print("f1-score:   %0.3f" % score)

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

        if print_topX:
            print("top feature per class:")
            for i, category in enumerate(categories):
                # topX = np.min(clf.coef_.shape[1], print_topX)
                topX = np.argsort(clf.coef_[i])[-print_topX:][::-1]
                print(trim("%s: %s"
                           % (category, " | ".join(feature_names[topX]))))
        print()

    if print_report:
        print("classification report:")
        print(metrics.classification_report(y_test, pred))
                                            # target_names=categories))

    if print_cm:
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time, clf, pred
开发者ID:jacobboy,项目名称:spring14,代码行数:49,代码来源:feat_select.py


示例18: benchmark

def benchmark(clf):
    print('_' * 80)
    print("Training: ")
    print(clf)
    t0 = time()
    try:
        clf.fit(X_train, y_train)
    except:
        clf.fit(X_train.toarray(), y_train)
    train_time = time() - t0
    print("train time: %0.3fs" % train_time)

    t0 = time()
    try:
        pred = clf.predict(X_test)
    except:
        pred = clf.predict(X_test.toarray())
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.f1_score(y_test, pred)
    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_report:
        print("classification report:")
        print(metrics.classification_report(y_test, pred,
                                            target_names=categories))

    if opts.print_cm:
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))

    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:osh,项目名称:newsgroup-classifier,代码行数:47,代码来源:document_classification_20newsgroups.py


示例19: benchmark

def benchmark(clf, clf_descr, X_train, X_test, y_train, y_test, feature_names, categories, silent, print_top10):
    """
    Benchmark a classifier.
    """
    if not silent:
        print('_' * 80)
        print("Training: ")
    if (not silent) or print_top10:
        print(clf)
    t0 = time()
    clf.fit(X_train, y_train)
    train_time = time() - t0
    if not silent:
        print("train time: %0.3fs" % train_time)

    t0 = time()
    pred = clf.predict(X_test)
    test_time = time() - t0
    if not silent:
        print("test time:  %0.3fs" % test_time)

    #score = metrics.f1_score(y_test, pred)
    score = np.mean(pred == y_test)
    if not silent:
        print("accuracy:   %0.3f" % score)

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

        if print_top10 and feature_names is not None:
            print("top 10 keywords per class:")
            #print(categories)
            if len(categories) > 2: # multi-class
                for i, category in enumerate(categories):
                    top10 = np.argsort(clf.coef_[i])[-10:]
                    print("%s: %s" % (category, " ".join(feature_names[top10])))
            else: # binary
                top10 = np.argsort(clf.coef_[0])[-10:]
                print("%s" % (" ".join(feature_names[top10])))
            print()

    #clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time, pred
开发者ID:tiefling-cat,项目名称:guessing-aspect,代码行数:45,代码来源:aspect_tools.py


示例20: benchmark

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()
    pred = clf.predict(X_test)
    test_time = time() - t0
    print("test time:  %0.3fs" % test_time)

    score = metrics.accuracy_score(y_test, pred)
    print("accuracy:   %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_report:
        print("classification report:")
        print(metrics.classification_report(y_test, pred))
#        print(metrics.classification_report(y_test, pred,
#                                            target_names=categories))

    if opts.print_cm:
        print("confusion matrix:")
        print(metrics.confusion_matrix(y_test, pred))

        print("Saving data to database:")
    save_my_data(cursor, name, testing_identifiant_produit_list, y_test, pred)
    print()
    clf_descr = str(clf).split('(')[0]
    return clf_descr, score, train_time, test_time
开发者ID:sduprey,项目名称:PYTHON_WEB,代码行数:44,代码来源:prototyping_hashing_classification.py



注:本文中的sklearn.utils.extmath.density函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python extmath.fast_dot函数代码示例发布时间:2022-05-27
下一篇:
Python extmath.cartesian函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap