无法检查python中数据的线性可分性

时间:2017-08-13 07:50:38

标签: python machine-learning scipy linear-programming

我有训练数据集。我想检查一下是否可以线性分离。我正在为此目的使用线性编程并使用此site中提到的等式。数据有11列,最后一列(第11列)的值(0,1)代表结果值。以下是我正在使用的代码:

    import os
    import math
    import traceback
    import numpy as np
    import scipy.io as sio
    from scipy.optimize import linprog
    os.system('cls')
    dic  = sio.loadmat('train.mat')
    M = dic.get('M')
    #s0 = np.zeros((1,11),np.int64)
    #s1 = np.zeros((1,11),np.int64)
    if M!=None:
        print(len(M))
        print(len(M[0]))
        m0 = M[M[:,10]==0]
        m1 = M[M[:,10]==1]
        m0 = m0[:,0:10]
        m1 = m1[:,0:10]
        m0 = -1*m0
        o0 = np.ones((len(m0),1),np.int64)
        m0 = np.hstack((m0,o0))
        o1 = -1*np.ones((len(m1),1),np.int64)
        m1 = np.hstack((m1,o1))
        del M
        del dic
        print('del variables')
        A = np.vstack((m1,m0))
        print(A)
        b = -1*np.ones((len(A),1),np.int64)
        print(b)
        c = np.zeros((1,11),np.int64)                     
        print('size c =[',len(c),',',len(c[0]),']')
        print('size A =[',len(A),',',len(A[0]),']')
        print('size b =[',len(b),',',len(b[0]),']')
        res = linprog(c, A_ub=A, b_ub=b, bounds=(-math.inf, math.inf),options={"disp": True})
        print(res)

运行上面的代码时出现以下错误。

res = linprog(c, A_ub=A, b_ub=b, bounds=(-math.inf, math.inf),options={"disp": True})
  File "E:\Python\lib\site-packages\scipy\optimize\_linprog.py", line 993, in linprog
    bounds=bounds, callback=callback, **options)
  File "E:\Python\lib\site-packages\scipy\optimize\_linprog.py", line 594, in _linprog_simplex
    cc = np.concatenate([np.array([0]), cc])
ValueError: all the input arrays must have same number of dimensions

所以,正如例外中所述,我设置错误的数组值。从我所理解的,c是找到超平面的种子值,b是方程的RHS。那么,我做错了什么?

0 个答案:

没有答案