ccr模型在python运行出错

ccr模型在pycharm里运行出错

import numpy as np
from scipy.optimize import fmin_slsqp
class DEA(object):
def init(self, inputs, outputs):

    # supplied data
    self.inputs = inputs
    self.outputs = outputs

    # parameters
    self.n = inputs.shape[0]
    self.m = inputs.shape[1]
    self.r = outputs.shape[1]

    # iterators
    self.unit_ = range(self.n)
    self.input_ = range(self.m)
    self.output_ = range(self.r)

    # result arrays
    self.output_w = np.zeros((self.r, 1), dtype=np.float)  # output weights
    self.input_w = np.zeros((self.m, 1), dtype=np.float)  # input weights
    self.lambdas = np.zeros((self.n, 1), dtype=np.float)  # unit efficiencies
    self.efficiency = np.zeros_like(self.lambdas)  # thetas
def __efficiency(self, unit):
    # compute efficiency
    denominator = np.dot(self.inputs, self.input_w)
    numerator = np.dot(self.outputs, self.output_w)

    return (numerator/denominator)[unit]
def __target(self, x, unit):

    in_w, out_w, lambdas = x[:self.m], x[self.m:(self.m+self.r)], x[(self.m+self.r):]  # unroll the weights
    denominator = np.dot(self.inputs[unit], in_w)
    numerator = np.dot(self.outputs[unit], out_w)

    return numerator/denominator

def     __constraints(self, x, unit):

    in_w, out_w, lambdas = x[:self.m], x[self.m:(self.m+self.r)], x[(self.m+self.r):]  # unroll the weights
    constr = []  # init the constraint array

    # for each input, lambdas with inputs
    for input in self.input_:
        t = self.__target(x, unit)
        lhs = np.dot(self.inputs[:, input], lambdas)
        cons = t*self.inputs[unit, input] - lhs
        constr.append(cons)

    # for each output, lambdas with outputs
    for output in self.output_:
        lhs = np.dot(self.outputs[:, output], lambdas)
        cons = lhs - self.outputs[unit, output]
        constr.append(cons)

    # for each unit
    for u in self.unit_:
        constr.append(lambdas[u])

    return np.array(constr)

def __optimize(self):

    d0 = self.m + self.r + self.n
    # iterate over units
    for unit in self.unit_:
        # weights
        x0 = np.random.rand(d0) - 0.5
        x0 = fmin_slsqp(self.__target, x0, f_ieqcons=self.__constraints, args=(unit,))
        # unroll weights
        self.input_w, self.output_w, self.lambdas = x0[:self.m], x0[self.m:(self.m+self.r)], x0[(self.m+self.r):]
        self.efficiency[unit] = self.__efficiency(unit)

def fit(self):
    self.__optimize()  # optimize
    return self.efficiency

if name == "main":
X = np.array([
[98, 70, 20, 5, 3, 2813.663, 1153.918, 624.743, 680.912, 294.733],
[20, 14, 6, 0, 0, 937.233, 237.621, 132.123, 505.975, 7.658],
[182, 153, 21, 6, 2, 4819.629, 2565.148, 880.181, 4489.623, 1328.115],
[142, 107, 28, 2, 4, 5291.493, 3330.615, 1585.342, 3090.903, 270.75],
[60, 49, 10, 1, 0, 1444.819, 480.443, 381.222, 694.645, 0],
[57, 52, 4, 1, 0, 1054.138, 784.174, 279.986, 895.603, 227.309],
[35, 24, 10, 0, 1, 1462.317, 683.232, 514.88, 422.395, 358.263],
[338, 287, 8, 3, 9, 1061.31, 455.393, 326.003, 857.05, 319.27],
[25, 14, 9, 2, 0, 291.5, 257.9, 194.7, 88.41, 8.3],
[12, 10, 2, 0, 0, 129.54, 125.29, 82.85, 47.47, 1.5],
[249, 212, 24, 7, 5, 5438.59, 2959.507, 2538.423, 3339.189, 828.32],
[30, 10, 16, 3, 1, 1241.524, 872.37, 465.732, 939.298, 497.926],
[359, 290, 44, 7, 13, 6102.468, 6013.099, 3981.399, 4728.463, 2752.135],
[58, 50, 7, 0, 1, 398.648, 319.305, 248.349, 712.425, 367.242],
[7, 4, 1, 1, 1, 1330.762, 597.213, 307.739, 456.165, 11.235],
[65, 50, 13, 2, 0, 1627.7, 1225, 494.9, 683.38, 109.1],
[3, 3, 0, 0, 0, 113.897, 81.222, 28.744, 175.495, 36.724],
[115, 89, 23, 3, 0, 2003.6, 1696.6, 858.24, 966.956, 277.94],
[148, 111, 31, 3, 3, 3139.874, 1420.201, 935.829, 3070.125, 1414.953],
[160, 141, 12, 2, 5, 3893.53, 2902.32, 1531.9, 4249.71, 881.9],
[214, 190, 17, 3, 4, 4456.622, 1113.632, 727.416, 2007.818, 1445.898],
[258, 192, 45, 10, 11, 6150.663, 4680.148, 2694.345, 7333.857, 2673.32],
[10, 9, 1, 0, 0, 36.42, 35.5, 27.44, 68.999, 0],
[115, 96, 15, 3, 1, 1096.283, 775.568, 542.627, 1480.879, 619.47],
[113, 81, 27, 2, 2, 1933.083, 1611.58, 922.653, 1224.558, 652.709],
[53, 41, 11, 1, 0, 1420.162, 1236.44, 464.255, 1247.3, 25.848],
[2, 1, 0, 0, 1, 343.01, 248.48, 123.453, 921.55, 14.2],
[1, 0, 1, 0, 0, 17.4, 17.3, 10, 75.06, 0],
[11, 7, 4, 0, 0, 410.817, 328.546, 204.041, 1505.44, 0],
[131, 88, 34, 6, 3, 6474.414, 3550.158, 1405.097, 4257.963, 755.465],
[71, 47, 19, 2, 3, 5121.559, 1253.654, 320.297, 2088.88, 3488.198],
[19, 144, 1, 0, 744.031, 464.449, 211.294, 529.989, 98.545],
[41, 26, 13, 1, 1, 611.458, 448.429, 279.5, 417.06, 277.588],
[15, 10, 5, 0, 0, 214.62, 210.446, 130.572, 138.806, 0],
[61, 49, 10, 0, 2, 1449.002, 850.552, 474.658, 1173.315, 194.65],
[67, 52, 12, 1, 2, 990.859, 807.847, 564.597, 694.858, 69.903],
[167, 128, 32, 6, 1, 1806.414, 1215.5, 733.4, 1747.941, 396.988],
[169, 131, 27, 3, 8, 7088.504, 3825.229, 1465.402, 4694.32, 1008.187],
[486, 366, 92, 13, 12, 9805.23, 7159.638, 3817.499, 9628.701, 2873.7],
[23, 18, 5, 0, 0, 414.9, 272.9, 227.7, 327.3, 0],
[45, 19, 15, 6, 4, 4306.24, 3362.18, 1819.5, 3401.1, 2948.66],
[83, 67, 13, 2, 1, 2410.567, 1019.07, 375.9, 982, 99.876],
[31, 25, 5, 0, 1, 1253.473, 643.31, 441.531, 1028.409, 335.646],
[13, 8, 4, 1, 0, 661.78, 479.09, 69.53, 371.127, 0],
[496, 395, 76, 12, 13, 13214.027, 7423.613, 2997.624, 11155.588, 4710.909],
[272, 222, 45, 1, 4, 3772.735, 2859.116, 1386.977, 2674.771, 450.66],
[13, 8, 5, 0, 0, 1931.602, 690.345, 534.015, 1152.931, 659.09],
[44, 34, 8, 0, 2, 1949.703, 1141.841, 365.223, 1390.055, 344.879],
[61, 49, 10, 2, 0, 1879.599, 1152.59, 403.308, 1498.675, 168.151],
[100, 75, 20, 2, 3, 2859.136, 2168.775, 687.426, 2491.187, 546.18],
])
y = np.array([
[1062.115, 381.203],
[536.215, 30.24],
[5468.945, 979.322],
[3786.528, 695.625],
[1044.244, 349.599],
[1033.503, 137.9],
[760.329, 337.934],
[932.952, 75.902],
[215.042, 26.632],
[54.66, 7.19],
[3473.136, 133.947],
[964.977, 25.679],
[4810.699, 82.236],
[827.077, 114.652],
[206.404, -249.761],
[726.5, 43.12],
[187.419, 11.924],
[1025.186, 58.23],
[3253.042, 182.917],
[4341.71, 92],
[2076.054, 68.236],
7076.454, 257.403,
[68.999, 0],
[1816.756, 335.877],
[1409.22, 184.662],
[1254.1, 6.8],
[915.397, -6.153],
[77.06, 2],
[1559.663, 54.223],
[4615.333, 357.37],
[2174.207, 85.327],
[540.475, 10.486],
[421.86, 4.8],
[176.966, 38.16],
[1214.115, 40.8],
[727.539, 32.681],
[1757.872, 9.931],
[4679.125, -15.195],
[9978.507, 349.806],
[322.3, -5],
[3556.5, 155.4],
[1146.76, 164.76],
[1081.108, 52.699],
[418.89, 47.763],
[12769.922, 1614.334],
[3069.106, 394.335],
[854.329, -298.602],
[1484.972, 94.917],
[1739.584, 240.909],
[2546.891, 55.704],
])
dea = DEA(X, y)
rs = dea.fit()
print(rs)

img