Iris Classification on Keras
Installation
Python3 版本为 3.6.4 : : Anaconda
conda install tensorflow==1.15.0
conda install keras==2.1.6
Code
# encoding:utf8
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.utils import to_categorical
if __name__ == '__main__':
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(
iris.data,
iris.target,
test_size=0.2,
random_state=20)
model = Sequential([
Dense(8, input_dim=4),
Activation('sigmoid'),
Dense(8),
Activation('relu'),
Dense(3),
Activation('softmax')
])
model.compile(
optimizer='Adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, to_categorical(y_train, num_classes=3), epochs=70)
y_pred = model.predict_classes(x_test)
print(classification_report(y_test, y_pred, target_names=iris.target_names))
Errors
Traceback (most recent call last):
File ".../Iris.py", line 32, in <module>
y_pred = model.predict(x_train)
File "...Miniconda3envs flibsite-packageskerasengine raining.py", line 1169, in predict
steps=steps)
File "...Miniconda3envs flibsite-packageskerasengine raining_arrays.py", line 300, in predict_loop
outs.append(np.zeros(shape, dtype=batch_out.dtype))
TypeError: data type not understood
Solution: 重新配置环境,重新安装 keras, Tensorflow 等。
conda env list # look up
conda remove -n [env name] --all # delete