How to define two related indexes in sum section by using Cplex - network-programming

I have been working on the mathematical model for a long time and now I have some problems. In this model we have nodes and edges and the flows that pass through these nodes according to the topology. We also have network functions that we use to create two length chains.
First, I don't know how to define two dependent indices that use the same range. for example: cons1 , sum (j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9). Is it correct or not?
Second How to define IL decision variable.
I just got this error:"Cannot use type range for int" in cons19 and cons20. I will write the explanation of the two constraints 19 and 20, but I don't know how to change the code.
Constraints (19) and (20) are flow conservation constraints for the (destination node). Constraint (19) makes sure that one of the incoming links of the destination node is assigned to route from the node hosting the last NF of the service chain (nJ ) to the destination node (df ) that is also represented as the J + 1 service. In addition, Constraint (20) assigns one of the outgoing links of the node serving the last service to the (J + 1)th service order. I put my whole code for you.
Dear Mr Fleischer, I have asked you several times questions and I am very grateful for your kindness, but if you can, please help me this time as well. Thanks in advance.
using CPLEX;
int Numnodes=10;
range Nodes = 0..Numnodes;
//...................................................
//total links number=> int L=13;
tuple edge{
key int fromnode;
key int tonode;
}
{edge} Edges with fromnode,tonode in Nodes =
{<0,1>,<1,3>,<2,3>,<3,4>,<3,5>,<3,6>,<4,5>,<4,6>,<4,8>,<5,6>,<6,7>,<6,9>,<9,10>};
{edge} Lin with fromnode,tonode in Nodes =
{<0,1>,<1,3>,<2,3>,<3,4>,<3,5>,<3,6>,<4,5>,<4,6>,<4,8>,<5,6>,<6,7>,<6,9>,<9,10>};
{edge} Lout with fromnode,tonode in Nodes =
{<0,1>,<1,3>,<2,3>,<3,4>,<3,5>,<3,6>,<4,5>,<4,6>,<4,8>,<5,6>,<6,7>,<6,9>,<9,10>};
tuple cflow{
key int node1;
key int node2;
}
{cflow} Flows with node1,node2 in Nodes= {<0,1>, <0,3>, <0,4>, <0,5>, <0,6>, <0,7>, <0,8>,
<0,9>, <0,10>, <1,3>, <1,4>, <1,5>, <1,6>, <1,7>, <1,8>, <1,9>, <1,10>, <2,3>, <2,4>, <2,5>,
<2,6>, <2,7>, <2,8>, <2,9>, <2,10>,<3,4>, <3,5>, <3,6>, <3,7>, <3,8>, <3,9>, <3,10>, <4,5>,
<4,6>, <4,7>, <4,8>, <4,9>, <4,10>, <5,6>, <5,7>, <5,9>, <5,10>,<6,7>, <6,9>, <6,10>,
<9,10>};
tuple arraytoset
{
cflow ed;
int rank;
}
{arraytoset} srout = union(ed in Flows) {<ed,i> | i in 1..card(Routes[ed])};
//....................................................................................
//number flows
range F = 1..50;
//length chains
int J[f in Flows] = 2;
int nj[1..2];
//.......................................................................................
//VNFs
{string} V = {"Proxy", "Firewall", "IDS", "DPI", "NAT"};
//An NF instance v hosted on node n is characterized by its service rate of requests.
float u[V][Nodes]=...;
//transmission rate.
float C[l in Edges]=...;
//Delays
float Dvn[V][Nodes]=...; //denote t
//landa
float landa[f in Flows]=(0.5+rand(2))/2;
//..............................................................
//MAIN DECISION VARIABLES
dvar int I[V][Nodes][Flows][1..2] in 0..1;
//denotes that an NF instance v hosted at node n is used by the j-th service on the service
chain of flow f.
dvar int IL[l in Edges][Flows][1..2][Nodes][1..1][0..9] in 0..1;
//denotes that link l is used by flow f to route from the j-th to (j + 1)-th NF service,
hosted at node nj and nj+1.
dvar int Y[V][Nodes];
//represents the number of NF type v instances that are hosted at node n.
//Decision variables related with non linear equations
dvar int z[l1 in Lout][Flows][1..2][Nodes][1..1][0..9][V] in 0..1;
//Related with floor function
dexpr float x[f in Flows] = sum(v in V, n in Nodes, j in 1..2) I[v][n][f][j] / J[f];
dvar int s[f in Flows];
dvar float floorequ[i in Flows] in 0..0.99999;
//Total delays
dexpr float DT = sum(l in Edges, f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes &&
nj[j+1] in 0..9) IL[l][f][j][nj[j]][j+1][nj[j+1]] * Dlink[l];
dexpr float DP = sum(v in V, n in Nodes, f in Flows, j in 1..2) I[v][n][f][j] * Dvn[v][n];
//MAIN objective functions
dexpr float objmodel1 = sum(n in Nodes, v in V) (Y[v][n] * Cpuvnf[v] / Cpunode[n]);//to minimize the use of cores
dexpr float objmodel2 = sum(l in Edges, f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes
&& nj[j+1] in 0..9) ((IL[l][f][j][nj[j]][j+1][nj[j+1]] * landa[f]) / C[l]); //to minimize the utilization of link capacities.
dexpr float objmodel3 = sum(f in Flows) s[f];
maximize staticLex(objmodel3, -objmodel1, -objmodel2);
subject to{
//constrains with j,j+1,nj[j],nj[j+1] are wrong, I don't know how to define these.
forall (<o,d> in Edges)
cons1: sum(f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9) (IL[<o,d>]
[f][j][nj[j]][j+1][nj[j+1]] * landa[f]) <= C[<o,d>];
forall (n in Nodes)
cons2: sum(v in V) Y[v][n] * Cpuvnf[v] <= Cpunode[n];
forall (v in V, n in Nodes)
cons3: sum(f in Flows, j in 1..2) I[v][n][f][j] * landa[f] <= u[v][n];
forall (n in Nodes, v in V, f in Flows, j in 1..2)
cons4: Y[v][n] >= I[v][n][f][j];
forall (f in Flows, j in 1..2)
cons5: sum(n in Nodes, v in V) I[v][n][f][j] == 1;
forall (i in Flows)
cons6a: x[i]==s[i]+floorequ[i];
forall (f in Flows)
cons7: DT + DP <= Dflow[f];
//convert non_linear_equation_11 to new linear constraints == constraints 8, 9, 10
forall (f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9, v in V)
cons8: sum(<o1,d1> in Lout) z[<o1,d1>][f][j][nj[j]][j+1][nj[j+1]][v] == 1;
forall (<o1,d1> in Lout, f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1]
in 0..9, v in V) {
cons9: 3 * z[<o1,d1>][f][j][nj[j]][j+1][nj[j+1]][v] <= (IL[<o1,d1>][f][j][nj[j]][j+1]
[nj[j+1]] + I[v][nj[j]][f][j] + I[v][nj[j+1]][f][j+1]);
cons10: z[<o1,d1>][f][j][nj[j]][j+1][nj[j+1]][v] >= (IL[<o1,d1>][f][j][nj[j]][j+1]
[nj[j+1]] + I[v][nj[j]][f][j] + I[v][nj[j+1]][f][j+1]) - 2; }
//convert non_linear_equation_12 to new linear constraints == constraints 11, 12, 13
forall (f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9, v in V)
cons11: sum(<o2,d2> in Lin) z[<o2,d2>][f][j][nj[j]][j+1][nj[j+1]][v] == 1;
forall (<o2,d2> in Lin, f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in
0..9, v in V) {
cons12: 3 * z[<o2,d2>][f][j][nj[j+1]][j+1][nj[j+1]][v] <= (IL[<o2,d2>][f][j][nj[j]][j+1]
[nj[j+1]] + I[v][nj[j]][f][j] + I[v][nj[j+1]][f][j+1]);
cons13: z[<o2,d2>][f][j][nj[j]][j+1][nj[j+1]][v] >= (IL[<o2,d2>][f][j][nj[j]][j+1]
[nj[j+1]] + I[v][nj[j]][f][j] + I[v][nj[j+1]][f][j+1]) - 2; }
//constraints 14, 15
forall(f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9, v in V){
cons14: sum(<o1,d1> in Lout) IL[<o1,d1>][f][j][nj[j]][j+1][nj[j+1]] <= 1;
cons15: sum(<o2,d2> in Lin) IL[<o2,d2>][f][j][nj[j]][j+1][nj[j+1]] <= 1; }
//constraints 16
forall (f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9)
cons16: (sum(<o2,d2> in Lin) IL[<o2,d2>][f][j][nj[j]][j+1][nj[j+1]]) - (sum(<o1,d1>
in Lout) IL[<o1,d1>][f][j][nj[j]][j+1][nj[j+1]]) == 0;
//constraints 17, 18
forall (f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9, v in
V) {
cons17: sum(<o2,d2> in Lin) IL[<o2,d2>][f][0][0][1][1] == I[v][1][f][1];
cons18: sum(<o1,d1> in Lout) IL[<o1,d1>][f][0][0][1][1] == I[v][1][f][1]; }
//constraints 19, 20
forall (f in Flows, j in 1..2: j+1 in 1..1 && nj[j] in Nodes && nj[j+1] in 0..9, v in
V) {
cons19: sum(<o2,d2> in Lin) IL[<o2,d2>][f][1..2][9][1..1][10] == I[v][9][f][1..2];
cons20: sum(<o1,d1> in Lout) IL[<o1,d1>][f][1..2][9][1..1][10] == I[v][9][f][1..2];}}
assert forall(f in Flows) s[f]==floor(x[f]);
execute DISPLAY_After_SOLVE {
writeln("objmodel1==", objmodel1, "objmodel2==", objmodel2, "objmodel3==", objmodel3);
}
data file:
Cpunode=[1, 1, 1, 5, 4, 4, 5, 1, 10, 1, 1]; //number of core for each
node
Cpuvnf=[1, 1, 1, 1, 1]; //number of core that each vnf wants
C=[1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
1000, 1000]; //1ms
u=[[10,10,10,10,10,10,10,10,10,10,10]
[10,10,10,10,10,10,10,10,10,10,10]
[10,10,10,10,10,10,10,10,10,10,10]
[10,10,10,10,10,10,10,10,10,10,10]
[10,10,10,10,10,10,10,10,10,10,10]]; //10Mbps
Dvn=[[0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003]
[0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003]
[0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003]
[0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003]
[0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003,
0.003, 0.003] ]; //3ms
Dlink=[0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01,
0.01, 0.01, 0.01]; //10ms
Dflow=[0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04,
0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04,
0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04,
0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04,
0.04, 0.04, 0.04, 0.04, 0.04, 0.04]; //40ms

If r is a range , v[r] is not a value!
So in your model you should change
cons19: sum(<o2,d2> in Lin) IL[<o2,d2>][f][1..2][9][1..1][10] == I[v][9][f][1..2];
cons20: sum(<o1,d1> in Lout) IL[<o1,d1>][f][1..2][9][1..1][10] == I[v][9][f][1..2];
into
cons19: sum(<o2,d2> in Lin) IL[<o2,d2>][f][1][9][1][10] == I[v][9][f][1];
cons20: sum(<o1,d1> in Lout) IL[<o1,d1>][f][1][9][1][10] == I[v][9][f][1];
At least this will solve the error and you will be able to go further

Related

How to convert non linear constraint to new linear constraints?

I have converted the non-linear expression [1]: https://i.stack.imgur.com/MzzSO.png into linear equations 11, 12 and 13. But when I run my code, I got these errors: "constraint labeling not supported for dimensions with variable size, use named constraints instead", "CPLEX cannot extract expression", "Element "cons12" not defined" and "Invalid initialization expression for element "cons12" ". Can you help me, what should I do? Thanks in advance.
using CPLEX;
//Total nodes number.
range Nodes = 1..9;
//{int} Nodes = {1,2,3,4,5,6,7,8,9};
//................................................................................
//Total links number
//two_directed
tuple edge{
int node_out;
int node_in;
};
{edge} L with node_out, node_in in Nodes = {<1,3>, <3,1>, <2,3>, <3,2>, <3,4>, <4,3>, <3,5>,
<5,3>, <3,6>, <6,3>, <4,5>, <5,4>, <4,6>, <6,4>,
<4,8>, <8,4>, <5,6>, <6,5>, <6,7>, <7,6>, <6,9>,
<9,6>};
{edge} Lout[Nodes] = [{<1,3>},//node1
{<2,3>},//node2
{<3,1>, <3,2>, <3,4>, <3,5>, <3,6>},//node3
{<4,3>, <4,5>, <4,6>, <4,8>},//node4
{<5,3>, <5,4>, <5,6>},//node5
{<6,3>, <6,4>, <6,5>, <6,7>, <6,9>},//node6
{<7,6>},//node7
{<8,4>},//node8
{<9,6>}];//node9
//Flows
tuple cflow{
int origin;
int destination;
}
{cflow} F with origin,destination in Nodes = {<1,2>, <1,3>, <1,4>, <1,5>, <1,6>, <1,7>,
<1,8>, <1,9>, <2,1>, <2,3>, <2,4>, <2,5>, <2,6>, <2,7>, <2,8>, <2,9>,
<3,1>, <3,2>, <3,4>, <3,5>, <3,6>, <3,7>, <3,8>, <3,9>,
<4,1>, <4,2>, <4,3>, <4,5>, <4,6>, <4,7>, <4,8>, <4,9>,
<5,1>, <5,2>, <5,3>, <5,4>, <5,6>, <5,7>, <5,8>, <5,9>,
<6,1>, <6,2>, <6,3>, <6,4>, <6,5>, <6,7>, <6,8>, <6,9>, <7,1>, <7,2>};
float landa_f[f in F]=[0.86, 0.3, 0.75, 0.23, 0.32, 0.4, 0.5, 0.6, 0.22, 0.14,
0.23, 0.42, 0.33, 0.5, 0.62, 0.36, 0.42, 0.35, 0.2, 0.16,
0.33, 0.9, 0.41, 0.51, 0.61, 0.33, 0.42, 0.51, 0.87, 0.96,
0.31, 0.55, 0.91, 0.36, 0.32, 0.72, 0.76, 0.32, 0.45, 0.64,
0.38, 0.71, 0.43, 0.55, 0.53, 0.9, 0.58, 0.97, 0.5, 0.33 ];
{string} V = {"IDS", "DPI", "NAT", "Proxy", "Firewall"};
//MAIN DECISION VARIABLES
dvar int I[v in V][n in Nodes][f in F][j in 1..2] in 0..1; //denotes that an NF instance v
hosted at node n is used by the j-th service on the service chain of flow f.
dvar int IL[l in L][f in F][j in 1..2][n in Nodes] in 0..1;//denotes that link l is used by
flow f to route from the j-th to (j + 1)-th NF service, hosted at node nj and nj+1.
dvar int Y[v in V][n in Nodes];
//Decision variables related with non linear equations
dvar int z[l in L][f in F][j in 1..2][n in Nodes][v in V] in 0..1;
subject to{
//convert non_linear_equations to new linear constraints
forall (f in F, j in 1..2, v in V)
cons11: sum( l in Lout[item(Routes[f],j-1)] ) z[l][f][j][item(Routes[f],j-1)][v] == 1;
forall (f in F, j in 1..2, l in Lout[item(Routes[f],j-1)], v in V) {
cons12: 3 * z[l][f][j][item(Routes[f],j-1)][v] <= ( IL[l][f][j][item(Routes[f],j-1)] +
I[v][item(Routes[f],j-1)][f][j] );
cons13: z[l][f][j][item(Routes[f],j-1)][v] >= ( IL[l][f][j][item(Routes[f],j-1)] +
I[v][item(Routes[f],j-1)][f][j] ) - 2; }
}
Indeed
dvar int x[1..2][1..2];
{int} s[1..2]=[{1,2},{1}];
subject to
{
forall(i in 1..2) forall(j in s[i]) ct:x[i][j]<=0;
}
execute
{
writeln(ct[1][1].UB);
}
does not work but if you write
dvar float x[1..2][1..2];
{int} s[1..2]=[{1,2},{1}];
subject to
{
forall(i in 1..2) forall(j in 1..2:j in s[i]) ct:x[i][j]<=0;
}
execute
{
writeln(ct[1][1].UB);
}
then it works fine
range r=1..2;
dvar float x[1..2][1..2];
constraint ct[r][r];
{int} s[1..2]=[{1,2},{1}];
subject to
{
forall(i in 1..2,j in 1..2)
ct[i][j]= if (j in s[i]) x[1][i]<=0;
}
execute
{
writeln(ct[1][1].UB);
}
works fine too

how to set condition in objective function in cvxpy

I have a brute force optimization algorithm with the objective function of the form:
np.clip(x # M, a_min=0, a_max=1) # P
where x is a Boolean decision vector, M is a Boolean matrix/tensor and P is a probability vector. As you can guess, x # M as an inner product can have values higher than 1 where is not allowed as the obj value should be a probability scalar or vector (if M is a tensor) between 0 to 1. So, I have used numpy.clip to fix the x # M to 0 and 1 values. How can I set up a mechanism like clip in cvxpy to achieve the same result? I have spent ours on internet with no lock so I appreciate any hint. I have been trying to use this to replicate clip but it raises Exception: Cannot evaluate the truth value of a constraint or chain constraints, e.g., 1 >= x >= 0. As a side note, since cvxpy cannot handle tensors, I loop through tensor slices with M[s].
n = M.shape[0]
m = M.shape[1]
w = M.shape[2]
max_budget_of_decision_variable = 7
x = cp.Variable(n, boolean=True)
obj = 0
for s in range(m):
for w in range(w):
if (x # M[s])[w] >= 1:
(x # M[s])[w] = 1
obj += x # M[s] # P
objective = cp.Maximize(obj)
cst = []
cst += [cp.sum(y) <= max_budget_of_decision_variable ]
prob = cp.Problem(objective, constraints = cst)
As an example, consider M = np.array([ [1, 0, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0], [1, 1, 1, 0, 1, 0]]) and P = np.array([0.05, 0.15, 0.1, 0.15, 0.5, 0.05]).

How do I optimize this code so that it executes faster (max ~5 mins)?

I am trying to make a program in turtle that creates a Lyapunov fractal. However, as using timeit shows, this should take around 3 hours to complete, 1.5 if I compromise resolution (N).
import turtle as t; from math import log; from timeit import default_timer as dt
t.setup(2000,1000,0); swid=t.window_width(); shei=t.window_height(); t.up(); t.ht(); t.tracer(False); t.colormode(255); t.bgcolor('pink')
def lyapunov(seq,xmin,xmax,ymin,ymax,N,tico):
truseq=str(bin(seq))[2:]
for x in range(-swid//2+2,swid//2-2):
tx=(x*(xmax-xmin))/swid+(xmax+xmin)/2
if x==-swid//2+2:
startt=dt()
for y in range(-shei//2+11,shei//2-1):
t.goto(x,y); ty=(y*(ymax-ymin))/shei+(ymax+ymin)/2; lex=0; xn=prevxn=0.5
for n in range(1,N+1):
if truseq[n%len(truseq)]=='0': rn=tx
else: rn=ty
xn=rn*prevxn*(1-prevxn)
prevxn=xn
if xn!=1: lex+=(1/N)*log(abs(rn*(1-2*xn)))
if lex>0: t.pencolor(0,0,min(int(lex*tico),255))
else: t.pencolor(max(255+int(lex*tico),0),max(255+int(lex*tico),0),0)
t.dot(size=1); t.update()
if x==-swid//2+2:
endt=dt()
print(f'Estimated total time: {(endt-startt)*(swid-5)} secs')
#Example: lyapunov(2,2.0,4.0,2.0,4.0,10000,100)
I attempted to use yield but I couldn't figure out where it should go.
On my slower machine, I was only able to test with a tiny N (e.g. 10) but I was able to speed up the code about 350 times. (Though this will be clearly lower as N increases.) There are two problems with your use of update(). The first is you call it too often -- you should outdent it from the y loop to the x loop so it's only called once on each vertical pass. Second, the dot() operator forces an automatic update() so you get no advantage from using tracer(). Replace dot() with some other method of drawing a pixel and you'll get back the advantage of using tracer() and update(). (As long as you move update() out of innermost loop as I noted.)
My rework of your code where I tried out these, and other, changes:
from turtle import Screen, Turtle
from math import log
from timeit import default_timer
def lyapunov(seq, xmin, xmax, ymin, ymax, N, tico):
xdif = xmax - xmin
ydif = ymax - ymin
truseq = str(bin(seq))[2:]
for x in range(2 - swid_2, swid_2 - 2):
if x == 2 - swid_2:
startt = default_timer()
tx = x * xdif / swid + xdif/2
for y in range(11 - shei_2, shei_2 - 1):
ty = y * ydif / shei + ydif/2
lex = 0
xn = prevxn = 0.5
for n in range(1, N+1):
rn = tx if truseq[n % len(truseq)] == '0' else ty
xn = rn * prevxn * (1 - prevxn)
prevxn = xn
if xn != 1:
lex += 1/N * log(abs(rn * (1 - xn*2)))
if lex > 0:
turtle.pencolor(0, 0, min(int(lex * tico), 255))
else:
lex_tico = max(int(lex * tico) + 255, 0)
turtle.pencolor(lex_tico, lex_tico, 0)
turtle.goto(x, y)
turtle.pendown()
turtle.penup()
screen.update()
if x == 2 - swid_2:
endt = default_timer()
print(f'Estimated total time: {(endt - startt) * (swid - 5)} secs')
screen = Screen()
screen.setup(2000, 1000, startx=0)
screen.bgcolor('pink')
screen.colormode(255)
screen.tracer(False)
swid = screen.window_width()
shei = screen.window_height()
swid_2 = swid//2
shei_2 = shei//2
turtle = Turtle()
turtle.hideturtle()
turtle.penup()
turtle.setheading(90)
lyapunov(2, 2.0, 4.0, 2.0, 4.0, 10, 100)
screen.exitonclick()

precision_recall_fscore_support returns same values for accuracy, precision and recall

I am training a logistic regression classification model and trying to compare the results using confusion matrix, and calculating precision, recall, accuracy
code is given below
# logistic regression classification model
clf_lr = sklearn.linear_model.LogisticRegression(penalty='l2', class_weight='balanced')
logistic_fit=clf_lr.fit(TrainX, np.where(TrainY >= delay_threshold,1,0))
pred = clf_lr.predict(TestX)
# print results
cm_lr = confusion_matrix(np.where(TestY >= delay_threshold,1,0), pred)
print("Confusion matrix")
print(pd.DataFrame(cm_lr))
report_lr = precision_recall_fscore_support(list(np.where(TestY >= delay_threshold,1,0)), list(pred), average='micro')
print ("\nprecision = %0.2f, recall = %0.2f, F1 = %0.2f, accuracy = %0.2f\n" % \
(report_lr[0], report_lr[1], report_lr[2], accuracy_score(list(np.where(TestY >= delay_threshold,1,0)), list(pred))))
print(pd.DataFrame(cm_lr.astype(np.float64) / cm_lr.sum(axis=1)))
show_confusion_matrix(cm_lr)
#linear_score = cross_validation.cross_val_score(linear_clf, ArrX, ArrY,cv=10)
#print linear_score
expected results are
Confusion matrix
0 1
0 4303 2906
1 1060 1731
precision = 0.37, recall = 0.62, F1 = 0.47, accuracy = 0.60
0 1
0 0.596893 1.041204
1 0.147038 0.620208
however my outputs are
Confusion matrix
0 1
0 4234 2891
1 1097 1778
precision = 0.60, recall = 0.60, F1 = 0.60, accuracy = 0.60
0 1
0 0.594246 1.005565
1 0.153965 0.618435
how do I get correct results ?
In a 'binary' case like yours (2 classes) you need to use average='binary' instead of average='micro'.
For example:
TestY = [0, 1, 1, 0, 1, 1, 1, 0, 0, 0]
pred = [0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
# print results
cm_lr = metrics.confusion_matrix(TestY, pred)
print("Confusion matrix")
print(pd.DataFrame(cm_lr))
report_lr = metrics.precision_recall_fscore_support(TestY, pred, average='binary')
print ("\nprecision = %0.2f, recall = %0.2f, F1 = %0.2f, accuracy = %0.2f\n" % \
(report_lr[0], report_lr[1], report_lr[2], metrics.accuracy_score(TestY, pred)))
and the output:
Confusion matrix
0 1
0 4 1
1 2 3
precision = 0.75, recall = 0.60, F1 = 0.67, accuracy = 0.70
Binary has a default definition of which class is the positive one (the class with the 1 label).
You can read the differences between all the average option in this link.

Gradient Descent Implementation in Python returns Nan

I am trying to implement gradient descent in python; the implementation works when I try it with training_set1 but it returns not a number(nan) when I try it training_set. Any idea why my code is broken?
from collections import namedtuple
TrainingInstance = namedtuple("TrainingInstance", ['X', 'Y'])
training_set1 = [TrainingInstance(0, 4), TrainingInstance(1, 7),
TrainingInstance(2, 7), TrainingInstance(3, 8),
TrainingInstance(8, 12)]
training_set = [TrainingInstance(60, 3.1), TrainingInstance(61, 3.6),
TrainingInstance(62, 3.8), TrainingInstance(63, 4),
TrainingInstance(65, 4.1)]
def grad_desc(x, x1):
# minimize a cost function of two variables using gradient descent
training_rate = 0.1
iterations = 5000
#while sqrd_error(x, x1) > 0.0000001:
while iterations > 0:
#print sqrd_error(x, x1)
x, x1 = x - (training_rate * deriv(x, x1)), x1 - (training_rate * deriv1(x, x1))
iterations -= 1
return x, x1
def sqrd_error(x, x1):
sum = 0.0
for inst in training_set:
sum += ((x + x1 * inst.X) - inst.Y)**2
return sum / (2.0 * len(training_set))
def deriv(x, x1):
sum = 0.0
for inst in training_set:
sum += ((x + x1 * inst.X) - inst.Y)
return sum / len(training_set)
def deriv1(x, x1):
sum = 0.0
for inst in training_set:
sum += ((x + x1 * inst.X) - inst.Y) * inst.X
return sum / len(training_set)
if __name__ == "__main__":
print grad_desc(2, 2)
Reduce training_rate so that the objective decreases at each iteration.
See Figure 6. in this paper: http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf

Resources