当前位置: 首页>>代码示例>>Python>>正文


Python SGDClassifier.classify方法代码示例

本文整理汇总了Python中sklearn.linear_model.SGDClassifier.classify方法的典型用法代码示例。如果您正苦于以下问题:Python SGDClassifier.classify方法的具体用法?Python SGDClassifier.classify怎么用?Python SGDClassifier.classify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.linear_model.SGDClassifier的用法示例。


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

示例1: SVMTest

# 需要导入模块: from sklearn.linear_model import SGDClassifier [as 别名]
# 或者: from sklearn.linear_model.SGDClassifier import classify [as 别名]
class SVMTest(TestCase):
    def set_up(self):
        corpus = CommentsCorpus(document_class=BagOfWords)
        train = corpus[0:1000]
        test = corpus[1000:1200]
        svm = SVM(corpus.label, corpus.featureDict)
        svm.setUp(train, test)
        X = svm.x_matrix
        y = svm.y_matrix
        # self.y = np.ravel(self.svm.y_matrix)
        test_X = svm.x_test
        test_y = svm.y_test
        return X, y, test_X, test_y, len(corpus.featureDict)

    def test_soft_margin_kernl_svm(self):
        pass
        # self.set_up()
        # self.svm.train(train, test)

    def test_sgd(self):
        """Test self write svm sgd"""
        train_x, train_y, test_x, test_y, n_features = self.set_up()
        self.svm = SVM_SGD(n_features)
        self.svm.train(train_x, train_y)
        self.svm.classify(test_x, test_y)

    def test_with_skleart_sgd(self):
        """Test svm sgd with sklearn"""
        train_x, train_y, test_x, test_y, n_features = self.set_up()
        train_y = np.ravel(train_y)
        self.svm = SGDClassifier(loss="hinge", penalty="l2")
        self.svm.fit(train_x, train_y)
        self.accuracy(test_x, test_y, "sgd")

    # def test_with_sklearn_svc(self):
    #     # pass
    #     """Test svc in sklearn"""
    #     train_x, train_y, test_x, test_y, n_features = self.set_up()
    #     train_y = np.ravel(train_y)
    #     self.svm = svm.SVC()
    #     self.svm.fit(train_x, train_y)
    #     self.accuracy(test_x, test_y, "svc")

    def test_with_sklearn_linearsvc(self):
        # pass
        """Test linear svc in sklearn """
        train_x, train_y, test_x, test_y, n_features = self.set_up()
        train_y = np.ravel(train_y)
        self.svm = svm.LinearSVC()
        self.svm.fit(train_x, train_y)
        self.accuracy(test_x, test_y, "linear svc")

    def accuracy(self, test_x, test_y, classfier_type):
        """Test the accuracy of the classifier"""
        result = []
        for i in test_x:
            result.append(self.svm.predict([i]))
        accuracy = float(np.sum(result == test_y)) / len(test_y)
        print("The accuracy of the " + classfier_type + " classifier is: %.3f%%" % (accuracy * 100))
开发者ID:luckyhusky,项目名称:Semantic-Youtube-Spam-Analysis,代码行数:61,代码来源:test_svm.py


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