Neural Network Algorithm (for Crab Age Prediction)
How it Works: A Neural Network consists of layers of nodes (neurons) where each node performs a mathematical
operation. For regression tasks like predicting the age of crabs, it learns complex patterns by adjusting weights
through backpropagation.
Steps:
1. Collect Data: Gather crab data (e.g., weight, size, shell dimensions) and corresponding age.
2. Preprocess Data: Normalize or standardize the features for better performance.
3. Build Network: Create a neural network with an input layer, one or more hidden layers, and an output layer.
4. Train Model: Use the training data to adjust weights and minimize the error between predicted and actual
ages.
5. Evaluate: Use metrics like Mean Squared Error (MSE) to assess model performance.
Advantages:
Can model highly complex relationships and non-linear patterns.
Works well for large datasets with intricate feature interactions
CODE
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.metrics import accuracy_score
# Load your dataset (replace 'your_dataset.csv' with your actual file)
dataset = pd.read_csv('your_dataset.csv')
# Features (X) and target variable (y)
X = dataset.iloc[:, :-1].values # Features (all columns except the target)
y = dataset.iloc[:, -1].values # Target variable (the last column)
# Split the data into training and testing sets (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Feature Scaling (important for neural networks)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Build the Neural Network model
model = Sequential()
# Input Layer (and first hidden layer)
model.add(Dense(units=64, activation='relu', input_dim=X_train.shape[1]))
# Hidden Layer
model.add(Dense(units=32, activation='relu'))
# Output Layer
model.add(Dense(units=1, activation='sigmoid')) # For binary classification (use 'softmax' for multi-class)
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=50, batch_size=32)
# Evaluate the model on the test set
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5) # Convert probabilities to 0 or 1 for binary classification
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
# Print the accuracy
print(f"Accuracy of Neural Network model: {accuracy * 100:.2f}%")
Accuracy of Neural Network model: 80.00%