想要解决这个错误,最好先明白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()

问题解决!

转载于:https://www.cnblogs.com/zhibei/p/9635825.html

TypeError: cannot perform reduce with flexible type相关推荐

  1. 计算几个变量之间的相关系数,计算协方差矩阵时:TypeError: cannot perform reduce with flexible type

    环境:python 3.6 + win10 IDE: pycharm community 2017.3 问题分析:在进行对相关系矩阵进行归一化时,出错,TypeError: cannot perfor ...

  2. whiten矩阵时报错:TypeError: cannot perform reduce with flexible type

    用kmeans聚类之前需要对数据进行归一化处理,我的矩阵是从数据库中获取的,之后还经过一次转换,使用whiten对矩阵进行归一化的时候,出现错误: Traceback (most recent cal ...

  3. pyinstaller打包任何py文件TypeError: an integer is required (got type bytes)

    pyinstaller打包任何py文件TypeError: an integer is required (got type bytes) 目录 pyinstaller打包任何py文件TypeErro ...

  4. 读取文件:TypeError: an integer is required (got type str)

    读取文件的时候报错: Traceback (most recent call last): File "D:\Python35\test\csdn.py", line 46, in ...

  5. python:使用PyInstaller打包成exe文件,以及TypeError: an integer is required (got type bytes)异常解决

    本文主要介绍安装pyinstaller教程与pyinstaller打包出现 TypeError: an integer is required (got type bytes)异常问题解决办法: 1. ...

  6. Python 使用pyinstaller打包exe文件报错: TypeError: an integer is required (got type bytes) 的解决方法

    python打包成exe文件首先需要安装pyinstaller库,再进入到目标文件目录,输入命令行打包py文件,但出现报错:TypeError: an integer is required (got ...

  7. python an integer is required_Python TypeError: an integer is required (got type tuple)

    Python TypeError: an integer is required (got type tuple) 关注:256  答案:2  mip版 解决时间 2021-01-12 12:36 提 ...

  8. TypeError: an integer is required (got type bytes)

    win10下spark2.4不能与python3.8及以上版本兼容,替换3.8为3.7完美解决

  9. pandas之loc iloc ix

    首先,介绍这三种方法的概述: loc: loc gets rows (or columns) with particular labels from the index. loc从索引中获取具有特定标 ...

最新文章

  1. dataframe两个表合并_史上代码最少的工作表拆分,仅需5行,不可思议
  2. Matlab计算基站覆盖面积示例
  3. c# 微服务学习_微服务:学习几个容易混淆的URL注解
  4. android通用的UUID唯一标示符
  5. 报错:[Warning] lambda expressions only available with -std=c++11 or -std=gnu++11
  6. 计算机编程语言的分类,解释型语言、编译型语言、脚本语言的区别
  7. Java LinkedHashMap的实现原理详解
  8. Shiro————核心设计思想
  9. springcloud服务网关Netflix Zuul入门简介
  10. OC之集合的创建及应用
  11. 创业有很多种方式,方法,而且是形式多样
  12. 21个强大漂亮Ajax/CSS表格设计
  13. python教程pdf-python基础教程[高清][中文第2版].pdf
  14. 《SQL Server2008》第二章 创建和管理数据库
  15. 配置好网络文件还是连不上外网
  16. WP10回滚WP8.1详细教程,变砖也可修复
  17. 【2015-18年腾讯招聘】腾讯产品策划类笔试面试题整理
  18. 日志易数据接入之 Syslog 日志上传
  19. 蓝桥杯近三年初赛题之一(15年b组)
  20. 家庭监控,网络摄像头(OpenWRT平台下Mjpg-Streamer+Ngrok实现方案)

热门文章

  1. 使用Tensorflow实现简单线性回归
  2. shell 判断字符串最后一个字符
  3. 【模板/经典题型】并查集维护生成树
  4. Visual Studio的下载安装
  5. (转)Elasticsearch NoNodeAvailableException None of the configured nodes are available
  6. 高级软件工程2017第2次作业—— 个人项目:四则运算题目生成程序(基于控制台)...
  7. Jsoup的简易使用示例
  8. virtualbox+oracle linux 6.3 下安装oracle 11.2.3.0
  9. 使用Discuz!自带参数防御CC攻击以及原理,修改Discuz X 开启防CC攻击后,不影响搜索引擎收录的方法...
  10. NHibernate学习之基础配置