python3 数组转字符串,如何在Python 3中将字符串数据类型转换为浮于numpy数组中

本文探讨了在从Python 2.7切换到Python 3时遇到的问题,即使用`np.loadtxt`加载包含字符串数据的CSV文件时,如何将部分字符串转换为浮点数失败。通过实例展示了使用`np.genfromtxt`代替`np.loadtxt`成功解决此问题的过程。
摘要由CSDN通过智能技术生成

I have been using Python 2.7 for some time now and have recently switched to Python 3. I have already updated my code on some points, but the problem I currently have deludes me. What I am trying to do is to load a dataset using np.loadtxt. Because this data also contains strings I am importing the full array as a string. I want to do type conversions after to convert some entries to float. This fails miserably and I do not understand why. All I see is that in Python 3 all strings get the prefix 'b' and I have the feeling this has something to do with this, but I cannot find a concise answer. Code and error below.

filename = 'train.csv'

raw_data = open(filename, 'rb')

data = np.loadtxt(raw_data, delimiter=",", dtype = 'str')

dataset = data[1:,1:]

print(dataset)

original_data = dataset

test = float(dataset[0,0])

print(test)

Result

[["b'60'" "b'RL'" "b'65'" ..., "b'WD'" "b'Normal'" "b'208500'"]

["b'20'" "b'RL'" "b'80'" ..., "b'WD'" "b'Normal'" "b'181500'"]

["b'60'" "b'RL'" "b'68'" ..., "b'WD'" "b'Normal'" "b'223500'"]

...,

["b'70'" "b'RL'" "b'66'" ..., "b'WD'" "b'Normal'" "b'266500'"]

["b'20'" "b'RL'" "b'68'" ..., "b'WD'" "b'Normal'" "b'142125'"]

["b'20'" "b'RL'" "b'75'" ..., "b'WD'" "b'Normal'" "b'147500'"]]

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

in ()

5 print(dataset)

6 original_data = dataset

----> 7 test = float(dataset[0,0])

8 print(test)

ValueError: could not convert string to float: "b'60'"

解决方案

As suggested by dnalow, something goes wrong in the type conversion because I first open the file and then read from it. The solution is to not use open open(filename, 'rb') and np.loadtxt, but to use np.genfromtxt. Code below.

filename = 'train.csv'

data = np.genfromtxt(filename, delimiter=",", dtype = 'str')

dataset = data[1:,1:]

print(dataset)

original_data = dataset

test = float(dataset[0,0])

print(test)

filename = 'train.csv'

data = np.genfromtxt(filename, delimiter=",", dtype = 'str')

dataset = data[1:,1:]

print(dataset)

original_data = dataset

test = float(dataset[0,0])

print(test)

Result

[['60' 'RL' '65' ..., 'WD' 'Normal' '208500']

['20' 'RL' '80' ..., 'WD' 'Normal' '181500']

['60' 'RL' '68' ..., 'WD' 'Normal' '223500']

...,

['70' 'RL' '66' ..., 'WD' 'Normal' '266500']

['20' 'RL' '68' ..., 'WD' 'Normal' '142125']

['20' 'RL' '75' ..., 'WD' 'Normal' '147500']]

60.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值