sklearn 创建自己的估计器 自定义estimators scikit-learn python

I should write an ELM_Estimator by inheriting the parent class BaseEstimator, RegressorMixin so that I can directly use GridSearchCV from sklearn more easily.

SUPER GOOD TUTRIAL!!

Code

Refer to Developing scikit-learn estimators!!!
Suuuuuuuuuuper important for coding!!!

  • Estimators

    NOTE!!!
  1. all the attributes in __init__ must be public
  2. and must have the same name as params
  3. becuase set_params function is necessary as it is used to set parameters during grid searches
class TemplateClassifier(BaseEstimator, ClassifierMixin):
	
	def __init__(self, demo_param='demo', param1=1, param2=2, param3=3):
		self.demo_param = demo_param
		# WRONG: parameters should not be modified
	    if param1 > 1:
	        param2 += 1
	    self.param1 = param1
	    # WRONG: the object's attributes should have exactly the name of
	    # the argument in the constructor
	    self.param3 = param2

     def fit(self, X, y):
		# Check that X and y have correct shape
		X, y = check_X_y(X, y)
        # Store the classes seen during fit
        self.classes_ = unique_labels(y)

        self.X_ = X
        self.y_ = y         
        # Return the classifier
        return self

	def predict(self, X):
    	
    	# Check is fit had been called
        check_is_fitted(self)
        
        # Input validation
        X = check_array(X)

		closest = np.argmin(euclidean_distances(X, self.X_), axis=1)
		return self.y_[closest]
     
  • Using

elm_param_grid = { 'L': list(range(25, 501)), 
				'lambda_num': list(np.arange(0.01, 100.01 ,0.1))}

# GridSearchCV
elm_gcv = GridSearchCV(elm_model, elm_param_grid, cv=5, scoring='neg_mean_squared_error', verbose = 1, n_jobs = -1)
# RandomizedSearchCV
model_gcv = RandomizedSearchCV(model, elm_param_grid, cv=5, scoring='neg_mean_squared_error', 
		n_iter=100, verbose = 1, n_jobs = -1, random_state=99)
		
model_gcv.fit(X, T)

References

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
随机森林(Random Forest)是一种集成学习(Ensemble Learning)方法,它通过构建多个决策树来进行分类或回归。scikit-learn一个流行的Python机器学习库,提供了实现随机森林算法的工具。 在scikit-learn中,可以使用ensemble模块中的RandomForestClassifier进行分类问题,或者使用RandomForestRegressor进行回归问题。随机森林通过随机选择特征子集和样本子集来构建多个决策树,然后通过投票或平均的方式来得到最终的预测结果。 使用scikit-learn中的随机森林算法,需要先导入相关的类和函数,然后创建一个随机森林模型对象,并对其进行训练和预测。例如,下面是一个使用随机森林分类进行分类的示例代码: ```python from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split # 加载数据集 data = load_iris() X = data.data y = data.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 创建随机森林分类 rf = RandomForestClassifier(n_estimators=100) # 训练模型 rf.fit(X_train, y_train) # 预测 predictions = rf.predict(X_test) ``` 这只是一个简单的示例,你可以根据自己的需求进行参数调整和功能扩展。随机森林在处理各种机器学习问题时具有较好的性能和鲁棒性,你可以进一步了解scikit-learn文档中关于RandomForestClassifier和RandomForestRegressor的详细用法和参数设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值