Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert data to int in python numpy.ndarray

Shulou Source: shulou.com Published: 2022-05-31 16:42:30 09月21日 Update

Most people do not understand the knowledge points of this article "how to convert data to int type in python numpy.ndarray", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to convert data into int type in python numpy.ndarray" article.

The data in numpy.ndarray is converted to int.

First, understand the content and type.

> > print (a) (array ([[0.01124722], [0.21752586], [0.05586815], [0.03558792]), array ([[327,366,1887], [1153,1792]]) Dtype=int64) > print (type (a)) > > print (a [1]) [[327] [366] [1887] [1153] [1792]] > > print (type (a [1]))

Next, you can iterate through a [1] and convert each element to int with list () and tolist (), as follows:

For i in a [1]: B = list (I) [0] .tolist ()

First change to the list type with list (), and [0] means to get the first value (in fact, there is only one value), then the type is numpy.int64, and then it is converted to int with tolist ().

The data in numpy.ndarray is converted to int.

Error only size-1 arrays can be converted to Python scalars occurred

After analysis, it is found that it is the error of the data type.

The original data is [63], and the data type obtained by using type is:

So change the ndarray type of numpy to int (or the type you need, here is int)

Using the method of list (m) [0] .tolist (), the result is as follows

sixty-three

Ok, the problem was solved successfully.

The above is about the content of this article on "how to convert data to int in python numpy.ndarray". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Data type content article knowledge article error success next code value element only most that is method more step knowledge point result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi MySQL macOS Microsoft