AI-LAB_Satish / train_tsh_model.py
DSatishchandra's picture
Create train_tsh_model.py
ee3e557 verified
raw
history blame contribute delete
939 Bytes
# train_tsh_model.py
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
import joblib
# Load your dataset (update path if necessary)
df = pd.read_csv("thyroid_dataset.csv")
# Choose features and drop rows with missing values
features = ["T3", "TT4", "T4U", "FTI", "age"]
df = df.dropna(subset=features + ["TSH"])
# Prepare input (X) and output (y)
X = df[features]
y = df["TSH"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the model
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Evaluate the model
y_pred = model.predict(X_test)
print("TSH Model R² Score:", r2_score(y_test, y_pred))
# Save the model
joblib.dump(model, "tsh_model.pkl")
print("✅ Model saved as tsh_model.pkl")