# local_search **Repository Path**: the_machine/local_search ## Basic Information - **Project Name**: local_search - **Description**: Local search - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-07-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # local_search #### Description Local search #### Software Architecture Software architecture description #### Installation ```` pip install git+https://gitee.com/the_machine/local_search.git ```` #### Instructions 1. Parameter search ```` from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_breast_cancer from local_search.parameter_search import search_parameter data = load_breast_cancer() X = data['data'] y = data['target'] X_tr = X[:400, :] X_te = X[400:, :] y_tr = y[:400] y_te = y[400:] params = { 'n_estimators': [7, 8, 9, 10], 'max_depth': [3, 4, 5, 6] } best_params, detail_res = search_parameter(estimator=RandomForestClassifier, params=params, metric='auc', X_tr=X_tr, X_te=X_te, y_tr=y_tr, y_te=y_te, max_iteration=50, alpha=0.9, verbose=False) ```` 2. Parameter search - use custom metric function ```` from local_search.helpers import predict_proba from copy import deepcopy def test_score(y_true, y_score): data = pd.DataFrame({'label': y_true, 'score': y_score}) g_s=data[data['label'] == 0] b_s=data[data['label'] == 1] a1=np.percentile(g_s['score'],99.9) a2=np.percentile(g_s['score'],99.5) a3=np.percentile(g_s['score'],99) b1=b_s[b_s['score']>=a1].shape[0] b2=b_s[b_s['score']>=a2].shape[0] b3=b_s[b_s['score']>=a3].shape[0] return 0.4 * b1 / b_s.shape[0] + 0.3 * b2 / b_s.shape[0] + 0.3 * b3 / b_s.shape[0] # A evaluation object must have the specified atrributes and an eval() function call class ATECEvaluation: """ An object to help with evaluating parameter results. """ detail_cols = ['train-score', 'test-score'] metric_name = 'score' def __init__(self, estimator, fixed_params, X_tr, y_tr, X_te, y_te, param_names, param_values, fit_params=None): """ Constructor. """ self.estimator = estimator self.fixed_params = fixed_params self.X_tr = X_tr self.y_tr = y_tr self.X_te = X_te self.y_te = y_te self.fit_params = fit_params self.param_names = param_names self.param_values = param_values def eval(self, problem): """ Evaluate a set of parameters. Parameters: problem: list Returns: test_auc, detail: float, tuple """ # Add parameters to fixed parameters tmp_param = deepcopy(self.fixed_params) for i in range(len(problem)): param_name = self.param_names[i] param_value = self.param_values[i][problem[i]] tmp_param[param_name] = param_value # Train model # print(tmp_param) # print(self.fixed_params) # print(self.param_names) tmp_model = self.estimator(**tmp_param) if self.fit_params is None: tmp_model = tmp_model.fit(self.X_tr, self.y_tr) else: tmp_model = tmp_model.fit(self.X_tr, self.y_tr, **self.fit_params) # Predict scores for both train and test data score_tr = predict_proba(estimator=tmp_model, x=self.X_tr) score_te = predict_proba(estimator=tmp_model, x=self.X_te) # Compute and return score score_train = test_score(y_true=self.y_tr, y_score=score_tr) score_test = test_score(y_true=self.y_te, y_score=score_te) tmp_model = None tmp_param = None return score_test, (score_train, score_test) best_params, detail_res = search_parameter(estimator=RandomForestClassifier, params=params, metric=ATECEvaluation, X_tr=X_tr, X_te=X_te, y_tr=y_tr, y_te=y_te, max_iteration=50, alpha=0.9, verbose=False) ```` 3. Parameter search - custom metric function ```` def test_score(y_true, y_score): data = pd.DataFrame({'label': y_true, 'score': y_score}) g_s=data[data['label'] == 0] b_s=data[data['label'] == 1] a1=np.percentile(g_s['score'],99.9) a2=np.percentile(g_s['score'],99.5) a3=np.percentile(g_s['score'],99) b1=b_s[b_s['score']>=a1].shape[0] b2=b_s[b_s['score']>=a2].shape[0] b3=b_s[b_s['score']>=a3].shape[0] return 0.4 * b1 / b_s.shape[0] + 0.3 * b2 / b_s.shape[0] + 0.3 * b3 / b_s.shape[0] best_params, detail_res = search_parameter( metric_func=test_score, metric_func_name='atec_score', estimator=lightgbm.LGBMClassifier, params=params, X_tr=X_tr, X_te=X_te, y_tr=y_tr, y_te=y_te, max_iteration=20, alpha=0.9, verbose=True, fit_params=model_params ) ```` #### Contribution 1. Fork the project 2. Create Feat_xxx branch 3. Commit your code 4. Create Pull Request #### Gitee Feature 1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md 2. Gitee blog [blog.gitee.com](https://blog.gitee.com) 3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) 4. The most valuable open source project [GVP](https://gitee.com/gvp) 5. The manual of Gitee [http://git.mydoc.io/](http://git.mydoc.io/) 6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)