3. Decision Tree Algorithm
3. Decision Tree Algorithm
How it Works: A Decision Tree splits data into branches based on feature values, forming a tree-like structure. Each
node represents a decision based on a feature, and each leaf node represents a predicted value (e.g., the crab's age).
Steps:
1. Collect Data: Gather data on crabs' physical features (e.g., weight, shell length) and their corresponding ages.
2. Preprocess Data: Handle missing data, and split into training and testing sets.
3. Train Model: Build a Decision Tree by recursively splitting the data based on the best feature that reduces
impurity (e.g., using metrics like Gini or MSE).
4. Evaluate: Use metrics like Mean Absolute Error (MAE) and R² to evaluate the model's accuracy.
Advantages:
import pandas as pd
dataset = pd.read_csv('your_dataset.csv')
# Split the data into training and testing sets (80% training, 20% testing)
model = DecisionTreeClassifier(random_state=42)
y_pred = model.predict(X_test)