การทำนายโรคเบาหวานด้วย Machine Learning
การทำนายโรคเบาหวานด้วย Machine Learning เป็นการประยุกต์ใช้เทคนิค AI ในการวิเคราะห์ข้อมูลสุขภาพเพื่อทำนายความเสี่ยงของคนที่จะเป็นโรคเบาหวาน โดยในบทความนี้จะใช้ Logistic Regression และ Support Vector Machine (SVM) ในการฝึกฝนโมเดลและทำนายผลจากชุดข้อมูลที่มีลักษณะต่างๆ เช่น ระดับน้ำตาลในเลือด, ความดันโลหิต, ดัชนีมวลกาย (BMI) เป็นต้น ในบทความนี้จะใช้ภาษา Python สำหรับการเขียนโค้ด และใช้ Google Colab โดยใช้ Machine Learning เพื่อทำนายโรคเบาหวาน โดยเลือกใช้สองโมเดลหลัก: Logistic Regression ซึ่งเป็นโมเดลเชิงเส้นที่ใช้ในการทำนายผลลัพธ์แบบสองค่า (binary classification) Support Vector Machine (SVM) ที่ใช้ในการหาค่าพิกัดที่ดีที่สุดในการแยกกลุ่มข้อมูลออกจากกัน มีการใช้ชุดข้อมูลที่มีตัวแปรหลายตัว เช่น ระดับน้ำตาลในเลือด, ความดันโลหิต, ดัชนีมวลกาย (BMI) เพื่อทำนายว่าแต่ละบุคคลจะเป็นโรคเบาหวานหรือไม่ โดยใช้เทคนิค Standardization หรือการทำให้ข้อมูลมีมาตรฐานก็เป็นสิ่งสำคัญในการปรับขนาดข้อมูลให้เท่ากันและช่วยให้โมเดลทำงานได้ดีขึ้น โดย Dataset ที่ใช้จะเป็น การวินิจฉัยโรคเบาหวานในหญิงชาวอินเดียเผ่า Pima (Pima Indians Diabetes Dataset) มีข้อมูล 8 columns ดังนี้ : Pregnancies – จำนวนครั้งที่ตั้งครรภ์ Glucose – ระดับน้ำตาลในเลือด (glucose concentration) จากการตรวจวัดแบบ 2 ชั่วโมง BloodPressure – ความดันโลหิตขณะพัก (diastolic blood pressure) (mm Hg) SkinThickness – ความหนาของผิวหนังบริเวณ triceps (มม.) Insulin – ระดับอินซูลินในเลือด (mu U/ml) BMI – ดัชนีมวลกาย (Body Mass Index) = น้ำหนัก(kg) / ส่วนสูง²(m²) DiabetesPedigreeFunction – คะแนนบ่งชี้ความน่าจะเป็นทางพันธุกรรมที่จะเป็นเบาหวาน Age – อายุของผู้ป่วย (ปี) Outcome – ผลลัพธ์ (0 = ไม่เป็นเบาหวาน, 1 = เป็นเบาหวาน) ขั้นตอนที่ 1: การนำเข้า library ที่จำเป็น เราจะเริ่มต้นด้วยการนำเข้า library ที่จำเป็นในการทำงาน เช่น pandas สำหรับการจัดการข้อมูล, sklearn สำหรับโมเดล Machine Learning และฟังก์ชันที่เกี่ยวข้องกับการประเมินผล import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.preprocessing import StandardScaler from sklearn import svm from sklearn.metrics import accuracy_score ขั้นตอนที่ 2: การโหลดชุดข้อมูล เราจะโหลดชุดข้อมูลที่เป็นไฟล์ CSV ซึ่งบันทึกข้อมูลเกี่ยวกับผู้ที่เป็นและไม่เป็นโรคเบาหวาน #Importing the diabetes dataset Diabetes_dataset = pd.read_csv("https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv", header=None) # Assigning column names Diabetes_dataset.columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome'] เราสามารถดูข้อมูลเบื้องต้นของชุดข้อมูลได้ด้วยคำสั่งนี้: Diabetes_dataset.head() ตรวจสอบขนาดของชุดข้อมูล: Diabetes_dataset.shape นอกจากนี้ยังสามารถดูสถิติเบื้องต้นของชุดข้อมูลได้: Diabetes_dataset.describe() และตรวจสอบจำนวนข้อมูลของผู้ป่วยเบาหวานและไม่เป็นเบาหวาน: Diabetes_dataset['Outcome'].value_counts() ขั้นตอนที่ 3: การแยกตัวแปรที่ขึ้นอยู่และไม่ขึ้นอยู่ ถัดไปเราจะแยกคุณลักษณะ(ตัวแปรที่ไม่ขึ้นอยู่) และตัวแปรเป้าหมาย(ตัวแปรที่ขึ้นอยู่) X = Diabetes_dataset.drop(columns = 'Outcome', axis=1) Y = Diabetes_dataset['Outcome'] ขั้นตอนที่ 4: การทำให้ข้อมูลมีมาตรฐาน เพื่อให้ข้อมูลทุกตัวแปรอยู่ในสเกลเดียวกัน เราจะทำการ Standardization ข้อมูล ซึ่งขั้นตอนนี้สำคัญสำหรับการใช้โมเดลเช่น Logistic Regression และ SVM scaler = StandardScaler() scaler.fit(X) standardized_data = scaler.transform(X) print(standardized_data) ข้อมูลจะมีค่าอยู่ในช่วง -1 ถึง +1 ขั้นตอนที่ 5: การแบ่งชุดข้อมูลเป็นการฝึกและทดสอบ X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, stratify=Y, random_state=2) ขั้นตอนที่ 6: การฝึกโมเดลด้วย Logistic Regression ตอนนี้เราจะฝึกโมเดล Logistic Regression ด้วยข้อมูลการฝึกฝน model = LogisticRegression() model.fit(X_train, Y_train) ขั้นตอนที่ 7: การประเมินผลความแม่นยำของโมเดล ประเมินความแม่นยำของโมเดล Logistic Regression โดยการคำนวณความแม่นยำบนข้อมูลการฝึกฝนและข้อมูลทดสอบ ความแม่นยำบนข้อมูลการฝึกฝน: X_train_prediction = model.predict(X_train) training_data_accuracy = accuracy_score(X_train_prediction, Y_train) print('Accuracy score on Training data : ', training_data_accuracy) ความแม่นยำบนข้อมูลทดสอบ: X_test_prediction = model.predict(X_test) test_data_accuracy = accuracy_score(X_test_prediction, Y_test) print('Accuracy score on Test Data : ', test_data_accuracy) ขั้นตอนที่ 8: การฝึกโมเดลด้วย Support Vector Machine (SVM) ถัดไปเราจะฝึกโมเดล Support Vector Machine (SVM) โดยใช้ kernel เชิงเส้น classifier = svm.SVC(kernel='linear') classifier.fit(X_train, Y_train) ขั้นตอนที่ 9: การประเมินผลความแม่นยำของโมเดล SVM เราจะประเมินความแม่นยำของโมเดล SVM ด้วยเช่นกัน ความแม่นยำบนข้อมูลการฝึกฝน: X_train_prediction = classifier.predict(X_train) training_data_accuracy = accuracy_score(X_train_prediction, Y_train) print('Accuracy score on the training data : ', training_data_accuracy) ความแม่นยำบนข

การทำนายโรคเบาหวานด้วย Machine Learning เป็นการประยุกต์ใช้เทคนิค AI ในการวิเคราะห์ข้อมูลสุขภาพเพื่อทำนายความเสี่ยงของคนที่จะเป็นโรคเบาหวาน โดยในบทความนี้จะใช้ Logistic Regression และ Support Vector Machine (SVM) ในการฝึกฝนโมเดลและทำนายผลจากชุดข้อมูลที่มีลักษณะต่างๆ เช่น ระดับน้ำตาลในเลือด, ความดันโลหิต, ดัชนีมวลกาย (BMI) เป็นต้น
ในบทความนี้จะใช้ภาษา Python สำหรับการเขียนโค้ด และใช้ Google Colab โดยใช้ Machine Learning เพื่อทำนายโรคเบาหวาน โดยเลือกใช้สองโมเดลหลัก:
- Logistic Regression ซึ่งเป็นโมเดลเชิงเส้นที่ใช้ในการทำนายผลลัพธ์แบบสองค่า (binary classification)
- Support Vector Machine (SVM) ที่ใช้ในการหาค่าพิกัดที่ดีที่สุดในการแยกกลุ่มข้อมูลออกจากกัน
มีการใช้ชุดข้อมูลที่มีตัวแปรหลายตัว เช่น ระดับน้ำตาลในเลือด, ความดันโลหิต, ดัชนีมวลกาย (BMI) เพื่อทำนายว่าแต่ละบุคคลจะเป็นโรคเบาหวานหรือไม่
โดยใช้เทคนิค Standardization หรือการทำให้ข้อมูลมีมาตรฐานก็เป็นสิ่งสำคัญในการปรับขนาดข้อมูลให้เท่ากันและช่วยให้โมเดลทำงานได้ดีขึ้น
โดย Dataset ที่ใช้จะเป็น การวินิจฉัยโรคเบาหวานในหญิงชาวอินเดียเผ่า Pima (Pima Indians Diabetes Dataset) มีข้อมูล 8 columns ดังนี้ :
- Pregnancies – จำนวนครั้งที่ตั้งครรภ์
- Glucose – ระดับน้ำตาลในเลือด (glucose concentration) จากการตรวจวัดแบบ 2 ชั่วโมง
- BloodPressure – ความดันโลหิตขณะพัก (diastolic blood pressure) (mm Hg)
- SkinThickness – ความหนาของผิวหนังบริเวณ triceps (มม.)
- Insulin – ระดับอินซูลินในเลือด (mu U/ml)
- BMI – ดัชนีมวลกาย (Body Mass Index) = น้ำหนัก(kg) / ส่วนสูง²(m²)
- DiabetesPedigreeFunction – คะแนนบ่งชี้ความน่าจะเป็นทางพันธุกรรมที่จะเป็นเบาหวาน
- Age – อายุของผู้ป่วย (ปี)
- Outcome – ผลลัพธ์ (0 = ไม่เป็นเบาหวาน, 1 = เป็นเบาหวาน)
ขั้นตอนที่ 1: การนำเข้า library ที่จำเป็น
เราจะเริ่มต้นด้วยการนำเข้า library ที่จำเป็นในการทำงาน เช่น pandas สำหรับการจัดการข้อมูล, sklearn สำหรับโมเดล Machine Learning และฟังก์ชันที่เกี่ยวข้องกับการประเมินผล
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn import svm
from sklearn.metrics import accuracy_score
ขั้นตอนที่ 2: การโหลดชุดข้อมูล
เราจะโหลดชุดข้อมูลที่เป็นไฟล์ CSV ซึ่งบันทึกข้อมูลเกี่ยวกับผู้ที่เป็นและไม่เป็นโรคเบาหวาน
#Importing the diabetes dataset
Diabetes_dataset = pd.read_csv("https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv", header=None)
# Assigning column names
Diabetes_dataset.columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']
เราสามารถดูข้อมูลเบื้องต้นของชุดข้อมูลได้ด้วยคำสั่งนี้:
Diabetes_dataset.head()
ตรวจสอบขนาดของชุดข้อมูล:
Diabetes_dataset.shape
นอกจากนี้ยังสามารถดูสถิติเบื้องต้นของชุดข้อมูลได้:
Diabetes_dataset.describe()
และตรวจสอบจำนวนข้อมูลของผู้ป่วยเบาหวานและไม่เป็นเบาหวาน:
Diabetes_dataset['Outcome'].value_counts()
ขั้นตอนที่ 3: การแยกตัวแปรที่ขึ้นอยู่และไม่ขึ้นอยู่
ถัดไปเราจะแยกคุณลักษณะ(ตัวแปรที่ไม่ขึ้นอยู่) และตัวแปรเป้าหมาย(ตัวแปรที่ขึ้นอยู่)
X = Diabetes_dataset.drop(columns = 'Outcome', axis=1)
Y = Diabetes_dataset['Outcome']
ขั้นตอนที่ 4: การทำให้ข้อมูลมีมาตรฐาน
เพื่อให้ข้อมูลทุกตัวแปรอยู่ในสเกลเดียวกัน เราจะทำการ Standardization ข้อมูล ซึ่งขั้นตอนนี้สำคัญสำหรับการใช้โมเดลเช่น Logistic Regression และ SVM
scaler = StandardScaler()
scaler.fit(X)
standardized_data = scaler.transform(X)
print(standardized_data)
ข้อมูลจะมีค่าอยู่ในช่วง -1 ถึง +1
ขั้นตอนที่ 5: การแบ่งชุดข้อมูลเป็นการฝึกและทดสอบ
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, stratify=Y, random_state=2)
ขั้นตอนที่ 6: การฝึกโมเดลด้วย Logistic Regression
ตอนนี้เราจะฝึกโมเดล Logistic Regression ด้วยข้อมูลการฝึกฝน
model = LogisticRegression()
model.fit(X_train, Y_train)
ขั้นตอนที่ 7: การประเมินผลความแม่นยำของโมเดล
ประเมินความแม่นยำของโมเดล Logistic Regression โดยการคำนวณความแม่นยำบนข้อมูลการฝึกฝนและข้อมูลทดสอบ
ความแม่นยำบนข้อมูลการฝึกฝน:
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)
print('Accuracy score on Training data : ', training_data_accuracy)
ความแม่นยำบนข้อมูลทดสอบ:
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
print('Accuracy score on Test Data : ', test_data_accuracy)
ขั้นตอนที่ 8: การฝึกโมเดลด้วย Support Vector Machine (SVM)
ถัดไปเราจะฝึกโมเดล Support Vector Machine (SVM) โดยใช้ kernel เชิงเส้น
classifier = svm.SVC(kernel='linear')
classifier.fit(X_train, Y_train)
ขั้นตอนที่ 9: การประเมินผลความแม่นยำของโมเดล SVM
เราจะประเมินความแม่นยำของโมเดล SVM ด้วยเช่นกัน
ความแม่นยำบนข้อมูลการฝึกฝน:
X_train_prediction = classifier.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)
print('Accuracy score on the training data : ', training_data_accuracy)
ความแม่นยำบนข้อมูลทดสอบ:
X_test_prediction = classifier.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
print('Accuracy score on the test data : ', test_data_accuracy)
จากความแม่นยำที่ได้จากทั้งสองโมเดล เราจะเห็นว่า Support Vector Machine ทำงานได้ดีกว่า Logistic Regression เล็กน้อย
ขั้นตอนที่ 10: การทดสอบโมเดล
จากคะแนนความแม่นยำ (accuracy score) ที่ได้จากทั้งสองโมเดล เราสามารถเห็นได้ว่าโมเดล Support Vector Machine มีประสิทธิภาพดีกว่าเล็กน้อย เมื่อเทียบกับโมเดล Logistic Regression.
ตอนนี้เราจะทดสอบโมเดลที่ฝึกฝนแล้วโดยการทำนายสถานะการเป็นเบาหวานของบุคคลหนึ่ง
# Step 1
individuals_data = (2,141,84,26,175,34,0.42,36)
# Step 2
individuals_data_as_numpy_array = np.asarray(individuals_data)
# Step 3
individuals_data_reshaped = individuals_data_as_numpy_array.reshape(1,-1)
# Step 4
std_data = scaler.transform(individuals_data_reshaped)
print(std_data)
#Step 5
prediction = classifier.predict(std_data)
print(prediction)
if (prediction[0] == 0):
print('The person is not diabetic')
else:
print('The person is diabetic')
ตัวอย่างเพิ่มเติม
ผมจะลองใช้ dataset อื่นในการทำนายบ้าง ในที่นี้ผมจะใช้ Heart Disease Dataset เพื่อทำนายโรคหัวใจโดยใช้ Machine Learning ด้วยอัลกอริธึม Support Vector Machine (SVM) และ Logistic Regression
โดย Dataset "Heart Disease Dataset" ประกอบไปด้วยข้อมูล 14 coluumns ดังนี้ :
- Age – อายุ (ปี)
- Sex – เพศ (1 = ชาย, 0 = หญิง)
- ChestPainType – ประเภทอาการเจ็บหน้าอก
- RestingBP – ความดันโลหิตขณะพัก
- Cholesterol – ระดับคอเลสเตอรอลในเลือด (mg/dl)
- FastingBS – น้ำตาลในเลือดขณะอดอาหาร (1 = ≥ 120 mg/dl, 0 = < 120 mg/dl)
- RestingECG – ผลคลื่นไฟฟ้าหัวใจขณะพัก
- MaxHR – อัตราการเต้นหัวใจสูงสุด
- ExerciseAngina – เจ็บหน้าอกจากการออกกำลังกาย (1 = ใช่, 0 = ไม่ใช่)
- Oldpeak – ST depression (ค่าความผิดปกติใน ECG)
- Slope – ลักษณะความชันของ ST segment
- NumVesselsFluoroscopy – จำนวนเส้นเลือดที่เห็นจากการฉายรังสี
- Thalassemia – ความผิดปกติของเลือด (0, 1, 2, 3)
- Output (Target) – ผลลัพธ์การวินิจฉัย (0 = ไม่เป็นโรคหัวใจ, 1 = เป็นโรคหัวใจ)
ขั้นตอนที่ 1: การนำเข้า library ที่จำเป็น
นำเข้า library ที่จำเป็น
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
ขั้นตอนที่ 2: การโหลดชุดข้อมูล
ผมจะใช้ข้อมูลจาก Heart Disease Dataset ซึ่งสามารถโหลดได้จาก www.kaggle.com
import zipfile
# แตกไฟล์ .zip
with zipfile.ZipFile("archive.zip", 'r') as zip_ref:
zip_ref.extractall(".")
# โหลดไฟล์ CSV
heart_data = pd.read_csv("heart.csv")
# Assigning column names
heart_data.columns = ['Age', 'Sex', 'ChestPainType', 'RestingBP', 'Cholesterol', 'FastingBS',
'RestingECG', 'MaxHR', 'ExerciseAngina', 'Oldpeak', 'Slope',
'NumVesselsFluoroscopy', 'Thalassemia', 'Output']
# แสดงข้อมูลเบื้องต้น
print(heart_data.head())
ตรวจสอบขนาดของข้อมูล
print(heart_data.shape)
ขั้นตอนที่ 3: การแยกตัวแปรที่ขึ้นอยู่และไม่ขึ้นอยู่
ถัดไปผมจะแยกตัวแปรคุณลักษณะ(ตัวแปรที่ไม่ขึ้นอยู่) และตัวแปรเป้าหมาย(ตัวแปรที่ขึ้นอยู่)
X = heart_data.drop(columns='Output', axis=1)
Y = heart_data['Output']
ขั้นตอนที่ 4: การทำให้ข้อมูลมีมาตรฐาน
เพื่อให้ข้อมูลทุกตัวแปรอยู่ในสเกลเดียวกัน เราจะทำการ Standardization ข้อมูล ซึ่งขั้นตอนนี้สำคัญสำหรับการใช้โมเดลเช่น Logistic Regression และ SVM
scaler = StandardScaler()
scaler.fit(X)
standardized_data = scaler.transform(X)
print(standardized_data)
ขั้นตอนที่ 5: การแบ่งชุดข้อมูลเป็นการฝึกและทดสอบ
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)
ขั้นตอนที่ 6: การฝึกโมเดลด้วย Logistic Regression
ตอนนี้ผมจะฝึกโมเดล Logistic Regression ด้วยข้อมูลการฝึกฝน
log_reg_model = LogisticRegression()
log_reg_model.fit(X_train, Y_train)
ขั้นตอนที่ 7: การประเมินผลความแม่นยำของโมเดล
ประเมินความแม่นยำของโมเดล Logistic Regression โดยการคำนวณความแม่นยำบนข้อมูลการฝึกฝนและข้อมูลทดสอบ
ตรวจสอบความแม่นยำของโมเดลบนชุดข้อมูลฝึก
X_train_prediction_log_reg = log_reg_model.predict(X_train)
training_data_accuracy_log_reg = accuracy_score(X_train_prediction_log_reg, Y_train)
print('Accuracy score on Training data (Logistic Regression): ', training_data_accuracy_log_reg)
ตรวจสอบความแม่นยำของโมเดลบนชุดข้อมูลทดสอบ
X_test_prediction_log_reg = log_reg_model.predict(X_test)
test_data_accuracy_log_reg = accuracy_score(X_test_prediction_log_reg, Y_test)
print('Accuracy score on Test Data (Logistic Regression): ', test_data_accuracy_log_reg)
ขั้นตอนที่ 8: การฝึกโมเดลด้วย Support Vector Machine (SVM)
ถัดไปผมจะฝึกโมเดล Support Vector Machine (SVM) โดยใช้ kernel เชิงเส้น
svm_classifier = svm.SVC(kernel='linear')
svm_classifier.fit(X_train, Y_train)
ขั้นตอนที่ 9: การประเมินผลความแม่นยำของโมเดล SVM
ผมจะประเมินความแม่นยำของโมเดล SVM ด้วยเช่นกัน
ตรวจสอบความแม่นยำของโมเดลบนชุดข้อมูลฝึก (SVM)
X_train_prediction_svm = svm_classifier.predict(X_train)
training_data_accuracy_svm = accuracy_score(X_train_prediction_svm, Y_train)
print('Accuracy score on the training data (SVM): ', training_data_accuracy_svm)
ตรวจสอบความแม่นยำของโมเดลบนชุดข้อมูลทดสอบ (SVM)
X_test_prediction_svm = svm_classifier.predict(X_test)
test_data_accuracy_svm = accuracy_score(X_test_prediction_svm, Y_test)
print('Accuracy score on the test data (SVM): ', test_data_accuracy_svm)
ขั้นตอนที่ 10: การทดสอบโมเดล
จากคะแนนความแม่นยำ (accuracy score) ที่ได้จากทั้งสองโมเดล สามารถเห็นได้ว่าโมเดล Support Vector Machine มีประสิทธิภาพ ดีกว่าเล็กน้อย เมื่อเทียบกับโมเดล Logistic Regression.
ตอนนี้ผมจะทดสอบโมเดลที่ฝึกฝนแล้วโดยการทำนายสถานะการเป็นโรคหัวใจของ
คนที่ 1 :
- Age = 35
- Sex = female 0
- ChestPaintype = เจ็บแบบปกติ 0
- RestingBP = 120
- Cholesterol = 180
- FastingBS = 0
- RestingECG = 0
- MaxHR = 160
- ExerciseAngina = 0
- Oldpeak = 0.0
- Slope = 1
- NumVesselsFluoroscopy = 0
- Thalassemia = 1
# ทดสอบโมเดล: ทำนายสถานะโรคหัวใจของบุคคล
individuals_data = (35, 0, 0, 120, 180, 0, 0, 160, 0, 0.0, 1, 0, 1) # ข้อมูลตัวอย่างบุคคล
# Step 1: แปลงข้อมูลตัวอย่างเป็น numpy array
individuals_data_as_numpy_array = np.asarray(individuals_data)
# Step 2: ปรับขนาดข้อมูล
individuals_data_reshaped = individuals_data_as_numpy_array.reshape(1, -1)
# Step 3: สเกลข้อมูล
std_data = scaler.transform(individuals_data_reshaped) # สเกลข้อมูลที่ปรับขนาดแล้ว
print("Standardized data:", std_data)
# Step 4: ทำนายผลด้วย SVM
svm_prediction = svm_classifier.predict(std_data) # ทำนายผล
print("SVM Prediction:", svm_prediction)
if svm_prediction[0] == 0:
print('บุคคลนี้ไม่เสี่ยงต่อโรคหัวใจ') # ถ้าไม่เสี่ยง
else:
print('บุคคลนี้เสี่ยงต่อโรคหัวใจ') # ถ้าเสี่ยง
คนที่ 2
- Age = 65
- Sex = male 1
- ChestPaintype = ไม่มีอาการ แต่เสี่ยง 3
- RestingBP = 165
- Cholesterol = 280
- FastingBS = 1
- RestingECG = 2
- MaxHR = 95
- ExerciseAngina = 1
- Oldpeak = 3.5
- Slope = 2
- NumVesselsFluoroscopy = 2
- Thalassemia = 2
# ทดสอบโมเดล: ทำนายสถานะโรคหัวใจของบุคคล
individuals_data = (65, 1, 3, 160, 280, 1, 2, 96, 1, 3.5, 2, 2, 2) # ข้อมูลตัวอย่างบุคคล
# Step 1: แปลงข้อมูลตัวอย่างเป็น numpy array
individuals_data_as_numpy_array = np.asarray(individuals_data)
# Step 2: ปรับขนาดข้อมูล
individuals_data_reshaped = individuals_data_as_numpy_array.reshape(1, -1)
# Step 3: สเกลข้อมูล
std_data = scaler.transform(individuals_data_reshaped) # สเกลข้อมูลที่ปรับขนาดแล้ว
print("Standardized data:", std_data)
# Step 4: ทำนายผลด้วย SVM
svm_prediction = svm_classifier.predict(std_data) # ทำนายผล
print("SVM Prediction:", svm_prediction)
if svm_prediction[0] == 0:
print('บุคคลนี้ไม่เสี่ยงต่อโรคหัวใจ') # ถ้าไม่เสี่ยง
else:
print('บุคคลนี้เสี่ยงต่อโรคหัวใจ') # ถ้าเสี่ยง
สรุป
การสร้างโมเดล Machine Learning เพื่อตรวจจับโรคสามารถทำได้โดยใช้ Support Vector Machine (SVM) ซึ่งเป็นเครื่องมือที่มีประสิทธิภาพในการจำแนกประเภท โมเดลที่สร้างขึ้นสามารถทำนายการมีโรคจากข้อมูลผู้ป่วยต่างๆได้ ผลลัพธ์จากการทดสอบโมเดลแสดงให้เห็นว่าโมเดลนี้สามารถให้ความแม่นยำในการทำนายได้ และสามารถนำไปประยุกต์ใช้ในงานด้านการแพทย์เพื่อช่วยในการตรวจจับโรคต่างๆในผู้ป่วยได้
Google Colab
การทำนายโรคเบาหวานด้วย Machine Learning :
https://colab.research.google.com/drive/1Rz1z9-G0JUeSzwLDtiz03KagnAKNVdJN?usp=sharing
การทำนายโรคหัวใจด้วย Machine Learning :
https://colab.research.google.com/drive/1Kpz2dsRuc8gdFyZUjbFusBcaDNqt4c3C?usp=sharing
References
Diabetes Prediction using Machine Learning :
https://dev.to/heyfunmi/diabetes-prediction-using-machine-learning-3kbc
Diabetes-Prediction-using-SVM :
https://github.com/heyfunmi/Diabetes-Prediction-using-SVM.git
Diabetes_dataset :
https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv
Heart Disease Dataset :
https://www.kaggle.com/datasets/johnsmith88/heart-disease-dataset?resource=download