【问题标题】:SVM Optimisation using Niapy使用 Niapy 进行 SVM 优化
【发布时间】:2021-07-16 18:24:07
【问题描述】:

亲爱的这个平台的好灵魂,

请让我非常不安,我的脑袋都快要爆炸了,因为我无法理解如何使用任何 Niapy 算法(如 PSO)来优化 SVM 参数。 我花了两天时间试图弄清楚如何做到这一点,但我做不到。 Niapy Github 网站上的示例对我来说不是很清楚。

下面是 Niapy 的代码

import sys
sys.path.append('../')

from NiaPy.task import StoppingTask, OptimizationType
from NiaPy.benchmarks import Benchmark
from NiaPy.algorithms.basic import GreyWolfOptimizer

##################################################################################
### our custom benchmark class   
class MyBenchmark(Benchmark):
    def __init__(self):
        Benchmark.__init__(self, -10, 10)

    def function(self):
        def evaluate(D, sol):
            val = 0.0
            for i in range(D): val += sol[i] ** 2
            return val
        return evaluate
####################################################################################

### we will run 10 repetitions of Grey Wolf Optimizer against our custom MyBenchmark benchmark function

for i in range(10):
    task = StoppingTask(D=20, nGEN=100, optType=OptimizationType.MINIMIZATION, benchmark=MyBenchmark())

    # parameter is population size
    algo = GreyWolfOptimizer(NP=20)

    # running algorithm returns best found minimum
    best = algo.run(task)

    # printing best minimum
    print(best[-1])

上面的代码工作正常。我想用下面的代码替换 MyBenchmark 类(用### 划分):

def SVR_PSO(params):
    global PSO_model    

    PSO_model = SVR(C=params[0][0], epsilon = params[0][1], gamma = params[0][2])
    PSO_model.fit(X_train,np.ravel(y_train))
    result = PSO_model.predict(X_val)
    MAPE_result = call_MAPE(y_val, result)   
    # print('New PSO: C = {c}, epsilon={e}, gamma={g}  MAPE={m}'.format(c=params[0][0], e=params[0][1], g=params[0][2], m=MAPE_result)) #, g=params[2],  m=resultCV))
    #print(params)
    return MAPE_result

谢谢大家。

拜托,请有人帮帮我。

【问题讨论】:

    标签: python optimization parameters svm


    【解决方案1】:

    自从提出这个问题以来,API 发生了很大变化。对于不清楚的文档,我深表歉意,我们正在努力改进它。

    您要做的第一件事是实现一个自定义的Problem 类(以前的Benchmark)。优化问题会有 3 个维度,因为有 3 个参数,上下界将是这些参数的上下界,并且它必须存储数据集:

    from niapy.problems import Problem
    from niapy.task import Task
    from niapy.algorithms.basic import ParticleSwarmOptimization
    
    from sklearn.svm import SVR
    
    def preprocess_data(dataset):
        """Split, scale, whatever..."""
        pass
    
    class SVMHyperparameterOptimization(Problem):
        def __init__(lower, upper, dataset):
            super().__init__(dimension=3, lower=lower, upper=upper)
            self.X_tr, self.y_tr, self.X_val, self.y_val = preprocess_data(dataset)
            # or do the preprocessing here...
        
        # This is the method containing the objective function.
        def _evaluate(x):
            model = SVR(C=x[0], epsilon=x[1], gamma=x[2])
            model.fit(self.X_tr, self.y_tr)
            result = model.predict(self.X_val)
            mape = call_MAPE(self.y_val, result)
            return mape
    
    

    然后你像这样运行这个问题的算法:

    # Load dataset...
    
    # set lower and upper bounds
    # lower[0] => min C, lower[1] => min gamma, lower[2] => min epsilon
    # upper[0] => max C, upper[1] => max gamma, upper[2] => max epsilon
    # lower and upper can be tuples, lists, numpy arrays or scalars. If they're scalars, each parameter will have the same bound.
    lower = (0.01, 0.0001, 0.0001)
    upper = (32000.0, 32.0, 1.0)
    
    problem = SVMHyperparameterOptimization(lower, upper, dataset)
    
    """
    The task class is basically a wrapper for Problem, it counts iterations and
    function evaluations and evaluates stopping conditions. Here I set the max number
    of iterations to 500, you can also set max_evals, which is the max number of 
    function evaluations, and a cutoff_value, so the algorithm will stop running if it
    reaches that value.
    """
    task = Task(problem=problem, max_iters=500)
    
    # Initialize the PSO algorithm
    pso = ParticleSwarmOptimization(population_size=10, c1=2.0, c2=2.0)
    
    # Run the algorithm on task
    best_params, best_mape = pso.run(task)
    C, gamma, epsilon = best_params
    print('Best parameters: C={}; gamma={}; epsilon={}'.format(C, gamma, epsilon))
    print('Best score:', best_mape)
    
    

    【讨论】:

    • 非常感谢 Ziga Stupan。
    猜你喜欢
    • 2016-05-28
    • 2021-07-17
    • 2017-05-27
    • 2015-08-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多