本文整理汇总了Python中sklearn.linear_model.SGDClassifier.train方法的典型用法代码示例。如果您正苦于以下问题:Python SGDClassifier.train方法的具体用法?Python SGDClassifier.train怎么用?Python SGDClassifier.train使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.SGDClassifier
的用法示例。
在下文中一共展示了SGDClassifier.train方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SVMTest
# 需要导入模块: from sklearn.linear_model import SGDClassifier [as 别名]
# 或者: from sklearn.linear_model.SGDClassifier import train [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))
示例2: print
# 需要导入模块: from sklearn.linear_model import SGDClassifier [as 别名]
# 或者: from sklearn.linear_model.SGDClassifier import train [as 别名]
#GaussianNB_classifier=SklearnClassifier(GaussianNB())
#GaussianNB_classifier.train(training_set)
#print("Gaussian Naive Bayes Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(GaussianNB_classifier,testing_set))*100)
BernoulliNB_classifier=SklearnClassifier(BernoulliNB())
BernoulliNB_classifier.train(training_set)
print("Bernuolli Naive Bayes Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(BernoulliNB_classifier,testing_set))*100)
LogisticRegression_classifier=SklearnClassifier(LogisticRegression())
LogisticRegression_classifier.train(training_set)
print("Logistic Regression Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(LogisticRegression_classifier,testing_set))*100)
SGDClassifier=SklearnClassifier(SGDClassifier())
SGDClassifier.train(training_set)
print("SGD Classifier Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(SGDClassifier,testing_set))*100)
SVC_Classifier=SklearnClassifier(SVC())
SVC_Classifier.train(training_set)
print("SVC classifier Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(SVC_Classifier,testing_set))*100)
LinearSVC=SklearnClassifier(LinearSVC())
LinearSVC.train(training_set)
print("Linear SVC Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(LinearSVC,testing_set))*100)
#NuSVC_Classifier=SklearnClassifier(NuSVC())
#NuSVC_Classifier.train(training_set)
#print("NuSVC Algorithm Accuracy for Lambda Calculas Lexical Semantic Parsing: ",(nltk.classify.accuracy(NuSVC_Classifier,testing_set))*100)
voted_classifier=VoteClassifier(classifier,MNB_classifier,BernoulliNB_classifier,LogisticRegression_classifier,SGDClassifier,LinearSVC)
开发者ID:CopotronicRifat,项目名称:Text-Classification-using-Supervised-Learning-Algorithms,代码行数:32,代码来源:STRING+INPUT.py