🎯 Mục tiêu bài học
Ôn tập toàn bộ khóa học:
✅ Python Basics → Functions → Control Flow
✅ NumPy → Pandas → Data Manipulation
✅ Visualization (Matplotlib, Seaborn, Plotly)
✅ Data Cleaning → Feature Engineering → Pipeline
✅ Data Crawling → Streamlit Dashboard
✅ Cheat Sheets tổng hợp để tra cứu nhanh
Thời gian: 2 giờ | Mục đích: Ôn tập & tra cứu | Yêu cầu: Hoàn thành Bài 01–12
🗺️ Lộ Trình Khóa Học
Hành trình Python Data Science
Kiến thức tổng quan
🐍 Cheat Sheet — Python Basics
1# === Variables & Types ===2name = "MinAI" # str3age = 25 # int4pi = 3.14 # float5is_active = True # bool67# === Collections ===8fruits = ["apple", "banana"] # list (mutable, ordered)9coords = (10.5, 20.3) # tuple (immutable)10unique = {1, 2, 3} # set (unique values)11person = {"name": "A", "age": 25} # dict (key-value)1213# === List Operations ===14fruits.append("cherry")15fruits.extend(["date", "fig"])16fruits.pop()17sorted_list = sorted(fruits)18squares = [x**2 for x in range(10)] # List comprehension19evens = [x for x in range(20) if x % 2 == 0] # Filtered2021# === Control Flow ===22if score >= 90:23 grade = "A"24elif score >= 80:25 grade = "B"26else:27 grade = "C"2829for item in fruits:30 print(item)3132for i, item in enumerate(fruits):33 print(f"{i}: {item}")3435# === Functions ===36def calculate(a, b, operation="add"):37 if operation == "add":38 return a + b39 return a - b4041# Lambda42square = lambda x: x ** 243sorted_data = sorted(students, key=lambda s: s['gpa'], reverse=True)4445# *args, **kwargs46def flexible(*args, **kwargs):47 print(args) # tuple48 print(kwargs) # dict4950# === String Methods ===51text = " Hello World "52text.strip() # Remove whitespace53text.lower() # lowercase54text.split() # ['Hello', 'World']55"-".join(["a","b"]) # "a-b"56f"Name: {name}" # f-stringCheckpoint
Tự kiểm tra: Bạn có thể viết 1 function nhận list số, trả về list chỉ chứa số chẵn, dùng list comprehension không? Nếu có, kiến thức Python Basics bạn đã vững!
🔢 Cheat Sheet — NumPy
1import numpy as np23# === Array Creation ===4a = np.array([1, 2, 3])5zeros = np.zeros((3, 4))6ones = np.ones((2, 3))7rng = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]8lin = np.linspace(0, 1, 5) # 5 points from 0 to 19rand = np.random.randn(3, 3) # Normal distribution1011# === Shape & Reshape ===12a.shape # (3,)13a.reshape(3, 1) # Column vector14a.flatten() # 1D1516# === Indexing ===17arr = np.array([[1,2,3],[4,5,6],[7,8,9]])18arr[0, :] # Row 0: [1, 2, 3]19arr[:, 1] # Col 1: [2, 5, 8]20arr[arr > 5] # Boolean: [6, 7, 8, 9]2122# === Math ===23np.mean(a), np.std(a), np.median(a)24np.sum(arr, axis=0) # Column sums25np.sum(arr, axis=1) # Row sums26np.dot(a, b) # Dot product27a @ b # Matrix multiply2829# === Broadcasting ===30arr + 10 # Add 10 to all elements31arr * np.array([1,2,3]) # Multiply each colCheckpoint
Tự kiểm tra: Bạn có phân biệt được axis=0 (tính theo cột) và axis=1 (tính theo hàng) không? Có hiểu broadcasting là gì không? Nếu có, NumPy bạn OK!
🐼 Cheat Sheet — Pandas
1import pandas as pd23# === Create & Read ===4df = pd.read_csv("file.csv")5df = pd.read_excel("file.xlsx")6df = pd.DataFrame({"a": [1,2], "b": [3,4]})78# === Explore ===9df.shape # (rows, cols)10df.info() # Types + non-null counts11df.describe() # Statistics12df.head() # First 5 rows13df.dtypes # Column types14df.isnull().sum() # Missing counts1516# === Selection ===17df['col'] # Series18df[['col1', 'col2']] # Multiple cols19df.loc[0, 'col'] # By label20df.iloc[0, 0] # By position2122# === Filtering ===23df[df['age'] > 30]24df.query('age > 30 and city == "Hanoi"')25df[df['city'].isin(['HN', 'HCM'])]2627# === Transform ===28df['new'] = df['a'] + df['b']29df['category'] = df['score'].apply(lambda x: 'A' if x > 90 else 'B')3031# === GroupBy ===32df.groupby('city')['salary'].mean()33df.groupby(['city', 'dept']).agg(34 avg_salary=('salary', 'mean'),35 count=('id', 'count')36)3738# === Merge ===39pd.merge(df1, df2, on='key', how='left') # left/right/inner/outer40pd.concat([df1, df2], axis=0) # Stack vertically41df.pivot_table(values='sales', index='region', columns='product', aggfunc='sum')4243# === Chain Operations (Best Practice) ===44result = (45 df46 .dropna(subset=['col'])47 .drop_duplicates()48 .assign(new_col=lambda x: x['a'] + x['b'])49 .query('value > 0')50 .groupby('category')['value'].mean()51)Checkpoint
Tự kiểm tra: Bạn có thể tự viết code: đọc CSV, lọc theo điều kiện, groupby tính trung bình, merge 2 bảng không? Nếu có, Pandas bạn vững rồi!
📊 Cheat Sheet — Visualization
Khi nào dùng chart nào?
| Mục đích | Chart | Library |
|---|---|---|
| Distribution | Histogram, KDE | Seaborn |
| Comparison | Bar, Box | Seaborn |
| Relationship | Scatter, Regression | Seaborn / Plotly |
| Composition | Pie, Stacked Bar | Plotly |
| Trend | Line | Plotly |
| Correlation | Heatmap | Seaborn |
| Interactive | Any chart | Plotly |
1import matplotlib.pyplot as plt2import seaborn as sns3import plotly.express as px45# === Seaborn (Statistical) ===6sns.histplot(df['col'], kde=True)7sns.boxplot(data=df, x='cat', y='num')8sns.scatterplot(data=df, x='x', y='y', hue='group')9sns.heatmap(df.corr(), annot=True, cmap='coolwarm')10sns.pairplot(df, hue='target')1112# === Plotly (Interactive) ===13px.scatter(df, x='x', y='y', color='cat', size='val',14 hover_name='name', title='Scatter Plot')15px.line(df, x='date', y='value', color='category')16px.bar(df, x='cat', y='val', color='sub_cat', barmode='group')17px.pie(df, values='val', names='cat', hole=0.4)18px.histogram(df, x='col', nbins=30)1920# === Matplotlib (Foundation) ===21fig, axes = plt.subplots(1, 2, figsize=(12, 5))22axes[0].plot(x, y)23axes[0].set_title('Line Plot')24axes[1].bar(categories, values)25axes[1].set_title('Bar Plot')26plt.tight_layout()27plt.show()Checkpoint
Tự kiểm tra: Bạn có biết khi nào dùng histogram vs boxplot vs scatterplot không? Có từng tạo subplot 2x2 không? Nếu có, Visualization bạn OK!
🧹 Cheat Sheet — Data Cleaning & Feature Engineering
Data Cleaning
1# === Missing Values ===2df.isnull().sum()3df.fillna(df['col'].median(), inplace=True) # Numeric4df['cat'].fillna(df['cat'].mode()[0], inplace=True) # Categorical5df.dropna(subset=['important_col'])67# === Duplicates ===8df.duplicated().sum()9df.drop_duplicates(subset=['col1', 'col2'], keep='first')1011# === Outliers (IQR) ===12Q1, Q3 = df['col'].quantile([0.25, 0.75])13IQR = Q3 - Q114mask = (df['col'] >= Q1 - 1.5*IQR) & (df['col'] <= Q3 + 1.5*IQR)15df_clean = df[mask]1617# === Data Types ===18df['date'] = pd.to_datetime(df['date'])19df['price'] = df['price'].str.replace(',', '').astype(float)Feature Engineering
1from sklearn.preprocessing import StandardScaler, OneHotEncoder2from sklearn.pipeline import Pipeline3from sklearn.compose import ColumnTransformer45# === Encoding ===6pd.get_dummies(df, columns=['cat_col'], drop_first=True) # One-Hot7df['edu_num'] = df['education'].map({'HS':0, 'BS':1, 'MS':2, 'PhD':3}) # Ordinal89# === Scaling ===10scaler = StandardScaler()11X_train_scaled = scaler.fit_transform(X_train)12X_test_scaled = scaler.transform(X_test) # KHÔNG fit lại!1314# === Feature Creation ===15df['revenue'] = df['price'] * df['quantity']16df['month'] = df['date'].dt.month17df['is_weekend'] = df['date'].dt.dayofweek.isin([5, 6]).astype(int)1819# === Feature Selection ===20from sklearn.ensemble import RandomForestClassifier21model = RandomForestClassifier().fit(X, y)22importance = model.feature_importances_2324# === Pipeline (tránh Data Leakage!) ===25preprocessor = ColumnTransformer([26 ('num', Pipeline([('imputer', SimpleImputer(strategy='median')),27 ('scaler', StandardScaler())]), numeric_cols),28 ('cat', Pipeline([('imputer', SimpleImputer(strategy='most_frequent')),29 ('ohe', OneHotEncoder(handle_unknown='ignore'))]), cat_cols)30])31full_pipe = Pipeline([('prep', preprocessor), ('model', RandomForestClassifier())])32full_pipe.fit(X_train, y_train)Checkpoint
Tự kiểm tra: Bạn có hiểu tại sao phải dùng Pipeline để tránh Data Leakage không? Có biết khi nào dùng .fillna() vs .dropna() không? Nếu có, Data Processing bạn hiểu rồi!
📚 Tổng Hợp Libraries
| Library | Mục đích | Hàm chính |
|---|---|---|
| numpy | Numerical computing | array, mean, std, reshape, dot |
| pandas | Data manipulation | read_csv, groupby, merge, pivot_table |
| matplotlib | Base visualization | plt.plot, plt.bar, plt.hist, subplots |
| seaborn | Statistical viz | histplot, boxplot, heatmap, pairplot |
| plotly | Interactive viz | px.scatter, px.line, px.bar, px.pie |
| sklearn | ML preprocessing | StandardScaler, OneHotEncoder, Pipeline |
| requests | HTTP requests | get, post, json, raise_for_status |
| beautifulsoup4 | HTML parsing | find, find_all, select, text |
| streamlit | Web apps | st.dataframe, st.plotly_chart, st.sidebar |
🚀 Tiếp Tục Học
Hành trình tiếp theo
Khóa học tiếp theo trên MinAI
- Machine Learning Fundamentals — Regression, Classification, Clustering
- Deep Learning — Neural Networks, CNN, RNN, Transformers
- Statistics Fundamentals — Xác suất, Thống kê suy luận
Practice Resources
| Resource | Mô tả |
|---|---|
| Kaggle | Competitions, Datasets, Notebooks |
| LeetCode | Luyện Python coding |
| Real projects | Áp dụng vào dữ liệu thực tế |
Bài tiếp theo: Mini Project & Bài kiểm tra — kiểm chứng kiến thức toàn khóa! 🎓
Câu hỏi tự kiểm tra
- Trong pipeline xử lý dữ liệu hoàn chỉnh, các bước từ import dữ liệu đến visualization gồm những gì?
- Khi nào dùng
pd.merge()và khi nào dùngpd.concat()? Cho ví dụ cụ thể. - So sánh Seaborn
histplotvàboxplot— mỗi loại phù hợp khi muốn phân tích điều gì? - Tại sao sklearn Pipeline quan trọng khi kết hợp preprocessing với model training?
🎉 Tuyệt vời! Bạn đã hoàn thành bài Ôn Tập Tổng Hợp!
Tiếp theo: Mini Project & Bài kiểm tra cuối khóa — hãy chứng minh kiến thức bạn đã tích lũy!
