Related
I have a list of numpy arrays and want to modify some numbers of arrays. This is my simplified list:
first_list=[np.array([[1.,2.,0.], [2.,1.,0.], [6.,8.,3.], [8.,9.,7.]]),
np.array([[1.,0.,2.], [0.,0.,2.], [5.,5.,1.], [0.,6.,2.]])]
I have a factor which defines how many splits I have in each arrays:
spl_array=2.
it means each array of the list can be splited into 2 ones. I want to add a fixed value (3.) into last column of each split of each array and also copy the last split and subtract this value (3.) from the third column of this copied split. Finally I want to have it as following:
final_list=[np.array([[1.,2.,3.], [2.,1.,3.], [6.,8.,6.], [8.,9.,10.], \
[6.,8.,0.], [8.,9.,4.]]), # copied and subtracted
np.array([[1.,0.,5.], [0.,0.,5.], [5.,5.,4.], [0.,6.,5.], \
[5.,5.,-2.], [0.,6.,-1.]])] # copied and subtracted
I tried some for loops but I totaly lost. In advance , I do appreciate any help.
final_list=[]
for i in first_list:
each_lay=np.split (i, spl_array)
for j in range (len(each_lay)):
final_list.append([each_lay[j][:,0], each_lay[j][:,1], each_lay[j][:,2]+3])
Is it what you expect:
m = np.asarray(first_list)
m = np.concatenate((m, m[:, 2:]), axis=1)
m[:, :4, 2] += 3
m[:, 4:, 2] -= 3
final_list = m.tolist()
>>> m
array([[[ 1., 2., 3.],
[ 2., 1., 3.],
[ 6., 8., 6.],
[ 8., 9., 10.],
[ 6., 8., 0.],
[ 8., 9., 4.]],
[[ 1., 0., 5.],
[ 0., 0., 5.],
[ 5., 5., 4.],
[ 0., 6., 5.],
[ 5., 5., -2.],
[ 0., 6., -1.]]])
I have a big numpy array and want to take the mean of the second columns of each two rows and save the array as a new one. I want to take the mean of each two row, i.e. the mean of second column of of rows 1 and 2. Then, mean of second column of rows 3 and 4, and so on. Then, I want to merge each two rows as a single one. First and third columns of this paired rows are also the same. This is my simplified array:
input= np.array ([[1., 2., 5.],
[1., 4., 5.],
[4., 10., 3.],
[4., 2., 3.],
[1., 0., 0.],
[1., 1., 0.]])
Then, I want to get:
output= np.array ([[1., 3., 5.],
[4., 6., 3.],
[1., 0.5, 0.]])
I tried the following but it was not successful at all:
output=np.array([])
for i in range (len(input)-1):
g=(input[i,1]+input[i+1,1])/2
output=np.append(g,output)
In advance, I do appreciate any help.
For two rows, I find it easier to do:
(arr[::2] + arr[1::2])/2
A little more robust method for reshape, using the input shape
i= np.array ([[1., 2., 5.],
[1., 4., 5.],
[4., 10., 3.],
[4., 2., 3.],
[1., 0., 0.],
[1., 1., 0.]])
i.reshape(-1, 2, i.shape[-1]).mean(1)
array([[1. , 3. , 5. ],
[4. , 6. , 3. ],
[1. , 0.5, 0. ]])
You could reshape and find the mean, as follows:
import numpy as np
ipt = np.array([[1., 2., 5.],
[1., 4., 5.],
[4., 10., 3.],
[4., 2., 3.],
[1., 0., 0.],
[1., 1., 0.]])
result = np.mean(ipt.reshape((3, 2, 3)), axis=1)
print(result)
Output
[[1. 3. 5. ]
[4. 6. 3. ]
[1. 0.5 0. ]]
As a side note, avoid using input as a variable name as it shadows the built-in input.
Take even rows (ipt[::2]), odd rows (ipt[1::2]), add them and divide by 2:
output = (ipt[::2] + ipt[1::2])/2
This question already has answers here:
How to add items into a numpy array
(7 answers)
Closed 6 years ago.
I have a 500x2 matrix which has been filled using numpy.random.rand.
The output looks like this (but obviously a larger version):
[ -3.28460744e+00 -4.29156493e-02]
[ -1.90772015e-01 -9.17618367e-01]
[ -2.41166994e+00 -3.76661496e+00]
[ -2.43169366e+00 -6.31493375e-01]
[ -1.48902305e+00 -9.78215901e-01]
[ -3.11016192e+00 -1.87178962e+00]
[ -3.72070031e+00 -1.66956850e+00]
I want to append 1 to the end of each row so that each line would look like this:
[ -3.72070031e+00 -1.66956850e+00 1]
Is this possible? I've been trying to use numpy.append() but struggling to work out what should be used.
Any help would be much appreciated!
a = np.ones((4,2)) * 2
>>> a
array([[ 2., 2.],
[ 2., 2.],
[ 2., 2.],
[ 2., 2.]])
Numpy.concatenate documentation:
The arrays must have the same shape, except in the dimension corresponding to axis ... along which the arrays will be joined.
>>> a.shape
(4, 2)
You want to concatenate along the second axis so make an array of ones that has a shape of (4,1) - use values from a.shape to do this.
b = np.ones((a.shape[0], 1))
>>> b.shape
(4, 1)
>>> b
array([[ 1.],
[ 1.],
[ 1.],
[ 1.]])
Now you can concatenate
z = np.concatenate((a,b), axis = 1)
>>> z
array([[ 2., 2., 1.],
[ 2., 2., 1.],
[ 2., 2., 1.],
[ 2., 2., 1.]])
Or use hstack
>>> np.hstack((a,b))
array([[ 2., 2., 1.],
[ 2., 2., 1.],
[ 2., 2., 1.],
[ 2., 2., 1.]])
Consider the following numpy.arrays:
a = np.array([1., 2., 3.])
b = np.array([4., 5.])
c = np.array([6., 7.])
I need to combine these so I end up with the following:
[(1., 4., 6.), (1., 5., 7.), (2., 4., 6.), (2., 5., 7.), (3., 4., 6.), (3., 5., 7.)]
Note that in this case, the array a happens to be the largest array. This is not guaranteed however. Nor is the length guaranteed. In other words, any array could be the longest and each array is of arbitrary length.
I tried using itertools.izip_longest but I can only use fillvalue for the tuple with 3. which will not work. I tried itertools.product also but my result is not a true cartesian product.
You can transpose b and c and then create a product of the a with the transposed array using itertools.product:
>>> from itertools import product
>>> [np.insert(j,0,i) for i,j in product(a,np.array((b,c)).T)]
[array([ 1., 4., 6.]), array([ 1., 5., 7.]), array([ 2., 4., 6.]), array([ 2., 5., 7.]), array([ 3., 4., 6.]), array([ 3., 5., 7.])]
>>>
Let's say you have:
a = np.array([4., 5.])
b = np.array([1., 2., 3.])
c = np.array([6., 7.])
d = np.array([5., 1])
e = np.array([3., 2.])
Now, if you know before-hand which one is the longest array, which is b in this case, you can use an approach based upon np.meshgrid -
# Concatenate elements from identical positions from the equal arrays
others = np.vstack((a,c,d,e)).T # If you have more arrays, edit this line
# Get grided version of the longest array and
# grided-indices for indexing into others array
X,Y = np.meshgrid(np.arange(others.shape[0]),b)
# Concatenate grided longest array and grided indexed others for final output
out = np.hstack((Y.ravel()[:,None],others[X.ravel()]))
Sample run -
In [47]: b
Out[47]: array([ 1., 2., 3.])
In [48]: a
Out[48]: array([ 4., 5.])
In [49]: c
Out[49]: array([ 6., 7.])
In [50]: d
Out[50]: array([ 5., 1.])
In [51]: e
Out[51]: array([ 3., 2.])
In [52]: out
Out[52]:
array([[ 1., 4., 6., 5., 3.],
[ 1., 5., 7., 1., 2.],
[ 2., 4., 6., 5., 3.],
[ 2., 5., 7., 1., 2.],
[ 3., 4., 6., 5., 3.],
[ 3., 5., 7., 1., 2.]])
If the length differences are not extreme (check inputs first) I'd be tempted to pad out the shorter lists to the length of the longest with None and generate all the permutations (27 of them for 3 lists of 3 elements). Then
results = []
for candidate in possibles:
if not (None in candidate): results.append(candidate)
Reasons not to do this: if the cube of the length of the longest list is significant in terms of memory usage (space to store N cubed possibles) or CPU usage.
In my Python application I have a 3D matrix (array) such this:
array([[[ 1., 2., 3.]], [[ 4., 5., 6.]], [[ 7., 8., 9.]]])
and I would like to add, in a particular "line", for example, in the middle, zero arrays. At the end I would like to end with the following matrix:
array([[[ 1., 2., 3.]],
[[ 4., 5., 6.]],
[[ 0., 0., 0.]],
[[ 0., 0., 0.]],
[[ 7., 8., 9.]]])
Anybody knows how to solve this issue? I tried to use "numpy.concatenate", but it allow me only to add more "lines".
Thanks in advance!
Possible duplicate of
Inserting a row at a specific location in a 2d array in numpy?
For example:
a = array([[[ 1., 2., 3.]], [[ 4., 5., 6.]], [[ 7., 8., 9.]]])
output = np.insert(a, 2, np.array([0,0,0]), 0)
output:
array([[[ 1., 2., 3.]],
[[ 4., 5., 6.]],
[[ 0., 0., 0.]],
[[ 7., 8., 9.]]])
Why this works on 3D array?
See doc here.
It says:
numpy.insert(arr, obj, values, axis=None)
...
Parameters :
values : array_like
Values to insert into arr.
If the type of values is different from that of arr,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
values is converted to the type of arr. values should be shaped so that
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arr[...,obj,...] = values is legal.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
So it's very wise function!!
Is this what you want?
result = np.r_[ a[:2], np.zeros(1,2,3), a[2][None] ]
I'd do it this way:
>>> a = np.array([[[ 1., 2., 3.]], [[ 4., 5., 6.]], [[ 7., 8., 9.]]])
>>> np.concatenate((a[:2], np.tile(np.zeros_like(a[0]), (2,1,1)), a[2:]))
array([[[ 1., 2., 3.]],
[[ 4., 5., 6.]],
[[ 0., 0., 0.]],
[[ 0., 0., 0.]],
[[ 7., 8., 9.]]])
The 2 in (2,1,1) given to tile() is how many zero "rows" to insert. The 2 in the slice indexes is of course where to insert.
If you're going to insert a large amount of zeros, it may be more efficient to just create a big array of zeros first and then copy in the parts you need from the original array.