associate values to classes in Keras - python

How can I associate values to classes in Keras?
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected:
[1, 2, 3], [4, 5, 6] associated with class 1
[7, 8, 9] associated with class 2
The problem is that [1, 2, 3], [4, 5, 6] come from one file and [7, 8, 9] from another. I have read an example with iris.csv but the samples have same size.

One suggestion would be to merge the two datasets into one like this:
X (shape=(3,3))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y (shape = (3,1))
[1, 1, 2]
Many classifiers support labels in that format out of the box.
If you need to further process the labels, you can use scikit-learn for that. Have a look at these:
LabelEncoder
OneHotEncoder

Related

Permutation of 2D matrix

I would like to be able to generate all unique permutations of a 2d array in python and keep the order.
Let's say I have a 2D matrix [[1, 2, 3], [4, 5, 6]]. Expected result should be a in 8 x 3 in the form
[[1, 2, 3],
[1, 2, 6],
[1, 5, 3],
[1, 5, 6],
[4, 2, 3],
[4, 2, 6],
[4, 5, 3],
[4, 5, 6]].
Thanks
Transpose the array, and then use itertools.product:
from itertools import product
list(map(list, product(*zip(*data))))
This outputs:
[[1, 2, 3], [1, 2, 6], [1, 5, 3], [1, 5, 6], [4, 2, 3], [4, 2, 6], [4, 5, 3], [4, 5, 6]]
The itertools method with product would be the most readible way to do this, but since your question is tagged numpy, here is how you can do this using only Numpy methods.
1. Permutations without expected order
You can use this method if order is not important for you. This uses np.meshgrid and np.stack with some .reshape to get the permutations you need, minus the order you expect.
import numpy as np
lst = [[1, 2, 3], [4, 5, 6]]
arr = np.array(lst)
np.stack(np.meshgrid(*arr.T),-1).reshape(-1,3)
array([[1, 2, 3],
[1, 2, 6],
[4, 2, 3],
[4, 2, 6],
[1, 5, 3],
[1, 5, 6],
[4, 5, 3],
[4, 5, 6]])
2. Permutations with expected order
Getting the order to work is a bit "hacky" but a small modification over the above array with simple reordering of columns can solve this with pretty much the same code.
import numpy as np
lst = [[1, 2, 3], [4, 5, 6]]
order = [1,0,2]
arr = np.array(lst)[:,order]
np.stack(np.meshgrid(*arr.T),-1).reshape(-1,3)[:,order]
array([[1, 2, 3],
[1, 2, 6],
[1, 5, 3],
[1, 5, 6],
[4, 2, 3],
[4, 2, 6],
[4, 5, 3],
[4, 5, 6]])

converting a normal list into a nested one using a condition

I want to convert a normal list(main_list) into a nested one(ans_list) but I don't know how.
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans_list = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Use a comprehension over appropriate slices:
n = 3
ans_list = [main_list[i::n] for i in range(n)]
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
if you are open to using NumPy and there are no other conditions other than how many rows you want to split in to then try this
import numpy as np
np.array(main_list).reshape(-1,3).tolist()

Swap/Transpose matrix columns

I have a 2d array like this:
m = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
And I want to swap the first and second columns, which would look like this:
[2, 1, 3]
[5, 4, 6]
[8, 7, 9]
I've looked around, but all I can find is stuff about turning rows into columns. I tried the zip function for example, but again it just does this:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Anyone know how to do this?
I think if you just want the first and second columns swapped you should change the code to be something like this:
for i in m:
i[1],i[0] = i[0],i[1]
for i in m:
i[0],i[1] = i[1],i[0]
output
[[2, 1, 3],
[5, 4, 6],
[8, 7, 9]]

Cartesian product of rows of a very big array

I have an array of size (100, 50). I need to generate an output array which represents a cartesian product of input array rows.
For simplification purposes, let's have an input array:
array([[2, 6, 5],
[7, 3, 6]])
As output I would like to have:
array([[2, 7],
[2, 3],
[2, 6],
[6, 7],
[6, 3],
[6, 6],
[5, 7],
[5, 3],
[5, 6]])
Note: itertools.product doesn't work here, because of the size of the input vector. Also all another similar answers, assumes number of rows smaller than 32, what is not the case here
This question has been asked many times, for example here.
The array of a size (100, 50) is too big and can't be handled by numpy. However, smaller array size might be solved.
Anyway, I prefer to use itertools for this kind of stuff:
import itertools
a = np.array([[2, 6, 5], [7, 3, 6]])
np.array(list(itertools.product(*a)))
array([[2, 7],
[2, 3],
[2, 6],
[6, 7],
[6, 3],
[6, 6],
[5, 7],
[5, 3],
[5, 6]])
a = np.array([[2, 6, 5],[7, 3, 6]])
out = np.array(np.meshgrid(a[0], a[1])).T.reshape(-1,2)
print(out)
"""
prints
[[2 7]
[2 3]
[2 6]
[6 7]
[6 3]
[6 6]
[5 7]
[5 3]
[5 6]]
"""

Filling an array with arrays or vectors in python using numpy without a loop

I'm trying to find a way to fill an array with rows of values. It's much easier to express my desired output with an example. Given the input of an N x M matrix, array1,
array1 = np.array([[2, 3, 4],
[4, 8, 3],
[7, 6, 3]])
I would like to output an array of arrays in which each row is an N x N consisting of the values from the respective row. The output would be
[[[2, 3, 4],
[2, 3, 4],
[2, 3, 4]],
[[4, 8, 3],
[4, 8, 3],
[4, 8, 3]],
[[7, 6, 3],
[7, 6, 3],
[7, 6, 3]]]
You can reshape the array from 2d to 3d, then use numpy.repeat() along the desired axis:
np.repeat(array1[:, None, :], 3, axis=1)
#array([[[2, 3, 4],
# [2, 3, 4],
# [2, 3, 4]],
# [[4, 8, 3],
# [4, 8, 3],
# [4, 8, 3]],
# [[7, 6, 3],
# [7, 6, 3],
# [7, 6, 3]]])
Or equivalently you can use numpy.tile:
np.tile(array1[:, None, :], (1,3,1))
Another solution which is sometimes useful is the following
out = np.empty((3,3,3), dtype=array1.dtype)
out[...] = array1[:, None, :]

Categories

Resources