如何解决上传到 ML-Predict 时出现 Google Cloud Platform (GCP) JSON 预测文件错误
我在将测试数据上传到 GCP 以进行预测时出错。我只是想将我的测试数据从 train_test_split 转换为 json 文件,以运行我部署的模型的 GCP 预测。
我的 X_test 都是 int 值。这是我将此数据框转换为换行符分隔的 json 文件。
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X_new,y,test_size=0.33,random_state=12,stratify = y) #stratify breaks up our split evenely
import json
X_test_10 = X_test.head(10) #gets first 10 items for prediction
X_test_10.to_json('test.json') #converts file to json
#creates newline delimited json per google instructions
with open("test.json","r") as read_file:
data = json.load(read_file)
result = [json.dumps(record) for record in data]
with open('nd-proceesed.json','w') as obj:
for i in result:
obj.write(i+'\n')
#Uploads created json file to GCP model for prediction
!gcloud ai-platform predict --model voluntary_turnover --region us-east1 --json-instances nd-proceesed.json
Using endpoint [https://us-east1-ml.googleapis.com/]
{
"error": "Prediction failed: Exception during sklearn prediction: could not convert string to float: 'response_count'"
}
错误信息 "error": "预测失败:sklearn 预测期间出现异常:无法将字符串转换为浮点数:'response_count'"
我不知道为什么会出现此错误,因为我的数据未按比例缩放并且我使用的是原始文件/数据。我是否需要转换为 numpy 数组然后上传?我不确定请帮忙!
解决方法
我能够使用下面的代码解决这个问题。
import json
X_test_10 = X_test.head(200)#gets first 200 items for prediction
X_test_10_list = X_test_10.values.tolist()
data = {}
data['instances'] = X_test_10_list
with open('data_for_prediction.txt','w') as outfile:
json.dump(data,outfile,indent = 2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。