Animal Species Prediction Using Python
Budget: ₹150,000 – ₹250,000 INR
Here’s a structured approach for your Animal Species Prediction using Random Forest in Python with pandas, seaborn, and scikit-learn:
1. Data Inspection & Cleaning
python
Copy code
import pandas as pd
# Load dataset
df = pd.read_csv("your_dataset.csv")
# Inspect dataset
print(df.info())
print(df.describe())
print(df.isnull().sum()) # Check missing values
# Handle missing values (if any)
df.fillna(df.median(), inplace=True) # Example: Filling with median values
# Check for duplicate rows
df.drop_duplicates(inplace=True)
2. Data Visualization
python
Copy code
import seaborn as sns
import matplotlib.pyplot as plt
# Countplot of species
sns.countplot(x="species", data=df)
plt.show()
# Pairplot to visualize relationships
sns.pairplot(df, hue="species")
plt.show()
3. Train-Test Split & Encoding
python
Copy code
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Encode categorical target variable
le = LabelEncoder()
df["species"] = le.fit_transform(df["species"])
# Split data
X = df.drop("species", axis=1)
y = df["species"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
4. Model Training with Random Forest
python
Copy code
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
# Initialize and train model
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
# Predictions
y_pred = rf.predict(X_test)
# Accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.2f}")
5. Confusion Matrix & Accuracy Improvement
python
Copy code
import seaborn as sns
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
Ways to Improve Accuracy:
Feature Engineering: Create new features based on domain knowledge.
Hyperparameter Tuning: Use GridSearchCV or RandomizedSearchCV to optimize parameters.
Increase n_estimators: More trees can improve accuracy but increase computation time.
Feature Selection: Remove irrelevant features to reduce overfitting.
Do you need help with hyperparameter tuning for better performance?
1. Data Inspection & Cleaning
python
Copy code
import pandas as pd
# Load dataset
df = pd.read_csv("your_dataset.csv")
# Inspect dataset
print(df.info())
print(df.describe())
print(df.isnull().sum()) # Check missing values
# Handle missing values (if any)
df.fillna(df.median(), inplace=True) # Example: Filling with median values
# Check for duplicate rows
df.drop_duplicates(inplace=True)
2. Data Visualization
python
Copy code
import seaborn as sns
import matplotlib.pyplot as plt
# Countplot of species
sns.countplot(x="species", data=df)
plt.show()
# Pairplot to visualize relationships
sns.pairplot(df, hue="species")
plt.show()
3. Train-Test Split & Encoding
python
Copy code
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Encode categorical target variable
le = LabelEncoder()
df["species"] = le.fit_transform(df["species"])
# Split data
X = df.drop("species", axis=1)
y = df["species"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
4. Model Training with Random Forest
python
Copy code
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
# Initialize and train model
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
# Predictions
y_pred = rf.predict(X_test)
# Accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.2f}")
5. Confusion Matrix & Accuracy Improvement
python
Copy code
import seaborn as sns
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
Ways to Improve Accuracy:
Feature Engineering: Create new features based on domain knowledge.
Hyperparameter Tuning: Use GridSearchCV or RandomizedSearchCV to optimize parameters.
Increase n_estimators: More trees can improve accuracy but increase computation time.
Feature Selection: Remove irrelevant features to reduce overfitting.
Do you need help with hyperparameter tuning for better performance?