DSatishchandra commited on
Commit
ee3e557
·
verified ·
1 Parent(s): a70b9fe

Create train_tsh_model.py

Browse files
Files changed (1) hide show
  1. train_tsh_model.py +34 -0
train_tsh_model.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # train_tsh_model.py
2
+
3
+ import pandas as pd
4
+ import numpy as np
5
+ from sklearn.ensemble import RandomForestRegressor
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.metrics import r2_score
8
+ import joblib
9
+
10
+ # Load your dataset (update path if necessary)
11
+ df = pd.read_csv("thyroid_dataset.csv")
12
+
13
+ # Choose features and drop rows with missing values
14
+ features = ["T3", "TT4", "T4U", "FTI", "age"]
15
+ df = df.dropna(subset=features + ["TSH"])
16
+
17
+ # Prepare input (X) and output (y)
18
+ X = df[features]
19
+ y = df["TSH"]
20
+
21
+ # Train-test split
22
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
23
+
24
+ # Train the model
25
+ model = RandomForestRegressor(n_estimators=100, random_state=42)
26
+ model.fit(X_train, y_train)
27
+
28
+ # Evaluate the model
29
+ y_pred = model.predict(X_test)
30
+ print("TSH Model R² Score:", r2_score(y_test, y_pred))
31
+
32
+ # Save the model
33
+ joblib.dump(model, "tsh_model.pkl")
34
+ print("✅ Model saved as tsh_model.pkl")