Multiply arrays in a list - arrays

I have a list of arrays with the same shape, like this:
my_list = [arr_1, arr_2, arr_3, ...]
arr_1.shape
(1988, 1221)
...
Is there a way to multiply every array in my list and get a final array with the same shape?
I've tried this way but it doesn't work:
for i in my_list:
arr_final = np.multiply(my_list[i])
The final array should be the same of every array in the initial list.
arr_final.shape
(1988, 1221)

You can stack them and take product:
mylist = [np.array([1,2]), np.array([2,3]), np.array([1,4])]
np.stack(mylist).prod(0)
Output:
array([ 2, 24])

Related

How to remove single quote in python list

I would like to remove the single quotes from a list
Creating loop:
results = []
for k in range(1,number_of_observation+2):
results += ['X'+str(k)]
results
Output :
['X1','X2','X3','X4','X5','X6','X7','X8','X9','X10','X11','X12','X13','X14','X15','X16','X17','X18']
Actually each element in the list contains numpy array like this :
X1 = array([ 5.29869582e+03, 4.78138124e+03, 4.66993519e+03, 4.63760715e+03,
4.24625776e+03, 6.82121026e-13, 3.67310328e+03, 3.62922983e+03,
4.67551867e+03, -2.01596513e+03, 5.17998388e+03, 0.00000000e+00,
5.44605355e+03, 4.51697631e+03, 4.62300856e+03, 4.44902873e+03])
X2 = array([ 5.15984732e+03, 3.69964719e+03, 4.88607026e+03, 5.06762424e+03,
4.54623661e+03, 9.09494702e-13, 4.04998815e+03, 3.91555776e+03,
5.07698709e+03, -1.11066480e+03, 4.49209767e+03, 4.54747351e-13,
4.97724688e+03, 4.24955479e+03, 4.72048717e+03, 5.58904656e+03])
And i want to create dataframe from it
Final = pd.DataFrame(data = [results], columns= column_name)
Final
Desire output :
But it gave me output like this :
Use eval function on the result list.Instead of just [results] use list comprehension as in the image.
You can store each element as list of key value pairs, having key as X1 and value as floating point list.
So from list of dictionary you can iterate over keys and values seperately.

Python: Finding a numpy array in a list of numpy arrays

I have a list of 50 numpy arrays called vectors:
[array([0.1, 0.8, 0.03, 1.5], dtype=float32), array([1.2, 0.3, 0.1], dtype=float32), .......]
I also have a smaller list (means) of 10 numpy arrays, all of which are from the bigger list above. I want to loop though each array in means and find its position in vectors.
So when I do this:
for c in means:
print(vectors.index(c))
I get the error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I've gone through various SO questions and I know why I'm getting this error, but I can't find a solution. Any help?
Thanks!
One possible solution is converting to a list.
vectors = np.array([[1, 2, 3], [4, 5, 6], [7,8,9], [10,11,12]], np.int32)
print vectors.tolist().index([1,2,3])
This will return index 0, because [1,2,3] can be found in index 0 of vectors
Th example above is a 2d Numpy array, however you seem to have a list of numpy arrays,
so I would convert it to a list of lists this way:
vectors = [arr.tolist() for arr in vectors]
Do the same for means:
means = [arr.tolist() for arr in means]
Now we are working with two lists of lists:
So your original 'for loop' will work:
for c in means:
print(vectors.index(c))

Trying to append content to numpy array

I have a script that searches Twitter for a certain term and then prints out a number of attributes for the returned results.
I'm trying to Just a blank array is returned. Any ideas why?
public_tweets = api.search("Trump")
tweets_array = np.empty((0,3))
for tweet in public_tweets:
userid = api.get_user(tweet.user.id)
username = userid.screen_name
location = tweet.user.location
tweetText = tweet.text
analysis = TextBlob(tweet.text)
polarity = analysis.sentiment.polarity
np.append(tweets_array, [[username, location, tweetText]], axis=0)
print(tweets_array)
The behavior I am trying to achieve is something like..
array = []
array.append([item1, item2, item3])
array.append([item4,item5, item6])
array is now [item1, item2, item3],[item4, item5, item6].
But in Numpy :)
np.append doesn't modify the array, you need to assign the result back:
tweets_array = np.append(tweets_array, [[username, location, tweetText]], axis=0)
Check help(np.append):
Note that
append does not occur in-place: a new array is allocated and
filled.
In the second example, you are calling list's append method which happens in place; This is different from np.append.
Here's the source code for np.append
In [178]: np.source(np.append)
In file: /usr/local/lib/python3.5/dist-packages/numpy/lib/function_base.py
def append(arr, values, axis=None):
....docs
arr = asanyarray(arr)
if axis is None:
.... special case, ravels
return concatenate((arr, values), axis=axis)
In your case arr is an array, starting with shape (0,3). values is a 3 element list. The is just a call to concatenate. So append call is just:
np.concateante([tweets_array, [[username, location, tweetText]]], axis=0)
But concatenate works with many items
alist = []
for ....:
alist.append([[username, location, tweetText]])
arr = np.concatenate(alist, axis=0)
should work just as well; better because list append is quicker. Or remove a level of nesting and let np.array stack them on a new axis, just as it does with np.array([[1,2,3],[4,5,6],[7,8,9]]):
alist = []
for ....:
alist.append([username, location, tweetText])
arr = np.array(alist) # or np.stack()
np.append has multiple problems. Wrong name. Doesn't act inplace. Hides concatenate. Flattens without much warning. Limits you to 2 inputs at a time. etc.

Neo4j Cypher - Collect two array values return one object

I have multiple nodes with same label. The node properties have two arrays. I want join both array values to an object or a map.
Example:
Array one: [1,2,3,4,5]
Array two: [5,4,3,2,1]
I want to return result as {output:[{1,5},{2,4},{3,3},{4,2},{5,1}]} so a total of 5 objects in that output result.
Is this possible in Cypher?
WITH [1,2,3,4,5] AS array_one, [5,4,3,2,1] AS array_two
UNWIND RANGE(0, SIZE(array_one) - 1) AS i
WITH [array_one[i], array_two[i]] AS output
WITH COLLECT(output) AS output_list
RETURN {outputs: output_list}
That will give you a map, whose sole value is a list of lists. If you want to dynamically build a list of maps instead (your question's syntax is ambiguous), you will have to look into apoc, Cypher doesn't natively support dynamic key assignment.
APOC procedures may be the way to go on this one. In the Collection Functions section, the procedure you want is apoc.coll.zip([list1],[list2]). It is similar to what you want, but not exact. Rather than returning a list of objects, it will return a list of list pairs:
with [1,2,3,4,5] as list1, [5,4,3,2,1] as list2
call apoc.coll.zip(list1,list2) yield value
return {outputs:value}
// returns {outputs:[[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]}
Regarding your desired output, objects in Cypher are like JSON objects and consist of key/value pairs, so it is impossible to return it in the format you're describing. You can test this for yourself.
// this works just fine
return {output: [[1,2]]}
// this causes a syntax error
return {output: [{1,2}]}
Array of maps
You can generate a map containing an array of maps without using APOC:
WITH [1,2,3,4,5] AS a1, [5,4,3,2,1] AS a2
RETURN {output: REDUCE(s = [], i IN RANGE(0, SIZE(a1)-1) | s + {one: a1[i], two: a2[i]})} AS res;
The output is:
{output:[{"one":1,"two":5},{"one":2,"two":4},{"one":3,"two":3},{"one":4,"two":2},{"one":5,"two":1}]}
Array of arrays
To generate a map containing an array of arrays:
WITH [1,2,3,4,5] AS a1, [5,4,3,2,1] AS a2
RETURN {output: REDUCE(s = [], i IN RANGE(0, SIZE(a1)-1) | s + [[a1[i], a2[i]]])} AS res;
The output is:
{output:[[1,5],[2,4],[3,3],[4,2],[5,1]]}

Replace zero array with new values one by one NumPy

I stuck with a simple question in NumPy. I have an array of zero values. Once I generate a new value I would like to add it one by one.
arr=array([0,0,0])
# something like this
l=[1,5,10]
for x in l:
arr.append(x) # from python logic
so I would like to add one by one x into array, so I would get: 1st iteration arr=([1,0,0]); 2d iteration arr=([1,5,0]); 3rd arr=([1,5,10]);
Basically I need to substitute zeros with new values one by one in NumPy (I am learning NumPy!!!!!!).
I checked many of NumPy options like np.append (it adds to existing values new values), but can't find the right.
thank you
There are a few things to pick up with numpy:
you can generate the array full of zeros with
>>> np.zeros(3)
array([ 0., 0., 0.])
You can get/set array elements with indexing as with lists etc:
arr[2] = 7
for i, val in enumerate([1, 5, 10]):
arr[i] = val
Or, if you want to fill with array with something like a list, you can directly use:
>>> np.array([1, 5, 10])
array([ 1, 5, 10])
Also, numpy's signature for appending stuff to an array is a bit different:
arr = np.append(arr, 7)
Having said that, you should just consider diving into Numpy's own userguide.

Resources