错误:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()

时间:2017-07-25 20:05:32

标签: python numpy

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate

m_c,p_s,complete = np.loadtxt('File1.txt',usecols=(1,0,2),unpack=True)
p_d,m_d = np.loadtxt('File2.txt',usecols=(2,3),unpack=True)
p_c,m_c = np.loadtxt('File3.txt',usecols=(1,2),unpack=True)

def function_oc(m_c,p_c,complete,min,max):
    average = 0
    comp = []
    x = 0
    while x<8000:
        if p_c[x]<50 and m_c[x]>=min and m_c[x]<=max:
            comp.append(complete[x])
        x+=1
    average = sum(comp)/len(comp)
    return average

average1 = function_oc(m_c,p_c,complete,3,10) 
average2 = function_oc(m_c,p_c,complete,10,30)
average3 = function_oc(m_c,p_c,complete,30,100)
average4 = function_oc(m_c,p_c,complete,100,300)
average5 = function_oc(m_c,p_C,complete,300,1000)

def function_pc(m_d,p_d,m_c,p_c,complete):
    f= interpolate.interp2d(m_c,p_c,complete)
    comp_d = f(p_d,m_d)
    return comp_d

comp_d = function_pc(m_d,p_d,m_c,p_c,complete)

def function_d(p_d,m_d,min,max):
    d = 0
    i = 0
    while i<33:
        if p_d[i]<50 and m_d[i]>=min and m_d[i]<=max:
            d+=1
        i+=1
    return d

d1 = function_d(p_d,m_d,3,10)
d2 = function_d(p_d,m_d,10,30)
d3 = function_d(p_d,ms_d,30,100)
d4 = function_d(p_d,m_d,100,300)
d5 = function_d(p_d,m_d,300,1000)

def function_c(p_c,m_c,min,max):
    c = 0
    y = 0
    while y<12:
        if p_c[y]<50 and m_C[y]>=min and m_C[y]<=max:
            c+=1
        y+=1
    return c

c1 = function_c(p_c,m_c,3,10)
c2 = function_c(p_c,m_c,10,30)
c3 = function_c(p_c,m_c,30,100)
c4 = function_c(p_C,m_c,100,300)
c5 = function_c(p_C,m_c,300,1000)

####Missed planets in each bin####
def function_m(c_d,p_d,m_d,min,max):
    m=0
    for mi in range(len(comp_d)):
        if p_d[mi]<50 and m_d[mi]>=min and ms_d[mi]<=max:
            m += 1/comp_d[mi] - 1
    return m

m1 = function_m(comp_d,p_d,m_d,3,10)
m2 = function_m(comp_d,p_dd,m_d,10,30)
m3 = function_m(comp_d,p_d,m_d,30,100)
m4 = function_m(comp_d,p_d,m_d,100,300)
m5 = function_m(comp_d,p_d,m_d,300,1000)

occ1 = (d1+c1+m1)/average1
occ2 = (d2+c2+m2)/average2
occ3 = (d3+c3+m3)/average3
occ4 = (d4+c4+m4)/average4
occ5 = (d5+c5+m5)/average5

N = 5
dp = (d1, d2, d3, d4, d5)
cp = (c1, c2, c3, c4, c5)
mp = (m1, m2, m3, m4, m5)
planets = (dp, cp, mp)
ind = np.arange(N)
width = 0.9
p1 = plt.bar(ind, dp, width, color='red')
p2 = plt.bar(ind, cp, width, color='blue', bottom=dp)
p3 = plt.bar(ind, mp, width, color='yellow', bottom=[i+j for i,j in zip(dp, cp)])
plt.legend((p1[0], p2[0], p3[0]), ('DP', 'CP', 'MP'))
plt.show()

我不明白为什么我的代码会出现此错误:

  

ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()

导致此问题的代码中的行是:

p3 = plt.bar(ind, mp, width, color='yellow', bottom=[i+j for i,j in zip(dp, cp)])

3 个答案:

答案 0 :(得分:1)

您收到此错误是因为您尝试使用plt.bar绘制数组与点的关系。即您正尝试绘制ind[0]dp[0] = dp1,这是一个数组。 如果要执行此操作,则应对数组中的每个点使用plt.bar

您应该plt.bardp中每个元素中的每个元素使用dp[i][j],因此/* Redirect author page to BuddyPress page */ function my_page_template_redirect() { /** Detect if author archive is being requested and redirect to bp user profile page */ if( is_author() ) { global $wp_query; if(isset($wp_query->query_vars['author'])) { $userID = urldecode($wp_query->query_vars['author']); } $url = bp_core_get_user_domain($userID); wp_redirect( $url ); exit(); } add_action( 'template_redirect', 'my_page_template_redirect' );

答案 1 :(得分:1)

当您执行以下操作时会出现此错误:

if a < b:
ab是数组时

由于我没有您的输入文本文件(并且您没有提供完整的错误跟踪),我无法追踪这可能是什么,但您有很多if个可能的语句罪魁祸首。

问题是在数组的情况下a < b解析为布尔值数组,例如,

array([True, True, False]) 

if无法解析。 np.anynp.all将根据我的示例解析布尔数组,True np.anyFalse np.all

答案 2 :(得分:0)

您必须使用:

np.logical_or(a,b)
np.logical_and(a,b)

对于np数组。对我来说真的很好!