0

I'm trying to find a least squared cubic spline fit of data using the following code:

from scipy import interpolate
plt.subplot(223)
l_hits = np.array(l_hits)
list1 = np.log(l_hits)
knots = list1.sort()
xnew = np.arange(min(l_bins), max(l_bins))
tck = interpolate.splrep(l_bins,list1,s=0,k=3,task=-1,t=knots)
fnew = interpolate.splev(xnew, tck, der=0)
plt.plot(xnew, fnew, 'b-')

where the x-values are l_bins and the y-values are np.log(l_hits) = list1. But I keep getting the error:

TypeError: Knots must be given for task =-1

Why am I getting this error and how do I fix it?

user2954167
  • 155
  • 1
  • 3
  • 14

2 Answers2

0

The numpy sort method operates in-place, and returns None, so this line:

knots = list1.sort()

sets knots to None.

To accomplish what I think you intended, you could do something like this:

knots = list1.copy()
knots.sort()
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
0

Based on API document

If provided, knots t must satisfy the Schoenberg-Whitney conditions, i.e., there must be a subset of data points x[j] such that t[j] < x[j] < t[j+k+1], for j=0, 1,...,n-k-2.

In your case,

l_bins = ?
knots = list(np.log(l_hits)).sort()

It's unclear whether knots satisfies the Schoenberg-Whitney conditions from the question.

Alex Xu
  • 196
  • 2
  • 10