怎么把一个float型的numpy数组转为整型
时间: 2024-05-01 17:19:05 浏览: 26
你可以使用numpy的astype()函数将float型的numpy数组转为整型,例如:
```python
import numpy as np
float_array = np.array([1.2, 2.5, 3.7])
int_array = float_array.astype(int)
print(int_array)
```
输出结果为:
```python
[1 2 3]
```
相关问题
将int型numpy数组转换成float型
可以使用astype()方法将int型numpy数组转换成float型,示例代码如下:
```python
import numpy as np
# 创建一个int型numpy数组
int_arr = np.array([1, 2, 3, 4, 5], dtype=np.int32)
# 将int型numpy数组转换成float型
float_arr = int_arr.astype(np.float32)
# 输出结果
print(float_arr)
```
输出结果如下:
```
[1. 2. 3. 4. 5.]
```
numpy数组转为一个图像数据集并在文件夹中显示
可以使用PIL库将numpy数组转换为图像数据集,并使用os库将其保存到文件夹中。以下是示例代码:
```python
import numpy as np
from PIL import Image
import os
# 创建一个随机的numpy数组
arr = np.random.randint(0, 255, size=(100, 100, 3), dtype=np.uint8)
# 将numpy数组转换为图像
img = Image.fromarray(arr)
# 创建一个文件夹来保存图像
if not os.path.exists('images'):
os.makedirs('images')
# 保存图像到文件夹中
img.save('images/image.png')
```
这将创建一个名为“image.png”的图像文件,并将其保存到名为“images”的文件夹中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)