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


Python RandomForestClassifier.min_samples_leaf方法代码示例

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


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

示例1: RFC

# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import min_samples_leaf [as 别名]
def RFC(x_train,y_train,x_test,udf_trees=100,udf_max_features='auto', udf_min_samples=50, do_CV=False,names=None):

	from sklearn.ensemble import RandomForestClassifier
	from sklearn.metrics import roc_auc_score

	if do_CV:
		param_grid = {'max_features': [2,3,4],
						'min_samples_leaf':[50,250,1000,2500]}

		est=RandomForestClassifier(n_estimators=100,verbose=1)
		cv_scores=list()
		params_list=list()

		start = time()
		for mfeatures in param_grid['max_features']:
			for minSamples in param_grid['min_samples_leaf']:
				print 'Trying parameter combination: (MaxFeatures=%i, minSamples=%i)' % (mfeatures,minSamples)
				est.min_samples_leaf=minSamples
				est.max_features=mfeatures

				cv_score=udf.cross_val_score_proba(x_train,y_train,5,est)
				cv_scores.append(np.mean(cv_score))

				### Create the labels for display purposes ###
				params_list.append((mfeatures,minSamples))

		print 'Took %.2f seconds for parameter tuning.' %(time()-start)
		print 'writing CV results to file...'
		results = np.array([params_list,cv_scores]).T ## should have 48 results...

		print 'Parameter tuning results........'
		print 'Parameters (max_features, min_samples_leaf), CV_Scores'
		for i in range(len(results)):
			print results[i]
	else:
		### Train the RFC Classifier with the optimal parameters found above ###
		print 'Fitting Random Forest with optimal user-defined parameters....'
		est=RandomForestClassifier(n_estimators=udf_trees, max_features=udf_max_features,min_samples_leaf=udf_min_samples,verbose=1)
		est.fit(x_train,y_train)
		y_pred=est.predict_proba(x_test)[:,1] ## Must predict probability!! ##

		### Plot feature importances ###
		plot_feature_importance(est, names)

		print 'Writing submission file....'
		with open('RFC_Submission.csv','wb') as testfile:
			w=csv.writer(testfile)
			w.writerow(('Id','Probability'))
			for i in range(len(y_pred)):
				w.writerow(((i+1),y_pred[i]))
		testfile.close()
		print 'File written to disk...' 
开发者ID:t36li,项目名称:FINRA,代码行数:54,代码来源:run_model.py


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