关于numpy中的函数return中加入字符串类型数据后,小数点精度变化

weekdays.pyimport numpy as npfrom datetime import datetimedef 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 Mondayprint (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



原文地址:https://www.cnblogs.com/master-road/p/10627609.html

时间: 2024-08-29 00:41:44

关于numpy中的函数return中加入字符串类型数据后,小数点精度变化的相关文章

Swift 中的函数(中)

学习来自<极客学院:Swift中的函数> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 1 import Foundation 2 3 //函数 4 5 //1.多个返回值 6 func area(width: Double,height: Double) -> (Double,Double) 7 { 8 var b = width 9 var a = width * height 10 return (a,b) 11 } 12 printl

shell中的函数 shell中的数组 告警系统需求分析

一.shell中的函数[[email protected] aming]# cd /root/shell/aming[[email protected] aming]# vim fun1.sh //需要注意函数名不能跟shell中的一些关键字冲突#!/bin/bashfunction inp(){echo $1 $2 $3 $0 $#} inp 1 a 2[[email protected] aming]# sh fun1.sh1 a 2 fun1.sh 3 //$0是脚本名称.3是参数个数 继

java中如果函数return可能抛出异常怎么办

protected JSONObject doInBackground(String... params) { try { return new JSONParser().getJSONFromUrl(params[0]); } catch (JSONException e) { e.printStackTrace(); } finally { return ?: }}以上的代码报编译错误:Missing return value ,但是好像也无法解决啊.发生异常时返回什么值合适呢?难道要自己手

JS中的函数声明存在的“先使用,后定义”

首先看一段JS代码,其中使用了两种方式声明了两个函数,分别在不同的地方调用两个函数: 1 <script> 2 'use strict'; 3 // 输出hello函数 4 console.log(hello); 5 // 定义hello函数 6 function hello(){ 7 alert("Hello"); 8 } 9 // 输出hi变量 10 console.log(hi); 11 // 给hi赋一个函数 12 var hi = function(){ 13 a

简单说明一下JS中的函数声明存在的“先使用,后定义”

首先看一段JS代码,其中使用了两种方式声明了两个函数,分别在不同的地方调用两个函数: 1 <script> 2 'use strict'; 3 // 输出hello函数 4 console.log(hello); 5 // 定义hello函数 6 function hello(){ 7 alert("Hello"); 8 } 9 // 输出hi变量 10 console.log(hi); 11 // 给hi赋一个函数 12 var hi = function(){ 13 a

为什么 MySQL 中 GROUP_CONCAT 函数返回 BLOB 大对象类型?(Why GROUP_CONCAT returns BLOB?)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 相似的文章列于下方, 我的问题是 CONCAT(字段名, ''),这个用法,就是为了将字段名所代表的数值字段转换成字符串结果. 那么,从以下三楼回

mysql中的字符串类型数据索引优化

摘自 "高性能mysql" 对于一些字符串类型较长的字段搜索时, 可以参考如下方法

MYSQL中的数值型数据类型与字符串类型

/* 数值型数据类型主要用来存储数字,包含的类型有: TINYINT.SMALLINT.MEDIUMINT. INT(INTEGER). BIGINT TINGINT占1个字节,SMALLINT占2个字节,MEDIUMINT占3个字节,INT占4个字节,BIGINT占8个字节 浮点类型:FLOAT.DOUBLE.定点类型:DECIMAL. 我们一般常用INT,和DOUBLE:*/ /* 字符串有如下几种类型: CHAR.VARCHAR.BINARY.VARBINARY.BLOB.TEXT.ENU

golang 函数证明数组为 值类型 数据

package main import "fmt" func modify(arr [5]int) {     arr[0] = 10 // 试图修改数组的第一元素     fmt.Println("In modify() arr values:", arr) } func main() {     arrmain := [5]int{1, 2, 3, 4, 5} //定义并初始化一个数组     modify(arrmain)                  /