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!!

  • Creating your own estimator in scikit-learn

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

  • 非常详细的sklearn介绍

  • How to write a custom estimator in sklearn and use cross-validation on it?

  • Here are some code of estimators depends on sklearn, including elm-estimator.
    Reverse Dependencies of scikit-learn

  • introduce the __init__ of sklearn: sklearn’s init

你可能感兴趣的:(机器学习,Python)