array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
I want to get
[1,2
4,5
7,8]
ndarray[:][0:2]
it get
array([[1, 2, 3],
[4, 5, 6]])
why!?
ndarray[:] returns an identical array and when you use [0:2], it returns an arry with the first 2 elements, and hence [[1,2,3],[4,5,6]]
What you want to do is this : ndarray[0:3,0:2] or simpler ndarray[:,:2]
This will return a slice of the array slicing in 2 dimensions
ndarray[:] gives you the whole array, and with the following [0:2] you select the first two element of it, which is [1,2,3] and [4,5,6].
You need to slice (as DavidG already suggested) in the first bracket for all desired dimensions:
ndarray[:,0:2]
P.S.: Please change your title to something, that relates closer to your problem.
Your code:
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(a[:])
Output: same as a, as you are selecting all of the rows:
[[1 2 3] [4 5 6] [7 8 9]]
Then indexing on that will apply the index on the result, all of the rows (i.e. a[:][0:2] is equivalent to a[0:2] - selecting the first two rows)
To select the first two columns:
print(a[:, 0:2])
Output (as expected):
[[1 2] [4 5] [7 8]]
I would recommend going through Numpy's indexing documentation: https://docs.scipy.org/doc/numpy/user/basics.indexing.html
Related
When using a numpy array as a matrix, in which order are rows and columns?
For example:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Is [1, 2, 3] the first row or the first column?
I cannot find this information in the documentation, perhaps because the answer is too obvious.
[1, 2, 3] is the first row.
The examples in numpy ndarray documentation actually gives you some hints:
>>> x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
>>> # The element of x in the *second* row, *third* column, namely, 6.
>>> x[1, 2] ```
I found it strange that indexing using range(:) operator for list of lists is not supported.
Sometimes this result in strange values :
a = [[1, 2], [3, 4], [5, 6], [7, 8]]
>>> a
[[1, 2], [3, 4], [5, 6], [7, 8]]
>>> a[0][1]
2
>>> a[1][1]
4
>>> a[2][1]
6
However,
>>> a[0:3][1]
[3, 4]
I was expecting [2,4,6]. What am I missing here ?
I tried this on Numpy arrays as well.enter code here
>>> a
[[1, 2], [3, 4], [5, 6], [7, 8]]
>>> a[0][1]
2
>>> a[1][1]
4
>>> a[2][1]
6
>>> a[0:3][1]
[3, 4]
I know I can use list comprehension, but my question is whether ":" is supported for list of lists?
numpy arrays do support slicing, but you're not considering the shape of the array. In numpy, this array has shape:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
print(a.shape)
>>>(4, 2)
meaning it's 4x2. If you slice [0:3] you're returning the first three elements of the 1st dimension. i.e.:
print(a[0:3])
>>>[[1 2]
[3 4]
[5 6]]
this output has shape:
print(a[0:3].shape)
>>>(3, 2)
if you do:
print(a[0:3][1])
>>>[3 4]
You are again calling the first element of the first dimension of the array that has shape (3, 2).
Instead you want to call:
print(a[0:3][:,1])
>>>[2 4 6]
Which gives you all of the row elements (i.e. all three elements of the first dimension) at column index 1 (where 0 and 1 represent the indexes for the two dimensions of the second dimension).
even cleaner (recommended):
print(a[0:3, 1])
>>>[2 4 6]
Using : is totally supported. Explained below...
So we start with:
a = [[1, 2], [3, 4], [5, 6], [7, 8]]
You asked about:
a[0:3][1]
We want the items from list a, from positions zero to three [0:3]. Those items returned are
[1, 2] --- position 0
[3, 4] --- position 1
[5, 6] --- position 2
[7, 8] --- position 3
Then we request from that list the item in position 1, which returns:
[3, 4]
If you want to access items inside that smaller list you need to add another index, like this:
a[0:3][1][1]
would return:
4
Diagram of basic string splitting:
Your first bracket (represented in blue) is saying "give me elements in list a between positions 0 and 3, which in this case, is ALL of them.
Your second bracket (represented in red) is saying "of the results of my first bracket, give me the element that is in position 1", which is the entire sub-list [3,4]
In this specific case
a[0:3][1]
could have simply been written as
a[1]
let us assume a list of list
list=[[1,2],[3,4],[5,6],[7,8]]
then,
list[0:3]
will return a list with elements(which are also list) from index 0 to 2
[[1, 2], [3, 4], [5, 6]]
so according list[0:3][1] will return the second element([3,4]) whose index is "1" .
a[0:3][1] will not return[2,4,6] , it returns the list of list with 3 element and chooses the second element.
When you call a[0:3] the result of that is a list with the first three elements of a. You then call a[0:3][1] which returns the 2nd element of that list which is the list [3,4].
Ordinary Python lists do not support this kind of slicing.
You can get [2, 4, 6] with Numpy:
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> a[0:3, 1]
array([2, 4, 6])
a = [[1, 2], [3, 4], [5, 6], [7, 8]]
a[0:3]
the output of this is a list:
>>> [[1, 2], [3, 4], [5, 6]]
Therefore:
a[0:3][1]
Accesses the element at index 1, which is [3, 4]
To get the desired output from your list, use list comprehension:
[x[1] for x in a[0:3]]
>>> [2, 4, 6]
How do I modify particular a row or column of a NumPy array?
For example I have a NumPy array as follows:
P = array([[1, 2, 3],
[4, 5, 6]])
How do I change the elements of first row, [1, 2, 3], to [7, 8, 9] so that the P will become:
P = array([[7, 8, 9],
[4, 5, 6]])
Similarly, how do I change second column values, [2, 5], to [7, 8]?
P = array([[1, 7, 3],
[4, 8, 6]])
Rows and columns of NumPy arrays can be selected or modified using the square-bracket indexing notation in Python.
To select a row in a 2D array, use P[i]. For example, P[0] will return the first row of P.
To select a column, use P[:, i]. The : essentially means "select all rows". For example, P[:, 1] will select all rows from the second column of P.
If you want to change the values of a row or column of an array, you can assign it to a new list (or array) of values of the same length.
To change the values in the first row, write:
>>> P[0] = [7, 8, 9]
>>> P
array([[7, 8, 9],
[4, 5, 6]])
To change the values in the second column, write:
>>> P[:, 1] = [7, 8]
>>> P
array([[1, 7, 3],
[4, 8, 6]])
In a similar way if you want to select only two last columns for example but all rows you can use:
print P[:,1:3]
If you have lots of elements in a column:
import numpy as np
np_mat = np.array([[1, 2, 2],
[3, 4, 5],
[5, 6, 5]])
np_mat[:,2] = np_mat[:,2] * 3
print(np_mat)
It is making a multiplied by 3 change in third column:
[[ 1 2 6]
[ 3 4 15]
[ 5 6 15]]
I have one large numpy.ndarray array that I want to extract the 4th and 5th columns out of and put those columns into a 2D array. The [i,0] element should be the value on the 4th column and [i,1] should be the element from the 5th column.
I trying to use the numpy.hstack function to do this.
a = numpy.asarray([1, 2, 3, 4, 5])
for i in range(5):
a = numpy.vstack([a, numpy.asarray([1, 2, 3, 4, 5])])
combined = np.hstack([a[:,3], a[:,4]])
However, this simply gives me an nx1 array. I have tried multiple approaches using concatenate that look like these examples:
combined = np.concatenate([a[:,3], a[:,4]])
combined = np.concatenate([a[:,3], a[:,4]], axis=1)
combined = np.concatenate([a[:,3].T, a[:,4].T])
I feel like hstack is the function I want, but I can't seem to figure out how to make it give me an nx2 array. Can anyone point me in the right direction? Any help is appreciated.
Just slice out your data as follows:
X = [[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]
slicedX = X[:,3:5]
results in:
[[3 4]
[3 4]
[3 4]
[3 4]]
I think this will do what you want:
a[:,[3,4]]
You can also use zip:
>>> c = numpy.array( zip( a[:, 3], a[:, 4]) )
>>> c
array([[4, 5],
[4, 5],
[4, 5],
[4, 5],
[4, 5],
[4, 5]])
Say we have a list:
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Then I want to create a new list through slicing away the first element in each element in the list:
b = a[][1:]
Obviously the above doesn't work, but what I want b to be now is:
[[2, 3], [5, 6], [8, 9]]
Of course I can just loop through it, but all I want to know is if it's possible to do it in any similar way of what I tried so wrongly to do above. Also, preferably is there a better/good way of doing this with numpy, in which case it doesn't need to be done through slicing the way I attempted to do it?
You can use list comprehensions:
b = [x[1:] for x in a]
Demo:
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b = [x[1:] for x in a]
>>> b
[[2, 3], [5, 6], [8, 9]]
>>>
Using numpy indexing/slicing notation, you use commas to delimit the slice for each dimension:
import numpy as np
a = np.array([[1,2,3],[1,2,3],[1,2,3]])
print a[:,1:]
output:
[[2 3]
[2 3]
[2 3]]
For additional reading on numpy indexing:
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
in python 3 you can also use *
b = [x for _,*x in a]
this approach is more flexible since you can for example left first and last elements of the inside list, no matter how long is the list:
b = [first,last for first,*middle,last in a]