Related
I have two numpy arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
and I want to concatenate them into two columns like,
1 4
2 5
3 6
is there any way to do this without transposing or reshaping the arrays?
You can try:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a[np.newaxis, :], b[np.newaxis, :]), axis = 0).T
And you get :
c = array([[1, 4],
[2, 5],
[3, 6]])
Best,
This question already has answers here:
numpy matrix. move all 0's to the end of each row
(2 answers)
Closed 3 years ago.
Suppose i have a numpy array
a = np.array([[1,2,3,4],
[3,4,5,6],
[2,3,4,4],
[3,3,1,2]])
I want to delete the submatrix [[3,4],[3,1]]. I can do it as follows
mask = np.ones(a.shape,dtype=bool)
mask[2:,1:-1] = False
a_new = a[mask,...]
print(a) #output array([1, 2, 3, 4, 3, 4, 5, 6, 2, 4, 3, 2])
However, i want the output as
np.array([[1,2,3,4],
[3,4,5,6],
[2,4,0,0],
[3,2,0,0]])
I just want numpy to remove the submatrix and shift others elements replacing the empty places with 0. How can i do this?
I cannot find a function that does what you ask, but combining np.roll with a mask with this routine produces your output. Perhaps there is a more elegant way:
a = np.array([[1,2,3,4],
[3,4,5,6],
[2,3,4,4],
[3,3,1,2]])
mask = np.ones(a.shape,dtype=bool)
mask[2:,1:-1] = False
mask2 = mask.copy()
mask2[2:, 1:] = False
n = 2 #shift length
a[~mask2] = np.roll((a * mask)[~mask2],-n)
a
>>array([[1, 2, 3, 4],
[3, 4, 5, 6],
[2, 4, 0, 0],
[3, 2, 0, 0]])
you can simply update those element entries to be zero.
a = np.array([[1,2,3,4],
[3,4,5,6],
[2,3,4,4],
[3,3,1,2]])
a[2:, 2:] = 0
returns
array([[1, 2, 3, 4],
[3, 4, 5, 6],
[2, 3, 0, 0],
[3, 3, 0, 0]])
I had two list:
a=[0,0,0,1,1,1,1,2,2]
b=[2,5,12,2,3,8,9,4,6]
And I wanted to get:
c=[[0,2,5,12],[1,2,3,8,9],[2,4,6]]
A and b correlated to each other, a[i] related to b[i], when the value in a change like 0 to 1, 12 end in the first inner-list of c.
I tried it with if else statement but it failed
How to get c in python?
This code produces c in a good enough way (provided a and b are always adjusted in the same way as in the example):
a=[0,0,0,1,1,1,1,2,2]
b=[2,5,12,2,3,8,9,4,6]
c = []
i = 0
while i < len(a):
d = a.count(a[i])
c.append([a[i]] + b[i:i + d])
i += d
print(c) # ==> [[0, 2, 5, 12], [1, 2, 3, 8, 9], [2, 4, 6]]
We can zip the lists, group by first value from a, and make lists with the second:
from itertools import groupby
from operator import itemgetter
a=[0,0,0,1,1,1,1,2,2]
b=[2,5,12,2,3,8,9,4,6]
[list(map(itemgetter(1), group)) for _, group in groupby(zip(a, b), key=itemgetter(0))]
#[[2, 5, 12], [2, 3, 8, 9], [4, 6]]
Similar to #Thierry Lathuille's answer, but does actually prepend the keys to the sub lists as requested by OP:
import itertools as it
ib = iter(b)
[[k, *(next(ib) for _ in gr)] for k, gr in it.groupby(a)]
# [[0, 2, 5, 12], [1, 2, 3, 8, 9], [2, 4, 6]]
Here's my simple solution. Notice that you are splitting the list by by the counts of elemets in the list a. deque is used for popping elements in O(1) time from the left.
import itertools
from collections import Counter, deque
a = [0,0,0,1,1,1,1,2,2]
b = deque([2,5,12,2,3,8,9,4,6])
c = Counter(a)
new_list=[]
for x in c:
new_list.append([x]+[b.popleft() for i in range(a[x])])
How do I find columns in a numpy array that are all-zero and then delete them from the array? I'm looking for a way to both get the column indices and then use those indices to delete.
You could use np.argwhere, with np.all to find your indices. To delete them, use np.delete.
Example:
Find your 0 columns:
a = np.array([[1, 2, 0, 3, 0],
[4, 5, 0, 6, 0],
[7, 8, 0, 9, 0]])
idx = np.argwhere(np.all(a[..., :] == 0, axis=0))
>>> idx
array([[2],
[4]])
Delete your columns
a2 = np.delete(a, idx, axis=1)
>>> a2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Here is a solution I got
Let say that OriginMat is the matrix with the original data,
And the Result is the matrix I would like to place the result, Then
Result = OriginMat[:,~np.all(OriginMat == 0, axis = 0)]
breaking it down it would be
This check over the column(axis 0) whether or not the values are 0
And negates this value so the columns with zero are taken as false
~np.all(OriginMat == 0, axis = 0)
The resulting matrix would be a vector with False where all elements
are 0 and True when they are not
And the last step just picks the columns that are True(Hence not 0)
I got this solution thanks to the website below:
https://www.science-emergence.com/Articles/How-to-remove-array-rows-that-contain-only-0-in-python/
# Some random array of 1's and 0's
x = np.random.randint(0,2, size=(3, 100))
# Find where all values in the columns are zero
mask = (x == 0).all(0)
# Find the indices of these columns
column_indices = np.where(mask)[0]
# Update x to only include the columns where non-zero values occur.
x = x[:,~mask]
The following works, simplifying #sacuL's anwer:
$ a = np.array([[1, 2, 0, 3, 0],
[4, 5, 0, 6, 0],
[7, 8, 0, 9, 0]])
$ a = a[:, np.any(a, axis=0)]
$ a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
I'd like to save in two variables the values of an array excluding the first and last elements.
For example:
prices = [9, 3, 5, 2, 1]
The elements I need are:
prices_excl_first = [3, 5, 2, 1]
prices_excl_last = [9, 3, 5, 2]
I figured out how to remove an element from an array a few ways, including slicing off the value by passing its index to the slice method like so:
first_price = prices.slice(0)
last_price = prices.slice(-1)
We could then save the modified arrays into variables:
array_except_first_price = prices.delete(first_price) #=> [3, 5, 2, 1]
array_except_last_index = prices.delete(last_price) #=> [3, 5, 2]
There are two problems with this:
array_except_last_index doesn't contain the first element now
I still need access to the full, original array prices later
So essentially, how can I just temporarily modify the elements in the array when necessary in the problem?
Slicing and dropping elements from array permanently affect the array.
Ruby has first and last to copy just the first and last elements.
Ask for the first and last prices.size-1 elements.
prices = [9, 3, 5, 2, 1]
except_first = prices.last(prices.size-1)
except_last = prices.first(prices.size-1)
#Schwern's answer is probably the best you can get. Here's the second best:
prices = [9, 3, 5, 2, 1]
prices[1..-1] # => [3, 5, 2, 1]
prices[0..-2] # => [9, 3, 5, 2]
Or drop/take (which more closely map to the wording of your question).
prices.drop(1) # => [3, 5, 2, 1]
prices.take(prices.size-1) # => [9, 3, 5, 2]
You could use each_cons:
a, b = prices.each_cons(prices.size - 1).to_a
a #=> [9, 3, 5, 2]
b #=> [3, 5, 2, 1]
Splat it.
*a, d = prices
c, *b = prices
a #=> [9, 3, 5, 2]
b #=> [3, 5, 2, 1]
You can use dup to duplicate the array before performing destructive operations.
prices = [9, 3, 5, 2, 1]
except_first = prices.dup
except_first.delete_at 0
except_last = prices.dup
except_last.delete_at -1
This does end up duplicating the array a couple of times. If you're dealing with large arrays, this may be a problem.