设计神经网络模型时无法将字符串转换为浮点数”

时间:2018-08-14 06:43:40

标签: python-3.x scikit-learn jupyter-notebook

当我尝试设计用于从csv文件导入数据的神经网络模型时,我不断遇到此错误。在这里,我包括我的数据集和给出错误的代码。 我的代码是;

CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'WoZ0ZnpXzvdAKoCPRrsa7Rniq7SNjAcZxL..............';

错误是,无法将字符串转换为float:'0:23:40'

我的数据集是,

GRANT CREATE ON DATABASE SCOPED CREDENTIAL::AZUREBLOBSTORAGECREDENTIALPERMISSION TO MYUSER

你们都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这是您代码的改进版本,可以正确转换数据并且运行没有问题;)

import numpy as np


def time_to_float(t):
    """ convert "hh:mm:ss" to float (0, 1) only of the correct format """
    if t == '-':
        return None
    a = [int(i) for i in t.split(":")]
    if len(a) == 3:
        return round((a[0] + a[1] / 60 + a[2] / 3600) / 24, 5)
    else:
        return t


def pick_column(data_, n, start=2):
    """ pick all the n'th column data starting from "start" """
    return [time_to_float(data_[i][n]) for i in range(start, len(data_))]


# read data
data = np.genfromtxt('data2.csv', dtype=str, delimiter=',')

# split data
data = [i.split() for i in data]


x = pick_column(data, 0)
y = pick_column(data, 1)
x1 = pick_column(data, 2)
y1 = pick_column(data, 3)
x2 = pick_column(data, 4)
y2 = pick_column(data, 5)


print(x)
print(y)
print(x1)
print(y1)
print(x2)
print(y2)

输出

[0.00434, 0.00654, 0.00764, 0.00961, 0.0114, 0.01319, 0.01389, 0.01534, 0.01644]
['141', '95', '149', '85', '135', '63', '111', '115', '287']
[0.00556, 0.00573, 0.0059, 0.00608, 0.00625, None, None, None, None]
['131', '117', '109', '103', '97', None, None, None, None]
[0.00469, 0.0066, 0.00799, 0.00937, 0.01071, 0.0125, 0.01354, 0.01545, None]
['136', '95', '139', '95', '105', '97', '100', '115', None]