TypeError:只能将length-1数组转换为Python标量问题

时间:2016-01-27 16:50:03

标签: python numpy

我有以下Python脚本:

sum

我有两个问题:

  • 我没有得到Traceback (most recent call last): File "myFunc.py", line 40, in <module> plt.plot(x, myFunc(scores).T, linewidth=2) File "myFunc.py", line 30, in myFunc return math.exp(x)/sum TypeError: only length-1 arrays can be converted to Python scalars 的预期输出,这导致总结果错误
  • 当我运行脚本时,我得到以下输出:

    string fileUrl = "/sites/dev1/MyTool/Lists/TicketsList/1_.000";
    FileVersionCollection fileVersionCollection;
    File file = clientcontext.Web.GetFileByServerRelativeUrl(fileUrl);
    fileVersionCollection = file.Versions;
    clientcontext.Load(file);
    clientcontext.Load(fileVersionCollection);
    clientcontext.ExecuteQuery();
    

在开头省略数字值,为什么我会收到此错误?我该如何解决?

感谢。

1 个答案:

答案 0 :(得分:0)

看起来你想要这样的东西:

import numpy as np

def func(scores):
    y = 0.5 * scores + 0.2
    return scores / np.sum(np.exp(y))

scores = np.array([3.0, 1.0, 0.2])

for res in func(scores):
    print(res)

输出:

0.339460254992
0.113153418331
0.0226306836661

现在你也可以绘制它:

from matplotlib import pyplot as plt
plt.plot(func(scores).T, linewidth=2)

enter image description here