I use UnivariateSpline from scipy module to fit data.It works for almost all cases except for this one, which gives rise to `Process finished with exit code -1073741819 (0xC0000005)`

error. If I change smoothing factor `s`

to 0, it also works. Any suggestions to solve this problem will help.

**Update1**

My working environment is:

- python 3.7
- scipy 1.3.2
- numpy 1.17.4

```
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import UnivariateSpline, InterpolatedUnivariateSpline
x = np.arange(78)
y = np.asarray([
0., 0., 0., 0., 0., 0.,
0., 0., 5.03989319, 4.03191455, 4.03191455, 3.02393591,
3.02393591, 2.01595727, 2.01595727, 1.00797864, 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.])
spl = UnivariateSpline(x, y, k=1, s=0.01)
knots = list(map(int, spl.get_knots()))
plt.plot(knots, y[knots], 'rx')
plt.plot(knots, y[knots], 'r-')
plt.plot(x, y, 'b-')
plt.show()
```

The combination of you `s`

and `k`

parameter are causing the issue.

According to the documentation, the number of knots increases until the condition `sum((w[i] * (y[i]-spl(x[i])))**2, axis=0) <= s`

is met. However, because you have a limited number of non-zero data points, you can only add so many meaningful knots to the data set, and because you are doing `k=1`

spline (as opposed to cubic for example), the difference between the spline value and the data values is never reaching the prescribed `s`

value.

Your options include increasing `k`

(I tested with `k=3`

and it worked) or increase the `s`

value to have a less strict condition (anything above `s=0.08`

worked for me). Note your code worked when `s=0`

because for that condition, instead of doing a smoothing, the algorithm just interpolates between each point and does no smoothing (which maybe is what you want).

answered on Stack Overflow Nov 13, 2019 by alexpiers

User contributions licensed under CC BY-SA 3.0