0

I have one array and i want to convert it into certain shape which I do not know how to do.

I tried, but it does not give me proper result.

Here is the array-: this is a numpy array

a=[[ [1,2,13],
     [12,2,32],
     [61,2,6],
     [1,23,3],
     [1,21,3],
     [91,2,38] ]]

expected outputs-:

1. [[ [1,2],
      [12,2],
      [61,2],
      [1,23],
      [1,21], 
      [91,2] ]]

2.  [ [1,2],
       [12,2],
       [61,2],
       [1,23],
       [1,21],
       [91,2] ]
3
  • 1
    Why do you want two identical outputs? Why does the input have a length-1 first dimension? Why are you discarding a third of the data? Commented Oct 12, 2020 at 11:23
  • convert list to ndarray then try print(a[:, 0:, 1:]) Commented Oct 12, 2020 at 11:25
  • "I tried, but it does not give me proper result." Okay, what did you try? And what result did it give you? And what was your reasoning for trying it? Commented Oct 12, 2020 at 11:25

3 Answers 3

1

So the question can be boiled down to

"Given a 3D numpy array with the shape (1, 6, 3) how can we make a copy but a shape of (1, 6, 2) by removing the last indexed value from the innermost nested array?"

Array Indexing

The below example achieves this by slicing the original array (a) to return the desired structure.

import numpy as np

a = np.array([[[1,2,13],[12,2,32],[61,2,6],[1,23,3],[1,21,3],[91,2,38]]])
o = a[:,:,:2]

List Comprehension

The below makes use of a list comprehension applied to filter a down in the manner described above.

import numpy as np

a = np.array([[[1,2,13],[12,2,32],[61,2,6],[1,23,3],[1,21,3],[91,2,38]]])
o = np.array([[j[:2] for i in a for j in i]])

In each of the above examples o will refer to the following array (the first output you are asking for).

array([[[ 1,  2],
        [12,  2],
        [61,  2],
        [ 1, 23],
        [ 1, 21],
        [91,  2]]])

Given o as defined by one of the above examples, your second sought output is accessible via o[0].

Sign up to request clarification or add additional context in comments.

4 Comments

Why not array indexing? a[:, :2]
@hpaulj Good point! Updated answer to highlight this option - though due to structure defined in OP question correct slice for desired output would be a[:,:,:2].
@JPI93 Thanks for your ans!!, can you tell me what to do for 2nd output?
@Jose Updated answer. In short your second output is accessible via o[0].
1

This will do

import numpy as np
a=[[ [1,2,13],
     [12,2,32],
     [61,2,6],
     [1,23,3],
     [1,21,3],
     [91,2,38] ]]
outputs=list()
for i in a[0]:
  outputs.append([i[0],i[1]])
print(np.array([outputs]))
""" OUTPUTS
[[[ 1  2]
  [12  2]
  [61  2]
  [ 1 23]
  [ 1 21]
  [91  2]]]
"""

2 Comments

Thanks for your ans!!, can you tell me what to do for 2nd output?
this will give output 2 print(np.array(outputs))
1

Instead of deriving output2 = output1[0] you could use squeeze method. It removes all the single - dimensional entries from your array

output1 = a[:,:,:2]
output2 = output1.squeeze() 

This is a vizualization of a process for a better understanding:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.