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


Python SGDClassifier.save方法代码示例

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


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

示例1: __init__

# 需要导入模块: from sklearn.linear_model import SGDClassifier [as 别名]
# 或者: from sklearn.linear_model.SGDClassifier import save [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]
开发者ID:stef1927,项目名称:hml,代码行数:70,代码来源:analysis.py


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