Machine learning 通过PyCharm存储预处理的数据供以后使用

Machine learning 通过PyCharm存储预处理的数据供以后使用,machine-learning,keras,pycharm,save,Machine Learning,Keras,Pycharm,Save,使用Keras为机器学习算法生成数据后,如何保存LabelEncoder数据 数据通过以下代码生成: from sklearn.preprocessing import LabelEncoder from keras.utils import to_categorical # Convert features and corresponding classification labels into numpy arrays X = np.array(featuresdf.feature.tol

使用Keras为机器学习算法生成数据后,如何保存LabelEncoder数据

数据通过以下代码生成:

from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical

# Convert features and corresponding classification labels into numpy arrays
X = np.array(featuresdf.feature.tolist())
y = np.array(featuresdf.class_label.tolist())

# Encode the classification labels
le = LabelEncoder()
yy = to_categorical(le.fit_transform(y))

# split the dataset
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(X, yy, test_size=0.2, random_state = 42)
pickle.dump(le, open(filename, 'wb')
通过使用Jupyter笔记本,您可以通过以下方式保存数据:

### store the preprocessed data for use in the next notebook

%store x_train
%store x_test
%store y_train
%store y_test
%store yy
%store le
savetxt(x_train.csv, x_train, delimiter=',')
LabelEnc = open(filename, 'rb')
le = pickle.load(LabelEnc)
在PyCharm中,我可以通过以下方式成功保存
x\u列车
数据:

### store the preprocessed data for use in the next notebook

%store x_train
%store x_test
%store y_train
%store y_test
%store yy
%store le
savetxt(x_train.csv, x_train, delimiter=',')
LabelEnc = open(filename, 'rb')
le = pickle.load(LabelEnc)
但是,此方法不适用于
LabelEncoder

然后,我尝试通过以下代码使用
pickle

from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical

# Convert features and corresponding classification labels into numpy arrays
X = np.array(featuresdf.feature.tolist())
y = np.array(featuresdf.class_label.tolist())

# Encode the classification labels
le = LabelEncoder()
yy = to_categorical(le.fit_transform(y))

# split the dataset
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(X, yy, test_size=0.2, random_state = 42)
pickle.dump(le, open(filename, 'wb')
保存编码数据。然而,当我通过以下方式回忆数据时:

### store the preprocessed data for use in the next notebook

%store x_train
%store x_test
%store y_train
%store y_test
%store yy
%store le
savetxt(x_train.csv, x_train, delimiter=',')
LabelEnc = open(filename, 'rb')
le = pickle.load(LabelEnc)
我得到错误:
…ValueError(“无法加载包含pickle数据的文件”ValueError:allow\u pickle=False时无法加载包含pickle数据的文件

如何正确保存和调用
le
数据

这是PyCharm的“显示变量”窗口中的数据片段:


le
变量是对LabelEncoder()的引用只是类的一个实例,不包含任何数据。能否显示完整的代码,因为pickle没有给出此类错误,可能是您以错误的方式保存了文件,也可能是您更新了pickle关键字。@ashwingeted'Sa。感谢您的回复!我添加了PyCharm“show Variables”中数据的屏幕截图窗口。我以后如何使用
le
LabelEncoder()
中的数据?您可以调用
new\u le=LabelEncoder()
;因为您没有任何特定的相关数据。另外,只需将文件读写更改为“r”和“w”,而不是“rb”和“wb”