本文整理汇总了Python中sklearn.linear_model.SGDClassifier.densify方法的典型用法代码示例。如果您正苦于以下问题:Python SGDClassifier.densify方法的具体用法?Python SGDClassifier.densify怎么用?Python SGDClassifier.densify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.SGDClassifier
的用法示例。
在下文中一共展示了SGDClassifier.densify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train_custom_one_vs_all
# 需要导入模块: from sklearn.linear_model import SGDClassifier [as 别名]
# 或者: from sklearn.linear_model.SGDClassifier import densify [as 别名]
def train_custom_one_vs_all(X_train,X_test,Y_train,topk):
#convert matrix to row for efficient splicing
Y_train = Y_train.tocsc()
tag_classifiers = []
num_training,numclasses = Y_train.shape
num_test_examples = X_test.shape[0]
# hold a vector mxk, containing top k prediction classes for each example, maintain m heaps for that
num_examples = X_test.shape[0]
num_classes = len(tag_classifiers)
topk_class_distances = []
for i in xrange(num_examples):
heap = []
topk_class_distances += [heap]
for j in xrange(numclasses):
# train on each class label for all the training examples
y = numpy.ravel(Y_train.getcol(j).todense());
clf = SGDClassifier(loss='hinge',penalty='l2',alpha=0.0001,fit_intercept=True,n_iter = 10,shuffle=True,n_jobs=4,learning_rate='optimal')
clf.fit(X_train,y);
print "Trained for class",j
# get the decision for all test examples
decision = clf.densify().decision_function(X_test)
# for each test example add its decision value to the heap of top k decision values
for i in xrange(num_test_examples):
h = topk_class_distances[i]
if len(h) < topk: heapq.heappush(h,(decision[i],j))
else: heapq.heappushpop(h,(decision[i],j))
print "Predicted for class",j
#clean the decision values and store the class labels
class_label_indices = []
for i in xrange(num_examples):
topk_labels = [label for dist,label in topk_class_distances[i]]
class_label_indices += [topk_labels]
return class_label_indices