Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase iterations for scipy.optimize.linprog function in python?

I am trying to check if the train data is linearly separable or not. For that I am using the following code.

try:
        import os
        import random
        import traceback
        import numpy as np
        import scipy.io as sio
        from scipy.optimize import linprog
        os.system('cls')
        dicA  = sio.loadmat('A.mat')
        A = dicA.get('A')
        lengthA = int(len(A)/1000)
        aRange = range(0,lengthA)
        selectedIndexes = random.sample(aRange,lengthA)
        A1 = A[selectedIndexes]
        del A
        b = -1*np.ones(len(A1),np.int64)
        c = np.zeros(11,np.int64)
        del dicA
        res = linprog(c, A_ub=A1, b_ub=b, bounds=(-float('inf'), float('inf')),options={"disp": True})
        print(res)
except:
        print('exception')
        tb = traceback.format_exc()
        print(tb)
finally:

        print('reached finally')

I am using the equation mentioned at this link. I get following output when I run the script.

Iteration limit reached.
     fun: -0.0
 message: 'Iteration limit reached.'
     nit: 1000
  status: 1
 success: False
       x: nan
reached finally

So, does the iteration limit reached means that data is not linearly separable, if not then how do I increase the limit.

like image 394
V K Avatar asked Aug 24 '17 04:08

V K


1 Answers

I think you can add maxiter to options.

options = {"disp": True, "maxiter": 5000}
res = linprog(c, A_ub=A1, b_ub=b, bounds=(None, None), options=options)

According with documentation you can use None in bounds to specify no bounds in the given direction.

like image 174
lcastillov Avatar answered Oct 20 '22 15:10

lcastillov