python-kerasHow do I use Python and Keras to perform binary classification?
Using Python and Keras to perform binary classification consists of the following steps:
-
Import the necessary packages.
import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier
-
Load the data and preprocess it.
#load data dataset = pd.read_csv('data.csv')
split into input (X) and output (Y) variables
X = dataset.iloc[:,0:8].values Y = dataset.iloc[:,8].values
3. Define the model.
define the keras model
def create_model():
create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
#compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
4. Create the model.
model = create_model()
5. Fit the model to the data.
model.fit(X, Y, epochs=150, batch_size=10)
6. Evaluate the model.
scores = model.evaluate(X, Y) print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
7. Make predictions.
predictions = model.predict(X) rounded = [round(x[0]) for x in predictions] print(rounded)
## Output example
accuracy: 85.25% [0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]
## Helpful links
- [Keras Documentation](https://keras.io/)
- [Using Keras for Binary Classification](https://machinelearningmastery.com/binary-classification-tutorial-with-the-keras-deep-learning-library/)
More of Python Keras
- How do I use Python Keras to zip a file?
- How do I set the input shape when using Keras with Python?
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How do I use the to_categorical function from TensorFlow in Python to convert data into a format suitable for a neural network?
- How can I use Python Keras to create a neural network with zero hidden layers?
- How do I use Python Keras to create a Zoom application?
- How can I use YOLO with Python and Keras?
- How can I use XGBoost, Python and Keras together to build a machine learning model?
- How do I use the Softmax activation function with Python and Keras?
- How do I use zero padding in Python Keras?
See more codes...