关于numpy中的函数return中加入字符串类型数据后,小数点精度变化
Posted master-road
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于numpy中的函数return中加入字符串类型数据后,小数点精度变化相关的知识,希望对你有一定的参考价值。
weekdays.py
import numpy as np
from datetime import datetime
def datestr2num(s):
return datetime.strptime(s.decode(‘ascii‘), "%d-%m-%Y").date().weekday()
dates, open, high, low, close=np.loadtxt(‘data.csv‘, dtype=float, delimiter=‘,‘, usecols=(1, 3, 4, 5, 6), converters={1: datestr2num}, unpack=True)
close = close[:16]
dates = dates[:16]
# get first Monday
print (dates)
print ("open:",open)
print (np.where(dates == 0))
first_monday = np.ravel(np.where(dates == 0))[0]
print ("The first Monday index is", first_monday)
print (np.where(dates == 4))
last_friday = np.ravel(np.where(dates == 4))[-1]
print ("The last Friday index is", last_friday)
weeks_indices = np.arange(first_monday, last_friday + 1)
print ("Weeks indices initial", weeks_indices)
weeks_indices = np.split(weeks_indices, 3)
print ("Weeks indices after split", weeks_indices)
def summarize(a, o, h, l, c):
monday_open = o[a[0]]
week_high = np.max( np.take(h, a) )
week_low = np.min( np.take(l, a) )
friday_close = c[a[-1]]
return( ‘APPL‘,monday_open, week_high, week_low, friday_close)
weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, low, close)
print ("Week summary", weeksummary)
------------------------
OUT:
[4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4.]
open: [344.17 335.8 341.3 344.45 343.8 343.61 347.89 353.68 355.19 357.39
354.75 356.79 359.19 360.8 357.1 358.21 342.05 338.77 344.02 345.29
351.21 355.47 349.96 357.2 360.07 361.11 354.91 354.69 349.69 345.4 ]
(array([ 1, 6, 11], dtype=int64),)
The first Monday index is 1
(array([ 0, 5, 10, 15], dtype=int64),)
The last Friday index is 15
Weeks indices initial [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Weeks indices after split [array([1, 2, 3, 4, 5], dtype=int64), array([ 6, 7, 8, 9, 10], dtype=int64), array([11, 12, 13, 14, 15], dtype=int64)]
Week summary [[‘APPL‘ ‘335.8‘ ‘346.7‘ ‘334.3‘ ‘346.5‘]
[‘APPL‘ ‘347.8‘ ‘360.0‘ ‘347.6‘ ‘356.8‘]
[‘APPL‘ ‘356.7‘ ‘364.9‘ ‘349.5‘ ‘350.5‘]]
-------------------------------------------------------
如果将summarize函数中return的‘APPL‘去掉:
def summarize(a, o, h, l, c):
monday_open = o[a[0]]
week_high = np.max( np.take(h, a) )
week_low = np.min( np.take(l, a) )
friday_close = c[a[-1]]
return( monday_open, week_high, week_low, friday_close)
---------------------------------------------------------------
out:
Week summary [[335.8 346.7 334.3 346.5 ]
[347.89 360. 347.64 356.85]
[356.79 364.9 349.52 350.56]]
-------------------------------------------
小数点的精度发生了变化
备注:data.csv
AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800
AAPL,31-01-2011, ,335.8,340.04,334.3,339.32,13473000
AAPL,01-02-2011, ,341.3,345.65,340.98,345.03,15236800
AAPL,02-02-2011, ,344.45,345.25,343.55,344.32,9242600
AAPL,03-02-2011, ,343.8,344.24,338.55,343.44,14064100
AAPL,04-02-2011, ,343.61,346.7,343.51,346.5,11494200
AAPL,07-02-2011, ,347.89,353.25,347.64,351.88,17322100
AAPL,08-02-2011, ,353.68,355.52,352.15,355.2,13608500
AAPL,09-02-2011, ,355.19,359,354.87,358.16,17240800
AAPL,10-02-2011, ,357.39,360,348,354.54,33162400
AAPL,11-02-2011, ,354.75,357.8,353.54,356.85,13127500
AAPL,14-02-2011, ,356.79,359.48,356.71,359.18,11086200
AAPL,15-02-2011, ,359.19,359.97,357.55,359.9,10149000
AAPL,16-02-2011, ,360.8,364.9,360.5,363.13,17184100
AAPL,17-02-2011, ,357.1,360.27,356.52,358.3,18949000
AAPL,18-02-2011, ,358.21,359.5,349.52,350.56,29144500
AAPL,22-02-2011, ,342.05,345.4,337.72,338.61,31162200
AAPL,23-02-2011, ,338.77,344.64,338.61,342.62,23994700
AAPL,24-02-2011, ,344.02,345.15,338.37,342.88,17853500
AAPL,25-02-2011, ,345.29,348.43,344.8,348.16,13572000
AAPL,28-02-2011, ,351.21,355.05,351.12,353.21,14395400
AAPL,01-03-2011, ,355.47,355.72,347.68,349.31,16290300
AAPL,02-03-2011, ,349.96,354.35,348.4,352.12,21521000
AAPL,03-03-2011, ,357.2,359.79,355.92,359.56,17885200
AAPL,04-03-2011, ,360.07,360.29,357.75,360,16188000
AAPL,07-03-2011, ,361.11,361.67,351.31,355.36,19504300
AAPL,08-03-2011, ,354.91,357.4,352.25,355.76,12718000
AAPL,09-03-2011, ,354.69,354.76,350.6,352.47,16192700
AAPL,10-03-2011, ,349.69,349.77,344.9,346.67,18138800
AAPL,11-03-2011, ,345.4,352.32,345,351.99,16824200
以上是关于关于numpy中的函数return中加入字符串类型数据后,小数点精度变化的主要内容,如果未能解决你的问题,请参考以下文章
couchdb - Map Reduce - 如何在 Reduce 函数中加入不同的文档和组结果