如何检查numpy数组中值的类型? [英] How to check the type of the values within a numpy array?

查看:672
本文介绍了如何检查numpy数组中值的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆numpy数组,并且我试图检查每个数组中值的类型,因为其中一些包含星号,这些星号会导致整个数组变成字符串类型,而另一些则是浮点数.我希望能够检查一个数组,看看它是否包含浮点数或字符串.下面的代码:

I have a bunch of numpy arrays, and i'm trying to check the type of the values within each array as some of them contain stars which cause the entire array to become of type string and others are floats. I want to be able to check an array and see if it contains floats or strings. Code below:

nums = np.array([123, 54, 645, 89, 465, 98])
nums2 = np.array(['4', '987', '*65*', '89'])

是否可以编写某种循环来检查此情况?我理想的输出应该是:

Is there a way to write some kind of loop to check this? My ideal output would be along the lines of:

nums = float
nums2 = string

推荐答案

您所拥有的有清单.要创建NumPy数组,您需要使用其构造函数,如下所示:

What you have there are lists. To make a NumPy array, you need to use its constructor as:

import numpy as np
nums = np.array([123, 54, 645, 89, 465, 98])
nums2 = np.array(['45', '987', '65', '89'])

NumPy数组具有一个称为dtype的属性.您可以使用它来查看数组中具有什么类型的数据.

NumPy arrays have an attribute called dtype. You can use that to see what type of data you have in your arrays.

>>> nums.dtype
dtype('int64')
>>> nums2.dtype
dtype('<U3')

有关NumPy数据类型的更多信息: https://docs .scipy.org/doc/numpy/user/basics.types.html

For more info on NumPy data types: https://docs.scipy.org/doc/numpy/user/basics.types.html

这篇关于如何检查numpy数组中值的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆