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


Python RandomForestRegressor.max_features方法代码示例

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


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

示例1: RFR

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

	from sklearn.ensemble import RandomForestRegressor
	from sklearn.metrics import mean_squared_error
	from sklearn.cross_validation import cross_val_score

	if do_CV:
		### Randomly split up training set into 80/20 split. ###
		### 80 for CV, 20 for "Test" score ###
		from sklearn.cross_validation import train_test_split
		x_train_cv, x_test_cv, y_train_cv, y_test_cv = train_test_split(x_train,y_train,test_size=0.20, random_state=42)

		param_grid = {'max_features': [4,5,6],
						'min_samples_leaf':[50,250,1000,2500]}

		est=RandomForestRegressor(n_estimators=100,verbose=1, n_jobs=-1)
		cv_scores=list()
		test_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 with 100 trees: (MaxFeatures=%i, minSamples=%i)' % (mfeatures,minSamples)
				est.min_samples_leaf=minSamples
				est.max_features=mfeatures

				cv_score=cross_val_score(est,x_train_cv,y_train_cv,scoring='mean_squared_error',cv=5)
				cv_scores.append(np.mean(cv_score))

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

				### Perform 20% test set score ###
				est.fit(x_train_cv,y_train_cv)
				y_pred=est.predict(x_test_cv)
				test_scores.append(mean_squared_error(y_test_cv,y_pred))

		print 'Took %.2f seconds for parameter tuning.' %(time()-start)
		print 'writing CV results to file...'
		results = np.array([params_list,cv_scores,test_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 ###
		### RFR only takes 'MSE', need to change it to RMSEPE as per contest rules ###
		print 'Fitting Random Forest with optimal user-defined parameters....'
		est=RandomForestRegressor(n_estimators=udf_trees, max_features=udf_max_features,min_samples_leaf=udf_min_samples,n_jobs=-1,verbose=1)
		est.fit(x_train,y_train)

		#idx=np.where(x_test[:,1]==0)
		#x_test=np.delete(x_test, 1, axis=1)
		y_pred=est.predict(x_test) 
		y_pred=np.exp(y_pred)
		#y_pred[idx] = 0

		### 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','Sales'))
			for i in range(len(y_pred)):
				w.writerow(((i+1),y_pred[i]))
		testfile.close()
		print 'File written to disk...' 
开发者ID:chriszeng8,项目名称:Rossman-Kaggle-2015,代码行数:72,代码来源:Bob_Pipeline.py


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