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]])
Related
For the following array:
array = [
[1, 5, 6, 8, 10, 3],
[3, 2, 4, 9, 11, 7],
[8, 0, 9, 6, 23, 4]
]
How could we sum the elements (per row) as indicated by these indices:
indices = [
[2, 4, 5],
[1, 3],
[4]
]
that is to say that:
for the first row only the values on indices [2, 4, 5] will be considered when summing up -> (6 + 10 + 3)
for the second row only the values on indices [1, 3] will be considered when summing up -> (2 + 9)
and so on
Output:
array([19, 11, 23])
The output has the same shape as if we did array.sum(axis=1) but not every value is included. Instead, the participants of each row are determined by the indices array.
I have thought of creating a mask for that purpose, but I did not know how to pass the indices to it.
Try this:
arr = np.array(array)
out = np.array([arr[idx, ind].sum() for idx, ind in enumerate(indices)])
out
Output : array([19, 11, 23])
The [::n] indexing option in numpy provides a very useful way to index every nth item in a list. However, is it possible to use this feature to extract multiple values, e.g. every other pair of values?
For example:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
And I want to extract every other pair of values i.e. I want to return
a[0, 1, 4, 5, 8, 9,]
Of course the index could be built using loops or something, but I wonder if there's a faster way to use ::-style indexing in numpy but also specifying the width of the pattern to take every nth iteration of.
Thanks
With length of array being a multiple of the window size -
In [29]: W = 2 # window-size
In [30]: a.reshape(-1,W)[::2].ravel()
Out[30]: array([0, 1, 4, 5, 8, 9])
Explanation with breaking-down-the-steps -
# Reshape to split into W-sized groups
In [43]: a.reshape(-1,W)
Out[43]:
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
# Use stepsize to select every other pair starting from the first one
In [44]: a.reshape(-1,W)[::2]
Out[44]:
array([[0, 1],
[4, 5],
[8, 9]])
# Flatten for desired output
In [45]: a.reshape(-1,W)[::2].ravel()
Out[45]: array([0, 1, 4, 5, 8, 9])
If you are okay with 2D output, skip the last step as that still be a view into the input and virtually free on runtime. Let's verify the view-part -
In [47]: np.shares_memory(a,a.reshape(-1,W)[::2])
Out[47]: True
For generic case of not necessarily a multiple, we can use a masking based one -
In [64]: a[(np.arange(len(a))%(2*W))<W]
Out[64]: array([0, 1, 4, 5, 8, 9])
You can do that reshaping the array into a nx3 matrix, then slice up the first two elements for each row and finally flatten up the reshaped array:
a.reshape((-1,3))[:,:2].flatten()
resulting in:
array([ 0, 1, 3, 4, 6, 7, 9, 10])
I've got a numpy matrix that has 2 rows and N columns, e.g. (if N=4):
[[ 1 3 5 7]
[ 2 4 6 8]]
The goal is create a string 1,2,3,4,5,6,7,8.
Merge the rows such that the elements from the first row have the even (1, 3, ..., N - 1) positions (the index starts from 1) and the elements from the second row have the odd positions (2, 4, ..., N).
The following code works but it isn't really nice:
xs = []
for i in range(number_of_cols):
xs.append(nums.item(0, i))
ys = []
for i in range(number_of_cols):
ys.append(nums.item(1, i))
nums_str = ""
for i in range(number_of_cols):
nums_str += '{},{},'.format(xs[i], ys[i])
Join the result list with a comma as a delimiter (row.join(','))
How can I merge the rows using built in functions (or just in a more elegant way overall)?
Specify F order when flattening (or ravel):
In [279]: arr = np.array([[1,3,5,7],[2,4,6,8]])
In [280]: arr
Out[280]:
array([[1, 3, 5, 7],
[2, 4, 6, 8]])
In [281]: arr.ravel(order='F')
Out[281]: array([1, 2, 3, 4, 5, 6, 7, 8])
Joining rows can be done this way :
>>> a = np.arange(12).reshape(3,4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> np.hstack([a[i,:] for i in range(a.shape[0])])
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Then it's simple to convert this array into string.
Here's one way of doing it:
out_str = ','.join(nums.T.ravel().astype('str'))
We are first transposing the array with .T, then flattening it with .ravel(), then converting each element from int to str, and then applying `','.join() to combine all the str elements
Trying it out:
import numpy as np
nums = np.array([[1,3,5,7],[2,4,6,8]])
out_str = ','.join(nums.T.ravel().astype('str'))
print (out_str)
Result:
1,2,3,4,5,6,7,8
Lets say I have a numpy 2D array like this:
a = np.array([[3,6,7],[1,9,4],[ 3,7,8],[2,5,10]])
a
# array([[ 3, 6, 7],
# [ 1, 9, 4],
# [ 3, 7, 8],
# [ 2, 5, 10]])
I need to sort the rows descending based on the first column and ascending on the second column to get the below result:
array([[ 3, 6, 7],
[ 3, 7, 8],
[ 2, 5, 10],
[ 1, 9, 4]])
Doing this in Matlab was simple using sortrows(my_matrix,[-1 2]) where -1 for the first column descending and 2 for the second column ascending.
I wonder if there is a function like that in numpy.
Here is how you can do it using the numpy_indexed package:
import numpy_indexed as npi
print(a[npi.argsort((a[:,1], -a[:,0]))])
If you're willing to use pandas, you can pass a list into the ascending keyword to control the sort order of each field:
>>> pd.DataFrame(a).sort_values([0,1], ascending=[False, True])
0 1 2
0 3 6 7
2 2 5 10
1 1 9 4
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?