votes up 1

x arrays must all contain at least 2 points

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

            extrapolate = True
        self.extrapolate = bool(extrapolate)

        ndim = len(self.x)
        if any(v.ndim != 1 for v in self.x):
            raise ValueError("x arrays must all be 1-dimensional")
        if any(v.size < 2 for v in self.x):
            raise ValueError("x arrays must all contain at least 2 points")
        if c.ndim < 2*ndim:
            raise ValueError("c must have at least 2*len(x) dimensions")
        if any(np.any(v[1:] - v[:-1] < 0) for v in self.x):
            raise ValueError("x-coordinates are not in increasing order")
        if any(a != b.size - 1 for a, b in zip(c.shape[ndim:2*ndim], self.x)):
            raise ValueError("x and c do not agree on the number of intervals")

Ways to fix

votes up 1 votes down

The function scipy.interpolate.NdPPoly expects the all array values of the parameter x to be 1D arrays.

Here is how to recreate this error.

Steps to reproduce the error:

$ pipenv insall scipy numpy

$ pipenv shell

import numpy as np
from scipy.interpolate import NdPPoly
np.random.seed(1234)
c = np.random.rand(4, 5)
x = np.random.rand(6,1)
p = NdPPoly(c, (x,))

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-5e6c15053ae5> in <module>()  8 xi = np.random.rand(200,1)  9  ---> 10 p = NdPPoly(c, (x,)) 
/usr/local/lib/python3.7/dist-packages/scipy/interpolate/interpolate.py in __init__(self, c, x, extrapolate)  1952 ndim = len(self.x)  1953 if any(v.ndim != 1 for v in self.x): -> 1954 raise ValueError("x arrays must all be 1-dimensional")  1955 if any(v.size < 2 for v in self.x):  1956 raise ValueError("x arrays must all contain at least 2 points") 
ValueError: x arrays must all be 1-dimensional

Fixed version of the code:

import numpy as np
from scipy.interpolate import NdPPoly
np.random.seed(1234)


c = np.random.rand(4, 5)
x = np.array([1,2,3,4,5,6])


p = NdPPoly(c, (x,))
Oct 13, 2021 kellemnegasi answer
kellemnegasi 13.5k

Add a possible fix

Please authorize to post fix