python-3.x - 如何修复 TypeError : G must be a 'd' matrix?

标签 python-3.x numpy optimization cvxopt cvxpy

目标:尝试通过优化过程运行玩具数据集。

我遇到了以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-2706eba23671> in <module>()
     50 # Solving the problem
     51 problem = cvxpy.Problem(cvxpy.Minimize(cost), constraints=constraints)
---> 52 problem.solve(solver=cvxpy.GLPK_MI)

D:\Anaconda3\lib\site-packages\cvxpy\problems\problem.py in solve(self, *args, **kwargs)
    193             return func(self, *args, **kwargs)
    194         else:
--> 195             return self._solve(*args, **kwargs)
    196 
    197     @classmethod

D:\Anaconda3\lib\site-packages\cvxpy\problems\problem.py in _solve(self, solver, ignore_dcp, warm_start, verbose, parallel, **kwargs)
    319             results_dict = solver.solve(objective, constraints,
    320                                         self._cached_data, warm_start, verbose,
--> 321                                         kwargs)
    322         # Presolve determined problem was unbounded or infeasible.
    323         else:

D:\Anaconda3\lib\site-packages\cvxpy\problems\solvers\glpk_mi_intf.py in solve(self, objective, constraints, cached_data, warm_start, verbose, solver_opts)
     97                                           data[s.B],
     98                                           set(data[s.INT_IDX]),
---> 99                                           set(data[s.BOOL_IDX]))
    100             results_dict = {}
    101             results_dict["status"] = results_tup[0]

TypeError: G must be a 'd' matrix

下面是我使用的代码:

from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import cvxpy

X, y = make_classification(n_samples=1000, n_features=20, n_classes=8,n_informative=5,
                           class_sep=0.2, random_state=2)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=1)

model = RandomForestClassifier(random_state=1)
model.fit(X_train, y_train)
test_probs = model.predict_proba(X_test)

#clipping so that we don't take log of 0 or 1
test_probs = np.clip(test_probs, 0.0001, 0.9999)

#turning into costs
model_costs = -np.log10(test_probs)

# Our allocation cannot exceed our supply
# 150 flyers, 80 pamphlets, 25 bumper stickers
supply = np.atleast_2d([150, 80, 25])

# Creating our cvxpy variable of assignments
selection = cvxpy.Bool(*test_probs.shape)

# Constant matrix that counts how many of each 
# material we sent to each constituent
TRANSFORMER = np.array([[1,0,0],
                        [0,1,0],
                        [0,0,1],
                        [1,1,0],
                        [1,0,1],
                        [0,1,1],
                        [1,1,1],
                        [0,0,0]])

supply_constraint = cvxpy.sum_entries(selection * TRANSFORMER, axis=0) <= supply

# We must make our choice per constituent
# remember that the last column is for "no materials"
feasibility_constraint = cvxpy.sum_entries(selection, axis=1) == 1
constraints = [supply_constraint, feasibility_constraint]

# Take the negative log of our probabilities to turn them into costs
cost = cvxpy.sum_entries(cvxpy.mul_elemwise(model_costs, selection))

# Solving the problem
problem = cvxpy.Problem(cvxpy.Minimize(cost), constraints=constraints)
problem.solve(solver=cvxpy.GLPK_MI)

如果更多上下文有帮助,我将继续 here .我确实找到了 this post在 SO 上,但无法理解我应该如何处理我的问题的当前状态。

最佳答案

尝试将 TRANSFORMER 转换为 float 组。

就其值(value)而言,该代码适用于 CVXPY v 1.0.11(经过一些小的修改,包括在下面):

from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import cvxpy

X, y = make_classification(n_samples=1000, n_features=20, n_classes=8,n_informative=5,
                           class_sep=0.2, random_state=2)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=1)

model = RandomForestClassifier(random_state=1)
model.fit(X_train, y_train)
test_probs = model.predict_proba(X_test)

#clipping so that we don't take log of 0 or 1
test_probs = np.clip(test_probs, 0.0001, 0.9999)

#turning into costs
model_costs = -np.log10(test_probs)

# Our allocation cannot exceed our supply
# 150 flyers, 80 pamphlets, 25 bumper stickers
supply = np.array([150, 80, 25])

# Creating our cvxpy variable of assignments
selection = cvxpy.Variable(shape=test_probs.shape, boolean=True)

# Constant matrix that counts how many of each
# material we sent to each constituent
TRANSFORMER = np.array([[1,0,0],
                        [0,1,0],
                        [0,0,1],
                        [1,1,0],
                        [1,0,1],
                        [0,1,1],
                        [1,1,1],
                        [0,0,0]])

supply_constraint = cvxpy.sum(selection * TRANSFORMER, axis=0) <= supply

# We must make our choice per constituent
# remember that the last column is for "no materials"
feasibility_constraint = cvxpy.sum(selection, axis=1) == 1
constraints = [supply_constraint, feasibility_constraint]

# Take the negative log of our probabilities to turn them into costs
cost = cvxpy.sum(cvxpy.multiply(model_costs, selection))

# Solving the problem
problem = cvxpy.Problem(cvxpy.Minimize(cost), constraints=constraints)
problem.solve(solver=cvxpy.GLPK_MI)
print(problem.value)
print(selection.value[0:5])
print(np.dot(selection.value, TRANSFORMER).sum(axis=0))

关于python-3.x - 如何修复 TypeError : G must be a 'd' matrix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54171455/

相关文章:

python - 导入 numpy 而不安装

python - Numpy - 用 NaN 替换数字

python - 如何选择带有边界框的 numpy 二维数组中唯一元素的所有位置?

javascript - 一个模块 : OPTIMIZER FAILED: InternalError: missing name after . 运算符(operator)的 Dojo 构建失败

python - Surface.scroll() 是在 Pygame 中实现可移动 2D 视点的正确方法吗?

performance - 为什么这个 Java 代码有这个年龄验证日期比较?

python-3.x - 如何在 VS Code 终端中激活 python 虚拟环境?

python - 如果python中的语句不打印

Python 3.x - 计算绘图&&计算坐标系中可能的正方形

python - 如何有条件地更改数组值