1

Hello :) I am a python beginner and i started working with numpy lately, basically i got a nd-array: data.shape = {55000, 784} filled with float32 values. Based on a condition i made, i want to append specific rows and their columns to a new array, its important that the formating stays the same. e.g. i want data[5][0-784] appended to an empty array.. i heard about something called fancy indexing, still couldn't figure out how to use it, an example would help me out big time. I would appreciate every help from you guys! - Greets

A. K.
  • 31
  • 4
  • Forget about 'append to an empty array'. The focus should be on the selection of rows and/columns, preferably with one action. You may need to elaborate on your selection conditions. – hpaulj Feb 25 '17 at 01:20

2 Answers2

1

I'd recommend skimming through the documentation for Indexing. But, here is an example to demonstrate.

import numpy as np

data = np.array([[0, 1, 2], [3, 4, 5]])
print(data.shape)
(2, 3)

print(data)
[[0 1 2]
 [3 4 5]]

selection = data[1, 1:3]
print(selection)
[4 5]

Fancy indexing is an advanced indexing function which allows indexing using integer arrays. Here is an example.

fancy_selection = data[[0, 1], [0, 2]]
print(fancy_selection)
[0 5]

Since you also asked about appending, have a look at Append a NumPy array to a NumPy array. Here is an example anyway.

data_two = np.array([[6, 7, 8]])
appended_array = np.concatenate((data, data_two))
print(appended_array)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Community
  • 1
  • 1
bluprince13
  • 4,607
  • 12
  • 44
  • 91
  • Thanks so much for your help, the examples were very helpful and i could solve my problem :) Greetings!!! – A. K. Feb 25 '17 at 19:12
0

As @hpaulj recommends in his comment appending to arrays is possible but inefficient and should be avoided. Let's turn to your example but make the numbers a bit smaller.

a = np.sum(np.ogrid[1:5, 0.1:0.39:0.1])
a
# array([[ 1.1,  1.2,  1.3],
#        [ 2.1,  2.2,  2.3],
#        [ 3.1,  3.2,  3.3],
#        [ 4.1,  4.2,  4.3]])
a.shape
# (4, 3)

Selecting an element:

a[1,2]
# 2.3

Selecting an entire row:

a[2, :] # or a[2] or a 2[, ...]
# array([ 3.1,  3.2,  3.3])

or column:

a[:, 1] # or a[..., 1]
# array([ 1.2,  2.2,  3.2,  4.2])

fancy indexing, observe that the first index is not a slice but a list or array:

a[[3,0,0,1], :] # or a[[3,0,0,1]]
# array([[ 4.1,  4.2,  4.3],
#        [ 1.1,  1.2,  1.3],
#        [ 1.1,  1.2,  1.3],
#        [ 2.1,  2.2,  2.3]])

fancy indexing can be used on multiple axes to select arbitrary elements and assemble them to a new shape for example you could make a 2x2x2 array like so:

a[ [[[0,1], [1,2]], [[3,3], [3,2]]], [[[2,1], [1,1]], [[2,1], [0,0]]] ]
# array([[[ 1.3,  2.2],
#         [ 2.2,  3.2]],
#
#        [[ 4.3,  4.2],
#         [ 4.1,  3.1]]])

There is also logical indexing

mask = np.isclose(a%1.1, 1.0)
mask
# array([[False, False, False],
#        [ True, False, False],
#        [False,  True, False],
#        [False, False,  True]], dtype=bool)
a[mask]
# array([ 2.1,  3.2,  4.3])

To combine arrays, collect them in a list and use concatenate

np.concatenate([a[1:, :2], a[:0:-1, [2,0]]], axis=1)
# array([[ 2.1,  2.2,  4.3,  4.1],
#        [ 3.1,  3.2,  3.3,  3.1],
#        [ 4.1,  4.2,  2.3,  2.1]])

Hope that help getting you started.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99