votes up 1

x coordinates are not in increasing order

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

""" 
        So that f'(1-0) = -1 and f'(1+0) = 2

        """
        xi = np.asarray(xi)
        if len(xi) != len(yi):
            raise ValueError("xi and yi need to have the same length")
        if np.any(xi[1:] - xi[:1] <= 0):
            raise ValueError("x coordinates are not in increasing order")

        # number of intervals
        m = len(xi) - 1

        # global poly order is k-1, local orders are <=k and can vary
        try:
            k = max(len(yi[i]) + len(yi[i+1]) for i in range(m))

Ways to fix

votes up 1 votes down

This happens when any of the value in the parameter x aren't increasing sequences of arrays.

How to reproduce the error:

$ pipenv install 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.array([1,2,3,7,5,6]) # notice the value 7 which violates the increasing order


p = NdPPoly(c, (x,))

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-e4808b6ef0f9> in <module>()  6 x = np.array([1,2,3,7,5,6])  7  ----> 8 p = NdPPoly(c, (x,)) 
/usr/local/lib/python3.7/dist-packages/scipy/interpolate/interpolate.py in __init__(self, c, x, extrapolate)  1958 raise ValueError("c must have at least 2*len(x) dimensions")  1959 if any(np.any(v[1:] - v[:-1] < 0) for v in self.x): -> 1960 raise ValueError("x-coordinates are not in increasing order")  1961 if any(a != b.size - 1 for a, b in zip(c.shape[ndim:2*ndim], self.x)):  1962 raise ValueError("x and c do not agree on the number of intervals") 
ValueError: x-coordinates are not in increasing order

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