0

New to Numpy and Arrays, my mind is getting bent like warm metal over coals not able to understand how this should work.

I got this homework question that I've been stuck on for days and I have tried understanding it but the better of me I just cant grasp the concept.

The Question

A 3 by 4 by 4 is created with “arr = np.linspace(1, 48, 48).reshape(3, 4,
4)”. Index or slice this array to obtain the following:
■ 20.0
■ [ 9. 10. 11. 12.]
■ [[33. 34. 35. 36.] [37. 38. 39. 40.] [41. 42. 43. 44.] [45. 46. 47. 48.]]
■ [[5. 6.], [21. 22.] [37. 38.]]
■ [[36. 35.] [40. 39.] [44. 43.] [48. 47.]]
■ [[13. 9. 5. 1.] [29. 25. 21. 17.] [45. 41. 37. 33.]]
■ [[1. 4.] [45. 48.]]
■ [[25. 26. 27. 28.], [29. 30. 31. 32.], [33. 34. 35. 36.], [37. 38. 39. 40.]]
Hint: use flatten and reshape.

The documentation they gave us with this question is limited and as a newbie to this is not making sense at all...

Any help would be appreciated.

Thanks in advance Faz

EDIT*

So I kinda get the concept


print("Array range printed for [9. 10. 11. 12.]\n")
print(arr[0:1, 2])

print(
    "\nArray range printed for 20")
print(arr[1, 0, -1])
print(
    "\nArray range printed for [[33. 34. 35. 36.] [37. 38. 39. 40.] [41. 42. 43. 44.] [45. 46. 47. 48.]]\n")
print(arr[2])

But I am sitting with this one at the moment

print(
    "\nArray range printed for [[5. 6.], [21. 22.] [37. 38.]]\n")

How does one print ONLY two digits with a range of 3?

My current solution gives two values but not the 3 ranges

print(arr[0:2, 1, 1])
Output = [ 6. 22.]
5
  • How does one get [ 9. 10. 11. 12.] with arr = np.linspace(1, 48, 48).reshape(3, 4, 4)? I have tried playing around with the values and see what happens but It is just giving me errors every time... Commented Aug 22, 2019 at 0:11
  • 2
    I would going into an interactive Python session with numpy, preferably ipython. Create arr as specified. print(arr) to set it. Then practice doing different kinds of indexing, presumably using information that you've been taught. Commented Aug 22, 2019 at 1:01
  • 1
    Concerning your edit. The disapointing solution is that you have to print it three times... Commented Aug 22, 2019 at 2:25
  • so basically print(arr[0:2, 1, 1],arr[0:2, 3, 2],arr[0:1, 3, 1]) @Yvain ? Commented Aug 22, 2019 at 2:28
  • I'm sorry there is indeed a method for this, check out the edit on the answer. Commented Aug 22, 2019 at 14:28

2 Answers 2

3
In [43]: arr = np.linspace(1, 48, 48).reshape(3, 4, 
    ...: 4)                                                                                                  
In [44]: arr                                                                                                 
Out[44]: 
array([[[ 1.,  2.,  3.,  4.],
        [ 5.,  6.,  7.,  8.],
        [ 9., 10., 11., 12.],
        [13., 14., 15., 16.]],

       [[17., 18., 19., 20.],
        [21., 22., 23., 24.],
        [25., 26., 27., 28.],
        [29., 30., 31., 32.]],

       [[33., 34., 35., 36.],
        [37., 38., 39., 40.],
        [41., 42., 43., 44.],
        [45., 46., 47., 48.]]])

where is 20? 2nd block, 1st row, 4th column, which translates into:

In [45]: arr[1, 0, 3]                                                                                        
Out[45]: 20.0

Where's the block that starts with 33,34,...? third block?


[[5. 6.], [21. 22.] [37. 38.]]

That's the part of the 2nd row of all blocks

a[:,   # all blocks
a[:, 1  # 2nd row
a[:, 1, :2]   # first two entries in each row

[[36. 35.] [40. 39.] [44. 43.] [48. 47.]] is part of the last block, but with values reversed (.e.g. -1 step size).

[[13. 9. 5. 1.] [29. 25. 21. 17.] [45. 41. 37. 33.]] first column, from bottom to top.

[[1. 4.] [45. 48.]] - 4 corners; that requires advanced indexing, i.e. with lists of lists or array. Have they covered that?

[[25. 26. 27. 28.], [29. 30. 31. 32.], [33. 34. 35. 36.], [37. 38. 39. 40.]] - rows from different blocks; simple indexing does not work. But they are contiguous, so with a flattening or reshaping to (12,4) puts them together, from which they can be selected as a simple block.

For all these I am looking at the Out[44] display, and identifying where the blocks of numbers occur. From that I try to describe the selection in terms of blocks, rows, and columns. The names aren't important to numpy, but they help me describe the action(s).

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

2 Comments

why are they three values for the linear space function ?
Did you look up the docs for that function? start, stop, number of points. np.arange(1,49) would have produced the same thing (except integers).
0
Import numpy as np

#print [[36. 35.] [40. 39.] [44. 43.] [48. 47.]]
mask = np.array([3,2]) #mask will swap 3rd and 2nd elements
x = arr[2, :]
print(x[:, mask])

#[[13. 9. 5. 1.] [29. 25. 21. 17.] [45. 41. 37. 33.]]
x = arr[:,:,0] #select all blocks, all rows, first column
mask1 = np.array([0,1,2])

x_t = x.T #Transpose matrix
#mask for manipulating the matrix
mask2 = np.array([3,2,1,0])
print(x_t[:][mask2].T)

# [[1. 4.] [45. 48.]]
mask_blocks = np.array([0,2]) #mask blocks
arr[mask_blocks]

mask_rows = np.array([0,3]) #mask the rows

x = arr[mask_blocks][:, mask_rows] #index/slice the matrix
mask_index = np.array([0,3]) #mask for indexes

#x[:][0,0][mask_index] gets the first values
#x[:][1,1][mask_index] gets the second values

print(np.array([x[:][0,0][mask_index], x[:][1,1][mask_index]])) #print [[ 1.  4.][45. 48.]]

#[[25. 26. 27. 28.], [29. 30. 31. 32.], [33. 34. 35. 36.], [37. 38. 39. 40.]]

mask_blocks = np.array([1,2])

x = arr[mask_blocks] #gets the columns
x
mask_rows = np.array([2,3]) #mask the rows

x = arr[mask_blocks][:, mask_rows] #index/slice the matrix

mask_index = np.array([0,3]) #mask for indexes

#arr[1][2:4,:] gets second block, last two rows
#arr[2][0:2,:] gets third block, first two rows

print(np.array([arr[1][2:4,:], arr[2][0:2,:]]).reshape(4,4).tolist()) #prints list

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.