I have numpy matrices collected in the list. I need to built an array which contains particular entry from each matrix, for example second entry from each matrix. I would like to avoid loop.
The data is already in this shape, I don't want to change the structure or change matrices into something else.
Example code - data structure:
L = []
m1 = np.mat([ 1, 2, 3]).T
m2 = np.mat([ 4, 5, 6]).T
m3 = np.mat([ 7, 8, 9]).T
m4 = np.mat([10,11,12]).T
m5 = np.mat([13,14,15]).T
L.append(m1)
L.append(m2)
L.append(m3)
L.append(m4)
L.append(m5)
The only way I managed to do it is through the loop:
S = []
for k in range(len(L)):
S.append(L[k][1,0])
print 'S = %s' % S
the output I need: S = [2, 5, 8, 11, 14] I thought something like: S1 = np.array(L[:][1,0]) should work but whatever I try I have the error like: TypeError: list indices must be integers, not tuple. What is the efficient way (numpy style) of accessing it?
Using list comprehension:
>>> x = [i[1] for i in L]
>>> x
[2, 5, 8, 11, 14]
>>>
You could also do
>>> M = np.column_stack([m1,m2,m3,m4,m5])
and then access the rows via
>>> M[1]
matrix([[ 2, 5, 8, 11, 14]])
If you've got larger vectors, and want to access multiple rows, this might be faster in the long run.
As DSM says, either you should have a 2D matrix and use numpy slicing, otherwise some form of list-comp as shown by Pawelmhm... A faster form will be:
from operator import itemgetter
els = map (itemgetter(1), (m1, m2, m3, m4, m5))
Related
When ı print out the following code Q is prints like it suppose to be (3 5 7 9) sum of the numbers with the next one. but in the variable explorer its a single integer ı want to get the result Q as an array like
Q = [3, 5, 7, 9]
import numpy as np
A = [1, 2, 3, 4, 5]
for i in range(0,4):
Q = np.array(A[i]+A[i+1])
print(Q)
for i in range(0,4):
Q = []
Q.append(Q[i] + A[i]+A[i+1])
print(Q)
This also doesnt work
Currently you're just re-declaring Q each time and it's never added to some collection of values
Instead, start with an empty list (or perhaps a numpy array in your case) and outside of your loop and append the values to it at each loop cycle
Q is a numpy array, but it's not what you're expecting!
It has no dimensions and only references a single value
>>> type(Q)
<class 'numpy.ndarray'>
>>> print(repr(Q))
array(9)
>>> import numpy as np
>>> A = [1, 2, 3, 4, 5]
>>> Q = np.array([], dtype=np.uint8)
>>> for i in range(4):
... Q = np.append(Q, A[i]+A[i+1]) # reassign each time for np
...
>>> print(Q)
[3 5 7 9]
Note that numpy arrays should be reassigned via np.append, while a normal python list has a .append() method (which does not return the list, but directly appends to it)
>>> l = ['a', 'b', 'c'] # start with a list of values
>>> l.append('d') # use the append method
>>> l # display resulting list
['a', 'b', 'c', 'd']
If you're not forced to use a numpy array to begin with, this can be done with a list comprehension
The resulting list can also be made into a numpy array afterwards
>>> [(x + x + 1) for x in range(1, 5)]
[3, 5, 7, 9]
All together with simplified math
>>> np.array([x*2+3 for x in range(4)])
array([3, 5, 7, 9])
If you want to use Numpy, then use Numpy. Start with a Numpy array (one-dimensional, containing the values), which looks like this:
A = np.array([1, 2, 3, 4, 5])
(Yes, you initialize it from the list).
Or you can create that kind of patterned data using Numpy's built-in tool:
A = np.arange(1, 6) # it works similarly to the built-in `range` type,
# but it does create an actual array.
Now we can get the values to use on the left-hand and right-hand sides of the addition:
# You can slice one-dimensional Numpy arrays just like you would lists.
# With more dimensions, you can slice in each dimension.
X = A[:-1]
Y = A[1:]
And add the values together element-wise:
Q = X + Y # yes, really that simple!
And that last line is the reason you would use Numpy to solve a problem like this. Otherwise, just use a list comprehension:
A = list(range(1, 6)) # same as [1, 2, 3, 4, 5]
# Same slicing, but now we have to do more work for the addition,
# by explaining the process of pairing up the elements.
Q = [x + y for x, y in zip(A[:-1], A[1:])]
I have an array of lists and I will like to join all the lists with repeated values.
For example if I have the next array a:
import numpy as np
a=np.array([[1,2,3],[4,3,2],[10,8,9],[72,3,6]])
The expected result should be:
result=array([[ 1, 2, 3, 4, 6, 72],
[ 10, 8, 9]])
The thing you are talking about is a data structure known as "Disjoint Set".
While this is not in-built in python, a simple mock-up of this can be easily made for your case.
def merge_sets (arrays): # arrays could be your np array
sets = list()
for a in arrays:
sets.append(a)
a, b = 0, 1
while a < len(sets):
if b < len(sets) and sets[a].intersect(sets[b]):
c = sets[a].union(sets[b])
sets.remove(a)
sets.remove(b)
sets.append(c)
return sets
P.S. This is just a rough implementation to demonstrate how you can do it. It has not been checked for fallacies.
Apologies if this has already been asked, but I searched quite a bit and couldn't find quite the right solution. I'm new to python, but I'll try to be as clear as possible. In short, I have a list of arrays in the following format resulting from a joining a multiprocessing pool:
array = [[[1,2,3], 5, 47, 2515],..... [[4,5,6], 3, 35, 2096]]]
and I want to get all values from the first array element to form a new array in the following form:
print(new_array)
[1,2,3,4,5,6]
In my code, I was trying to get the first value through this function:
new_array = array[0][0]
but this only returns the first value as such:
print(new_array)
[1,2,3]
I also tried np.take after converting the array into a np array:
array = np.array(array)
new_array = np.take(results,0)
print(new_array)
[1,2,3]
I have tried a number of np functions (concatenate, take, etc.) to try and iterate this over the list, but get back the following error (presumably because the size of the array changes):
ValueError: autodetected range of [[], [1445.0, 1445.0, -248.0, 638.0, -108.0, 649.0]] is not finite
Thanks for any help!
You can achieve it without numpy using reduce:
from functools import reduce
l = [[[1,2,3], 5, 47, 2515], [[4,5,6], 3, 35, 2096]]
res = reduce(lambda a, b: [*a, *b], [x[0] for x in l])
Output
[1, 2, 3, 4, 5, 6]
Maybe it is worth mentioning that [*a, *b] is a way to concatenate lists in python, for example:
[*[1, 2, 3], *[4, 5, 6]] # [1, 2, 3, 4, 5, 6]
You could also use itertools' chain() function to flatten an extraction of the first subArray in each element of the list:
from itertools import chain
result = list(chain(*[sub[0] for sub in array]))
I need to perform a calculation on a list using both the values of that list, and the index of those values, eg:
a = [1,2,3,4,5]
b= a[1]*1
but for all values of a, to get:
b = [1,4,9,16,25]
Is this possible to do without a for loop? I could put it in one, but it would change the structure of my code for the worse. This is meant to be used by a wide variety of users with varying skill levels, so I'm willing to sacrifice speed for ease of readability.
You can use list comprehension and enumerate() function, that provides the index as well as value of the element, simultaneously. Example -
b = [elem * (i+1) for i, elem in enumerate(a)]
Demo -
>>> a = [1,2,3,4,5]
>>> b = [elem * (i+1) for i, elem in enumerate(a)]
>>> b
[1, 4, 9, 16, 25]
If its fine to use numpy library , most probably a faster solution would be to use numpy's vectorised multiplication . Example -
In [1]: import numpy as np
In [2]: a = [1,2,3,4,5]
In [3]: a_arr = np.array(a)
In [4]: b = a_arr * np.arange(1,a_arr.shape[0]+1)
In [5]: b
Out[5]: array([ 1, 4, 9, 16, 25])
Though numpy method may be a bit more complex to understand that the list comprehension.
I recommend you to use map built-in function and let your code be simpler.
a = [1,2,3,4,5]
b = map(lambda x: x*x, a)
print b
# [1, 4, 9, 16, 25]
If you want to dynamically enter your list, you would use range built-in function (It's available for python2):
b = map(lambda x: x*x, range(1,6))
print b
# [1, 4, 9, 16, 25]
I'm new to Python, but I use it to process data. I've got a large amount of data stored as float arrays:
data1
data2
data3
I want to run similar processing for each data file. I was thinking of using a for loop:
for i in range(1,4):
I would like to then multiply the three data files by two, but I'm not sure how to continue afterwards. I imagine it would look like his:
for i in range(1,4):
data_i=data_i*2
Thank you.
You could make a two-dimensional array, meaning you put your float arrays inside another array.
Your situation right now would look like this:
data1 = [12, 2, 5]
data2 = [2, 4, 8]
data3 = [3, 0, 1]
By putting your arrays inside another array by doing this:
datax = [data1, data2, data3]
Your new situation would look like this:
datax = [[12, 2, 5], [2, 4, 8], [3, 0, 1]]
Now we can loop over the new datax array and perform an action on it's elements, data1, data2 and data3.
Something along the lines of:
datax = [[12, 2, 5], [2, 4, 8], [3, 0, 1]]
for sub_array in datax:
perform_action(sub_array)
You can simply store the data in e.g. list
data_list = [data1, data2, data3]
for i, data in enumerate(data_list):
some_fancy_stuff
data_list[i] = data * 2
Some explanation - enumerate will literally enumerate the items of the list with index i and also assigns data_list[i] to variable data. Than you can do whatever you want with data and its index i.
You could also use Python comprehension instead of loops, here is an example to illustrate it:
>>> a1 = [1,2,3]
>>> a2 = [4,5,6]
>>> a3 = [7,8,9]
>>> A = [a1, a2, a3]
>>>
>>> print [[x*2 for x in a] for a in A]
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]
>>>
Let me explain it also.
The following construction is called comprehension:
a = [x*2 for x in X]
It produces an array (as you could see the brackets [ ]) of processed values (in the example value multiplication by two) from array X. It's as if we wrote:
a = []
for x in X:
a.append(x*2)
but in more concise way.
In your situation we used two comprehension one in one:
[x*2 for x in a]
and:
[ [....] for a in A]
So it's the same as if we did:
result = []
for a in A:
for x in a:
result.append(x*2)
but in more concise way again.
If you asked about variable names modification, so it is not possible in the Python without altering the language processor. But I guess you don't need it in that task at all.