【问题标题】:Python changing arrays (numpy) using the map commandPython 使用 map 命令更改数组(numpy)
【发布时间】:2015-03-27 20:44:12
【问题描述】:

我从文件中导入数据以制作散点图。来自 csv 文件的数据并不完全是我要绘制的数据,至于第二个 col,我希望 col 中先前条目的总和成为我的 y 轴。

我无法使用 map 命令从数组转换为对数据求和。当我收到错误消息时:

TypeError: 'numpy.float64' 对象不可迭代

from matplotlib import pyplot
import numpy

x,y = numpy.loadtxt ('LAX_booths_data_set_1.csv',
                  unpack = True,
                  delimiter = ',')
x2,y2 = numpy.loadtxt ('LAX_booths_data_set_2.csv',
                  unpack = True,
                  delimiter = ',')


newy = map(sum, y)
newy2 = map(sum, y2)


pyplot.plot(x,newy)
pyplot.plot(x2,newy2)

pyplot.title('Comparing Different Booth Schedules')
pyplot.ylabel('Number of Booths (Total)')
pyplot.xlabel('Time (Minutes)')

pyplot.show()

【问题讨论】:

  • 您是否打印出y 和/或y2 以查看它们与您的预期有何不同? (顺便说一句:这里没有必要使用map,因为numpy 有一个更简单更快的sum 方法,但不妨从你所在的位置开始。)[我在这里假设它是map这导致了问题 - 您没有发布完整的回溯。]
  • 是的 x 和 y 的内容示例会有所帮助
  • 完整回溯:文件“C:\Users\fkrueg1\Dropbox\forest_python_test\Graphnew.py”,第 12 行,在 newy = map(sum, y) TypeError: 'numpy.float64 ' 对象不可迭代
  • x 是 [ 30. 100. 200. 400. 600. 900. 1200. 1400. 1450.] y 是 [ 10. -5. 20.-5。 50。-20。 30.-20。 20.]
  • 你希望 newy 是 y 的累积和吗? [10., 5., 25., 20. ... ]

标签: python arrays dictionary plot scatter


【解决方案1】:

所以 map 将函数独立地应用于数组的所有元素,我认为你想使用 numpy.cumsum

newy = numpy.cumsum(y)

【讨论】:

    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2017-03-04
    • 2018-12-12
    相关资源
    最近更新 更多