핸즈온머신러닝
-
핸즈온 머신러닝[6] 결정 트리핸즈온머신러닝 2022. 7. 6. 15:36
https://www.youtube.com/watch?v=h9PRMril20M&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=19 결정 트리 학습과 시각화 from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier iris = load_iris() X = iris.data[:, 2:] # 꽃잎 길이와 너비 y = iris.target tree_clf = DecisionTreeClassifier(max_depth=2, random_state=42) tree_clf.fit(X, y) 붓꽃 데이터셋에 DecisionClassifier를 훈련시키는 코드 from graphviz i..
-
핸즈온 머신러닝[5] 서포트 벡터 머신(3)핸즈온머신러닝 2022. 7. 5. 18:24
https://www.youtube.com/watch?v=pIq527ZHiAE&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=18 SVM 이론 결정 함수와 예측 w는 가중치 벡터로, w^Tx + b = w1x1+w2x2+...+wnxn+b 이다. 음수일때 양성 클래스인지 양수일때 양성 클래스인지는 라이브러리마다 차이가 있다. iris = datasets.load_iris() X = iris["data"][:, (2, 3)] # 꽃잎 길이, 꽃잎 너비 y = (iris["target"] == 2).astype(np.float64) # Iris virginica 특성은 꽃잎 길이, 꽃잎 너비 두가지를 사용하고 타깃은 이진 분류기로 Iris virginica인지 아닌지로 분..
-
핸즈온 머신러닝[5] 서포트 벡터 머신(2)핸즈온머신러닝 2022. 7. 3. 17:51
https://www.youtube.com/watch?v=gjxQo4KwFxQ&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=17 비선형 분류 X1D = np.linspace(-4, 4, 9).reshape(-1, 1) X2D = np.c_[X1D, X1D**2] y = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0]) plt.figure(figsize=(10, 3)) plt.subplot(121) plt.grid(True, which='both') plt.axhline(y=0, color='k') plt.plot(X1D[:, 0][y==0], np.zeros(4), "bs") plt.plot(X1D[:, 0][y==1], np.zeros(5), "g..
-
핸즈온 머신러닝[5] 서포트 벡터 머신(1)핸즈온머신러닝 2022. 6. 22. 21:59
https://www.youtube.com/watch?v=dP-cDdP_Y3A&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=16 선형 SVM 분류 라지 마진 분류 from sklearn.svm import SVC from sklearn import datasets iris = datasets.load_iris() X = iris["data"][:, (2, 3)] # 꽃잎 길이, 꽃잎 너비 y = iris["target"] setosa_or_versicolor = (y == 0) | (y == 1) X = X[setosa_or_versicolor] y = y[setosa_or_versicolor] # SVM 분류 모델 svm_clf = SVC(kernel="linear..
-
핸즈온 머신러닝[4] 모델 훈련(3)핸즈온머신러닝 2022. 6. 14. 17:56
https://www.youtube.com/watch?v=wquIJHKX7T0&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=15 로지스틱 회귀 로지스틱 회귀는 샘플이 특정 클래스에 속할 확률을 추정하는 데 널리 사용된다. 결정 경계 t = np.linspace(-10, 10, 100) sig = 1 / (1 + np.exp(-t)) plt.figure(figsize=(9, 3)) plt.plot([-10, 10], [0, 0], "k-") plt.plot([-10, 10], [0.5, 0.5], "k:") plt.plot([-10, 10], [1, 1], "k:") plt.plot([0, 0], [-1.1, 1.1], "k-") plt.plot(t, sig, "b-"..
-
핸즈온 머신러닝[4] 모델 훈련(2)핸즈온머신러닝 2022. 6. 13. 21:47
https://www.youtube.com/watch?v=0CaLoYMBk6c&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=14 다항 회귀 (Polynomial regression) import numpy as np import numpy.random as rnd np.random.seed(42) m = 100 X = 6 * np.random.rand(m, 1) - 3 # -3 ~ 3 y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1) # 노이즈 추가 2차 함수 형태의 그래프를 생성하고 랜덤한 수를 사용해서 노이즈를 추가해준다. plt.plot(X, y, "b.") plt.xlabel("$x_1$", fontsize=18) plt.yl..
-
핸즈온 머신러닝[4] 모델 훈련(1)핸즈온머신러닝 2022. 5. 13. 18:58
https://www.youtube.com/watch?v=6omvN1nuZMc&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=13 선형회귀 MSE를 최소화하는 모델 파라미터를 찾는다. 행렬 곱셈: [θ0,θ1,..,θn][x0,x1,...,xn]^(T) 선형 회귀 계산 방식1: 정규 방정식 import numpy as np X = 2 * np.random.rand(100, 1) y = 4 + 3 * X + np.random.randn(100, 1) rand 함수로 0과 1 사이의 난수를 발생, 100x1 행렬이 생긴다. randn -> 표준 분포 난수 plt.plot(X, y, "b.") plt.xlabel("$x_1$", fontsize=18) plt.ylabel("..
-
핸즈온 머신러닝[3] 분류(2)핸즈온머신러닝 2022. 5. 5. 13:40
https://www.youtube.com/watch?v=Ie5pFrpKyvM&list=PLJN246lAkhQjX3LOdLVnfdFaCbGouEBeb&index=12 거짓 양성 비율에 대한 진짜 양성 비율 곡선 (Receiver Operating Characteristic) from sklearn.metrics import roc_curve fpr, tpr, thresholds = roc_curve(y_train_5, y_scores) fpr: 특이도, tpr: 민감도 def plot_roc_curve(fpr, tpr, label=None): plt.plot(fpr, tpr, linewidth=2, label=label) plt.plot([0, 1], [0, 1], 'k--') # 대각 점선 plt.ax..