The simplest way to delete rows and columns from arrays is the numpy.delete method.
Suppose I have the following array x:
x = array([[1,2,3],
[4,5,6],
[7,8,9]])
To delete the first row, do this:
x = numpy.delete(x, (0), axis=0)
To delete the third column, do this:
x = numpy.delete(x,(2), axis=1)
So you could find the indices of the rows which have a 0 in them, put them in a list or a tuple and pass this as the second argument of the function.
Answer from Jaidev Deshpande on Stack OverflowThe simplest way to delete rows and columns from arrays is the numpy.delete method.
Suppose I have the following array x:
x = array([[1,2,3],
[4,5,6],
[7,8,9]])
To delete the first row, do this:
x = numpy.delete(x, (0), axis=0)
To delete the third column, do this:
x = numpy.delete(x,(2), axis=1)
So you could find the indices of the rows which have a 0 in them, put them in a list or a tuple and pass this as the second argument of the function.
Here's a one liner (yes, it is similar to user333700's, but a little more straightforward):
>>> import numpy as np
>>> arr = np.array([[ 0.96488889, 0.73641667, 0.67521429, 0.592875, 0.53172222],
[ 0.78008333, 0.5938125, 0.481, 0.39883333, 0.]])
>>> print arr[arr.all(1)]
array([[ 0.96488889, 0.73641667, 0.67521429, 0.592875 , 0.53172222]])
By the way, this method is much, much faster than the masked array method for large matrices. For a 2048 x 5 matrix, this method is about 1000x faster.
By the way, user333700's method (from his comment) was slightly faster in my tests, though it boggles my mind why.
Faster way to delete numpy array rows than numpy.delete?
python - Delete rows at select indexes from a numpy array - Stack Overflow
Deleting rows and columns from numpy arrays
how can I remove rows from a numpy array based on a condition
I've to two text files of data I'm importing with np.loadtxt, each 1.7 million lines long, and each contains many rows of zeros I'd like to remove.
numpy.delete doesn't seem to be up to the job to make this a realistic project (taking many minutes each iteration), so I'm wondering what other options people might know about?
To delete indexed rows from numpy array:
arr = np.delete(arr, indexes, axis=0)
One approach would be to get the remaining row indices with np.setdiff1d and then use those row indices to get the desired output -
out = arr[np.setdiff1d(np.arange(arr.shape[0]), indexes)]
Or use np.in1d to leverage boolean indexing -
out = arr[~np.in1d(np.arange(arr.shape[0]), indexes)]
I have this numPlayers-long list of numpy arrays called payoffMatrix. That is, payoffMatrix[x] is a numpy array. Given this removeStrategy function:
def removeStrategy(self, player, s):
"""Removes strategy s from player in the payoff matrix
Args:
player (int): index of the player
s (int): index of the strategy
"""
if player == 0: # player is player 1
for x in range(self.numPlayers):
if self.numPlayers < 3:
# deleting s-th row from every x-th matrix
self.payoffMatrix[x] = np.delete(self.payoffMatrix[x], s, axis=0)
else:
for ar in self.payoffMatrix[x]:
ar = np.delete(ar, s, axis=0)
elif player == 1: # player is player 2
for x in range(self.numPlayers):
if self.numPlayers < 3:
# deleting s-th column from every x-th matrix
self.payoffMatrix[x] = np.delete(self.payoffMatrix[x], s, axis=1)
else:
for ar in self.payoffMatrix[x]:
ar = np.delete(ar, s, axis=1)
else: # player > 1
(...)
# Decrement the number of strategies for player
self.players[player].numStrats -= 1
returnthe following works as expected:
arr_2players = np.array([
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
])
G = simGame(2)
G.enterPayoffs(arr_2players, 2, [2, 2])
G.removeStrategy(0, 1)
G.print()output:
[[1 2]] [[5 6]]
The following doesn't work:
arr_3players = np.array([
[ # player 1's matrices
[
[1, 1],
[1, 1],
],
[
[1.1, 1.1],
[1.1, 1.1]
]
],
[ # player 2's matrices
[
[2, 2],
[2, 2]
],
[
[2.1, 2.1],
[2.1, 2.1]
]
],
[ # player 3's matrices
[
[3, 3],
[3, 3]
],
[
[3.1, 3.1],
[3.1, 3.1]
]
]
])
G = simGame(3)
G.enterPayoffs(arr_3players, 3, [2, 2, 2])
G.removeStrategy(0, 1)
G.print()
The output is the same as arr_3players. The expected output is
[[[1. 1. ]] [[1.1 1.1]]] [[[2. 2. ]] [[2.1 2.1]]] [[[3. 3. ]] [[3.1 3.1]]]
I also need it to be capable of deleting columns in these matrices (i.e., when player == 1). For instance,
G = simGame(3) G.enterPayoffs(arr_3players, 3, [2, 2, 2]) G.removeStrategy(1, 1) G.print()
should result in something like
[[[1.] [1.]] [[1.1] [1.1]]] [[[2.] [2.]] [[2.1] [2.1]]] [[[3.] [3.]] [[3.1] [3.1]]]
The problem seems to be that when there are more than 2 players, the matrices are nested one set of brackets deeper in the numpy array. Is there a way to make this work?