File size: 2,069 Bytes
5cbc1e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
# Load the data
train_data = pd.read_csv("./input/train.csv")
test_data = pd.read_csv("./input/test.csv")
# Separate features and target
X = train_data.drop(["Attrition", "id"], axis=1)
y = train_data["Attrition"]
X_test = test_data.drop("id", axis=1)
# Identify categorical and numerical columns
categorical_cols = X.select_dtypes(include=["object"]).columns
numerical_cols = X.select_dtypes(include=["int64", "float64"]).columns
# Create the preprocessing pipelines for both numeric and categorical data
numeric_transformer = Pipeline(steps=[("scaler", StandardScaler())])
categorical_transformer = Pipeline(
steps=[("onehot", OneHotEncoder(handle_unknown="ignore"))]
)
# Combine preprocessing steps
preprocessor = ColumnTransformer(
transformers=[
("num", numeric_transformer, numerical_cols),
("cat", categorical_transformer, categorical_cols),
]
)
# Create a pipeline that combines the preprocessor with a classifier
model = Pipeline(
steps=[
("preprocessor", preprocessor),
("classifier", LogisticRegression(solver="liblinear")),
]
)
# Split the data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the model
model.fit(X_train, y_train)
# Predict probabilities on the validation set
y_pred_proba = model.predict_proba(X_val)[:, 1]
# Calculate the AUC
auc = roc_auc_score(y_val, y_pred_proba)
print(f"Validation AUC: {auc}")
# Predict probabilities on the test set
test_pred_proba = model.predict_proba(X_test)[:, 1]
# Create a submission file
submission = pd.DataFrame(
{"EmployeeNumber": test_data["id"], "Attrition": test_pred_proba}
)
submission.to_csv("./working/submission.csv", index=False)
|