🤖 Machine Learning Fundamentals
คู่มือครบถ้วนเกี่ยวกับพื้นฐาน Machine Learning
สำหรับการประยุกต์ใช้ในงานอุตสาหกรรมและระบบอัตโนมัติ
การเรียนรู้แบบมีผู้สอน
การเรียนรู้แบบไม่มีผู้สอน
การเรียนรู้เชิงลึก
การใช้งานอุตสาหกรรม
Machine Learning คืออะไร?
Machine Learning เป็นสาขาหนึ่งของ Artificial Intelligence ที่เน้นการพัฒนาอัลกอรึทึม ให้คอมพิวเตอร์สามารถเรียนรู้และปรับปรุงประสิทธิภาพจากข้อมูล (Data) ได้อย่างอัตโนมัติ โดยไม่ต้องเขียนโปรแกรมสำหรับแต่ละกรณีโดยตรง ในงานอุตสาหกรรม ML ถูกใช้เพื่อ การทำนาย การจำแนกประเภท การตรวจจับความผิดปกติ และการปรับปรุงกระบวนการผลิต
💻 Traditional Programming
🤖 Machine Learning
ประเภทของ Machine Learning
Supervised Learning
เรียนรู้จากข้อมูลที่มีคำตอบที่ถูกต้องไว้แล้ว
- • การจำแนกประเภท (Good/Bad, A/B/C)
- • Image classification
- • Text categorization
- • Spam detection
- • การทำนายค่าตัวเลข
- • Price prediction
- • Temperature forecasting
- • Sales estimation
Unsupervised Learning
ค้นหารูปแบบและโครงสร้างในข้อมูลที่ไม่มีคำตอب
- • การจัดกลุ่มข้อมูล
- • Customer segmentation
- • Market research
- • Gene sequencing
- • การลดมิติข้อมูล
- • Data visualization
- • Feature selection
- • Compression
Reinforcement Learning
เรียนรู้จากการโต้ตอบกับสิ่งแวดล้อมและรับ feedback
- • Game playing (Chess, Go)
- • Robot control
- • Trading algorithms
- • Resource allocation
- • Agent และ Environment
- • Reward และ Punishment
- • Policy optimization
- • Exploration vs Exploitation
Semi-supervised Learning
ใช้ข้อมูลที่มีและไม่มี label ผสมกัน
Self-supervised Learning
สร้าง supervisory signal จากข้อมูลเอง
อัลกอรึทึมยอดนิยม
Linear Regression
อัลกอรึทึมพื้นฐานสำหรับการทำนายค่าตัวเลข
Decision Trees
อัลกอรึทึมที่ตัดสินใจแบบลำดับขั้น
Random Forest
การรวม Decision Trees หลายต้นเข้าด้วยกัน
Support Vector Machine
หาขอบเขตที่ดีที่สุดสำหรับการแยกประเภท
K-Means Clustering
จัดกลุ่มข้อมูลเป็น K กลุ่มตามความคล้ายคลึง
Neural Networks
จำลองการทำงานของเซลล์ประสาทในสมอง
การประยุกต์ใช้ใน Industry 4.0
Predictive Maintenance
ทำนายการเสื่อมสภาพของเครื่องจักรล่วงหน้า
Automated Quality Control
ตรวจสอบคุณภาพผลิตภัณฑ์อัตโนมัติ
Process Optimization
ปรับปรุงกระบวนการผลิตให้มีประสิทธิภาพ
Supply Chain Intelligence
ปรับปรุงการจัดการห่วงโซ่อุปทานด้วย AI
Smart Energy Management
จัดการพลังงานอย่างมีประสิทธิภาพด้วย ML
AI-powered Safety
เพิ่มความปลอดภัยด้วยระบบ AI
Implementation Guide
Python Machine Learning Example
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import StandardScaler, LabelEncoder
import matplotlib.pyplot as plt
import seaborn as sns
class IndustrialMLPipeline:
def __init__(self):
self.model = None
self.scaler = StandardScaler()
self.label_encoder = LabelEncoder()
self.feature_names = None
def load_and_preprocess_data(self, file_path):
"""Load and preprocess industrial sensor data"""
# Load data
df = pd.read_csv(file_path)
print(f"Loaded data shape: {df.shape}")
# Handle missing values
df = df.fillna(df.mean())
# Feature engineering
df['temperature_pressure_ratio'] = df['temperature'] / df['pressure']
df['vibration_rms'] = np.sqrt(np.mean(df['vibration']**2))
df['moving_avg_temp'] = df['temperature'].rolling(window=5).mean()
# Remove outliers using IQR method
Q1 = df.quantile(0.25)
Q3 = df.quantile(0.75)
IQR = Q3 - Q1
df = df[~((df < (Q1 - 1.5 * IQR)) | (df > (Q3 + 1.5 * IQR))).any(axis=1)]
return df
def prepare_features(self, df, target_column):
"""Prepare features and target variables"""
# Separate features and target
X = df.drop(columns=[target_column])
y = df[target_column]
# Store feature names
self.feature_names = X.columns.tolist()
# Encode target labels
y_encoded = self.label_encoder.fit_transform(y)
# Scale features
X_scaled = self.scaler.fit_transform(X)
return X_scaled, y_encoded
def train_model(self, X, y, test_size=0.2):
"""Train Random Forest model"""
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42, stratify=y
)
# Initialize model
self.model = RandomForestClassifier(
n_estimators=100,
max_depth=10,
min_samples_split=5,
min_samples_leaf=2,
random_state=42
)
# Train model
self.model.fit(X_train, y_train)
# Make predictions
y_pred = self.model.predict(X_test)
# Evaluate model
print("Model Performance:")
print(classification_report(y_test, y_pred,
target_names=self.label_encoder.classes_))
# Feature importance
feature_importance = pd.DataFrame({
'feature': self.feature_names,
'importance': self.model.feature_importances_
}).sort_values('importance', ascending=False)
print("\nTop 10 Most Important Features:")
print(feature_importance.head(10))
return X_test, y_test, y_pred, feature_importance
def plot_results(self, y_test, y_pred, feature_importance):
"""Plot model results"""
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', ax=axes[0,0])
axes[0,0].set_title('Confusion Matrix')
axes[0,0].set_xlabel('Predicted')
axes[0,0].set_ylabel('Actual')
# Feature Importance
top_features = feature_importance.head(10)
sns.barplot(data=top_features, x='importance', y='feature', ax=axes[0,1])
axes[0,1].set_title('Top 10 Feature Importance')
# Prediction Distribution
axes[1,0].hist(y_pred, bins=20, alpha=0.7, label='Predictions')
axes[1,0].hist(y_test, bins=20, alpha=0.7, label='Actual')
axes[1,0].set_title('Prediction vs Actual Distribution')
axes[1,0].legend()
# Learning Curve (simplified)
train_sizes = np.linspace(0.1, 1.0, 10)
train_scores = []
val_scores = []
for size in train_sizes:
# This is a simplified version - real implementation would use cross-validation
sample_size = int(len(y_test) * size)
if sample_size > 10: # Minimum sample size
sample_indices = np.random.choice(len(y_test), sample_size, replace=False)
y_sample_pred = y_pred[sample_indices]
y_sample_test = y_test[sample_indices]
score = np.mean(y_sample_pred == y_sample_test)
train_scores.append(score)
val_scores.append(score * 0.95) # Simulated validation score
axes[1,1].plot(train_sizes[-len(train_scores):], train_scores, 'o-', label='Training Score')
axes[1,1].plot(train_sizes[-len(val_scores):], val_scores, 'o-', label='Validation Score')
axes[1,1].set_title('Learning Curve')
axes[1,1].set_xlabel('Training Set Size')
axes[1,1].set_ylabel('Accuracy')
axes[1,1].legend()
plt.tight_layout()
plt.show()
def predict_new_data(self, new_data):
"""Make predictions on new data"""
if self.model is None:
raise ValueError("Model not trained yet!")
# Preprocess new data
new_data_scaled = self.scaler.transform(new_data)
# Make prediction
prediction = self.model.predict(new_data_scaled)
prediction_proba = self.model.predict_proba(new_data_scaled)
# Decode labels
predicted_labels = self.label_encoder.inverse_transform(prediction)
return predicted_labels, prediction_proba
def save_model(self, filepath):
"""Save trained model"""
import joblib
model_data = {
'model': self.model,
'scaler': self.scaler,
'label_encoder': self.label_encoder,
'feature_names': self.feature_names
}
joblib.dump(model_data, filepath)
print(f"Model saved to {filepath}")
def load_model(self, filepath):
"""Load trained model"""
import joblib
model_data = joblib.load(filepath)
self.model = model_data['model']
self.scaler = model_data['scaler']
self.label_encoder = model_data['label_encoder']
self.feature_names = model_data['feature_names']
print(f"Model loaded from {filepath}")
# Usage Example
def main():
# Initialize pipeline
ml_pipeline = IndustrialMLPipeline()
# Load and preprocess data
# df = ml_pipeline.load_and_preprocess_data('sensor_data.csv')
# For demo purposes, create synthetic data
np.random.seed(42)
n_samples = 1000
df = pd.DataFrame({
'temperature': np.random.normal(75, 10, n_samples),
'pressure': np.random.normal(150, 25, n_samples),
'vibration': np.random.normal(2.5, 0.5, n_samples),
'humidity': np.random.normal(45, 15, n_samples),
'motor_speed': np.random.normal(1800, 200, n_samples)
})
# Create synthetic target based on conditions
conditions = []
for _, row in df.iterrows():
if row['temperature'] > 85 or row['pressure'] > 180 or row['vibration'] > 3.5:
conditions.append('Abnormal')
elif row['temperature'] < 65 or row['pressure'] < 120:
conditions.append('Low_Performance')
else:
conditions.append('Normal')
df['condition'] = conditions
# Prepare features
X, y = ml_pipeline.prepare_features(df, 'condition')
# Train model
X_test, y_test, y_pred, feature_importance = ml_pipeline.train_model(X, y)
# Plot results
# ml_pipeline.plot_results(y_test, y_pred, feature_importance)
# Save model
ml_pipeline.save_model('industrial_ml_model.joblib')
# Example prediction on new data
new_data = np.array([[80, 160, 2.8, 50, 1750]]) # temperature, pressure, vibration, humidity, motor_speed
predictions, probabilities = ml_pipeline.predict_new_data(new_data)
print(f"\nPrediction for new data: {predictions[0]}")
print(f"Prediction probabilities: {probabilities[0]}")
if __name__ == "__main__":
main()
พร้อมเริ่มต้น Machine Learning Project แล้วหรือยัง?
ปรึกษาผู้เชี่ยวชาญเพื่อออกแบบโซลูชัน ML ที่เหมาะสมกับธุรกิจของคุณ