Split numpy array into similar array based on its content

efirvida

I have a 2D numpy array that represents the coordinates (x, y) of a curve, and I want to split that curve into parts of the same length, obtaining the coordinates of the division points.

The most easy example is a line defined for two points, for example [[0,0],[1,1]], and if I want to split it in two parts the result would be [0.5,0.5], and for three parts [[0.33,0.33],[0.67,0.67]] and so on.

How can I do that in a large array where the data is less simple? I'm trying to split the array by its length but the results aren't good.

Julien Spronck

If I understand well, what you want is a simple interpolation. For that, you can use scipy.interpolate (http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html):

from scipy.interpolate import interp1d
f = interp1d(x, y) ## for linear interpolation
f2 = interp1d(x, y, kind='cubic') ## for cubic interpolation
xnew = np.linspace(x.min(), x.max(), num=41, endpoint=False)
ynew = f(xnew) ## or f2(xnew) for cubic interpolation

You can create a function that returns the coordinates of the split points, given x, y and the number of desired points:

def split_curve(x, y, npts):
    from scipy.interpolate import interp1d
    f = interp1d(x, y)
    xnew = np.linspace(x.min(), x.max(), num=npts, endpoint=False)
    ynew = f(xnew)
    return zip(xnew[1:], ynew[1:])

For example,

split_curve(np.array([0, 1]), np.array([0, 1]), 2) ## returns [(0.5, 0.5)]
split_curve(np.array([0, 1]), np.array([0, 1]), 3) ## [(0.33333333333333331, 0.33333333333333331), (0.66666666666666663, 0.66666666666666663)]

Note that x and y are numpy arrays and not lists.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP - Split an array into chunks based on its content

From Dev

Python: Split NumPy array based on values in the array

From Dev

Numpy sum elements in array based on its value

From Dev

Initialise a NumPy array based on its index

From Dev

Sort an array based on a similar array?

From Dev

How to split numpy array into numpy arrays based on columns?

From Dev

Cloning an array with its content

From Dev

Extending a numpy array up to a specific range with its own content

From Dev

Randomly split a numpy array

From Dev

Re-order numpy array based on where its associated ids are positioned in the `master_order` array

From Dev

numpy array split/partition efficiency

From Dev

How to split numpy array in batches?

From Dev

Split Numpy array using Index

From Dev

Split of numpy array into unequal chunks

From Dev

How to split my numpy array

From Dev

Split NumPy array according to values in the array (a condition)

From Dev

In Python / numpy, how do I unravel / split an array according to its first column, which is an index?

From Dev

Split associative array based on keys

From Dev

Split associative array based on keys

From Dev

split array based on value php

From Dev

Split array into new array based on property occurences

From Dev

how to Split a list/array based on index of array

From Dev

Split array in array of arrays based on sum of attribute

From Dev

Filter strings in Array based on content

From Dev

Array filtering based on cell content

From Dev

Split array keys and its values into new vars

From Dev

How would you simplify this ruby method that splits an array based on its content?

From Dev

operations on a NumPy array based on info in another Array

From Dev

Search a Numpy Array based on array index

Related Related

  1. 1

    PHP - Split an array into chunks based on its content

  2. 2

    Python: Split NumPy array based on values in the array

  3. 3

    Numpy sum elements in array based on its value

  4. 4

    Initialise a NumPy array based on its index

  5. 5

    Sort an array based on a similar array?

  6. 6

    How to split numpy array into numpy arrays based on columns?

  7. 7

    Cloning an array with its content

  8. 8

    Extending a numpy array up to a specific range with its own content

  9. 9

    Randomly split a numpy array

  10. 10

    Re-order numpy array based on where its associated ids are positioned in the `master_order` array

  11. 11

    numpy array split/partition efficiency

  12. 12

    How to split numpy array in batches?

  13. 13

    Split Numpy array using Index

  14. 14

    Split of numpy array into unequal chunks

  15. 15

    How to split my numpy array

  16. 16

    Split NumPy array according to values in the array (a condition)

  17. 17

    In Python / numpy, how do I unravel / split an array according to its first column, which is an index?

  18. 18

    Split associative array based on keys

  19. 19

    Split associative array based on keys

  20. 20

    split array based on value php

  21. 21

    Split array into new array based on property occurences

  22. 22

    how to Split a list/array based on index of array

  23. 23

    Split array in array of arrays based on sum of attribute

  24. 24

    Filter strings in Array based on content

  25. 25

    Array filtering based on cell content

  26. 26

    Split array keys and its values into new vars

  27. 27

    How would you simplify this ruby method that splits an array based on its content?

  28. 28

    operations on a NumPy array based on info in another Array

  29. 29

    Search a Numpy Array based on array index

HotTag

Archive