I created a list of RGB values for an image (let's say it's 3D_image, composed of 3D_image_slice). I want to extract the unique RGB values from it, but I'm running into problems.
rgb_values_unique = []
for 3D_image_slice in 3D_image:
for y in range(3D_image_slice.shape[0]):
for x in range(3D_image_slice.shape[1]):
if 3D_image_slice[y, x] not in rgb_values_unique:
rgb_values_unique.append(3D_image_slice[y, x])
I was thinking of using np.unique, but that doesn't apply to lists. Is there another way to find unique values within a list?
You have a couple of easy options; one is to create a unique list of strings (I don't love this since it changes the datatype, but it's look something like this):
rgb_values_unique = set()
for 3D_image_slice in 3D_image:
for y in range(3D_image_slice.shape[0]):
for x in range(3D_image_slice.shape[1]):
rgb_values_unique.add("-".join(3D_image_slice[y, x])
print(rgb_values_unique)
# {"r0-g0-b0", "r1-g1-b1", ...}
#which you could convert back into numbers like this:
result = [[int(j) for j in i.split("-")] for i in rgb_values_unique]
What I'd probably do is leverage the uniqueness of a dictionary:
rgb_values_unique = {}
for 3D_image_slice in 3D_image:
for y in range(3D_image_slice.shape[0]):
for x in range(3D_image_slice.shape[1]):
r,g,b = 3D_image_slice[y, x]
rgb_values_unique .setdefault(r, {}).setdefault(g, []).append(b)
print(rgb_values_unique)
# {r0: {g0: [b0, b1, b2]}, {g1: ...
Which you can then turn into a unique listing as follows:
result = [(r,g,b) for r,v in rgb_values_unique.items() for g, b_list in v.items() for b in b_list]
u can use sets to save only unique values:
rgb_values_unique = {}
for 3D_image_slice in 3D_image:
for y in range(3D_image_slice.shape[0]):
for x in range(3D_image_slice.shape[1]):
rgb_values_unique |= 3D_image_slice[y, x]
Related
I have a number of coupled lists x and y with the same number of values as shown below:
[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
where x = [0,2,5,4,3...]
and y = [1,3,4,5,4...]
The complete zipped list looks like zipped = [([0,2,3,5,4],[4,3,4,6,5]),([0,2,6,5,4],[4,2,4,6,5]),([0,2,3,1,4],[4,3,2,6,5]),([0,2,4,5,4],[4,3,3,6,5])]
My goal is to filter y values in lists y1, y2, y3, y4 below a certain threshold k, and to remove the corresponding x values in lists x1, x2, x3, x4.
So far I tried the following
zipped = list(zip([x1,x2,x3,x4],[y1,y2,y3,y4]))
for data in zipped:
filtered = filter(lambda z: z[1]<k, data)
Unfortunately, when I print "filtered", I am not getting the output I want. I want to get the updated zipped list but without the y values below threshold k and without the corresponding x values.
Any ideas? Many thanks in advance.
data = [([0,2,3,5,4],[4,3,4,6,5]),([0,2,6,5,4],[4,2,4,6,5]),([0,2,3,1,4],[4,3,2,6,5]),([0,2,4,5,4],[4,3,3,6,5])]
k = 4
filtered_data = []
for data in l:
filtered = list(filter(lambda pair: pair[1] < k, zip(*data)))
filtered_data.append(filtered)
# rebuild the original input format
print(list(list(zip(*i)) for i in filtered_data))
I have a list of classes and their pre-requisites stored in the paired list:
classes = [
['Calculus', 'Geometry'],
['Trigonometry', 'Algebra'],
['Geometry', 'Trigonometry']
]
I want to map these classes out to specific indexes to build a graph for the courses using something like:
course_index = {
0: 'Calculus'
1: 'Geometry'
2: 'Trigonometry'
3: 'Algebra'
}
To obtain a new integer map mapped_classes = [[0,1],[2,3],[1,2]]
In order to create the course_index I created an empty indexed dict and added the items from classes that weren't present in course_index
course_index = {key: "" for key in range(4)}
index = 0
for x, y in classes:
if x not in course_index.values():
course_index[index] = x
index += 1
if y not in course_index.values():
course_index[index] = y
index += 1
What I can't figure out is how to map classes to their corresponding course_index to create mapped_classes.
Is there some mapping function that can simplify this process? My attempt to solve this is below, I'm not able to mutate the values in the current class list. It is also very inefficient with the nested loops.
for key, val in course_index:
for x, y in classes:
if x = val:
x = key
if y = val:
y = key
Invert the course_index dictionary.
Enumerate over classes.
invert = {v: k for k, v in course_index.items()}
for i, row in enumerate(classes):
classes[i][0] = invert[classes[i][0]]
classes[i][1] = invert[classes[i][1]]
print(classes)
Try using something like this:
mapped_classes = classes.copy()
for key, val in course_index.items():
for e, f in enumerate(classes):
for x in f:
if x == val:
mapped_classes[e][mapped_classes[e].index(x)] = key
print(mapped_classes)
I want to turn my array which consists out of 2 lists into a ranked list.
Currently my code produces :
[['txt1.txt' 'txt2.txt' 'txt3.txt' 'txt4.txt' 'txt5.txt' 'txt6.txt'
'txt7.txt' 'txt8.txt']
['0.13794219565502694' '0.024652340886571225' '0.09806335128916213'
'0.07663118536707426' '0.09118273488073968' '0.06278926571143634'
'0.05114729750522118' '0.02961812647701087']]
I want to make it so that txt1.txt goes with the first value, txt2 goes with the second value etc.
So something like this
[['txt1.txt', '0.13794219565502694'], ['txt2.txt', '0.024652340886571225']... etc ]]
I do not want it to become tuples by using zip.
My current code:
def rankedmatrix():
matrix = numpy.array([names,x])
ranked_matrix = sorted(matrix.tolist(), key=lambda score: score[1], reverse=True)
print(ranked_matrix)
Names being :
names = ['txt1.txt', 'txt2.txt', 'txt3.txt', 'txt4.txt', 'txt5.txt', 'txt6.txt', 'txt7.txt', 'txt8.txt']
x being:
x = [0.1379422 0.01540234 0.09806335 0.07663119 0.09118273 0.06278927
0.0511473 0.02961813]
Any help is appreciated.
You can get the list of lists with zip as well:
x = [['txt1.txt', 'txt2.txt', 'txt3.txt', 'txt4.txt', 'txt5.txt', 'txt6.txt'
'txt7.txt', 'txt8.txt'], ['0.13794219565502694', '0.024652340886571225', '0.09806335128916213',
'0.07663118536707426', '0.09118273488073968', '0.06278926571143634',
'0.05114729750522118', '0.02961812647701087']]
res = [[e1, e2] for e1, e2 in zip(x[0], x[1])]
print(res)
Output:
[['txt1.txt', '0.13794219565502694'], ['txt2.txt', '0.024652340886571225'], ['txt3.txt', '0.09806335128916213'], ['txt4.txt', '0.07663118536707426'], ['txt5.txt', '0.09118273488073968'], ['txt6.txttxt7.txt', '0.06278926571143634'], ['txt8.txt', '0.05114729750522118']]
You can use map to convert the tuple to list.
list(map(list, zip(names, x)))
[['txt1.txt', 0.1379422],
['txt2.txt', 0.01540234],
['txt3.txt', 0.09806335],
['txt4.txt', 0.07663119],
['txt5.txt', 0.09118273],
['txt6.txt', 0.06278927],
['txt7.txt', 0.0511473],
['txt8.txt', 0.02961813]]
I have a dataframe having categorical variables. I want to convert them to the numerical using the following logic:
I have 2 lists one contains the distinct categorical values in the column and the second list contains the values for each category. Now i need to map these values in place of those categorical values.
For Eg:
List_A = ['A','B','C','D','E']
List_B = [3,2,1,1,2]
I need to replace A with 3, B with 2, C and D with 1 and E with 2.
Is there any way to do this in Python.
I can do this by applying multiple for loops but I am looking for some easier way or some direct function if there is any.
Any help is very much appreciated, Thanks in Advance.
Create a mapping dict
List_A = ['A','B','C','D','E',]
List_B = [3,2,1,1,2]
d=dict(zip(List_A, List_B))
new_list=['A','B','C','D','E','A','B']
new_mapped_list=[d[v] for v in new_list if v in d]
new_mapped_list
Or define a function and use map
List_A = ['A','B','C','D','E',]
List_B = [3,2,1,1,2]
d=dict(zip(List_A, List_B))
def mapper(value):
if value in d:
return d[value]
return None
new_list=['A','B','C','D','E','A','B']
map(mapper,new_list)
Suppose df is your data frame and "Category" is the name of the column holding your categories:
df[df.Category == "A"] = 3,2, 1, 1, 2
df[(df.Category == "B") | (df.Category == "E") ] = 2
df[(df.Category == "C") | (df.Category == "D") ] = 1
If you only need to replace values in one list with the values of other and the structure is like the one you say. Two list, same lenght and same position, then you only need this:
list_a = []
list_a = list_b
A more convoluted solution would be like this, with a function that will create a dictionary that you can use on other lists:
# we make a function
def convert_list(ls_a,ls_b):
dic_new = {}
for letter,number in zip(ls_a,ls_b):
dic_new[letter] = number
return dic_new
This will make a dictionary with the combinations you need. You pass the two list, then you can use that dictionary on other list:
List_A = ['A','B','C','D','E']
List_B = [3,2,1,1,2]
dic_new = convert_list(ls_a, ls_b)
other_list = ['a','b','c','d']
for _ in other_list:
print(dic_new[_.upper()])
# prints
3
2
1
1
cheers
You could use a solution from machine learning scikit-learn module.
OneHotEncoder
LabelEncoder
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html
The pandas "hard" way:
https://stackoverflow.com/a/29330853/9799449
I have a dictionary with a tuple of 5 values as a key. For example:
D[i,j,k,g,h] = value.
Now i need to process all elements with a certain partial key pair (i1,g1):
I need now for each pair (i1,g1) all values that have i == i1 and g == g1 in the full key.
What is an pythonic and efficient way to retrieve this, knowing that i need the elements for all pairs and each full key belongs to exactly one partial key?
Is there a more appropriate data structure than dictionaries?
One reference implementation is this:
results = {}
for i in I:
for g in G:
results[i,g] = []
for i,j,k,g,h in D:
if i1 == i and g1 == g:
results[i,g].append(D[i,j,k,g,h])
Assuming you know all the valid values for the different indices you can get all possible keys using itertools.product:
import itertools
I = [3,6,9]
J = range(10)
K = "abcde"
G = ["first","second"]
H = range(10,20)
for tup in itertools.product(I,J,K,G,H):
my_dict[tup] = 0
To restrict the indices generated just put a limit on one / several of the indices that gets generated, for instance all of the keys where i = 6 would be:
itertools.product((6,), J,K,G,H)
A function to let you specify you want all the indices where i==6 and g =="first" would look like this:
def partial_indices(i_vals=I, j_vals=J, k_vals=K, g_vals = G, h_vals = H):
return itertools.product(i_vals, j_vals, k_vals, g_vals, h_vals)
partial_indices(i_vals=(6,), g_vals=("first",))
Or assuming that not all of these are present in the dictionary you can also pass the dictionary as an argument and check for membership before generating the keys:
def items_with_partial_indices(d, i_vals=I, j_vals=J, k_vals=K, g_vals = G, h_vals = H):
for tup in itertools.product(i_vals, j_vals, k_vals, g_vals, h_vals):
try:
yield tup, d[tup]
except KeyError:
pass
for k,v in D.iteritems():
if i in k and p in k:
print v