如何从TensorFlow模型预测输出中提取单个数值元素?
提取TensorFlow模型预测的单个数值结果
你得到的输出是一个二维numpy数组(形状为(1,1)),要提取其中的单个数值,可以用以下两种简洁方法:
- 方法一:通过数组索引提取并转换为Python原生浮点类型
prediction = UberLyft_model.predict(my_UberLyft_encoded) single_value = float(prediction[0][0])
- 方法二:使用numpy数组的
.item()方法(推荐,更简洁)
prediction = UberLyft_model.predict(my_UberLyft_encoded) single_value = prediction.item()
.item()方法会直接从形状为(1,1)或(1,)的numpy数组中提取出单个Python数值,无需手动索引,同时自动完成类型转换。
内容的提问来源于stack exchange,提问作者Swifty
相关产品推荐
相关产品推荐

