pickle模块进行对象的序列化

也可以用joblib对象序列化和反序列化,二者的区别见pickle与joblib的区别

把对象存入文件

import pickle
with open('all_features_imputed_df.pkl', 'wb') as file:
    pickle.dump(all_features_imputed_df, file)
with open('all_labels.pkl', 'wb') as file:
    pickle.dump(all_labels, file)

从文件中加载对象

import pickle

 #打开文件,从二进制模式读取
with open('all_features_imputed_df.pkl', 'rb') as file:
    all_features_imputed_df = pickle.load(file)
    
with open('all_labels.pkl', 'rb') as file:
    all_labels = pickle.load(file)