this my code by giving me error on the code can someone help me with code
Question:
this my code by giving me error on the code can someone help me with code
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Feature engineering: Create 'Child' feature
train['Child'] = train['Age'].apply(lambda x: 1 if x
# Encode categorical columns
train['Embarked'] = train['Embarked'].fillna('S')
train['Embarked'] = train['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
train['Sex'] = train['Sex'].map({'male': 0, 'female': 1})
# Split dataset into features and target
X = train.drop(['Survived', 'Name', 'Ticket', 'Cabin', 'Age'], axis=1)
y = train['Survived']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize models
models = {
'Logistic Regression': LogisticRegression(max_iter=1000),
'Decision Tree': DecisionTreeClassifier(random_state=42),
'Random Forest': RandomForestClassifier(random_state=42)
}
# Train and evaluate models
for model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f" Accuracy: {accuracy:.4f}")
Cornerstones of Financial and Managerial Accounting
ISBN: 978-1111879044
2nd edition
Authors: Rich, Jeff Jones, Dan Heitger, Maryanne Mowen, Don Hansen