0

I have no more knowledge of python. This is my ANN modal code in python. This code contains to predict customers situation in binary output. Which is customers leave or not.

Code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Part 2 - Now let's make the ANN!

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 11))

# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)

# Part 3 - Making predictions and evaluating the model

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

I want to know how to save this modal as h5 using keras. After I saved it how to load again in another project to predict the data.

Ind
  • 377
  • 2
  • 6
  • 16
  • "m5" or ".h5"? if it's the latter, these links may help - https://machinelearningmastery.com/save-load-keras-deep-learning-models/ and https://jovianlin.io/saving-loading-keras-models/ – gireesh4manu Jan 19 '19 at 06:25
  • This link on SO gives a more detailed explanation for the question in picture - https://stackoverflow.com/questions/47266383/save-and-load-weights-in-keras – gireesh4manu Jan 19 '19 at 06:26
  • try pickle module in python.. – Ashu Grover Jan 19 '19 at 06:31
  • why pickle is good? – Ind Jan 19 '19 at 06:36
  • Possible duplicate of [How to save final model using keras?](https://stackoverflow.com/questions/42763094/how-to-save-final-model-using-keras) – sdcbr Jan 19 '19 at 10:33

1 Answers1

3

Inorder to save the model, You can do the one below:

model.save('model_file.h5')

And to load the model back use:

 from keras.models import load_model
 my_model = load_model('model_file.h5')
Fasty
  • 784
  • 1
  • 11
  • 34