Python-辨析type/dtype/astype用法

Python中与数据类型相关函数及属性有如下三个:type/dtype/astype。

名称描述
type()返回参数的数据类型
dtype返回数组中元素的数据类型
astype()对数据类型进行转换
  1. type()用于获取数据类型
#type用于获取数据类型
import numpy as np
a=[1,2,3]
print(type(a))
#>>><class 'list'>
b=np.array(a)
print(type(b))
#>>><class 'numpy.ndarray'>
  1. dtype用于获取数组中元素的类型
#dtype用于获取数组中元素的类型
print(b.dtype)
#>>>int64

x,y,z=1,2,3
c=np.array([x,y,z])
print(c.dtype)
#>>>int64

d=np.array([1+2j,2+3j,3+4j])
print(d.dtype)
#>>>complex128
  1. astype()修改数据类型
    Array中用法
#astype修改数据类型
e=np.linspace(1,5,20)
print(e)
#>>>
'''
[1.         1.21052632 1.42105263 1.63157895 1.84210526 2.05263158
 2.26315789 2.47368421 2.68421053 2.89473684 3.10526316 3.31578947
 3.52631579 3.73684211 3.94736842 4.15789474 4.36842105 4.57894737
 4.78947368 5.        ]
'''
print(e.dtype)
#>>>float64

e=e.astype(int)
print(e)
#>>>[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5]
print(e.dtype)
#>>>int64

Dataframe中用法:转换指定字段数据的类型

import pandas as pd
e=np.linspace(1,5,20)
edf=pd.DataFrame(e)
edf[0].head(3)
#>>>
0    1.000000
1    1.210526
2    1.421053
Name: 0, dtype: float64

print(edf[0].dtype)
#>>>float64

edf[0]=edf[0].astype(int)
edf[0].head(3)
#>>>
0    1
1    1
2    1
Name: 0, dtype: int64

print(edf[0].dtype)
#>>>int64
### 批量预测并输出到 `pred_result.txt` 文件 在 PaddlePaddle 的 PaddleDetection 框架中,完成批量预测并将结果输出到文件需要以下步骤。以下是基于框架功能实现的详细说明: #### 1. 加载模型与配置 首先需要加载训练好的 YOLOX 模型及其配置文件。确保模型权重文件和配置文件路径正确。 ```python from ppdet.engine import Trainer from ppdet.core.workspace import load_config, merge_config # 加载配置文件 cfg = load_config('path_to_your_config.yml') # 替换为你的配置文件路径[^1] merge_config({'weights': 'path_to_best_ckpt.pdparams'}) # 替换为你的模型权重路径[^1] # 初始化训练器 trainer = Trainer(cfg) ``` #### 2. 数据预处理 对于批量预测,需要将输入数据进行预处理,并将其转换为模型可接受的格式。 ```python import cv2 import numpy as np def preprocess_image(image_path, input_size=(640, 640)): image = cv2.imread(image_path) image = cv2.resize(image, input_size) image = image.astype('float32') / 255. image = np.transpose(image, [2, 0, 1]) # 转换为 (C, H, W) return image # 加载图像列表 image_paths = ['image1.jpg', 'image2.jpg'] # 替换为实际图像路径列表 batch_images = [preprocess_image(path) for path in image_paths] batch_images = np.array(batch_images, dtype='float32') ``` #### 3. 执行批量预测 使用模型对批量数据进行预测,并提取预测结果。 ```python import paddle # 将数据转换为张量 batch_tensor = paddle.to_tensor(batch_images) # 执行预测 predictions = trainer.model(batch_tensor) # 使用模型进行预测[^1] ``` #### 4. 后处理与结果保存 将预测结果后处理,并将其保存到 `pred_result.txt` 文件中。 ```python with open('pred_result.txt', 'w') as f: for i, pred in enumerate(predictions): boxes = pred['boxes'] # 获取边界框 scores = pred['scores'] # 获取置信度 labels = pred['labels'] # 获取类别标签 for box, score, label in zip(boxes, scores, labels): line = f"Image {i}: Box={box.tolist()}, Score={score:.4f}, Label={label}\n" f.write(line) ``` 以上代码实现了从加载模型、批量预测到结果保存的完整流程。 --- #### 注意事项 - 确保配置文件和模型权重路径正确无误。 - 如果出现类似引用[3]中的 cls 参数不匹配问题,可能是因为模型架构或配置文件版本不一致,需检查模型是否与配置文件完全匹配[^3]。 - 预测时,模型的最后一层通常会包含 sigmoid 激活函数,若未包含,则需手动添加以确保输出值范围在 [0, 1] 内[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值