加载Tensorflow数据集"Iris"并将标签更改为one-hot编码的步骤如下:
import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder
iris = tf.keras.datasets.iris
(x_train, y_train), (x_test, y_test) = iris.load_data()
encoder = OneHotEncoder(sparse=False)
y_train = encoder.fit_transform(y_train.reshape(-1, 1))
y_test = encoder.transform(y_test.reshape(-1, 1))
print("训练集维度:", x_train.shape)
print("训练集标签维度:", y_train.shape)
print("测试集维度:", x_test.shape)
print("测试集标签维度:", y_test.shape)
完整代码示例:
import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder
# 加载"Iris"数据集
iris = tf.keras.datasets.iris
(x_train, y_train), (x_test, y_test) = iris.load_data()
# 将标签进行one-hot编码
encoder = OneHotEncoder(sparse=False)
y_train = encoder.fit_transform(y_train.reshape(-1, 1))
y_test = encoder.transform(y_test.reshape(-1, 1))
# 打印数据集的维度信息
print("训练集维度:", x_train.shape)
print("训练集标签维度:", y_train.shape)
print("测试集维度:", x_test.shape)
print("测试集标签维度:", y_test.shape)
这段代码加载了Tensorflow中的"Iris"数据集,并使用sklearn库中的OneHotEncoder将标签进行了one-hot编码。最后打印了训练集和测试集的维度信息。
领取专属 10元无门槛券
手把手带您无忧上云