栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python】选择合适的机器学习模型

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python】选择合适的机器学习模型


图来源:《如何选择机器学习的算法模型?》

用Python脚本梳理了一下图中的选择规则,就是用if...else...实现。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
n: 样本个数
predicting_a_category:  True: 样本数据是类别预测,False: 数据不是类别预测
data_labeled: True: 数据有标签, False: 数据没有标签

# num_categories_known: True:类别数目是已知的; False: 类别数据不是已知的  # 对于不是类别预测的数据

# predicting_a_quality: True: 是量化数据预测; False: 不是量化数据预测
# few_feature_important: True: 少数的特征是重要的;False: 多数特征是重要的
# JustLooking: True: 只是看看数据;Fals: 不只是看看
# predicting_structure: True: 是结构化预测 False: 不是结构化预测(没有具体的模型建议)

# ModelWorking:  True: 当前模型起作用,False: 当前模型不起来作用
'''
import argparse
# n = '10000'
# predicting_a_category = True
# data_labeled = True
# ModelWorking = True  # 默认用True, 如果提供的模型,测试后不起作用,设置False



def read_args():
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('-n', '--num_samples', dest='num_samples',
                        metavar='INT', type=int, required=True,
                        help="set number of samples.")
    parser.add_argument('--predicting_a_category', dest='predicting_a_category',
                        action='store_true', default=False,
                        help="set predict type is category or not, default: [False]")
    parser.add_argument('--data_labeled', dest='data_labeled',
                        action='store_true', default=False,
                        help="set data is labeled or not, default: [False]")
    parser.add_argument('--is_Text_Data', dest='is_Text_Data',
                        action='store_true', default=False,
                        help="set data format is text or not, default: [False]")
    parser.add_argument('--num_categories_known', dest='num_categories_known',
                        action='store_true', default=False,
                        help="number of categories known or not, default: [False]")
    parser.add_argument('--predicting_a_quality', dest='predicting_a_quality',
                        action='store_true', default=False,
                        help="set predict type is quality or not, default: [False]")
    parser.add_argument('--few_feature_important', dest='few_feature_important',
                        action='store_true', default=False,
                        help="few feature should be inportant or not, default: [False]")
    parser.add_argument('--JustLooking', dest='JustLooking',
                        action='store_true', default=False,
                        help="just looking or not, default: [False]")
    parser.add_argument('--predicting_structure', dest='predicting_structure',
                        action='store_true', default=False,
                        help="set predict type is structure or not, default: [False]")
    parser.add_argument('--ModelWorking', dest='ModelWorking',
                        action='store_true', default=False,
                        help="set default recommend model working or not, default: [False]")                        
    args = parser.parse_args()
    return args


def recommend_model(n, predicting_a_category, data_labeled, 
                    is_Text_Data=False,
                    num_categories_known=True, 
                    predicting_a_quality=False,
                    few_feature_important=False, 
                    JustLooking=False,
                    predicting_structure=False,
                    ModelWorking=True):

    model = ''
    if n <= 50:
        print('NOTE: need more samples! at least >=50')
        exit(1)  # 样本量太小,需要获取更多样本
    else:
        if predicting_a_category:  # 如果是一个类别预测
            if data_labeled:  # 数据有标签
                 # classification,分类
                 if n < 100000:  # n < 100,000
                     model = 'Linear SVC'
                     if not ModelWorking:  # 如果该模型不起作用
                         if is_Text_Data:  # 如果是文本数据
                             model = 'Naive Bayes'
                         else:  # 如果不是文本数据
                             model = 'KNeighbors Classifier'
                             if not ModelWorking:  # 如果该模型不起作用
                                 model = 'SVC Ensemble Classifier'
                 else:  # n >= 100,000
                     model = 'SGD Classifier'
                     if not ModelWorking:
                         model = 'kernel approximotion'
            else:
                # clustering, 聚类
                if num_categories_known:  # 如果类别数目已知
                    if n < 10000:  # n < 10,000
                        model = 'KMeans'
                        if not ModelWorking:
                            model = 'Spectral Clustering GMM'
                    else:
                        model = 'MiniBatch KMeans'
                else:  # 如果类别数目未知
                    if n < 10000:  # n < 10,000
                        model = 'MeanShift VBGMM'
                    else:
                        model = 'tough luck?'  # 碰运气?  
                 
        else:
            if predicting_a_quality:  # 如果是一个量化预测
                if n < 100000:  # n < 100,000
                    if few_feature_important:  # 如果少数的特征是重要的
                        model = 'ElasticNet Lasso'
                    else:
                        model = "RidgeRegression SVR(kernel='liner')"
                        if not ModelWorking: 
                            model = "SVR(kenel='rbf') EsembleRegression"
                else:  # n >= 100,000
                    model = 'SGD Regressor'
            else:   # 不是量化预测
                if JustLooking:  # 如果只是看以看?
                    # dimentionality reduction, 降维
                    model = 'Randomized PCA'
                    if not ModelWorking:
                        if n < 10000:  # n < 10,000
                            model = 'Isomap Spectral Embedding'
                            if not ModelWorking:
                                model = 'LLE'
                        else:  # n >= 10,000
                            model = 'kernel approximation'
                else:  # 不是只看看
                    if predicting_structure:  # 如果是结构预测??
                        model = 'touch luck?'  # 碰运气??
    
    if model == '':
        print('Opps, NotFound any model for you!')
    else:
        print('Found model: "%s" for you~' % model)
    return model


if __name__ == '__main__':
    params = read_args()
    recommend_model(params.num_samples, params.predicting_a_category, params.data_labeled, 
                    is_Text_Data=params.is_Text_Data,
                    num_categories_known=params.num_categories_known, 
                    predicting_a_quality=params.predicting_a_quality,
                    few_feature_important=params.few_feature_important,
                    JustLooking=params.JustLooking,
                    predicting_structure=params.predicting_structure,
                    ModelWorking=params.ModelWorking)

不同类别的算法:图片来源《如何学习机器学习算法》

简要介绍了有哪些"比较流行的机器学习算法"。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/983414.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号