解决importError :numpy.core.multiarrary failed to import

(一)报错情况如下

import cvxpy as cp
from numpy import array

c = array([40, 90])  # 定义目标向量
a = array([[9, 7], [-7, -20]])  # 定义约束矩阵
b = array([56, -70])  # 定义约束条件的右边向量
x = cp.Variable(2, integer=True)  # 定义两个整数决策变量
obj = cp.Minimize(c @ x)  # 构造目标函数
cons = [a @ x <= b, x >= 0]  # 构造约束条件
prob = cp.Problem(obj, cons)  # 构建问题模型
prob.solve(solver='GLPK_MI', verbose=True)  # 求解问题
print("最优值为:", prob.value)
print("最优解为:\n", x.value)

报错如下:
importError :numpy.core.multiarrary failed to import

(二)解决方法

2.1 方法一:重新安装numpy

importError :numpy.core.multiarrary failed to import:属于numpy版本出错。
可能是pip install cvxpy···.whl文件时,扰乱了numpy的版本。
解决步骤:

  • 进入Anaconda promp 命令中。

  • 卸载numpy(pip uninstall numpy)

  • 重新安装numpy(pip install numpy)即可。
    但是可能会导致你的tensoflow版本与现在numpy版本埠匹配,所以之后使用tf时,还需要重新安装对应版本的numpy。不过问题不大,这些安装都很简单。(我在从新安装numpy就出现了这种情况,如下图。)
    在这里插入图片描述
    这样cvxpy就可以成功运行了,上述代码运行结果如下:

      D:\ProgramData\Anaconda3\python.exe C:/Users/Administrator/PycharmProjects/pythonProject/数学建模/线性规划/线性规划3.py
      ===============================================================================
                                           CVXPY                                     
                                          v1.1.13                                    
      ===============================================================================
      (CVXPY) Jul 31 12:36:41 PM: Your problem has 2 variables, 2 constraints, and 0 parameters.
      (CVXPY) Jul 31 12:36:41 PM: It is compliant with the following grammars: DCP, DQCP
      (CVXPY) Jul 31 12:36:41 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
      (CVXPY) Jul 31 12:36:41 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
      -------------------------------------------------------------------------------
                                        Compilation                                  
      -------------------------------------------------------------------------------
      (CVXPY) Jul 31 12:36:41 PM: Compiling problem (target solver=GLPK_MI).
      (CVXPY) Jul 31 12:36:41 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> GLPK_MI
      (CVXPY) Jul 31 12:36:41 PM: Applying reduction Dcp2Cone
      (CVXPY) Jul 31 12:36:41 PM: Applying reduction CvxAttr2Constr
      (CVXPY) Jul 31 12:36:41 PM: Applying reduction ConeMatrixStuffing
      (CVXPY) Jul 31 12:36:41 PM: Applying reduction GLPK_MI
      (CVXPY) Jul 31 12:36:41 PM: Finished problem compilation (took 3.001e-03 seconds).
      -------------------------------------------------------------------------------
                                      Numerical solver                               
      -------------------------------------------------------------------------------
      (CVXPY) Jul 31 12:36:41 PM: Invoking solver GLPK_MI  to obtain a solution.
            0: obj =   2.700000000e+02 inf =   6.250e-01 (1)
            1: obj =   3.150000000e+02 inf =   0.000e+00 (0)
      Long-step dual simplex will be used
      +     1: mip =     not found yet >=              -inf        (1; 0)
      Solution found by heuristic: 360
      +     2: >>>>>   3.500000000e+02 >=   3.500000000e+02   0.0% (1; 0)
      +     2: mip =   3.500000000e+02 >=     tree is empty   0.0% (0; 1)
      -------------------------------------------------------------------------------
                                          Summary                                    
      -------------------------------------------------------------------------------
      (CVXPY) Jul 31 12:36:41 PM: Problem status: optimal
      (CVXPY) Jul 31 12:36:41 PM: Optimal value: 3.500e+02
      (CVXPY) Jul 31 12:36:41 PM: Compilation took 3.001e-03 seconds
      (CVXPY) Jul 31 12:36:41 PM: Solver (including time spent in interface) took 0.000e+00 seconds
      最优值为: 350.0
      最优解为:
       [2. 3.]
    

2.2 方法二:重新安装cvxpy

2.2.1方法一:安装cvxpy

Python下载并安装第三方库(cvxpy)

2.2.2方法二:安装cvxpy

    1. 进入Anaconda promp 命令中,pip uninstall cvxpy,如果卸载不了,直接进去D:\ProgramData\Anaconda3\Lib\site-packages中找到cvxpy文件夹删除。(注意:你们要找到自己安装路径,我用的时Anaconda所以是上面的路径)
    1. 输入两行命令
	 conda install -c conda-forge lapacl
	 conda install -c cvxgrp cvxpy

Guess you like

Origin blog.csdn.net/weixin_54546190/article/details/119274253