想要解决这个错误,最好先明白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的更多相关文章

  1. 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 ' ...

  2. TypeError: Fetch argument None has invalid type <type 'NoneType'>

    (fetch, type(fetch)))TypeError: Fetch argument None has invalid type <type 'NoneType'> 我的解决方案是 ...

  3. Airflow:TypeError an integer is required (got type NoneType) 一次诡异问题排查

    ​ 当使用rabbitmq作为airflow的broker的时候,启动scheduler,即执行airflow scheduler命令的时候抛出以下异常: Traceback (most recent ...

  4. tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor

    最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...

  5. 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__() 当调 ...

  6. Pycharm:设置完Anaconda后报错TypeError: an integer is required (got type bytes)

    背景:安装了最新版本的Anaconda3.9后,在Pycharm中设置Python Interpreter为这个最新版本Anaconda文件下的python.exe后,控制台无法启动并报错TypeEr ...

  7. 判断np.array里面为空字符串的方法

    #多在编译器里尝试新操作 import numpy as np for i range(100): eval1 = {"A": ''"} eval2 = {"A ...

  8. python多分类预测模版,输出支持度,多种分类器,str的csv转float

    预测结果为1到11中的1个 首先加载数据,训练数据,训练标签,预测数据,预测标签: if __name__=="__main__": importTrainContentdata( ...

  9. python3报错

    这个错误是我在从Excel中导入数据,,x,y 和z(z代表了强度)  然后通过xyz画出一个二维的灰度图片所出现的错误 原因是因为用mcml生成的数据如: TypeError: cannot per ...

随机推荐

  1. bootstrap标签tab切换

    <ul class="nav nav-tabs" id="myTab"> <li class="active">&l ...

  2. 查看mysql表大小

    //先进去MySQL自带管理库:information_schema //自己的数据库:dbwww58com_kuchecarlib //自己的表:t_carmodelparamvalue mysql ...

  3. VMware NAT端口映射外网访问虚拟机linux可能会出现的错误总结

    博主因为做实验报告的缘故,尝试以NAT的方式从外网远程连接到虚拟机的linux操作系统:https://www.cnblogs.com/jluzhsai/p/3656760.html,本文主要举出在此 ...

  4. 打包ios软件并发布到应用商店

    真心感慨程序员是一个神奇的动物. 昨天接到任务,将项目打包并发布到apple商店.于是乎... 利用Hbuilder打包 需要的3个文件: AppId,描述文件profile,以及私钥证书 必须条件: ...

  5. windows剪切板暂存

    其实最初是因为在项目中使用了html网页编辑器,通过ie的com组件和javascript通讯完成一些事情,其中有一个功能是插入表格,我们原本使用的range.pasteHTML(HTMLstr);根 ...

  6. 打造颠覆你想象中的高性能,轻量级的webform框架---js直接调后台的封装(第三天)

    如果你没有看我第二天写的内容的,我想你是看不懂的!!!! 好了,废话不多说,怎么才能让我们的代码变得牛逼起来呢?怎么封装我们的代码呢?我们不可能 每个页面都需要那样写吧,那我们来一步一步来封装 我们的 ...

  7. wu2198:难得的波段抄底机会

    很好的波段抄底机会 个人浅见看,目前染料股跌出的机会明显,养殖股波段机会明显,芯片.半导体.集成电路.北导.软件.国产操作系统等科技股短线机会不错.另外,大盘指数2856/2806区域的波段操作机会不 ...

  8. 将数据库数据添加到ListView控件中

    实现效果: 知识运用: ListView控件中的Items集合的Clear方法 //从listView控件的数据项集合中移除所有数据项 补充:可以使用Remove或RemoveAt方法从集合中移除单个 ...

  9. 解决wget下载https时报错 --no-check-certificate (不检查证书)

    如果使用 wget下载https开头的网址域名 时报错,你需要加上 --no-check-certificate (不检查证书)选项 例如: wget https://pypi.python.org/ ...

  10. 输入hostname -f提示:hostname: Unknown host

    解决方法:将/etc/hosts文件中的内容添加如下所示 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdo ...