无法将String转换为在numpy ndarray中浮动

时间:2019-04-20 09:50:05

标签: python-3.x numpy numpy-ndarray

我一直在从Fer 2013 csv文件中进行一些读取,其中包含三栏情感,像素值和用法。像素值据说是以字符串格式给出的,我想将像素值从0-255调整为0-1之间,因此我需要将其转换为int / float,然后只有我才能做任何数学运算对它们进行操作。

我首先尝试使用pandas read_csv函数读取csv文件,然后使用iloc读取名为x_tr的变量中的像素值。然后在打印其值时将其d类型显示为object.confused.x_tr是numpy ndarray然后我应该如何将其转换为整数值。 我尝试了x_tr.astype(np.float),但它放弃了代码中提到的错误。

@Get('/:id')
  @ApiOperation({ title: 'Get user ressource' })
  @UseGuards(AuthGuard('jwt'))
  async index(@Param() query: GetUserByIdQuery): Promise<object> {
    const user = await this.queryBus.execute(query);

    if (!(user instanceof User)) {
      throw new NotFoundException();
    }

    return {
      id: user.id,
      fullName: user.getFullName(),
      email: user.email
    };
  }

The screenshot of what x_tr contains

我试图转换为float

x_tr = train.iloc[:,1].values
x_tr

以及我遇到的错误

enter image description here

请帮助。

1 个答案:

答案 0 :(得分:1)

请勿将您的pixel转换为数组,而应将其视为简单字符串。然后使用numpy.fromstring()方法。这是一个供参考的示例。

>>> s = '1 2 3 4'
>>> f = np.fromstring(s, dtype=float, sep=' ')
>>> f
array([1., 2., 3., 4.])
相关问题