Simple classifier for tabular data such as IRIS (model can be replaced)

Source: Internet
Author: User
Tags keras

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

Test success

1 #-*-coding:utf-8-*-2 ImportNumPy3 ImportPandas4  fromKeras.layers.coreImportdense, dropout, Activation5  fromKeras.modelsImportSequential6  fromKeras.utilsImportnp_utils7  fromKeras.utilsImportPlot_model8  fromSklearnImportUtils9  fromSklearn.model_selectionImportStratifiedshufflesplitTen  fromSklearn.preprocessingImportLabelencoder One  A  - defload_data (): -     " " the Get Data - : Return X_train, Y_train, X_test, Y_test, encoder: -     " " -     #Loading Data +Data_frame = Pandas.read_csv ("Iris.csv", header=None) -Data_set =data_frame.values +     #take all rows, from column NO. 0 to column 4th (not including column 4th) AX_data = data_set[:, 0:4].astype (float) at     #fetch All rows, 4th column -Y_data = data_set[:, 4] -     #Tag Encoding -Encoder =Labelencoder () -     #compiling a string into a 0,1,2,3 class -     #Encoder.classes_ with NPY can save the load Encoding rules (Np.save (' Encoder.npy ', Encoder.classes_), Encoder.classes_=np.load (' Encoder.npy ')) inEncoded_transform_y =encoder.fit_transform (y_data) -     #compiled 0,1,2,3 one_hot toY_data =np_utils.to_categorical (encoded_transform_y) +     #Disrupt data sets -X_data, Y_data =utils.shuffle (X_data, Y_data) the     #Slicing data sets *Train_idx, Test_idx =Next (ITER ( $Stratifiedshufflesplit (N_splits=1, test_size=0.2,Panax NotoginsengRandom_state=0). Split (X_data, Y_data ))) -X_train =X_data[train_idx] theY_train =Y_data[train_idx] +X_test =X_data[test_idx] AY_test =Y_data[test_idx] the     returnX_train, Y_train, X_test, y_test, encoder +  -  $ defCompile_model (): $     #Model -_model =Sequential () -_model.add (Dense, input_shape= (4,))) the_model.add (Activation ('Tanh')) -_model.add (Dropout (0.2))Wuyi_model.add (Dense (3)) the_model.add (Activation ('Softmax')) - _model.compile ( Wuloss="categorical_crossentropy", -Optimizer='Adam', Aboutmetrics=['accuracy']) $     #Create a model picture -Plot_model (_model, to_file='Model.png', show_shapes='True') -     return_model -  A  + defTrain_model (_model, _x_train, _y_train, _x_test, _y_test): the     #Training -History = _model.fit (_x_train, _y_train, epochs=100, batch_size=12, $Verbose=1, Validation_data=[_x_test, _y_test]) the     #Test Training Set theScore = _model.evaluate (_x_test, _y_test, verbose=1) the     Print('Test Score:', score[0]) the     Print('Test accuracy:', score[1]) -  in  the defTest (_model, _encoder, _x_test): the     #Check, return label Aboutresult =_model.predict (_x_test) theresult = Numpy.argmax (result, Axis=1) theresult =_encoder.inverse_transform (Result) the     Print(Result) +  -  the if __name__=='__main__':BayiX_train, Y_train, X_test, y_test, encoder =Load_data () theModel =Compile_model () the Train_model (model, X_train, Y_train, X_test, y_test) -Test (model, encoder, x_test)

Simple classifier for tabular data such as IRIS (model can be replaced)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.