Git Product home page Git Product logo

Comments (1)

blankjul avatar blankjul commented on June 18, 2024

Yes, you can! But keep in mind most optimization problems in ML models are defined to be solved efficiently (and are in fact single objective). However, you can of course treat them as a black-box problem as well. The example below illustrates this for Ridge Regression.

import numpy as np
from sklearn import linear_model

from pymoo.algorithms.soo.nonconvex.de import DE
from pymoo.core.problem import ElementwiseProblem
from pymoo.operators.sampling.lhs import LHS
from pymoo.optimize import minimize

A = np.array([[0, 0], [0, 0], [1, 1]])
b = np.array([0, .1, 1])
alpha = 0.5

reg = linear_model.Ridge(alpha=alpha)
reg.fit(A, b)
print(reg.intercept_, reg.coef_)


class RidgeRegression(ElementwiseProblem):

    def __init__(self):
        super().__init__(n_var=3, n_obj=1, xl=-10, xu=+10)

    def _evaluate(self, x, out, *args, **kwargs):
        coef_ = x[:2]
        intercept_ = x[2]
        y_hat = A @ coef_ + intercept_

        out['coef'] = coef_
        out['intercept'] = intercept_
        out['F'] = ((b - y_hat) ** 2).sum() + alpha * (coef_ ** 2).sum()


problem = RidgeRegression()

algorithm = DE(
    pop_size=100,
    sampling=LHS(),
    variant="DE/rand/1/bin",
    CR=0.3,
    dither="vector",
    jitter=False
)

res = minimize(problem,
               algorithm,
               seed=1,
               verbose=False)

sol = res.opt[0]
print(sol.get('intercept'), sol.get('coef'))

A whole other topic is to optimize the hyperparameters of ML models. Please search for hyper parameter optimization to get more information about this.

PS: Please open a discussion and not an issue as this is nothing that needs to be added as a feature or fixed.

from pymoo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.