python numpy.ndarray中如何将数据转为int型
作者:dcjmessi 时间:2022-11-21 16:14:09
numpy.ndarray中数据转为int型
首先了解内容与类型
>>>print(a)
(array([[0.01124722],
[0.21752586],
[0.05586815],
[0.03558792]]), array([[ 327],
[ 366],
[1887],
[1153],
[1792]], dtype=int64))
>>>print(type(a))
<class 'tuple'>
>>>print(a[1])
[[ 327]
[ 366]
[1887]
[1153]
[1792]]
>>>print(type(a[1]))
<class 'numpy.ndarray'>
接下来可以遍历a[1],对每个元素用list()和tolist()转int,代码如下:
for i in a[1]:
b = list(i)[0].tolist()
先用list()转为列表类型,[0]表示获取第一个值(其实也只有一个值),此时类型为numpy.int64,然后用tolist()便转为int型。
numpy.ndarray中数据转为int型
出现错误only size-1 arrays can be converted to Python scalars
经过分析后发现是数据类型出现的错误。
原数据为 [63],使用type得到数据类型为:<class 'numpy.ndarray'>
故将numpy的ndarray类型转为int (或者你需要的类型,此处为int)
使用 list(m)[0].tolist() 的方法得到如下的结果
63
<class 'int'>
Ok,问题成功解决。
来源:https://blog.csdn.net/dcjmessi/article/details/105945792
标签:python,numpy.ndarray,数据,int
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文速学Python+Pyecharts绘制树形图
2023-07-28 12:05:27
![](https://img.aspxhome.com/file/2023/1/61201_0s.png)
如何获知文件被改动的情况?
2009-11-24 20:42:00
ASP中生成文本文件的两种方式
2008-04-30 19:33:00
pyqt5实现绘制ui,列表窗口,滚动窗口显示图片的方法
2023-03-22 16:52:56
![](https://img.aspxhome.com/file/2023/2/112472_0s.jpg)
Java中用Mybatis插入mysql报主键重复的解决方案
2024-01-20 01:49:45
![](https://img.aspxhome.com/file/2023/1/75671_0s.png)
sql server中批量插入与更新两种解决方案分享(存储过程)
2012-05-22 18:29:59
python使用matplotlib显示图像失真的解决方案
2021-03-30 22:31:02
![](https://img.aspxhome.com/file/2023/6/73006_0s.jpg)
Python ttkbootstrap 制作账户注册信息界面的案例代码
2021-02-10 04:05:11
![](https://img.aspxhome.com/file/2023/6/124396_0s.png)
Pandas Shift函数的基础入门学习笔记
2023-02-16 20:46:12
向外扩展SQL Server 实现更高扩展性
2008-12-18 14:45:00
解决Django中checkbox复选框的传值问题
2023-09-11 17:15:22
![](https://img.aspxhome.com/file/2023/5/70205_0s.jpg)
在ASP中连接使用数据库
2007-09-22 10:46:00
Python全栈之for循环
2021-06-04 20:55:06
![](https://img.aspxhome.com/file/2023/8/87608_0s.png)
使用python向MongoDB插入时间字段的操作
2021-05-21 06:54:19
Django 如何实现文件上传下载
2021-07-16 02:54:15
![](https://img.aspxhome.com/file/2023/0/86270_0s.png)
OpenCV学习之图像加噪与滤波的实现详解
2022-09-20 04:40:57
![](https://img.aspxhome.com/file/2023/0/105040_0s.png)
python切换hosts文件代码示例
2023-07-19 15:41:43
Python中利用sorted()函数排序的简单教程
2022-09-13 20:26:36
python 读写中文json的实例详解
2022-12-12 20:27:36
asp.net得到本机数据库实例的两种方法代码
2024-01-27 16:00:42