鸢尾花等表格数据简单分类器(模型可以替换)
pip install keras
pip install tensorflow
pip install graphviz
pip install pydot-ng
pip install pydot
pip install pandas
pip install sklearn
https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi
加入path后 报错修改pydot.py dot.bat 后缀 .bat 为 .exe
def get_executable_extension():
# type: () -> str
if is_windows():
return '.exe' if is_anacoda() else '.exe'
else:
return ''
Keras 2.2.4
Keras-Applications 1.0.6
Keras-Preprocessing 1.0.5
tensorflow 1.11.0
numpy 1.15.2
pandas 0.23.4
scikit-learn 0.20.0
# -*- coding: utf-8 -*-
import numpy
import pandas
from keras.layers.core import Dense, Dropout, Activation
from keras.models import Sequential
from keras.utils import np_utils
from keras.utils import plot_model
from sklearn import utils
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.preprocessing import LabelEncoder
def load_data():
'''
获取数据
:return x_train, y_train, x_test, y_test, encoder:
'''
# 载入数据
data_frame = pandas.read_csv("iris.csv", header=None)
data_set = data_frame.values
# 取所有行,从第0列到第4列(不包含第4列)
x_data = data_set[:, 0:4].astype(float)
# 取所有行,第4列
y_data = data_set[:, 4]
# 标签编码
encoder = LabelEncoder()
# 将字符串编译成0,1,2,3分类
# encoder.classes_以npy可以保存加载编码规则(np.save('encoder.npy',encoder.classes_),encoder.classes_=np.load('encoder.npy'))
encoded_transform_y = encoder.fit_transform(y_data)
# 编译好的0,1,2,3 One_Hot
y_data = np_utils.to_categorical(encoded_transform_y)
# 打乱数据集
x_data, y_data = utils.shuffle(x_data, y_data)
# 切分数据集
train_idx, test_idx = next(iter(
StratifiedShuffleSplit(n_splits=1, test_size=0.2,
random_state=0).split(x_data, y_data)))
x_train = x_data[train_idx]
y_train = y_data[train_idx]
x_test = x_data[test_idx]
y_test = y_data[test_idx]
return x_train, y_train, x_test, y_test, encoder
def compile_model():
# 模型
_model = Sequential()
_model.add(Dense(10, input_shape=(4,)))
_model.add(Activation('tanh'))
_model.add(Dropout(0.2))
_model.add(Dense(3))
_model.add(Activation('softmax'))
_model.compile(
loss="categorical_crossentropy",
optimizer='adam',
metrics=['accuracy'])
# 生成模型图片
plot_model(_model, to_file='model.png', show_shapes='True')
return _model
def train_model(_model, _x_train, _y_train, _x_test, _y_test):
# 训练
history = _model.fit(_x_train, _y_train, epochs=100, batch_size=12,
verbose=1, validation_data=[_x_test, _y_test])
# 测试训练集
score = _model.evaluate(_x_test, _y_test, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
def test(_model, _encoder, _x_test):
# 校验,返回标签
result = _model.predict(_x_test)
result = numpy.argmax(result, axis=1)
result = _encoder.inverse_transform(result)
print(result)
if __name__ == '__main__':
x_train, y_train, x_test, y_test, encoder = load_data()
model = compile_model()
train_model(model, x_train, y_train, x_test, y_test)
test(model, encoder, x_test)