弥合其他机器学习和深度学习工具之间的差距,使可靠的事后分析成为可能。
gaplearn的Python项目详细描述
gaplearn
gaplearn在其他机器学习和深度学习工具之间架起了桥梁。所有模型都可以传递到下面的函数,而不管它们是基于什么框架构建的(scikit learn、tensorflow、xgboost,甚至是好的ole numpy)。
我的第一个软件包目标是通过使对超参数和特征选择过程的健壮的死后分析成为可能,使经常是黑箱型的模型训练过程透明化。下面的功能还可以进一步自动化一些过程,同时让用户完全控制结果。
许多功能正在开发中。不久将添加单元测试和完整文档。
源代码可以在github上找到
安装
pip install gaplearn
请参阅最新版本的
子模块
CV
cv
子模块将具有这些类(sfs已发布):
SFS
说明:
- 这是一个顺序特征选择器,使您能够对任何模型执行向后消除(而不仅仅是线性回归)。
- 在每个步骤中,选择具有最低置换重要性的特征进行移除。默认情况下,排列重要性的度量是准确性的降低,但用户可以通过任何自定义评分功能。
路上的改进:
- 添加正向选择和所有子集测试
- 为评估功能排列的重要性添加更多内置评分功能
- 创建自定义排列评分方法以删除作为依赖项的eli5
方法
向后消除(x,y,model,params={},fit_function=none,predict_function=none,score_function=none,score_name='score',cols=[],verbose=0)
- 反向消除
- 参数:
- x:(数据帧或矩阵)具有自变量
- y:(iterable)因变量对应值
- params:(dict)模型的参数集
- 模型:需要培训和评估的模型体系结构
- 拟合函数:(函数)用于训练模型的函数;函数必须接受参数
模型
、x
和y
;如果未设置此值,向后消除
将尝试使用模型的拟合
方法 - predict_function:(函数)将用于对模型进行预测的函数;函数必须接受参数
model
和x
;如果未设置此值,向后消除
将尝试使用模型的predict
方法。OD - score_函数:(函数)用于对模型进行评分并确定特征排列重要性的函数;函数必须接受参数
y
和preds
;如果未设置此值,则将使用精度 - score_name:(str)由score_函数计算的分数的名称;"score"默认值
- cols:(list)矩阵或数据框中列的名称
- 详细:(0、1或2)确定打印量
获取摘要
- 逐步得到结果的摘要;这可以用于更加可靠和全面地确定哪个功能集最适合您的问题
获取结果
获取功能
按分数设置(最小分数=无,步数=全部) 按功能获取设置(num-features,max-features=none) 示例1: 示例2: 说明: 文档:
马上就来。有关详细信息,请参见文档:https://github.com/awhedon/gaplearn" rel="nofollow">https://github.com/awhedon/gaplearn 搜索聚类(model,params,x,cols=none,fit_function=none,label_function=none,centroid_function=none,score_function=none,metric_name=score,random=none,centroid=false,verbose=0) 获取最佳模式(返回分数=假) 获取"最佳"模型(n=1,return_scores=false) 获取最佳参数 获取标签() 获取参数结果() 示例1: 示例2: 说明: 数据工程子模块将具有以下类: 说明:num_steps
确定),这些特征集的得分大于或等于min_score
num-features
功能的所有功能集;如果设置了max-features
,则返回num-features
和max-features
功能之间的功能集
max_features
则函数是最小值
#### Perform a backwards elimination with sci-kit learn's random forest model ####importpandasaspdfromgaplearn.cvimportSFSX=pd.read_csv('X_classification.csv')y=pd.read_csv('y_classification.csv')fs=SFS()print('The backwards elimination has been run: {}'.format(fs.be_complete))# prints Falsefromsklearn.ensembleimportRandomForestClassifierrfc=RandomForestClassifier()# Run the backwards eliminationfs.backwards_elimination(X,y,model=rfc,params={'n_jobs':-1})# Get the step-by-step summarysummary=fs.get_summary_be()# Alternatively, `summary = fs.summary_be`# Get the predictions and true values for each observationresults=fs.get_results_be()# Alternatively, `results = fs.results_be# Get the features used in the analysisfeatures=fs.features_be# Alternatives, `sorted(list(results['feature to remove']))# Identify which feature set can achieve at least 85% accuracy with the smallest number of featuresat_least_85=fs.get_set_by_score_be(min_score=.85,num_steps=1)# Identify the best model with only 4 featuresfeatures_4=fs.get_set_by_features_be(num_features=4)
#### Perform a more complex backwards elimination with sci-kit learn's naive bayes model ####importpandasaspdfromgaplearn.cvimportSFSfromsklearn.linear_modelimportSGDRegressormodel_sgd=SGDRegressor(loss='modified_huber',penalty='elasticnet')X=pd.read_csv('X_regression.csv')y=pd.read_csv('y_regression.csv')fs=SFS()# Define a score_functiondefmse(y,preds):score=sum([(preds[i]-y[i])**2foriinrange(y.shape[0])])/y.shape[0]returnscore# Define a predict_functiondefarbitrary_prediction(model,X):preds=model.predict(X)+1# arbitrarily deciding to add 1 to the prediction (realistically, this would be a wrapper for model that don't have a `fit` method)returnpreds# Define a predict_functiondefpredict_w_proba(model,X):preds=[1ifx[1]>0.6else0forxinmodel.predict_proba(X)]returnpreds# Run the backwards eliminationfs.backwards_elimination(X,y,model=model_sgd,predict_function=predict_w_proba,score_function=mse)# Get the step-by-step summarysummary=fs.get_summary_be()# Alternatively, `summary = fs.summary_be`# Get the predictions and true values for each observationresults=fs.get_results_be()# Alternatively, `results = fs.results_be`# Get the features used in the analysisfeatures=fs.features_be# Alternatively, `sorted(list(results['feature to remove']))`# Identify which two feature sets can achieve at least 85% accuracy with the smallest number of featuresat_least_85=fs.get_set_by_score_be(min_score=.85,num_steps=2)# Identify the best models with 3-5 featuresfeatures_3_5=fs.get_set_by_features_be(num_features=3,max_features=5)
搜索群集
方法
model
,params
和x
;如果未设置此值,则search_cluster
将尝试使用模型的fit
方法model
;如果未设置此值,则search_cluster
将尝试提取模型的label s属性值
model
;如果未设置此值,搜索簇
将尝试提取模型的簇中心
属性。E值x
和标签
;如果未设置此值,则搜索_cluster
将计算轮廓评分形心函数
(如果模型具有簇中心
属性,则将其保留为none),以便提取簇中心fromgaplearn.cvimportSearchClusterimportpandasaspdfromsklearnimportKMeanssc=SearchCluster()X=pd.read_csv('X_clustering.csv')params={'':,'':[],'':[]}# Run the hyperparameter grid searchsc.search_cluster(model=KMeans,params=params,X=X,centroid=True)# Get the best modelbest_model=sc.get_best_model()# Alternatively, `best_model = sc.best_model`# Get the five best models and their performancebest_models_5,best_models_5_scores=sc.get_best_models(n=5,return_scores=True)# Get the model params that performed bestbest_params=sc.get_best_params()# Alternatively, `best_params = sc.best_params`# Get a summary of the results for each parameter setparam_results=sc.get_param_results()# Alternatively, `param_results = sc.param_results`# Get the labels for each observation for each parameter set's k-fold validation to robustly analyze the differences in performancelabels=sc.get_labels()# Alternatively, `labels = sc.labels_df`
fromgaplearn.cvimportSearchClusterimportpandasaspdimportrandomfrommy_moduleimportModelClasssc=SearchCluster()X=pd.read_csv('X_clustering.csv')params={'param1':['value1','value2','value3'],'param2':['value1','value2','value3'],'param3':['value1','value2','value3']}deffit_function(model,params,X):model(**params).fit_my_model(X)returnmodeldefscore_function(X,labels):score=random.random()returnscore# Run the hyperparameter random search, testing 50% of the parameter setssc.search_cluster(model=KMeans,params=params,X=X,fit_function=fit_function,score_function=score_function,random=0.5)# Get the best model and resultsbest_model,best_model_scores=sc.get_best_model(return_scores=True)# Get the five best models and their performancebest_models_5,best_models_5_scores=sc.get_best_models(n=5,return_scores=True)# Get the model params that performed bestbest_params=sc.get_best_params()# Alternatively, `best_params = sc.best_params`# Get a summary of the results for each parameter setparam_results=sc.get_param_results()# Alternatively, `param_results = sc.param_results`# Get the labels for each observation for each parameter set's k-fold validation to robustly analyze the differences in performancelabels=sc.get_labels()# Alternatively, `labels = sc.labels_df`
搜索(正在开发中)
数据引擎
分布式SQL(开发中)
推荐PyPI第三方库