Is there a multi-dimensional version of arange/linspace in numpy?

前端 未结 9 1455
有刺的猬
有刺的猬 2020-12-02 08:31

I would like a list of 2d NumPy arrays (x,y) , where each x is in {-5, -4.5, -4, -3.5, ..., 3.5, 4, 4.5, 5} and the same for y.

I could do

x = np.ar         


        
相关标签:
9条回答
  • You can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step:

    import numpy as np
    X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5]
    

    For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:

    X, Y = np.mgrid[-5:5:21j, -5:5:21j]
    

    You can then create your pairs as:

    xy = np.vstack((X.flatten(), Y.flatten())).T
    

    As @ali_m suggested, this can all be done in one line:

    xy = np.mgrid[-5:5.1:0.5, -5:5.1:0.5].reshape(2,-1).T
    

    Best of luck!

    0 讨论(0)
  • 2020-12-02 09:04

    I think you want np.meshgrid:

    Return coordinate matrices from coordinate vectors.

    Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,..., xn.

    import numpy as np
    x = np.arange(-5, 5.1, 0.5)
    y = np.arange(-5, 5.1, 0.5)
    X,Y = np.meshgrid(x,y)
    

    you can convert that to your desired output with

    XY=np.array([X.flatten(),Y.flatten()]).T
    
    print XY
    array([[-5. , -5. ],
           [-4.5, -5. ],
           [-4. , -5. ],
           [-3.5, -5. ],
           [-3. , -5. ],
           [-2.5, -5. ],
           ....
           [ 3. ,  5. ],
           [ 3.5,  5. ],
           [ 4. ,  5. ],
           [ 4.5,  5. ],
           [ 5. ,  5. ]])
    
    0 讨论(0)
  • 2020-12-02 09:12

    Based on this example, you can make any dim you want

    def linspace3D(point1,point2,length):
        v1 = np.linspace(point1[0],point2[0],length)
        v2 = np.linspace(point1[1],point2[1],length)
        v3 = np.linspace(point1[2],point2[2],length)
        line = np.zeros(shape=[length,3])
        line[:,0]=v1
        line[:,1]=v2
        line[:,2]=v3
        return line
    
    0 讨论(0)
  • 2020-12-02 09:15

    I still did it with Linspace because I prefer to stick to this command.

    You can create like the following format: np.linspace(np.zeros(width)[0], np.full((1,width),-1)[0], height)

    np.linspace(np.zeros(5)[0],np.full((1,5),-1)[0],5)
    

    Output the following:

    array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
           [-0.25, -0.25, -0.25, -0.25, -0.25],
           [-0.5 , -0.5 , -0.5 , -0.5 , -0.5 ],
           [-0.75, -0.75, -0.75, -0.75, -0.75],
           [-1.  , -1.  , -1.  , -1.  , -1.  ]])
    

    Add .tranpose() then you get:

    array([[ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ]])
    
    0 讨论(0)
  • 2020-12-02 09:16

    Not sure if I understand the question - to make a list of 2-element NumPy arrays, this works:

    import numpy as np
    x = np.arange(-5, 5.1, 0.5)
    X, Y = np.meshgrid(x, x)
    Liszt = [np.array(thing) for thing in zip(X.flatten(), Y.flatten())] # for python 2.7
    

    zip gives you a list of tuples, and the list comprehension does the rest.

    0 讨论(0)
  • 2020-12-02 09:17

    It is not super fast solution, but works for any dimension

    import numpy as np
    def linspace_md(v_min,v_max,dim,num):
        output = np.empty( (num**dim,dim)  )
        values = np.linspace(v_min,v_max,num)
        for i in range(output.shape[0]):
            for d in range(dim):
                output[i][d] = values[( i//(dim**d) )%num]
        return output
    
    0 讨论(0)
提交回复
热议问题