Within a for loop, I am extracting a Numpy array of size 10x256. I would like to concatenate all those arrays (the iteration in total are 20) and create an array of size of 200x256. I managed to do that by using a for loop within the for loop:
my_list= []
for i in range(0,20):
my_arr = process() # 10x256
for item in my_arr:
my_list.append(item)
How can I do the same thing without using the second for loop?
With single numpy.concatenate routine:
new_arr = np.concatenate([process() for i in range(20)])
Related
I have a NumPy array with size (4061, 3). I need to iterate through it using a for loop so that I can segment it into 4061 (1,3) arrays which will be assigned to a list as the for loop executes.
A possible answer could be:
my_list= []
count= 0
my_array= np.eye(4061, 3) # here define your (4061,3) array
for i in my_array:
count=count+1
if (count%3)==0 :
my_list.append(i)
I have an array of numbers, and a list of functions.
I want to run every function on every number to get back a matrix.
Is there a way to do it without slow python for looping / mapping?
import numpy
arr = numpy.array([1,2,3,4,5])
fns = [numpy.sin, numpy.cos, numpy.exp]
results = numpy.zeros(shape=( len(fns), len(arr) ))
for i, fn in enumerate(fns):
for j, val in enumerate(arr):
results[i][j] = fn(val)
print ('results', results)
I can get rid of one loop with function broadcasting:
results2 = numpy.zeros(shape=( len(fns), len(arr) ))
for i, fn in enumerate(fns):
results2[i] = fn(arr)
print ('results2', results2)
Is there some clever pythonic numpy-ish way to get rid of my second loop?
Perhaps some built in outer-product-ish interaction which is difficult to google?
results3 = numpy.function_outer( fns, arr)
You can use a list comprehension and transform it back to a numpy array like this:
results3 = numpy.array([ fn(arr) for fn in fns])
My array consists of 400 values, I would like to assign the first 4 values to one matrix and next 4values to other matrix until the end of the array. So I will result in 100 Matrix consisting of 4 values.How do I do it effectively in Python?
Let's call A your original array
With python lists :
l = [[A[4*i+j]for j in range(4)] for i in range(100)]
You can do
a = np.arange(400).reshape((100, 4))
or if oyu have a list
l=[1,2,3,4,5,6]
a = np.array(l).reshape((100, 4))
as yatu suggested.
a=np.array(range(400))
a.reshape(100,4)
This will take less time to execute.
I want to know how to code an efficient index over a numpy array. For the moment, I go over the array elements using repeated additions. For example, I have to make some loops over an array A like this:
import numpy as np
A = np.arange(0,100)
M = 10
for i in range(A.shape[0]-M):
B = []
for j in range(M):
value = A[i+j]
B.append(value)
Is there a way for me to get the values without repeatedly doing the i+j addition?
I'm having some trouble understanding how nested single line for loops work. Here's an example:
I have this code:
NewArray = np.array([ get_position(i, size-1, t) for i in range(0, size)])
and I'm trying to rewrite this to:
for i in range(0,size):
NewArray = np.array([ get_position(i, size-1, t)])
But I'm getting different outputs, so I'm guessing there's a logic error here.
Could you point out the issue?
Thank you
It's because the first one creates a numpy array containing all your values (you create all values because you're using a list comprehension) and the second one creates a new array containing the last value each iteration (and it discards the array created in the last iteration because you reuse the same name).
You could rewrite the second one as:
lst = []
for i in range(0,size):
lst.append(get_position(i, size-1, t))
NewArray = np.array(lst)
that should give the same result as your first operation.
In the first you create an array of length size.
In the second you repeatedly (size times) create an array of length 1.