Package: py-automl
Py-AutoMLIntroductionWhat is Py-AutoML?Py-AutoML is an open sourcelow-codemachine learning library in Python that aims to reduce the hypothesis to insights cycle time in a ML experiment. It mainly helps to do our pet projects quickly and efficiently. In comparison with the other open source machine learning libraries, Py-AutoML is an alternative low-code library that can be used to perform complex machine learning tasks with only few lines of code. Py-AutoML is essentially a Python wrapper around several machine learning libraries and frameworks such asscikit-learn, 'tensorflow','keras' and many more.The design and simplicity of Py-AutoML is inspired by the  two principles KISS (keep it simple and sweet) and DRY (Don't Repeat Yourself) . We as engineers have to find a way  effective way to mitigate this gap and address data related challenges in business setting.ModulesPy-AutoML is a minimalistic library which not  simplifies the machine learning tasks and also makes our work easier.Py-AutoML consists of so many functionalities. such asmodel.py- implementing popular neural networks such as googlenet , vgg16, simple cnn ,basic cnn, lenet5, alexnet, lstm, mlp etc..checkpoint.py - consists of callbacks function which is used to store metricsutils.py - consists of some functionalities used to preprocess test images, spliting the data.preprocess.py - used to preprocess image dataset such as resize, reshape, convert to greyscale, normalisation etc..ml.py - allow us to implement and check metrics of popular classical machine learning models such as random forest, decision tree, svm , logistic regression and also displays metric reports of every modelvisualize.py - allow us to visualize neural networks in pictorial and graphs form.ml.py -> Implemented algorithmsLogistic RegressionSupport Vector MachineDecision Tree ClassifierRandom Forest ClassifierK-Nearest Neighborsmodel.py -> Implemented popular neural network architecturesGoogleNetVGG16AlexNetLenet5Inceptionsimple & basic cnnbasic_mlp & deep_mlplstmwith predefined configurationsGetting startedInstall the packagepipinstallpy-automlNavigate to folder and install requirements:pipinstall-rrequirements.txtUsageImporting the packageimportpyAutoMLfrompyAutoMLimport*frompyAutoML.modelimport*# like that...Assign the variables X and Y to the desired columns and assign the variable size to the desired test_size.X=<df.features>Y=<df.target>size=<test_size>Encoding Categorical DataEncode target variable if non-numerical:frompyAutoMLimport*Y=EncodeCategorical(Y)Running py-automlsignature is as follows :   ML(X, Y, size=0.25, *args)frompyAutoML.mlimportML,ml,EncodeCategoricalimportpandasaspdimportnumpyasnpfromsklearn.ensembleimportRandomForestClassifierfromsklearn.treeimportDecisionTreeClassifierfromsklearn.neighborsimportKNeighborsClassifierfromsklearn.linear_modelimportLogisticRegressionfromsklearn.svmimportSVCfromsklearnimportdatasets##reading the Iris dataset into the codedf=datasets.load_iris()##assigning the desired columns to X and Y  in preparation for running fastMLX=df.data[:,:4]Y=df.target##running the EncodeCategorical function from fastML to handle the process of categorial encoding of dataY=EncodeCategorical(Y)size=0.33ML(X,Y,size,SVC(),RandomForestClassifier(),DecisionTreeClassifier(),KNeighborsClassifier(),LogisticRegression(max_iter=7000))output____________________________________________________.....................Py-AutoML......................____________________________________________________SVC______________________________AccuracyScoreforSVCis0.98ConfusionMatrixforSVCis[[1600][0181][0015]]ClassificationReportforSVCisprecisionrecallf1-scoresupport01.001.001.001611.000.950.971920.941.000.9715accuracy0.9850macroavg0.980.980.9850weightedavg0.980.980.9850____________________________________________________RandomForestClassifier______________________________AccuracyScoreforRandomForestClassifieris0.96ConfusionMatrixforRandomForestClassifieris[[1600][0181][0114]]ClassificationReportforRandomForestClassifierisprecisionrecallf1-scoresupport01.001.001.001610.950.950.951920.930.930.9315accuracy0.9650macroavg0.960.960.9650weightedavg0.960.960.9650____________________________________________________DecisionTreeClassifier______________________________AccuracyScoreforDecisionTreeClassifieris0.98ConfusionMatrixforDecisionTreeClassifieris[[1600][0181][0015]]ClassificationReportforDecisionTreeClassifierisprecisionrecallf1-scoresupport01.001.001.001611.000.950.971920.941.000.9715accuracy0.9850macroavg0.980.980.9850weightedavg0.980.980.9850____________________________________________________KNeighborsClassifier______________________________AccuracyScoreforKNeighborsClassifieris0.98ConfusionMatrixforKNeighborsClassifieris[[1600][0181][0015]]ClassificationReportforKNeighborsClassifierisprecisionrecallf1-scoresupport01.001.001.001611.000.950.971920.941.000.9715accuracy0.9850macroavg0.980.980.9850weightedavg0.980.980.9850____________________________________________________LogisticRegression______________________________AccuracyScoreforLogisticRegressionis0.98ConfusionMatrixforLogisticRegressionis[[1600][0181][0015]]ClassificationReportforLogisticRegressionisprecisionrecallf1-scoresupport01.001.001.001611.000.950.971920.941.000.9715accuracy0.9850macroavg0.980.980.9850weightedavg0.980.980.9850ModelAccuracy0SVC0.981RandomForestClassifier0.962DecisionTreeClassifier0.983KNeighborsClassifier0.984LogisticRegression0.98you can also write as followsML(X,Y)output____________________________________________________.....................Py-AutoML......................____________________________________________________SVC______________________________AccuracyScoreforSVCis0.9736842105263158ConfusionMatrixforSVCis[[1300][0151][009]]ClassificationReportforSVCisprecisionrecallf1-scoresupport01.001.001.001311.000.940.971620.901.000.959accuracy0.9738macroavg0.970.980.9738weightedavg0.980.970.9738____________________________________________________RandomForestClassifier______________________________AccuracyScoreforRandomForestClassifieris0.9736842105263158ConfusionMatrixforRandomForestClassifieris[[1300][0151][009]]ClassificationReportforRandomForestClassifierisprecisionrecallf1-scoresupport01.001.001.001311.000.940.971620.901.000.959accuracy0.9738macroavg0.970.980.9738weightedavg0.980.970.9738____________________________________________________DecisionTreeClassifier______________________________AccuracyScoreforDecisionTreeClassifieris0.9736842105263158ConfusionMatrixforDecisionTreeClassifieris[[1300][0151][009]]ClassificationReportforDecisionTreeClassifierisprecisionrecallf1-scoresupport01.001.001.001311.000.940.971620.901.000.959accuracy0.9738macroavg0.970.980.9738weightedavg0.980.970.9738____________________________________________________KNeighborsClassifier______________________________AccuracyScoreforKNeighborsClassifieris0.9736842105263158ConfusionMatrixforKNeighborsClassifieris[[1300][0151][009]]ClassificationReportforKNeighborsClassifierisprecisionrecallf1-scoresupport01.001.001.001311.000.940.971620.901.000.959accuracy0.9738macroavg0.970.980.9738weightedavg0.980.970.9738____________________________________________________LogisticRegression______________________________AccuracyScoreforLogisticRegressionis0.9736842105263158ConfusionMatrixforLogisticRegressionis[[1300][0151][009]]ClassificationReportforLogisticRegressionisprecisionrecallf1-scoresupport01.001.001.001311.000.940.971620.901.000.959accuracy0.9738macroavg0.970.980.9738weightedavg0.980.970.9738ModelAccuracy0SVC0.97368421052631581RandomForestClassifier0.97368421052631582DecisionTreeClassifier0.97368421052631583KNeighborsClassifier0.97368421052631584LogisticRegression0.9736842105263158Defining popular neural networksimplementing alexNet may looks like this#InstantiationAlexNet=Sequential()#1st Convolutional LayerAlexNet.add(Conv2D(filters=96,input_shape=input_shape,kernel_size=(11,11),strides=(4,4),padding='same'))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))AlexNet.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))#2nd Convolutional LayerAlexNet.add(Conv2D(filters=256,kernel_size=(5,5),strides=(1,1),padding='same'))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))AlexNet.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))#3rd Convolutional LayerAlexNet.add(Conv2D(filters=384,kernel_size=(3,3),strides=(1,1),padding='same'))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))#4th Convolutional LayerAlexNet.add(Conv2D(filters=384,kernel_size=(3,3),strides=(1,1),padding='same'))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))#5th Convolutional LayerAlexNet.add(Conv2D(filters=256,kernel_size=(3,3),strides=(1,1),padding='same'))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))AlexNet.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))#Passing it to a Fully Connected layerAlexNet.add(Flatten())# 1st Fully Connected LayerAlexNet.add(Dense(4096,input_shape=(32,32,3,)))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))# Add Dropout to prevent overfittingAlexNet.add(Dropout(0.4))#2nd Fully Connected LayerAlexNet.add(Dense(4096))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))#Add DropoutAlexNet.add(Dropout(0.4))#3rd Fully Connected LayerAlexNet.add(Dense(1000))AlexNet.add(BatchNormalization())AlexNet.add(Activation('relu'))#Add DropoutAlexNet.add(Dropout(0.4))#Output LayerAlexNet.add(Dense(10))AlexNet.add(BatchNormalization())AlexNet.add(Activation(classifier_function))AlexNet.compile('adam',loss_function,metrics=['acc'])returnAlexNetBut we implement this in a single line of code like below using this package.alexNet_model=model(input_shape=(30,30,4),arch="alexNet",classify="Mulit")Similarly we can also implementalexNet_model=model("alexNet")lenet5_model=model("lenet5")googleNet_model=model("googleNet")vgg16_model=model("vgg16")### etc...For more generalization , let's observe following code.# Lets take all models that are defined in the py_automl and which are implemented in a signle line of codemodels=["simple_cnn","basic_cnn","googleNet","inception","vgg16","lenet5","alexNet","basic_mlp","deep_mlp","basic_lstm","deep_lstm"]d={}foriinmodels:d[i]=model(i)# assigning all architectures to its model names using dictionaryVisualizationwe can visualize neural networks architecture in different forms with ease.Let's observe the following code for better understandingimportkerasfromkerasimportlayersmodel=keras.Sequential()model.add(layers.Conv2D(filters=6,kernel_size=(3,3),activation='relu',input_shape=(32,32,1)))model.add(layers.AveragePooling2D())model.add(layers.Conv2D(filters=16,kernel_size=(3,3),activation='relu'))model.add(layers.AveragePooling2D())model.add(layers.Flatten())model.add(layers.Dense(units=120,activation='relu'))model.add(layers.Dense(units=84,activation='relu'))model.add(layers.Dense(units=10,activation='softmax'))now let's visualise thisnn_visualize(model)By default , it returns keras visualization objectoutput:fromkeras.modelsimportSequentialfromkeras.layersimportDenseimportnumpy# fix random seed for reproducibilitynumpy.random.seed(7)# load pima indians datasetdataset=numpy.loadtxt("pima-indians-diabetes.csv",delimiter=",")# split into input (X) and output (Y) variablesX=dataset[:,0:8]Y=dataset[:,8]# create modelmodel=Sequential()model.add(Dense(12,input_dim=8,activation='relu'))model.add(Dense(8,activation='relu'))model.add(Dense(1,activation='sigmoid'))# Compile modelmodel.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])# Fit the modelmodel.fit(X,Y,epochs=150,batch_size=10)# evaluate the modelscores=model.evaluate(X,Y)print("\n%s:%.2f%%"%(model.metrics_names[1],scores[1]*100))#Neural network visualizationnn_visualize(model,type="graphviz")outputThis library is so developer friendly that even we declare type with starting letters.frompyAutoML.modelimport*model2=model(arch="alexNet")nn_visualize(model2,type="k")output:This is a minimal documentation about the package.For more information and understanding, see examplesHEREand source code:GITHUBAuthor:Prudhvi GNVContact:LinkedInGithubInstagram