How to delete last n rows from Numpy array?
Date : March 29 2020, 07:55 AM
Does that help You can use slice notation for your indexing. To remove the last n rows from an array: a = np.array(range(10)).reshape(5, 2)
>>> a
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
n = 2 # Remove last two rows of array.
>>> a[:-n, :]
array([[0, 1],
[2, 3],
[4, 5]])
>>> a[n:, :] # Remove first two rows.
array([[4, 5],
[6, 7],
[8, 9]])
|
Python array rows*1column numpy for numpy.dot(matrix,matrix2)
Tag : python , By : user106284
Date : March 29 2020, 07:55 AM
I hope this helps . Is a = [1, 2, 3] x = numpy.array(a) a matrix of 3 cols and 1 row? I know that x = numpy.array([a]) is a 1x4 matrix but i need the opossite. I need to multiply two matrix but the first one is a list inserted into a numpy.array(a) , It is a 1 dimensional array: In [653]: x = np.array([1,2,3])
In [654]: x
Out[654]: array([1, 2, 3])
In [655]: x.shape
Out[655]: (3,)
In [656]: x.ndim
Out[656]: 1
In [657]: y = np.array([[1,2,3]])
In [658]: y
Out[658]: array([[1, 2, 3]])
In [659]: y.shape
Out[659]: (1, 3)
In [660]: y.ndim
Out[660]: 2
In [661]: z = y.T
In [662]: z
Out[662]:
array([[1],
[2],
[3]])
In [663]: z.shape
Out[663]: (3, 1)
In [664]: np.dot(x,y)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-664-6849a5f7ad6c> in <module>()
----> 1 np.dot(x,y)
ValueError: shapes (3,) and (1,3) not aligned: 3 (dim 0) != 1 (dim 0)
In [665]: np.dot(y,x)
Out[665]: array([14])
In [666]: x*y
Out[666]: array([[1, 4, 9]])
In [667]: x*z
Out[667]:
array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]])
In [671]: x[:,None]
Out[671]:
array([[1],
[2],
[3]])
In [672]: np.dot(x[:,None],y)
Out[672]:
array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]])
|
Delete all rows in a python numpy array that do not correspond to the top n values of a column
Tag : python , By : user152423
Date : March 29 2020, 07:55 AM
around this issue Since, the sorting needs to be done only on the 2nd column, you can use pandas. It makes the stuff a bit easier: import pandas
In [725]: x
Out[725]:
array([[0, 3],
[1, 4],
[2, 5],
[3, 5],
[2, 4]])
In [724]: pd.DataFrame(x).sort_values(1, ascending=False).head(3).values.tolist()
Out[724]: [[2, 5], [3, 5], [1, 4]]
|
Delete some rows of a numpy array
Date : March 29 2020, 07:55 AM
With these it helps I have a numpy array like below , Take advantage of numpy's vectorized methods. Say your array is a: trimmed = cf[(cf != 0).any(axis=1)]
|
How to delete multiple rows of NumPy array?
Date : March 29 2020, 07:55 AM
may help you . There are several ways to delete rows from NumPy array. The easiest one is to use basic indexing as with standard Python lists: >>> import numpy as np
>>> x = np.arange(35).reshape(7, 5)
>>> x
array([[ 0, 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]])
>>> result = x[5:]
>>> result
array([[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]])
>>> x[:2, 1:4]
array([[1, 2, 3],
[6, 7, 8]])
>>> x[[0, 2, 6]]
array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[30, 31, 32, 33, 34]])
>>> np.take(x, [0, 2, 6], axis=0)
array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[30, 31, 32, 33, 34]])
>>> np.delete(x, slice(0, 5), axis=0)
array([[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]])
>>> np.delete(x, [0, 2, 3], axis=0)
array([[ 5, 6, 7, 8, 9],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]])
>>> mask_array = x[:, 0] < 12 # comparing values of the first column
>>> mask_array
array([ True, True, True, False, False, False, False])
>>> x[mask_array]
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> x[~mask_array] # ~ is an element-wise inversion
array([[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]])
|