Combine two or multiple classes with MultiLabelBinarizer - deep-learning

Is it possible to combine two classes to get a single entry for the labels with MultiLabelBinarizer?
Normally one gets
labels = ['a', 'b', 'c', 'd', 'e']
x = ['a', 'c', 'd']
y = ['a', 'b', 'e']
z = ['c', 'd', 'e']
mlb = MultiLabelBinarizer(classes=labels)
a = mlb.fit_transform([x, y, z])
print(a)
[[1 0 1 1 0]
[1 1 0 0 1]
[0 0 1 1 1]]
But what I want is to combine two classes e.g if 'a' or 'b' is present, the label for x should look like
[[1 1 1]]
So for 'a'+'b', 'c', 'd'+'e' the above should look like
[[1 1 1]
[1 0 1]
[0 1 1]]
I just want to know if it is possible. Implementing some functions to get the same result is also possible, but I have a lot of classes and I want to speed things up.

Related

Problems with sub2ind function in Octave

I am new to Octave, so I was reading documentation and I found sub2ind function. I started to test it, but sometimes it works weird or I just don't understand how it must work.
So this is how subscripts must be converted to linear indices. (Example from documentation)
[(1,1), (1,2), (1,3)] [1, 4, 7]
[(2,1), (2,2), (2,3)] ==> [2, 5, 8]
[(3,1), (3,2), (3,3)] [3, 6, 9]
And this is another example from documentation
s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
So if we look at the first example the (2, 2) == 5, but second example says [2, 2] == 2.
The (1, 3) has different results too.
Practically It works as the second example shows.
If I try to use this function with only 1 pair it return the same pair
sub2ind([3, 3], [2, 2])
# ans = [2, 2]
In this test I can't see any relation between input and output
sub2ind([3, 3], [2, 2], [3, 3])
# ans = [8, 8]
Function works this strange(maybe not) way only when it gets 1 pair or when one of pairs is pair kind [x, x](two same values).
But otherwise it works fine, so this test returns that it should:
sub2ind([3, 3], [2, 1], [1, 3])
# ans = [2, 7]
Also it works fine when this variant is used sub2ind (dims, i, j).
How does the function works?
You misunderstand the input format.
Change
s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
to this:
row = [2, 2]; % x1 and x2
col = [1, 3]; % y1 and y2
ind = sub2ind ([3, 3], row, col)
⇒ ind = 2 8
You have two inputs that you convert to linear indices:
[x1, y1] = [2, 1] = 2 and [x2 y2] = [2, 3] = 8.
This:
sub2ind([3, 3], [2, 2])
# ans = [2, 2]
appears to be equivalent to:
sub2ind([3, 3], [2, 2], [1, 1])
even though it's not in the documentation.

Python pandas, set background colors for dataframe cells by another dataframe

I have a dataframe:
df = pd.DataFrame({'grp':['A', 'A', 'B', 'B', 'B'], 'pos' : [1, 1, 1, 1, 2]})
>>> df
grp pos
0 A 1
1 A 1
2 B 1
3 B 1
4 B 2
I would like to set background color for the same values in each column.
For example, in column grp, first two values AA should have background color 1, and BBB background color 2. In pos column, 1111 should have color x, 2 color y.
I tried to construct a dataframe with colors:
df_colors = pd.DataFrame({'grp':['#1d77ab', '#1d77ab', '#1a7899', '#1a7899', '#1a7899'], 'pos' : ['#1d77ab', '#1d77ab', '#1d77ab', '#1d77ab', '#167a7e']})
>>> df_colors
grp pos
0 #1d77ab #1d77ab
1 #1d77ab #1d77ab
2 #1a7899 #1d77ab
3 #1a7899 #1d77ab
4 #1a7899 #167a7e
If I have colors in dataframe how to apply style to dataframe df with colors in dataframe df_colors?
Regards.
Use Styler.apply with add background-color, only necessary same index and columns names of both DataFrames:
(df.style.apply(lambda x: 'background-color: ' + df_colors, axis=None)
.to_excel('styled.xlsx', engine='openpyxl', index=False))

Compute the mean of each element, the previous and the next in a row vector, for all elements starting from the second one - vectorized implementation

I'd like to compute the mean of each element, the previous and the next in a row vector, for all elements starting from the second one, and I'd like to do it with a vectorized implementation.
Suppose I have this row vector:
a = [4, 7, 1, 3, 2];
what I want to obtain is:
b = [4, 3.66, 2, 1.66];
which is, in turn, the mean of the subsequent triplets:
[4 7 1], [7 1 3], [1 3 2], [3 2 0] (the zero is conventional).
By the way, approximation to two figures is irrelevant here, it's just for the sake of the example.
I have come up with this code:
a = [4 7 1 3 2];
function shifted = generateShiftedValues(rowVec)
shifted = rowVec;
for i=2:3
shifted(i, :) = [rowVec(1, i:end), zeros(1, i-1)];
endfor
endfunction
b = mean(generateShiftedValues(a)(:, 1:end-1), 1)
but what I'd like to have is a fully vectorized implementation.
Is that possible? Any ideas?
Thank you very much indeed.
Convolution is the key to success.
I would go for this:
a = [4 7 1 3 2]
n = 3;
b = conv(a, ones(n, 1)) / n;
b = b(3:end-1)
a =
4 7 1 3 2
b =
4.00000 3.66667 2.00000 1.66667
One could easily build a generalized solution for any number of elements to be averaged and arbitrary "starting point". If you need such, maybe provide a general description in your question. If that "special case" is sufficient, that's all.
Hope that helps!

How many elements from one list are present in the other list

I can't seem to be able to create a function that takes two lists as arguments and returns how many elements there are common in both lists.
e.g. f [1, 2, 4, 2] [2, 3, 4, 4] returning 2 (repetitions are ignored).
Any suggestions? I tried this
*Main> a = [1, 2, 3]
*Main> b = [2, 3, 4]
*Main> [x | x <- a, x <- b]
[2,3,4,2,3,4,2,3,4]
Then I was planning to use the length function to know how many item there are in common.
You don't want to extract an x from both lists; extract from one list, and check if it is present in the other.
> a = [1,2,3]
> b = [4,3,2]
> [x | x <- a, x `elem` b]
[2,3]
> [x | x <- b, x `elem` a]
[3,2]
Note that the order in which items appear in the result depends on the order in which they appear in the list you pull from.

how to define dynamic nested loop python function

a = [1]
b = [2,3]
c = [4,5,6]
d = [a,b,c]
for x0 in d[0]:
for x1 in d[1]:
for x2 in d[2]:
print(x0,x1,x2)
Result:
1 2 4
1 2 5
1 2 6
1 3 4
1 3 5
1 3 6
Perfect, now my question is how to define this to function, considering ofcourse there could be more lists with values. The idea is to get function, which would dynamicaly produce same result.
Is there a way to explain to python: "do 8 nested loops for example"?
You can use itertools to calculate the products for you and can use the * operator to convert your list into arguments for the itertools.product() function.
import itertools
a = [1]
b = [2,3]
c = [4,5,6]
args = [a,b,c]
for combination in itertools.product(*args):
print combination
Output is
(1, 2, 4)
(1, 2, 5)
(1, 2, 6)
(1, 3, 4)
(1, 3, 5)
(1, 3, 6)