本文整理汇总了Python中sklearn.linear_model.SGDClassifier.load方法的典型用法代码示例。如果您正苦于以下问题:Python SGDClassifier.load方法的具体用法?Python SGDClassifier.load怎么用?Python SGDClassifier.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.SGDClassifier
的用法示例。
在下文中一共展示了SGDClassifier.load方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn.linear_model import SGDClassifier [as 别名]
# 或者: from sklearn.linear_model.SGDClassifier import load [as 别名]
#.........这里部分代码省略.........
i = 0
rs = ShuffleSplit(self.data.shape[0], n_iter=n_iter, test_size=.1, random_state=0)
for train, test in rs:
x_train, y_train, w_train = self.data[train], self.labels[train], self.weights[train]
x_test, y_test = self.data[test], self.labels[test]
start_time = time.clock()
self.classifier.fit(x_train, y_train)
test_score = self.classifier.score(x_test, y_test)
train_score = self.classifier.score(x_train, y_train)
test_amss[i], test_threshold = self.calculateAMS(test, self.classifier)
train_amss[i], train_threshold = self.calculateAMS(train, self.classifier)
end_time = time.clock()
print(('Test score %f / AMS %f, Train score %f / AMS %f ran for %.2fs') %
(test_score, test_amss[i], train_score, train_amss[i], (end_time - start_time)));
i = i + 1
print(('AMS %0.4f (+/- %0.4f)') % (test_amss.mean(), test_amss.std()))
""" Purpose: to train the best classifier with full data set """
def train(self):
self.classifier.fit(self.data, self.labels)
ams, self.threshold = self.calculateAMS(np.arange(self.data.shape[0]), self.classifier)
print(('AMS %f, threshold %f') % (ams, self.threshold));
def save(self):
if hasattr(self.classifier, 'save'):
self.classifier.save('saved/model.dmp')
else:
joblib.dump(self.classifier, 'saved/model.pkl')
def load(self):
if hasattr(self.classifier, 'load'):
self.classifier.load('saved/model.dmp')
else:
self.classifier = joblib.load('saved/model.pkl')
def get_scores(self, data, clf):
if hasattr(clf, 'predict_proba'):
return clf.predict_proba(data)[:,1]
# Appendix B of http://jmlr.csail.mit.edu/papers/volume2/zhang02c/zhang02c.pdf
return (np.clip(clf.decision_function(data), -1, 1) + 1) / 2
def calculateAMS(self, indexes, clf):
def AMS(s,b):
bReg = 10.
return math.sqrt(2 * ((s + b + bReg) * math.log(1 + s / (b + bReg)) - s))
scores = self.get_scores(self.data[indexes], clf)
threshold = np.percentile(scores, 85)
pred = scores >= threshold
#pred = clf.predict(self.data[indexes])
numPoints = len(scores)
labels = self.labels[indexes]
weights = self.xs.weights[indexes]
sIndexes = labels == self.xs.pLabel # true positive
bIndexes = labels == self.xs.nLabel # true negative
s = 0
b = 0
wFactor = 1. * self.xs.numPoints / numPoints
for i in range(numPoints):
if pred[i]:
if sIndexes[i]:
s += weights[i] * wFactor
else:
b += weights[i] * wFactor
ams = AMS(max(0, s), max(0, b))
return (ams, threshold)
def computeSubmission(self, xsTest, output_file):
data = self.transform(xsTest.data)
scores = self.get_scores(data, self.classifier)
sortedIndexes = scores.argsort()
rankOrder = list(sortedIndexes)
for tI,tII in zip(range(len(sortedIndexes)), sortedIndexes):
rankOrder[tII] = tI
submission = np.array([[str(xsTest.testIds[tI]),str(rankOrder[tI]+1),
's' if scores[tI] >= self.threshold else 'b'] for tI in range(len(xsTest.testIds))])
submission = np.append([['EventId','RankOrder','Class']], submission, axis=0)
np.savetxt(output_file, submission, fmt='%s', delimiter=',')
print "Finished generating submission file"