numpy将数组从浮点数转换为字符串 [英] Numpy converting array from float to strings

查看:911
本文介绍了numpy将数组从浮点数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准化的浮点数数组(即数组中最大的数字是1),我想将其用作图形的颜色索引.在使用matplotlib来使用灰度时,这需要使用0到1之间的字符串,因此我想将float数组转换为string数组.我试图通过使用"astype('str')"来执行此操作,但这似乎会创建一些与原始值不相同(甚至接近)的值.

I have an array of floats that I have normalised to one (i.e. the largest number in the array is 1), and I wanted to use it as colour indices for a graph. In using matplotlib to use grayscale, this requires using strings between 0 and 1, so I wanted to convert the array of floats to an array of strings. I was attempting to do this by using "astype('str')", but this appears to create some values that are not the same (or even close) to the originals.

我注意到这一点是因为matplotlib抱怨在数组中找到数字8,这很奇怪,因为它被标准化为1!

I notice this because matplotlib complains about finding the number 8 in the array, which is odd as it was normalised to one!

简而言之,我有一个float64的数组phis,例如:

In short, I have an array phis, of float64, such that:

numpy.where(phis.astype('str').astype('float64') != phis)

非空.令人困惑的是(希望是天真的)它似乎是numpy中的错误,我是否有做错任何事情导致此错误?

is non empty. This is puzzling as (hopefully naively) it appears to be a bug in numpy, is there anything that I could have done wrong to cause this?

经过调查,这似乎是由于字符串函数处理高精度浮点数的方式所致.使用向量化的toString函数(如从robbles回答中得出的),情况也是这样,但是,如果lambda函数为:

after investigation this appears to be due to the way the string function handles high precision floats. Using a vectorized toString function (as from robbles answer), this is also the case, however if the lambda function is:

lambda x: "%.2f" % x

然后绘制图形-越来越好奇. (显然,数组不再相等!)

Then the graphing works - curiouser and curiouser. (Obviously the arrays are no longer equal however!)

推荐答案

对于numpy数组在幕后的工作方式,您似乎有些困惑.数组中的每个项目都必须为相同大小.

You seem a bit confused as to how numpy arrays work behind the scenes. Each item in an array must be the same size.

浮点数的字符串表示不能以这种方式工作.例如,repr(1.3)产生'1.3',但是repr(1.33)产生'1.3300000000000001'.

The string representation of a float doesn't work this way. For example, repr(1.3) yields '1.3', but repr(1.33) yields '1.3300000000000001'.

浮点数的精确字符串表示形式会生成可变长度字符串.

A accurate string representation of a floating point number produces a variable length string.

由于numpy数组由大小均相同的元素组成,因此numpy要求您在使用字符串数组时指定数组中字符串的长度.

Because numpy arrays consist of elements that are all the same size, numpy requires you to specify the length of the strings within the array when you're using string arrays.

如果使用x.astype('str'),它将始终将内容转换为长度为1的字符串数组.

If you use x.astype('str'), it will always convert things to an array of strings of length 1.

例如,使用x = np.array(1.344566)x.astype('str')会产生'1'

您需要更加明确,并使用'|Sx' dtype语法,其中x是数组中每个元素的字符串长度.

You need to be more explict and use the '|Sx' dtype syntax, where x is the length of the string for each element of the array.

例如,使用x.astype('|S10')将数组转换为长度为10的字符串.

For example, use x.astype('|S10') to convert the array to strings of length 10.

甚至更好,只是避免完全使用numpy的字符串数组.通常这是一个坏主意,从您对问题的描述中我看不出要首先使用它们的原因...

Even better, just avoid using numpy arrays of strings altogether. It's usually a bad idea, and there's no reason I can see from your description of your problem to use them in the first place...

这篇关于numpy将数组从浮点数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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