Reduce and append in Numpy - python

Is there a way to use reduce with numpy's append? I want to append 3 arrays together like this:
a = np.array([1,2,3])
b = np.array([11,12,13])
c = np.array([21,22,23])
#below is my real code - the above is just for this example)
np.append.reduce((a,b,c))
but it looks like reduce isn't implemented with append. Thanks for any suggestions.
The output should be:
array([ 1, 2, 3, 11, 12, 13, 21, 22, 23])

np.r_[] will do this cleanly...
a = np.array([1,2,3])
b = np.array([11,12,13])
c = np.array([21,22,23])
#below is my real code - the above is just for this example)
np.r_[a,b,c]

Related

how to create a function in python that gives elements of an array?

I have an array. Let's say a=array([[10, 2, 13, 55]])
I want to create a function that gives me the 1st element for t=0, the second element for t=1...
I have tried the following:
def a(t):
return a[t]
You can do it like this :
a=array([[10, 2, 13, 55]])
def get_value(t):
return a[t]
get_value(0) #results [10, 2, 13, 55]
Since your example data is 2D , if we want to access each of them we must pass 2 numbers as index.
a=array([[10, 2, 13, 55]])
def get_value(t1,t2):
return a[t1][t2]
get_value(0, 1) #results 2
This function, only works if you have arrays of the shape [[...]], otherwise, you have to change the level parameter.
from numpy import array
a=array([[10, 2, 13, 55]])
def matrix_reader(a,t,level=0):
return a[level][t]
matrix_reader(a,1)
Your example is 2D array so you need 2 parameter to return the correct number that you want.
Example: With your array a=array([[10, 2, 13, 55]]), a[0,0] return 10, a[0,1] return 2.
I recommend you create 1D array, put your array into function and named your function different from your array
from numpy import array
temp=array([10, 2, 13, 55])
def a(arr,t):
return arr[t]
print(a(temp, 2))
The example return 13

How in a numpy array we can select just rows that are multiples of for example 3?

I search for a numpy statement for doing this:
If
a = [11,12,13,14,15,16,17,18,19,20,21]
and b=3 then:
c = [rows that are multiples of b]=[11,14,17,20]
The following should work:
a[::b]
Result:
array([11, 14, 17, 20])

Python make second array from first

I'm working with python and I read out a column from a CSV file. I save the values in an array by grouping them. This array looks something like this:
[1, 5, 10, 15, 7, 3]
I want to create a second array where I take the number of the array and make the sum with the previous values. So in this case I would like the have the following output:
[1, 6, 16, 31, 38, 41]
My code is as follows:
import csv
import itertools
with open("c:/test", 'rb') as f:
reader = csv.reader(f, delimiter=';')
data1 = []
data2 = []
for column in reader:
data1.append(column[2])
results = data1
results = [int(i) for i in results]
results = [len(list(v)) for _, v in itertools.groupby(results)]
print results
data2.append(results[0])
data2.append(results[0]+results[1])
data2.append(results[0]+results[1]+results[2])
print data2
So I can make the array by doing it manually, but this costs a lot of time and is probably not the best way to do it. So what is the best way to do something like this?
You are looking for the cumulative sum of a list. The easiest way is to let numpy do it.
>>> import numpy as np
>>> np.cumsum([1, 5, 10, 15, 7, 3])
array([ 1, 6, 16, 31, 38, 41])
a = [1, 5, 10, 15, 7, 3]
b = [a[0]]
for i in range(1, len(a)):
b.append(b[-1]+ a[i])
a is your column from .csv. b is a list with already one value in it, which is first item of a. Then we loop through a starting from it's second item and we add the consequent values from it to last item of b and append it to b.
Using your code objects, what you look for would be something like:
from __future__ import print_function
import csv
import itertools
"""
with open("c:/test", 'rb') as f:
reader = csv.reader(f, delimiter=';')
for column in reader:
data1.append(column[2])
"""
data1 = [1, 5, 10, 15, 7, 3]
results = [data1[0]]
for i in range(1, len(data1)):
results.append(results[i-1] + data1[i])
print(data1, results)

Method to get the max distance (step) between values in python?

Given an list of integers does exists a default method find the max distance between values?
So if I have this array
[1, 3, 5, 9, 15, 30]
The max step between the values is 15. Does the list object has a method for do that?
No, list objects have no standard "adjacent differences" method or the like. However, using the pairwise function mentioned in the itertools recipes:
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return izip(a, b)
...you can (concisely and efficiently) define
>>> max(b-a for (a,b) in pairwise([1, 3, 5, 9, 15, 30]))
15
No, but it's trivial to code:
last = data[0]
dist = 0
for i in data[1:]:
dist = max(dist, i-last)
last = i
return dist
You can do:
>>> s = [1, 3, 5, 9, 15, 30]
>>> max(x[0] - x[1] for x in zip(s[1:], s))
15
This uses max and zip. It computes the difference between all consecutive elements and returns the max of those.
l=[1, 3, 5, 9, 15, 30]
max([j-i for i, j in zip(l[:-1], l[1:])])
That is using pure python and gives you the desired output "15".
If you like to work with "numpy" you could do:
import numpy as np
max(np.diff(l))
The list object does not. However, it is pretty quick to write a function that does that:
def max_step(my_list):
max_step = 0
for ind in xrange(len(my_list)-1):
step = my_list[ind+1] - my_list[ind]
if step > max_step:
max_step = step
return max_step
>>> max_step([1, 3, 5, 9, 15, 30])
15
Or if you prefer even shorter:
max_step = lambda l: max([l[i+1] - l[i] for i in xrange(len(l)-1)])
>>> max_step([1, 3, 5, 9, 15, 30])
15
It is possible to use the reduce() function, but it is not that elegant as you need some way to keep track of the previous value:
def step(maxStep, cur):
if isinstance(maxStep, int):
maxStep = (abs(maxStep-cur), cur)
return (max(maxStep[0], abs(maxStep[1]-cur)), cur)
l = [1, 3, 5, 9, 15, 30]
print reduce(step, l)[0]
The solution works by returing the previous value and the accumulated max calculation as a tuple for each iteration.
Also what is the expected outcome for [10,20,30,5]? Is it 10 or 25? If 25 then you need to add abs() to your calculation.

Running maximum of numpy array values

I need a fast way to keep a running maximum of a numpy array. For example, if my array was:
x = numpy.array([11,12,13,20,19,18,17,18,23,21])
I'd want:
numpy.array([11,12,13,20,20,20,20,20,23,23])
Obviously I could do this with a little loop:
def running_max(x):
result = [x[0]]
for val in x:
if val > result[-1]:
result.append(val)
else:
result.append(result[-1])
return result
But my arrays have hundreds of thousands of entries and I need to call this many times. It seems like there's got to be a numpy trick to remove the loop, but I can't seem to find anything that will work. The alternative will be to write this as a C extension, but it seems like I'd be reinventing the wheel.
numpy.maximum.accumulate works for me.
>>> import numpy
>>> numpy.maximum.accumulate(numpy.array([11,12,13,20,19,18,17,18,23,21]))
array([11, 12, 13, 20, 20, 20, 20, 20, 23, 23])
As suggested, there is scipy.maximum.accumulate:
In [9]: x
Out[9]: [1, 3, 2, 5, 4]
In [10]: scipy.maximum.accumulate(x)
Out[10]: array([1, 3, 3, 5, 5])

Categories

Resources