-
-
Save anjilinux/12cf3c5138bf89e7fad4a935c99c0429 to your computer and use it in GitHub Desktop.
Build and Run a Docker Container for your Machine Learning Model (train.py - code 8)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| # train.py | |
| # Xavier Vasques 13/04/2021 | |
| import platform; print(platform.platform()) | |
| import sys; print("Python", sys.version) | |
| import numpy; print("NumPy", numpy.__version__) | |
| import scipy; print("SciPy", scipy.__version__) | |
| import os | |
| import numpy as np | |
| from sklearn.discriminant_analysis import LinearDiscriminantAnalysis | |
| from sklearn.neural_network import MLPClassifier | |
| import pandas as pd | |
| from joblib import dump | |
| from sklearn import preprocessing | |
| def train(): | |
| # Load directory paths for persisting model | |
| MODEL_DIR = os.environ["MODEL_DIR"] | |
| MODEL_FILE_LDA = os.environ["MODEL_FILE_LDA"] | |
| MODEL_FILE_NN = os.environ["MODEL_FILE_NN"] | |
| MODEL_PATH_LDA = os.path.join(MODEL_DIR, MODEL_FILE_LDA) | |
| MODEL_PATH_NN = os.path.join(MODEL_DIR, MODEL_FILE_NN) | |
| # Load, read and normalize training data | |
| training = "./train.csv" | |
| data_train = pd.read_csv(training) | |
| y_train = data_train['# Letter'].values | |
| X_train = data_train.drop(data_train.loc[:, 'Line':'# Letter'].columns, axis = 1) | |
| print("Shape of the training data") | |
| print(X_train.shape) | |
| print(y_train.shape) | |
| # Data normalization (0,1) | |
| X_train = preprocessing.normalize(X_train, norm='l2') | |
| # Models training | |
| # Linear Discrimant Analysis (Default parameters) | |
| clf_lda = LinearDiscriminantAnalysis() | |
| clf_lda.fit(X_train, y_train) | |
| # Save model | |
| from joblib import dump | |
| dump(clf_lda, MODEL_PATH_LDA) | |
| # Neural Networks multi-layer perceptron (MLP) algorithm | |
| clf_NN = MLPClassifier(solver='adam', activation='relu', alpha=0.0001, hidden_layer_sizes=(500,), random_state=0, max_iter=1000) | |
| clf_NN.fit(X_train, y_train) | |
| # Record model | |
| from joblib import dump, load | |
| dump(clf_NN, MODEL_PATH_NN) | |
| if __name__ == '__main__': | |
| train() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment