我试图找出每天最高的价格(第5列)(第1列)发生在什么时间(第2列),然后直观地绘制这些时间。
我相信第一步是查看日期,然后找到第5列的最大值。这是否可以通过for循环来完成?如果有两个相同的东西会发生什么?
接下来,我想直观地看到在文件中加载更多天后最高价格发生的时间(历史记录)。
当我尝试找到最大值时,出现错误。
使用以下代码成功加载数据:
import numpy as np
my_data = np.genfromtxt('downloads/USDJPY.csv', delimiter=",", dtype=None, names=True, encoding='utf-8')
print (my_data)
这是输出...我需要找到最大值(第5列),这只是一天的数据。
[('6/3/19', '7:05', 'USD/JPY', 108.37 , 108.37 , 108.345, 108.345)
('6/3/19', '7:10', 'USD/JPY', 108.345, 108.345, 108.325, 108.325)
('6/3/19', '7:15', 'USD/JPY', 108.33 , 108.36 , 108.33 , 108.34 )
('6/3/19', '7:20', 'USD/JPY', 108.335, 108.335, 108.295, 108.305)
('6/3/19', '7:25', 'USD/JPY', 108.305, 108.305, 108.27 , 108.305)
('6/3/19', '7:30', 'USD/JPY', 108.3 , 108.3 , 108.25 , 108.26 )
('6/3/19', '7:35', 'USD/JPY', 108.265, 108.295, 108.265, 108.29 )
('6/3/19', '7:40', 'USD/JPY', 108.275, 108.29 , 108.25 , 108.29 )
('6/3/19', '7:45', 'USD/JPY', 108.285, 108.29 , 108.275, 108.29 )
('6/3/19', '7:50', 'USD/JPY', 108.295, 108.35 , 108.295, 108.35 )
('6/3/19', '7:55', 'USD/JPY', 108.355, 108.355, 108.325, 108.33 )
('6/3/19', '8:00', 'USD/JPY', 108.335, 108.36 , 108.325, 108.35 )
('6/3/19', '8:05', 'USD/JPY', 108.345, 108.375, 108.32 , 108.37 )
('6/3/19', '8:10', 'USD/JPY', 108.375, 108.38 , 108.365, 108.365)
('6/3/19', '8:15', 'USD/JPY', 108.365, 108.37 , 108.33 , 108.33 )
('6/3/19', '8:20', 'USD/JPY', 108.335, 108.345, 108.33 , 108.345)
('6/3/19', '8:25', 'USD/JPY', 108.35 , 108.38 , 108.35 , 108.38 )
('6/3/19', '8:30', 'USD/JPY', 108.37 , 108.39 , 108.37 , 108.38 )
('6/3/19', '8:35', 'USD/JPY', 108.375, 108.435, 108.37 , 108.42 )
('6/3/19', '8:40', 'USD/JPY', 108.42 , 108.425, 108.4 , 108.405)
('6/3/19', '8:45', 'USD/JPY', 108.41 , 108.415, 108.35 , 108.355)
('6/3/19', '8:50', 'USD/JPY', 108.355, 108.36 , 108.3 , 108.325)
('6/3/19', '8:55', 'USD/JPY', 108.32 , 108.33 , 108.265, 108.27 )
('6/3/19', '9:00', 'USD/JPY', 108.27 , 108.29 , 108.25 , 108.265)
('6/3/19', '9:05', 'USD/JPY', 108.22 , 108.34 , 108.195, 108.27 )
('6/3/19', '9:10', 'USD/JPY', 108.27 , 108.365, 108.25 , 108.34 )
('6/3/19', '9:15', 'USD/JPY', 108.33 , 108.355, 108.3 , 108.32 )
('6/3/19', '9:20', 'USD/JPY', 108.31 , 108.33 , 108.29 , 108.33 )
('6/3/19', '9:25', 'USD/JPY', 108.325, 108.33 , 108.315, 108.325)
('6/3/19', '9:30', 'USD/JPY', 108.335, 108.345, 108.32 , 108.345)
('6/3/19', '9:35', 'USD/JPY', 108.345, 108.345, 108.325, 108.33 )
('6/3/19', '9:40', 'USD/JPY', 108.34 , 108.37 , 108.33 , 108.355)
('6/3/19', '9:45', 'USD/JPY', 108.355, 108.4 , 108.345, 108.395)
('6/3/19', '9:50', 'USD/JPY', 108.39 , 108.41 , 108.38 , 108.385)
('6/3/19', '9:55', 'USD/JPY', 108.385, 108.385, 108.35 , 108.35 )
('6/3/19', '10:00', 'USD/JPY', 108.355, 108.39 , 108.355, 108.375)
('6/3/19', '10:05', 'USD/JPY', 108.37 , 108.41 , 108.36 , 108.405)
('6/3/19', '10:10', 'USD/JPY', 108.4 , 108.405, 108.37 , 108.37 )
('6/3/19', '10:15', 'USD/JPY', 108.375, 108.375, 108.35 , 108.36 )
('6/3/19', '10:20', 'USD/JPY', 108.36 , 108.37 , 108.355, 108.37 )
('6/3/19', '10:25', 'USD/JPY', 108.37 , 108.425, 108.37 , 108.41 )
('6/3/19', '10:30', 'USD/JPY', 108.405, 108.42 , 108.395, 108.405)
('6/3/19', '10:35', 'USD/JPY', 108.405, 108.435, 108.405, 108.415)
('6/3/19', '10:40', 'USD/JPY', 108.405, 108.405, 108.38 , 108.405)
('6/3/19', '10:45', 'USD/JPY', 108.4 , 108.425, 108.395, 108.415)
('6/3/19', '10:50', 'USD/JPY', 108.42 , 108.445, 108.4 , 108.41 )
('6/3/19', '10:55', 'USD/JPY', 108.4 , 108.415, 108.4 , 108.405)
('6/3/19', '11:00', 'USD/JPY', 108.395, 108.395, 108.38 , 108.39 )
('6/3/19', '11:05', 'USD/JPY', 108.39 , 108.41 , 108.39 , 108.39 )]
我已经尝试过这样做以找到最大值并得到一个错误。
import numpy as np
my_data = np.genfromtxt('downloads/USDJPY.csv', delimiter=",", dtype=None, names=True, encoding='utf-8')
High = my_data.max[:4]
print (High)
我希望输出为High = 108.435,并且发生在8:35。
找到最高点后,如何将其传递给时间档?
我也会做低谷的。
答案 0 :(得分:0)
如果我正确理解了您的问题,则可以通过查看np数组并在第5列中找到最大值的行号来解决此问题,然后在对应行的第1列中获取值。
loc_max = my_data[:,4].argmax() #gives row of the max in col 5
time_max = my_data[loc_max,0]
但由于定界符,您似乎在导入时遇到了问题。如果您能够按照问题中所述将数据正确地导入到np数组中,则此解决方案将起作用。
答案 1 :(得分:0)
我可以使用以下方法重新创建数组的一部分:
In [210]: data = np.array([('6/3/19', '7:05', 'USD/JPY', 108.37 , 108.37 , 108.345, 108.345),
...: ('6/3/19', '7:10', 'USD/JPY', 108.345, 108.345, 108.325, 108.325),
...: ('6/3/19', '7:15', 'USD/JPY', 108.33 , 108.36 , 108.33 , 108.34 ),
...: ('6/3/19', '7:20', 'USD/JPY', 108.335, 108.335, 108.295, 108.305),
...: ('6/3/19', '7:25', 'USD/JPY', 108.305, 108.305, 108.27 , 108.305),
...: ('6/3/19', '7:30', 'USD/JPY', 108.3 , 108.3 , 108.25 , 108.26 )],
...: dtype='U7,U4,U10,f,f,f,f')
...:
...:
...:
In [211]: data
Out[211]:
array([('6/3/19', '7:05', 'USD/JPY', 108.37 , 108.37 , 108.345, 108.345),
('6/3/19', '7:10', 'USD/JPY', 108.345, 108.345, 108.325, 108.325),
('6/3/19', '7:15', 'USD/JPY', 108.33 , 108.36 , 108.33 , 108.34 ),
('6/3/19', '7:20', 'USD/JPY', 108.335, 108.335, 108.295, 108.305),
('6/3/19', '7:25', 'USD/JPY', 108.305, 108.305, 108.27 , 108.305),
('6/3/19', '7:30', 'USD/JPY', 108.3 , 108.3 , 108.25 , 108.26 )],
dtype=[('f0', '<U7'), ('f1', '<U4'), ('f2', '<U10'), ('f3', '<f4'), ('f4', '<f4'), ('f5', '<f4'), ('f6', '<f4')])
这是一个复合dtype-具有7个字段
In [212]: data.dtype
Out[212]: dtype([('f0', '<U7'), ('f1', '<U4'), ('f2', '<U10'), ('f3', '<f4'), ('f4', '<f4'), ('f5', '<f4'), ('f6', '<f4')])
字段按名称访问。数字访问行,记录:
In [213]: data['f4']
Out[213]:
array([108.37 , 108.345, 108.36 , 108.335, 108.305, 108.3 ],
dtype=float32)
最大值及其位置是:
In [214]: np.argmax(data['f4'])
Out[214]: 0
In [215]: np.max(data['f4'])
Out[215]: 108.37