🐍 Python Programming for AI
คู่มือครบถ้วนการเขียนโปรแกรม Python
สำหรับ AI, Machine Learning และ Computer Vision
ไลบรารีหลัก
เครื่องมือพัฒนา
แนวทางปฏิบัติที่ดี
การใช้งานอุตสาหกรรม
🐍 Python for AI & ML Overview
Python เป็นภาษาโปรแกรมที่ได้รับความนิยมมากที่สุดสำหรับการพัฒนา AI และ Machine Learning เนื่องจากความง่ายในการเรียนรู้ และมี ecosystem ของ libraries ที่แข็งแกร่ง
📚 Essential Libraries
🔢 NumPy
การคำนวณเชิงตัวเลขและการจัดการ arrays หลายมิติ
📊 Pandas
การจัดการและวิเคราะห์ข้อมูลในรูปแบบตาราง
📈 Matplotlib/Seaborn
การสร้างกราฟและการแสดงผลข้อมูลแบบต่าง ๆ
🤖 scikit-learn
Machine Learning algorithms และ tools สำหรับการพัฒนา ML models
🧠 TensorFlow/PyTorch
Deep Learning frameworks สำหรับสร้าง Neural Networks
👁️ OpenCV
Computer Vision และการประมวลผลภาพ
🔧 Development Environment
💻 IDEs & Editors
- • Jupyter Notebook - Interactive development
- • VS Code - Modern code editor
- • PyCharm - Professional Python IDE
- • Google Colab - Cloud-based notebooks
📦 Package Management
- • pip - Python package installer
- • conda - Scientific package manager
- • venv - Virtual environment
- • requirements.txt - Dependency management
💡 Code Examples
📊 Basic Data Processing
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Load and explore data
data = pd.read_csv('sensor_data.csv')
print(data.head())
print(data.info())
# Data preprocessing
data_cleaned = data.dropna()
data_normalized = (data_cleaned - data_cleaned.mean()) / data_cleaned.std()
# Basic visualization
plt.figure(figsize=(10, 6))
plt.plot(data_normalized['timestamp'], data_normalized['sensor_value'])
plt.title('Normalized Sensor Data')
plt.xlabel('Time')
plt.ylabel('Normalized Value')
plt.show()
👁️ Computer Vision with OpenCV
import cv2
import numpy as np
# Load and process image
image = cv2.imread('gauge_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect circular gauges using HoughCircles
circles = cv2.HoughCircles(
gray,
cv2.HOUGH_GRADIENT,
dp=1,
minDist=100,
param1=50,
param2=30,
minRadius=50,
maxRadius=200
)
# Draw detected circles
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(image, (x, y), r, (0, 255, 0), 2)
cv2.circle(image, (x, y), 2, (0, 0, 255), 3)
# Display result
cv2.imshow('Detected Gauges', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
🤖 Simple ML Classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
# Prepare data
X = data[['feature1', 'feature2', 'feature3']] # Features
y = data['target'] # Target variable
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate model
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
print(classification_report(y_test, y_pred))
💡 Best Practices
🏗️ Code Organization
- • ใช้ virtual environments
- • แยก modules และ functions
- • เขียน docstrings และ comments
- • ใช้ type hints
- • Follow PEP 8 style guide
⚡ Performance Tips
- • ใช้ NumPy arrays แทน Python lists
- • Vectorize operations
- • Profile และ optimize bottlenecks
- • ใช้ GPU acceleration เมื่อเหมาะสม
- • Cache intermediate results
🏭 Industrial Applications
Data Analytics
วิเคราะห์ข้อมูล sensor, production metrics และ quality control
Computer Vision
Gauge reading, defect detection, object recognition
Automation
Process automation, predictive maintenance, smart control