# Adding a custom function to FitRegularization

**URL:** <https://community.wolfram.com/t/adding-a-custom-function-to-fitregularization/22312>\
**Category:** Questions\
**Tags:** data-science, modeling, machine-learning\
**Created:** [July 3, 2023, 11:12am UTC](https://community.wolfram.com/t/adding-a-custom-function-to-fitregularization/22312 "2023-07-03T11:12:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladimirmangazeev](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vladimirmangazeev](https://community.wolfram.com/u/vladimirmangazeev)\
**Post date:** [July 3, 2023, 11:12am UTC](https://community.wolfram.com/t/adding-a-custom-function-to-fitregularization/22312/1 "2023-07-03T11:12:57Z")

</div>

Hi,  
I am trying to add a custom function to FitRegularization in FindFit. The description says to use FitRegularization-\>rfun for a user function rfun[a] where “a” indicates points in data sets.  
I could not find any working examples and the above does not work. Could anyone who used/implemented this feature explain the correct format?

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/ad071f16-aba9-4622-ae38-c95b5605ca5f)

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [July 3, 2023, 8:47pm UTC](https://community.wolfram.com/t/adding-a-custom-function-to-fitregularization/22312/2 "2023-07-03T20:47:59Z")

</div>

Crossposted [here](https://mathematica.stackexchange.com/q/287204/58370).

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [July 5, 2023, 4:25pm UTC](https://community.wolfram.com/t/adding-a-custom-function-to-fitregularization/22312/3 "2023-07-05T16:25:53Z")

</div>

I am not convinced whether this is a good answer, but maybe helpful though. When I try:

```
FindFit[data, function[x], {a1}, x, FitRegularization -> (g[#] &)]

```

then I get the error message

```
FindFit::nrrfun: The regularization function g[#1]& evaluates to {0.} that is not a real number for a residual vector.

```

i.e. it evaluates to a `List`. This brings me to the idea of doing it simply like so:

```
FindFit[data, function[x], {a1}, x, FitRegularization -> (First[g[#]] &)]

```

which MMA can obviously digest whithout problems. Does that make sense to you, can you use that approach with your real data?
