Hi guys how would I go on converting numpy arrays as such:
[[ 8.82847075 -5.70925653]
[ 1.07032615 -1.77975378]
[-10.41163742 -0.33042086]
[ 0.23799394 5.5978591 ]
[ 7.7386861 -4.16523845]]
To what I desire in Python 3.10. That includes having the keys and values rounded to the nearest integer:
{'9':-6, '1':-2, '-10':0, '0':6, '8':-4}
The following should work
a = np.round(a)
d = dict(zip(a[:, 0].astype(str), a[:, 1]))
Note: Equal keys will merge.
dict(zip(*d.round().astype(int).T))
Out: {9: -6, 1: -2, -10: 0, 0: 6, 8: -4}
The data
d = np.array([[ 8.82847075, -5.70925653],
[ 1.07032615, -1.77975378],
[-10.41163742, -0.33042086],
[ 0.23799394, 5.5978591 ],
[ 7.7386861 , -4.16523845]])
You can convert array to dictionary without any external library.
this method uses list comprehension:
assuming input array is in_array and the output dictionary is out_dict
out_dict = {int(x[0]):int(x[1]) for x in in_array}
Related
I have a CSV file:
8.84,17.22,13.22,3.84
3.99,11.73,19.66,1.27
Def jo(x):
data=np.loadtxt(x,delimiter=',')
Return data
Print(jo('data.csv')
The code returns:
[ [8.84 17.22 13.22 3.84]
[3.99 11.73 19.66 1.27] ]
But I want all these elements in a single array, because I want to find their mean and median.
How to combine these 2 arrays into 1 ?
use numpy.rehshape
# data: data is your array
>>> data.reshape(-1)
In [245]: txt="""8.84,17.22,13.22,3.84
...: 3.99,11.73,19.66,1.27"""
In [246]: data = np.loadtxt(txt.splitlines(), delimiter=',')
In [247]: data
Out[247]:
array([[ 8.84, 17.22, 13.22, 3.84],
[ 3.99, 11.73, 19.66, 1.27]])
In [248]: data.shape
Out[248]: (2, 4)
That is one array, just 2d.
There are various ways of turning that into a 1d array:
In [259]: arr = data.ravel()
In [260]: arr
Out[260]: array([ 8.84, 17.22, 13.22, 3.84, 3.99, 11.73, 19.66, 1.27])
But there's no need to do that. mean (and median) without axis parameter acts on the raveled array. Check the docs:
In [261]: np.mean(data)
Out[261]: 9.971250000000001
In [262]: np.mean(arr)
Out[262]: 9.971250000000001
I have got a 3d array (an array of triangles). I would like to get the triangles (2d arrays) containing a given point (1d array).
I went through in1d, where, argwhere but I am still unsuccessfull....
For instance with :
import numpy as np
import numpy.random as rd
t = rd.random_sample((10,3,3))
v0 = np.array([1,2,3])
t[1,2] = v0
t[5,0] = v0
t[8,1] = v0
I would like to get:
array([[[[[ 0.87312 , 0.33411403, 0.56808291],
[ 0.36769417, 0.66884858, 0.99675896],
[ 1. , 2. , 3. ]],
[[ 0.31995867, 0.58351034, 0.38731405],
[ 1. , 2. , 3. ],
[ 0.04435288, 0.96613852, 0.83228402]],
[[ 1. , 2. , 3. ],
[ 0.28647107, 0.95755263, 0.5378722 ],
[ 0.73731078, 0.8777235 , 0.75866665]]]])
to then get the set of v0 adjacent points
{[ 0.87312 , 0.33411403, 0.56808291],
[ 0.36769417, 0.66884858, 0.99675896],
[ 0.31995867, 0.58351034, 0.38731405],
[ 0.04435288, 0.96613852, 0.83228402],
[ 0.28647107, 0.95755263, 0.5378722 ],
[ 0.73731078, 0.8777235 , 0.75866665]}
without looping, the array being quite big.
For instance
In [28]: np.in1d(v0,t[8]).all()
Out[28]: True
works as a test on a line, but I can't get it over the all array.
Thanks for your help.
What I mean is the vectorized equivalent to:
In[54]:[triangle for triangle in t if v0 in triangle ]
Out[54]:
[array([[ 0.87312 , 0.33411403, 0.56808291],
[ 0.36769417, 0.66884858, 0.99675896],
[ 1. , 2. , 3. ]]),
array([[ 0.31995867, 0.58351034, 0.38731405],
[ 1. , 2. , 3. ],
[ 0.04435288, 0.96613852, 0.83228402]]),
array([[ 1. , 2. , 3. ],
[ 0.28647107, 0.95755263, 0.5378722 ],
[ 0.73731078, 0.8777235 , 0.75866665]])]
You can simply do -
t[(t==v0).all(axis=-1).any(axis=-1)]
We are performing ALL and ANY reduction along the last axis with axis=-1 there. First .all(axis=-1) looks for rows exactly matching the array v0 and then the latter .any(axis=-1) looks for ANY match in each of the 2D blocks. This results in a boolean array of the same length as the length of input array. So, we use the boolean array to filter out valid elements off the input array.
I have an array that represents distances, so I try to get an array with the indexes from the 3 smaller and increasing distances, this way:
array([[ 2.8],
[ 206. ],
[ 84.4],
[ 297.6],
[ 112.7],
[ 235.4],
[ 170.7],
[ 22.2],
[ 264.1],
[ 163.2],
[ 43.7],
[ 131.2]])
Result = [0, 7, 10]
Any idea o suggestion? Thanks in advance.
I found a not very elegant but working approach. If someone has another fast or better version, will be welcome.
from operator import itemgetter
# b distances array
b = [i for i in enumerate(b)]
b = sorted(b, key=itemgetter(1))
b = [i[0] for i in b]
result = b[:3]
result
# [0,7,10]
My list looks like this:
['"date","supermarket","categoryA",10',
'"date","candy store","categoryB",5',
'"date","drugstore","categoryC",6',
'"date","supermarket","categoryA",20',
'"date","candy store","categoryB",2',
'"date","drugstore","categoryC",90']
etc
I'm trying to aggregate the numbers per category -- categoryA B C etc
So far, It's been three days of mostly sideways action. I really should get a book on Python as I've just jumped in and now here I'm asking you guys.
I know how to do this in mysql but that logic is not helping me here.
My code:
for x in range(0 , len(list)):
for y in list[x][2]:
value += list[x][3]
Tearing my hairs out, and I don't have many of those left...
Use dictionary to hold the aggregation and iterate list using in:
aggregate = {}
for x in list:
if (x[2] not in aggregate):
aggregate[x[2]] = 0
aggregate[x[2]] += x[3]
The above assumes your list of lists looks like this:
[
["date","supermarket","categoryA",10],
["date","candy store","categoryB",5]
]
Using python dictionaries, simplifies lot of things. This would work:
category_aggregate_dictionary = {}
for x in range(0 , len(list)):
for y in list[x][2]:
value = list[x][3]
category_aggregate_dictionary[y] = 0 if category_aggregate_dictionary.get(y, None) == None
category_aggregate_dictionary[y] += float(value)
Finally, category_aggregate_dictionary["categoryA"] should give you aggregate number of categoryA.
Hope it Helps : )
Here I've assumed you actually have a list of lists. (See my value for "entries" below.)
from collections import Counter
entries = [
["date", "supermarket", "categoryA", 10],
["date", "candy store", "categoryB", 5],
["date", "drugstore", "categoryC", 6],
["date", "supermarket", "categoryA", 20],
["date", "candy store", "categoryB", 2],
["date", "drugstore", "categoryC", 90]
]
# A Counter is much like a dictionary with a default value of 0
category_counts = Counter()
for entry in entries:
category = entry[2]
count = entry[3]
category_counts[category] += count
# You have the counts already at this point. This loop will
# just print them out in sorted order (by category name).
for category in sorted(category_counts.keys()):
print('{}: {}'.format(category, category_counts[category]))
# Output:
# categoryA: 30
# categoryB: 7
# categoryC: 96
If you are dealing with a list of string like that you can use a ast.literal_eval() function in order to evaluate your strings as tuples then use defaultdict() for aggregating the numbers:
>>> from collections import defaultdict
>>> from ast import literal_eval
>>> d = defaultdict(int)
>>> for item in lst:
... *_, cat, num = literal_eval(item)
... d[cat]+=num
...
>>> d
defaultdict(<class 'int'>, {'9': 0, 'categoryA': 30, 'categoryC': 96, 'categoryB': 7})
I have the following object:
languages:
english: [ 1, 2, 3 ]
german: [ 4, 5, 6 ]
My goal is to get an array of all values of languagesso that the result looks like [ 1, 2, 3, 4, 5, 6 ].
This is what I have tried:
(word for word in value for key, value of languages)
or
(word for word in languages[lang] for lang in Object.keys languages)
Both methods return a two dimensional array the arrays as first dimension and the values as second dimension
Is there a way to get the desired result using a one-liner?
Use the concat() function:
[1, 2, 3].concat [4, 5, 6]
Yes, you can:
[].concat (val for key, val of languages)...
or
Array::concat (val for key, val of languages)...
which are the same.
(val for key, val of languages) here is the array of all languages arrays to concatenate with one another.
... operator is just a shortcut for java-script apply function.
I am not sure why it has to be in one line ... but here you have it in 2 LOC
result = []
result.splice(result.length, 0, languages[key]...) for key of languages