TypeError: cannot perform reduce with flexible type 解决方案

for i in all_data[600000]:
			close_price_list.append(i['close'])
		for j in range(len(close_price_list)):
			if 2*N+j > len(close_price_list):  # 通过验证,索引正确
				continue
			else:
				temp_sigma = np.std(close_price_list[N+j:2*N+j])  # 通过验证,索引正确
				sigma_list.append(temp_sigma)
		print(sigma_list)

这段代码,解释一下,报错的是标黄的这一行。

我是用for循环获取的变量j是一个列表类型,里面是一串价格。我想获取这个列表价格的标准差np.std()

但是总报错:

  File "versions2.py", line 155, in Creat_std
    temp_sigma = np.std(close_price_list[N+j:2*N+j])  # 通过验证,索引正确
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\fromn
 3075, in std
    **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\_meth
5, in _std
    keepdims=keepdims)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\_meth
2, in _var
    arrmean = umr_sum(arr, axis, dtype, keepdims=True)
TypeError: cannot perform reduce with flexible type

解决方案:

在确认了一切数据正确,list类型也没错的情况下。

我发现,是列表中的元素,并不是数值型,而是string字符串类型,这导致了np.std()函数的报错。

close_price_list.append(i['close'])

修改为:

close_price_list.append(float(i['close']))

即可解决


  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值