How to slice on arbitrary indices with breeze? - scala-breeze

In Python's numpy, I can do this:
>>> import numpy as np
>>> m = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
>>> indices = [1,3]
>>> m[:,indices]
array([[ 2, 4],
[ 6, 8],
[10, 12]])
In other words, I can slice based on an arbitrary (not necessarily contiguous) list of indices. How can I do something similar in Breeze? I'm looking for something efficient and preferably elegant.

More or less identically to numpy:
scala> import breeze.linalg._
import breeze.linalg._
scala> val m = DenseMatrix((1,2,3,4),(5,6,7,8),(9,10,11,12))
m: breeze.linalg.DenseMatrix[Int] =
1 2 3 4
5 6 7 8
9 10 11 12
scala> val indices = IndexedSeq(1,3)
indices: IndexedSeq[Int] = Vector(1, 3)
scala> m(::, indices)
res0: breeze.linalg.SliceMatrix[Int,Int,Int] =
2 4
6 8
10 12

Related

Why Indexing error returns in python GEKKO

I want to transform LINGO code to Python GEKKO code. Here is Lingo code, lingo results and gekko codes. I cant write second and third constraints. It returns indexing error but, I dont understand why? Can someone help? (It's a graph coloring problem)
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
# x = m.Array(m.Var,(7,5),lb=0,ub=1,integer=True)
x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],
[5,4],
[3,6],
[2,4],
[2,5],
[2,6],
[4,5],
[4,6]])
for i in range(6):
m.Minimize(y)
for i in range(6):
# for j in range(2):
# m.Equation(m.sum(x[i,j])==1)
m.Equation(m.sum(x[i,:])==1)
for k in range (6):
for i in range(8):
m.Equation(x[arcs[i,1],k]+x[arcs[i,2],k]<=1)
# m.Equation(x[arcs[i,1],k]+x[arcs[i,2],k])<=1)
# m.Equation(m.sum(x[arcs[i,1],k],x[arcs[i,2],k]))<=1)
Revised version is=
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y = m.Array(m.Var,6,lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],
[1,4],
[3,4],
[3,4],
[4,5],
[2,6],
[4,5],
[4,6]])
for i in range(6):
m.Minimize(y[i])
for i in range(6):
m.Equation(m.sum(x[i,:])==1)
for k in range (6):
for i in range(8):
m.Equation(x[arcs[i,0]-1,k-1]+x[arcs[i,1]-1,k-1]<=1)
for i in range(6):
m.Equation(m.sum(x[i,:]<=y[i])
m.options.solver = 1
m.solve()
print('Objective Function: ' + str(m.options.objfcnval))
print(x)
print(y)
now it gives invalid syntax error for m.solve and m options?
A couple things that you need to consider for Python:
Lists and arrays are zero-index so you need to shift them by -1 relative to the LINDO / LINGO language.
The objective function y is a list of constants. Gekko generates warnings that there are no variables in that expression.
Here is a corrected version of your Python script that you probably need to supplement with a correct objective statement and any additional equations that are needed.
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],
[1,4],
[3,4],
[2,4],
[2,5],
[2,6],
[4,5],
[4,6]])
for i in range(6):
m.Minimize(y[i])
for i in range(6):
m.Equation(m.sum(x[i,:])==1)
for k in range (6):
for i in range(8):
m.Equation(x[arcs[i,0]-1,k]\
+x[arcs[i,1]-1,k]<=1)
m.solve()
Response to Edit
The revised version is missing a closing parenthesis on the m.sum(). Here is a corrected version.
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y = m.Array(m.Var,6,lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],[1,4],[3,4],[3,4],[4,5],[2,6],[4,5],[4,6]])
for i in range(6):
m.Minimize(y[i])
for i in range(6):
m.Equation(m.sum(x[i,:])==1)
for k in range (6):
for i in range(8):
m.Equation(x[arcs[i,0]-1,k-1]+x[arcs[i,1]-1,k-1]<=1)
for i in range(6):
m.Equation(m.sum(x[i,:])<=y[i])
m.options.solver = 1
m.solve()
print('Objective Function: ' + str(m.options.objfcnval))
print(x)
print(y)
You can find additional tips on troubleshooting gekko applications with tutorial 18.

Find local min based on the length of occurences of successive means without falling in wrong min

1. Problem description
I have the following list of values [10, 10, 10, 10, 5, 5, 5, 5, 7, 7, 7, 2, 4, 3, 3, 3, 10] It is shown in the following picture.
What I want to do is find the minimum based on the value of the element and
its duration. From the previous list we can construct the following dictionary (key:val) :[10:4, 5:4, 7:2, 2:1, 4:1, 3:3, 10:1]. Meaning we have 4 sucessive 10s followed by 4 successive 5s, 2 successive 7s and 3 successive 3s.
Based on what I said the local min is 5. But I don't want that The local min should be 3. We didn't select 2 because it happened only once.
Do you have an idea on how we can solve that problem. Is there an existing method that can be used to solve it?
Of course we can sort the dictionary by values [10:4, 5:4, 7:2, 3:3, 10:1] and select the lowest key that has a value different than 1. Is that a good solution?
2. Selection criteria
must be a local min (find_local_min(prices))
must have the highest numbers of succession
the min succession must be > 1
AND I AM STUCK! because now I have 3 as local minimum but it is repeated only 3 times. I was testing if My idea is correct and I tried to find a counter example and I shot my foot
3. source code
the following code extracts the minimums with the dictionary:
#!/usr/bin/env python
import csv
import sys
import os
from collections import defaultdict
def find_local_min(prices):
i = 1
minPrices = []
while i < len(prices):
if prices[i] < prices[i-1]:
minPrices.append(prices[i])
j = i + 1
while j < len(prices) and prices[j] == prices[j-1]:
minPrices.append(prices[j])
j += 1
i = j
else:
i += 1
return minPrices
if __name__ == "__main__":
l = [10, 10, 10, 10, 5, 5, 5, 5, 7, 7, 7, 2,4, 3, 3, 3, 10]
minPrices = find_local_min(l)
minPriceDict = defaultdict(int)
for future in minPrices :
minPriceDict[future] += 1
print minPriceDict
As output if gives the following: defaultdict(<type 'int'>, {2: 1, 3:
3, 5: 4}) Based on this output the algorithm will select 5 as the min
because it is repeated 5 successive times. But that's wrong! it
should be 3. I really want to know how to solve that problem

Augment a matrix in NumPy

I start with a 2x4 matrix A
import numpy as np
A = np.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
I also have another 1x4 matrix B
B = np.matrix([9, 10, 11, 12])
How do I concatenate A and B so that I get a 3x4 matrix C
C = [[9 10 11 12]
[1 2 3 4]
[5 6 7 8]]
Note that B is prepended before row 0 of matrix A.
From: http://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html
C = np.concatenate((B, A))
The r_ command also works:
np.r_[C,B]
also works (and np.c_ is a variant on r_ for gluing columns)

Balanced layout of n items in a grid

I have a list of n logos to display in a grid, with a maximum of 3 per row.
What's an algorithm to decide how many to display per row such that the number of logos per row is as balanced as possible without using more than the minimum possible number of rows?
For example:
n -> number in each row
1 -> 1
2 -> 2
3 -> 3
4 -> 2, 2
5 -> 3, 2
6 -> 3, 3
7 -> 3, 2, 2
8 -> 3, 3, 2
9 -> 3, 3, 3
10 -> 3, 3, 2, 2
For N <= 3 just use N.
If N is exactly divisible by 3 then use: 3 3 ... 3
If N when divided by 3 has remainder 1 then use: 3 3 ... 2 2
If N when divided by 3 has remainder 2 then use: 3 3 ... 3 2
AS confusing as your question is, I think what you need to do is first determine:
number_of_rows = ceil(number_of_logos / 3.0)
Then add a logo to each row, one at a time.
Python:
import math
def partition_logos(count, lsize):
num_lines = int(math.ceil(count / float(lsize)))
partition = [0] * num_lines
for i in xrange(count):
partition[i%num_lines] += 1
return partition
>>> for i in xrange(1,11):
... print partition_logos(i, 3)
[1]
[2]
[3]
[2, 2]
[3, 2]
[3, 3]
[3, 2, 2]
[3, 3, 2]
[3, 3, 3]
[3, 3, 2, 2]
A recursive solution, in Python:
def logos_by_row(N, rows):
width = 0
if N > 4 or N == 3:
width = 3
elif N == 4 or N == 2:
width = 2
elif N == 1:
width = 1
if width != 0:
rows.append(width)
logos_by_row(N - width, rows)
answer = []
for i in range(10):
logos_by_row(i+1, answer)
print answer
just use n/3 to calculate the row and n%3 to calculate the column
edit: ok i saw you edited your question.... i din't saw that you want to display 2 in each row if the are 4 logos. but then you can use n mod 3 to calculate if their is a reminder as others already suggested
if n%3 = 0 then just put 3 logos in each row
if n%3 = 1 then put the last 4 logos in two rows
if n%3 = 2 then put 3 logos in n row and the last 2 logos in a separate row

Scala version of Rubys' each_slice?

Does Scala have a version of Rubys' each_slice from the Array class?
Scala 2.8 has grouped that will chunk the data in blocks of size n (which can be used to achieve each_slice functionality):
scala> val a = Array(1,2,3,4,5,6)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> a.grouped(2).foreach(i => println(i.reduceLeft(_ + _)) )
3
7
11
There isn't anything that will work out of the box in 2.7.x as far as I recall, but it's pretty easy to build up from take(n) and drop(n) from RandomAccessSeq:
def foreach_slice[A](s: RandomAccessSeq[A], n: Int)(f:RandomAccessSeq[A]=>Unit) {
if (s.length <= n) f(s)
else {
f(s.take(n))
foreach_slice(s.drop(n),n)(f)
}
}
scala> val a = Array(1,2,3,4,5,6)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6)
scala> foreach_slice(a,2)(i => println(i.reduceLeft(_ + _)) )
3
7
11
Tested with Scala 2.8:
scala> (1 to 10).grouped(3).foreach(println(_))
IndexedSeq(1, 2, 3)
IndexedSeq(4, 5, 6)
IndexedSeq(7, 8, 9)
IndexedSeq(10)

Resources