[英]ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,)
[英]x and y must have same first dimension, but have shapes (40,) and (1, 80) error
我收到錯誤 x 和 y 必須具有相同的第一維,但具有形狀 (40,) 和 (1, 80),我嘗試查找它並被告知使用 numpy.array 如下所示但它仍然無法正常工作。 我知道這是由於“- Stot”部分,但不確定該怎么做
import numpy
import matplotlib.pyplot as plt
import math
import scipy.special
kb = 1.380649*(10**-23)
Na = 20
Nb = 20
Ntot = int(Na + Nb)
qa = 0
qb = 41
Stot = 0
qaa = []
Saa = []
Sba = []
Stota = []
Snew = []
prob = 0
h = 1
pos = 0
suma = []
sumnew = []
#creates an array based on the value of qb starting from 0 to qb in increments of 1
for k in range (qa, qb-1):
qa = qa + 1
qb = qb - 1
qaa += [qa]
Sa = (qa + Na - 1) * (numpy.log(qa + Na - 1)) - (qa*numpy.log(qa)) - (Na*numpy.log(Na-1))
Sb = (qb + Nb - 1) * (numpy.log(qb + Nb - 1)) - (qb*numpy.log(qb)) - (Nb*numpy.log(Nb-1))
Stot = Sa + Sb + Stot
Saa += [Sa]
Sba += [Sb]
Prob = numpy.array([(Saa + Sba - Stot)])
plt.plot(qaa, Prob)
plt.title("Graph to show probability distribution of the macrostates, P(qa)")
plt.xlabel('qa')
plt.ylabel('Probability')
我不能確定 rhis 代碼。 該消息似乎來自 plot 命令,但是具有表達式“(Saa + Sba - Stot)”的行應該會觸發錯誤,因為您的 Saa 和 Sba 是列表,並且它們的附加結果是兩個列表的串聯. 列表減去浮點數(Stot 是浮點數)是不允許的操作如果要按成員添加列表的值,然后從每個項目中減去一個值,請在執行減法之前將每個列表轉換為數組(數組減去允許浮動)
然而,這個熵計算似乎有一個錯誤。 Stota 的列表是做什么用的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.