2. Neural Networks
Activity 2.01: Build a Multilayer Neural Network to Classify Sonar Signals
Solution
Let's see how the solution looks. Remember—this is one solution, but there could be many variations:
- Import all the required libraries:
import tensorflow as tf import pandas as pd from sklearn.preprocessing import LabelEncoder # Import Keras libraries from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense
- Load and examine the data:
df = pd.read_csv('sonar.csv') df.head()
The output is:
Observe that there are 60 features, and the target has two values—Rock and Mine.
This means that this is a binary classification problem. Let's prepare the data before we build the neural network.
- Separate the features and the labels:
X_input = df.iloc[:, :-1] Y_label = df['Class'].values
In this code,
X_input
is selecting all the rows of all the columns except theClass...