File size: 939 Bytes
ee3e557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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")