Related
This question already has answers here:
concatenate numpy arrays which are elements of a list
(2 answers)
Closed 2 years ago.
I have a list of numpys of the same length each. for example:
my_list = [np.array([2, 3, 5, 5]),
np.array([5, 4, 1, 4]),
np.array([8, 4, 5, 1]),
np.array([7, 4, 5, 1])]
I want to turn the list into 2d numpy:
[[2, 3, 5, 5],
[5, 4, 1, 4],
[8, 4, 5, 1],
[7, 4, 5, 1]]
The following code does perform the operation but in a sloppy manner.
The result is also not arranged in the desired order:
combined = []
for i in my_list :
if len(combined) == 0:
combined = i
else:
combined = np.vstack((i,combined))
print(combined)
What needs to be changed to get the desired result?
The most straightforward way
np.vstack(my_list)
or
np.concatenate(my_list).reshape(len(my_list),-1)
Simply doing np.array(my_list) can get the job done.
I have to delete last three rows of the array. It was list but I had to convert it into array so that I can use np.delete function
I tried np.delete function. It deletes column wise instead of row wise.
I want to delete row not column. When I change the axis to 1. it gives an error message of AxisError: axis 1 is out of bounds for array of dimension 1
featureStr2=np.delete(f, slice(3,-1), axis=0). I want to delete last 3 rows. Array looks like below
1 2 3 4 5
6 7 8 9 20
11 23 54 6 7
2 3 4 5 6 7
1 2 3 4 5
Out put of the code is. I want output to delete last 3 rows.
Don't delete in numpy. Deleting triggers a reallocation, which is expensive. The cheap (proper) solution is to just create a view using indexing:
arr = arr[:-3, ...]
you can drop rows by using pandas with drop, indexing and condition function
import numpy as np
import pandas as pd
df = ([1,2,3,4,5], [6,7,8,9,20],[11,23,54,6,7],[2,3,4,5,6,7],[1,2,3,4,5])
series = pd.DataFrame(df)
by using drop function
series1 = series.drop([2,3,4])
print(series1)
using index function
series1 = series.drop(series.index[2,3,4]
print(series1)
What you need is Axis and object:
Syntax: numpy.delete(arr, obj, axis=None)
object : is the row number or column number or a indices
Axis: 0 for the rows and 1 for the columns
e.g. i'm assuming your array looks like this.
a = np.array([[1,2,3,4,5], [2,4,5,6,7], [3,4,5,6,7], [5,7,8,9,1]])
>>> np.delete(a, [2,3], axis=0)
array([[1, 2, 3, 4, 5],
[2, 4, 5, 6, 7]])
P.S. for now np.delete doesn't support negative indices, in future it will , so i suggest you to get the indices of the rows you want to delete first and then pass it to obj in np.delete()
The simplest way is to just use basic indexing
>>>import numpy as np
>>>arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 20], [11, 23, 54, 6, 7],
[2, 3, 4, 6, 7],[1,2, 3, 4, 5]])
>>>arr = arr[:-3]
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 20]])
np.delete(arr, obj, axis=None) doesn't take in negative indices in its object argument
Also, if array size is large, then supplying index of every row, column or element to be deleted becomes tedious.
>>>np.delete(arr, [2,3,4], axis=0)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 20]])
But by using np.s_ you can supply a slice to the function
>>>np.delete(arr, np.s_[2:5], axis=0)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 20]])
You can supply negative indexing to np.s_
>>>np.delete(arr, np.s_[-3:], axis=0)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 20]])
This question already has answers here:
Index n dimensional array with (n-1) d array
(3 answers)
Closed 4 years ago.
It is hard to find a clear title but an example will put it clearly.
For example, my inputs are:
c = np.full((4, 3, 2), 5)
c[:,:,1] *= 2
ix = np.random.randint(0, 2, (4, 3))
if ix is:
array([[1, 0, 1],
[0, 0, 1],
[0, 0, 1],
[1, 1, 0]])
if want as a result:
array([[10, 5, 10],
[ 5, 5, 10],
[ 5, 5, 10],
[10, 10, 5]])
My c array can be of arbitrary dimensions, as well a the dimension I want to sample in.
It sounds like interpolation, but I'm reluctant to construct a be array of indices each time I want to apply this. Is there a way of doing this using some kind of indexing on numpy arrays ? Or do I have to use some interpolation methods...
Speed and memory are a concern here because I have to do this many times, and the arrays can be really large.
Thanks for any insight !
Create the x, y indices with numpy.ogrid, and then use advanced indexing:
idx, idy = np.ogrid[:c.shape[0], :c.shape[1]]
c[idx, idy, ix]
#array([[10, 5, 10],
# [ 5, 5, 10],
# [ 5, 5, 10],
# [10, 10, 5]])
This question already has answers here:
Concatenating two one-dimensional NumPy arrays
(6 answers)
Closed 5 years ago.
I have written this code to append two numpy arrays:
td_XN = searchNegative(X,y,10)
td_XP = searchPosotive(X,y,10)
print(np.array(td_XN).shape, np.array(td_XP).shape)
print(type(td_XN), type(td_XP))
td_X = np.concatenate(td_XP, td_XN)
td_y = [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
print(td_X.shape, len(td_y))
However, it produces this error:
TypeError: only length-1 arrays can be converted to Python scalars
On this line:
td_X = np.concatenate(td_XP, td_XN)
If you want to concatenate side by side (that is, create an array of 10 -by- 2544*2): you can do
td_X = np.concatenate([td_XP, td_XN],axis=1)
For example
td_X = np.concatenate([[[1,2,3,7],[4,5,6,8]],[[1,2,3],[4,5,6]]],axis=1)
gives
array([[1, 2, 3, 7, 1, 2, 3],
[4, 5, 6, 8, 4, 5, 6]])
On the other hand, if you want to add td_XN below td_XP you can do
td_X = np.concatenate([td_XP, td_XN],axis=0)
For example,
td_X = np.concatenate([[[1,2,3],[4,5,6]],[[1,2,7],[4,5,8]]],axis=0)
gives
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 7],
[4, 5, 8]])
I have a one dimensional array from which I would like to create a new array containing only parts of user wished sizes of the beginning, the middle, and the end of the former.
import numpy
a = range(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
I would like b to be equal to:
b
array([0, 1, 2, 5, 6, 7, 9])
Assuming that b is constructed of the concatenation of a[:3], a[5:6], and a[9].
I can of course use things such as np.concatenate, but is there a way to do that with slicing method, or anything else in one line?
One way is to create an array of the indices you want to index your array with:
import numpy
a = numpy.arange(10)
i = numpy.array([0, 1, 2, 5, 6, 7, 9]) # An array containing the indices you want to extract
print a[i] # Index the array based on the indices you selected
OUTPUT
[0 1 2 5 6 7 9]
I found a solution:
import numpy as np
a = range(10)
b = np.hstack([a[:3], a[5:6], a[9])
b
array([0, 1, 2, 5, 6, 7, 9])
but does slicing allow such a move?