TypeError: cannot perform reduce with flexible type
想要解决这个错误,最好先明白numpy数据类型的dtype转换
生成一个浮点数组
a=np.random.random(4)
输出
a
array([0.0945377,0.52199916,0.62490646,0.2160126])
a.dtype
dtype('float64')
a.shape
(4,)
改变dtype,发现数组长度翻倍!
>>> a.dtype = 'float16'
>>> a
array([ -9.58442688e-05, 7.19000000e+02, 2.38159180e-01,
1.92968750e+00, nan, -1.66034698e-03,
-2.63427734e-01, 1.96875000e+00, -1.07519531e+00,
-1.19625000e+02, nan, 1.97167969e+00,
-1.60156250e-01, -7.76290894e-03, 4.07226562e-01,
1.94824219e+00], dtype=float16)
>>> a.shape
(16,)
改变dtype='float',发现默认就是float64,长度也变回最初的4
>>> a.dtype = 'float'
>>> a
array([ 0.0945377 , 0.52199916, 0.62490646, 0.21260126])
>>> a.shape
(4,)
>>> a.dtype
dtype('float64')
把a变为整数,观察其信息
>>> a.dtype = 'int64'
>>> a
array([4591476579734816328, 4602876970018897584, 4603803876586077261,
4596827787908854048], dtype=int64)
>>> a.shape
(4,)
改变dtype,发现数组长度翻倍!
>>> a.dtype = 'int32'
>>> a
array([ 1637779016, 1069036447, -1764917584, 1071690807, -679822259,
1071906619, -1611419360, 1070282372])
>>> a.shape
(8,)
改变dtype,发现数组长度再次翻倍!
>>> a.dtype = 'int16'
>>> a
array([-31160, 24990, 13215, 16312, 32432, -26931, -19401, 16352,
-17331, -10374, -197, 16355, -20192, -24589, 13956, 16331], dtype=int16)
>>> a.shape
(16,)
改变dtype,发现数组长度再次翻倍!
>>> a.dtype = 'int8'
>>> a
array([ 72, -122, -98, 97, -97, 51, -72, 63, -80, 126, -51,
-106, 55, -76, -32, 63, 77, -68, 122, -41, 59, -1,
-29, 63, 32, -79, -13, -97, -124, 54, -53, 63], dtype=int8)
>>> a.shape
(32,)
改变dtype,发现整数默认int32!
>>> a.dtype = 'int'
>>> a.dtype
dtype('int32')
>>> a
array([ 1637779016, 1069036447, -1764917584, 1071690807, -679822259,
1071906619, -1611419360, 1070282372])
>>> a.shape
(8,)
很多时候我们用numpy从文本文件读取数据作为numpy的数组,默认的dtype是float64。
但是有些场合我们希望有些数据列作为整数。如果直接改dtype=‘int’的话,就会出错!原因如上,数组长度翻倍了
下面的场景假设我们得到了导入的数据。我们的本意是希望他们是整数,但实际上却是浮点数(float64)
>>> b = np.array([1., 2., 3., 4.])
>>> b.dtype
dtype('float64')
用astype(int)得到整数,并且不改变数组长度
>>> c = b.astype(int)
>>> c
array([1, 2, 3, 4])
>>> c.shape
(8,)
>>> c.dtype
dtype('int32')
如果直接改变b的dtype的话,b的长度翻倍了,这不是我们想要的
>>> b
array([ 1., 2., 3., 4.]) >>> b.dtype = 'int'
>>> b.dtype
dtype('int32')
>>> b
array([ 0, 1072693248, 0, 1073741824, 0,
1074266112, 0, 1074790400])
>>> b.shape
(8,)
结论:
numpy中的数据类型转换,不能直接改原数据的dtype!只能用函数astype().
现在来说这个错误:
我是对矩阵进行一次np.arry之后求最小值,再次np.array求最小值时报了这个错误:TypeError: cannot perform reduce with flexible type
解决办法是用astype可以改变矩阵的dtype类型,于是我试着把我的矩阵的dtype改一下:
e=np.array(A[j]).astype(float).min()
问题解决!
TypeError: cannot perform reduce with flexible type的更多相关文章
- TypeError: Fetch argument 0 has invalid type <type 'int'>, must be a string or Tensor. (Can not convert a int into a Tensor or Operation.)
6月5日的時候,修改dilated_seg.py(使用tensorflow)出現了報錯: TypeError: Fetch argument 0 has invalid type <type ' ...
- TypeError: Fetch argument None has invalid type <type 'NoneType'>
(fetch, type(fetch)))TypeError: Fetch argument None has invalid type <type 'NoneType'> 我的解决方案是 ...
- Airflow:TypeError an integer is required (got type NoneType) 一次诡异问题排查
当使用rabbitmq作为airflow的broker的时候,启动scheduler,即执行airflow scheduler命令的时候抛出以下异常: Traceback (most recent ...
- tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor
最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...
- Python super(Todo,self).__init__() TypeError: super() argument 1 must be type, not classobj
示例如下 class A(): def __init__(self):pass class B(A): def __init__(self): super(A, self).__init__() 当调 ...
- Pycharm:设置完Anaconda后报错TypeError: an integer is required (got type bytes)
背景:安装了最新版本的Anaconda3.9后,在Pycharm中设置Python Interpreter为这个最新版本Anaconda文件下的python.exe后,控制台无法启动并报错TypeEr ...
- 判断np.array里面为空字符串的方法
#多在编译器里尝试新操作 import numpy as np for i range(100): eval1 = {"A": ''"} eval2 = {"A ...
- python多分类预测模版,输出支持度,多种分类器,str的csv转float
预测结果为1到11中的1个 首先加载数据,训练数据,训练标签,预测数据,预测标签: if __name__=="__main__": importTrainContentdata( ...
- python3报错
这个错误是我在从Excel中导入数据,,x,y 和z(z代表了强度) 然后通过xyz画出一个二维的灰度图片所出现的错误 原因是因为用mcml生成的数据如: TypeError: cannot per ...
随机推荐
- click事件的累加绑定
click事件的累加绑定,绑定一次点击事件,执行多次. 在页面中为一个元素绑定事件,事件执行后页面未刷新且元素还在,然后你再次点击,元素又被绑定一个点击事件,这样第二次点击就会执行两次,以此类推. 如 ...
- 初识Notification
通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...
- spring boot 下 spring security 自定义登录配置与form-login属性详解
package zhet.sprintBoot; import org.springframework.beans.factory.annotation.Autowired;import org.sp ...
- AD7190的小总结
1.单次转换模式 通过配置“模式寄存器的MD2.MD1.MD0为001”,便可启动单次转换. 流程“上电 -> 单次转换 -> 省电模式 ” , 片内振荡上电需要大约1ms. 单次转换 ...
- weka属性选择使用
醉了--- package edu.dcy.weka; import java.io.FileWriter; import java.util.ArrayList; import java.util. ...
- linux 命令——51 lsof(转)
lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 如传输控制 ...
- fiddle连接终端测试配置
第一次做app,对app的数据要进行一些数据抓包和数据分析,知道客户端发送到服务器端的过程和逻辑,通过抓包了解和分析出错,前提要先连接fiddle
- FETCH - 用游标从查询中抓取行
SYNOPSIS FETCH [ direction { FROM | IN } ] cursorname where direction can be empty or one of: NEXT P ...
- 2017.12.1 如何用java写出一个菱形图案
上机课自己写的代码 两个图形原理都是一样的 1.一共有仨个循环 注意搞清楚每一层循环需要做的事情 2.第一层循环:是用来控制行数 3.第二层循环控制打印空格数 4.第三层循环是用来循环输出星星 imp ...
- PAM认证机制
PAM:Pluggable Authentication Modules 认证库:文本文件,MySQL,NIS,LDAP等 Sun公司于1995 年开发的一种与认证相关的通用框架机制 PAM 是关注如 ...