SheetWrite Data in Columns - excel

I am using OPL CPLEX 12.9
and I want to write my results in an excel file.
It is a two dimension array and I want to write the results in two columns but is it only possible to write the Data in lines?
Example Code in .dat
Product_Cost to SheetWrite (Daten, "Result!A1:B10");
What can I write for A1:B10 to get the results in two columns?

before doing SheetWrite you could transpose your matrix:
int M=2;
int N=5;
int A[i in 1..M][j in 1..N] = rand(4);
int B [j in 1..N][i in 1..M]=A[i][j];
execute
{
writeln("A=",A);
writeln("B=",B);
}
gives
A= [[0 0 0 0 1]
[3 2 3 2 0]]
B= [[0 3]
[0 2]
[0 3]
[0 2]
[1 0]]

Related

Pandas convert column where every cell is list of strings to list of integers

I have a dataframe with columns that has list of numbers as strings:
C1 C2 l
1 3 ['5','9','1']
7 1 ['7','1','6']
What is the best way to convert it to list of ints?
C1 C2 l
1 3 [5,9,1]
7 1 [7,1,6]
Thanks
You can try
df['l'] = df['l'].apply(lambda lst: list(map(int, lst)))
print(df)
C1 C2 l
0 1 7 [5, 9, 1]
1 3 1 [7, 1, 6]
Pandas' dataframes are not designed to work with nested structures such as lists. Thus, there is no vectorial method for this task.
You need to loop. The most efficient is to use a list comprehension (apply would also work but with much less efficiency).
df['l'] = [[int(x) for x in l] for l in df['l']]
NB. There is no check. If you have anything that cannot be converted to integers, this will trigger an error!
Output:
C1 C2 l
0 1 3 [5, 9, 1]
1 7 1 [7, 1, 6]

Inverse X.toarray into a CountVectorizer in sklearn

I'm following documentation here:
https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> corpus = [
... 'This is the first document.',
... 'This document is the second document.',
... 'And this is the third one.',
... 'Is this the first document?',
... ]
>>> vectorizer = CountVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.toarray())
[[0 1 1 1 0 0 1 0 1]
[0 2 0 1 0 1 1 0 1]
[1 0 0 1 1 0 1 1 1]
[0 1 1 1 0 0 1 0 1]]
Suppose I already have a term frequency matrix like the one given in X.toarray(), but I didn't use CountVectorizer to obtain it.
I want to apply a TfIDF to this matrix. Is there a way for me to take a count array + a dictionary and apply some inverse of this function as a constructor to get a fit_transformed X?
I'm looking for...
>>> print(X.toarray())
[[0 1 1 1 0 0 1 0 1]
[0 2 0 1 0 1 1 0 1]
[1 0 0 1 1 0 1 1 1]
[0 1 1 1 0 0 1 0 1]]
>>> V = CountVectorizerConstructorPrime(array=(X.toarray()),
vocabulary=['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this'])
such that:
>>> V == X
True
The X constructed by the CountVectorizer is a sparse matrix in SciPy's compressed sparse row (csr) format. So you can construct it directly from any word count matrix with the appropriate SciPy function:
from scipy.sparse import csr_matrix
V = csr_matrix(X.toarray())
Now V and X are equal, although this may not be obvious, because V == X will give you another sparse matrix (or rather complain that the matrix is not sparse despite the intended format, see this question). But you can check it like this:
(V != X).toarray().any()
False
Note that the word list was not needed, because the matrix only encodes the frequencies of all distinct words, no matter what they are.

Reshaping 1D array does not work properly

Assume I have the following array, where all binary values are assumed to be in the same length:
A = [10101010, 10011010, 10111101, 11110000]
This is 1D of size 4. I want to be able to convert it to 2D numpy. Thus, using this example I should get (4,8). I use the following code but it doesn't reshape it. Any suggestions?
import numpy as np
A = [10101010, 10011010, 10111101, 11110000]
A = np.asarray(A)
A = np.reshape(A, [-1,])
You got a list of integers that are > 10 million each - not binary values.
You can fix that by making them a string, seperate into single digits and convert that:
import numpy as np
A = [10101010, 10011010, 10111101, 11110000]
B = [list(map(int,t)) for t in list(map(str,A))]
npA = np.asarray(B)
print(npA)
Output:
[[1 0 1 0 1 0 1 0]
[1 0 0 1 1 0 1 0]
[1 0 1 1 1 1 0 1]
[1 1 1 1 0 0 0 0]]

Is there a way to build the Dot product of two matrices with different shape?

Is there a way to build the Dot product of two matrices with different shape, without using anything else as pure python and numpy?
The shape of the columns should be equal, but the rows should be different. (example below)
Of course I know the brute force way:
for i in A:
for j in B:
np.dot(A,B)
but is there something else?
Here an example:
import numpy as np
A = np.full((4,5),3)
B = np.full((3,5),5)
print(A)
print(B)
result = np.zeros((A.shape[0],B.shape[0]))
for i in range(A.shape[0]):
for j in range(B.shape[0]):
result[i,j] = np.dot(A[i],B[j])
print(dot)
Output:
A = [[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]]
B = [[5 5 5 5 5]
[5 5 5 5 5]
[5 5 5 5 5]]
result = [[75. 75. 75.]
[75. 75. 75.]
[75. 75. 75.]
[75. 75. 75.]]
The coal is to calculate the dot product without two loops. So is there a more efficient way?

Confused about keras Dot Layer. How is the Dot product computed?

I read all posts about the Dot Layer but none explains how this and so the output shape is computed! It seems so standard though!
How exactly are the values computed with a along a specific axis?
val = np.random.randint(2, size=(2, 3, 4))
a = K.variable(value=val)
val2 = np.random.randint(2, size=(2, 2, 3))
b = K.variable(value=val)
print("a")
print(val)
print("b")
print(val2)
out = Dot(axes = 2)([a,b])
print(out.shape)
print("DOT")
print(K.eval(out))
I get:
a
[[[0 1 1 1]
[1 1 0 0]
[0 0 1 1]]
[[1 1 1 0]
[0 0 1 0]
[0 1 0 0]]]
b
[[[1 0 1]
[1 0 1]]
[[1 0 1]
[1 1 0]]]
(2, 3, 3)
DOT
[[[ 3. 1. 2.]
[ 1. 2. 0.]
[ 2. 0. 2.]]
[[ 3. 1. 1.]
[ 1. 1. 0.]
[ 1. 0. 1.]]]
I cannot understand with my mathematical and algebraic matrix know-how how the heck this is computed?
Here's how the Dot product works. Internally it is calling K.batch_dot.
First, I think you might have intended to do,
val = np.random.randint(2, size=(2, 3, 4))
a = K.variable(value=val)
val2 = np.random.randint(2, size=(2, 2, 3))
b = K.variable(value=val2) # You have val here
But fortunately, you had (or could have been your initial intention too. Anyway just pointing out)
b = K.variable(value=val)
If you had the intended code, it will throw an error because the dimension you want the dot product on, doesn't match. Moving on,
How dot product is computed
You have
a.shape = (2,3,4)
b.shape = (2,3,4)
First you are only performing element-wise dot over the batch dimension. So that dimension stays that way.
Now you can ignore the first dimension of both a and b and consider the dot product between two matrices (3,4) and (3,4) and do the dot product over the last axis, which results in a (3,3) matrix. Now add the batch dimension you get a,
(2, 3, 3) tensor
Let's now take your example. You got,
a
[[[0 1 1 1]
[1 1 0 0]
[0 0 1 1]]
[[1 1 1 0]
[0 0 1 0]
[0 1 0 0]]]
b
[[[0 1 1 1]
[1 1 0 0]
[0 0 1 1]]
[[1 1 1 0]
[0 0 1 0]
[0 1 0 0]]]
Then you do the following two dot products.
# 1st sample
[0 1 1 1] . [0 1 1 1]
[1 1 0 0] . [1 1 0 0]
[0 0 1 1] . [0 0 1 1]
# 2nd sample
[1 1 1 0] . [1 1 1 0]
[0 0 1 0] . [0 0 1 0]
[0 1 0 0] . [0 1 0 0]
This gives,
# 1st sample
[3 1 2]
[1 2 0]
[2 0 2]
# 2nd sample
[ 3 1 1]
[ 1 1 0]
[ 1 0 1]
Finally by adding the missing batch dimension you get,
[[[ 3. 1. 2.]
[ 1. 2. 0.]
[ 2. 0. 2.]]
[[ 3. 1. 1.]
[ 1. 1. 0.]
[ 1. 0. 1.]]]

Resources