<jupyter_start><jupyter_text>PipeLine, PCA, SVM Practice    - Labeled Faces in Wild 얼굴 이미지 사진에 대해, 다음 조건을 만족하는 분류 모형을 만들고 평가해본다.1. 컴포넌트가 100개인 PCA와 선형 커널 서포트 벡터 머신을 파이프라인으로 연결하여 분류 모형을 만든다.2. Validation_curve를 이용하여 가장 accuracy가 높은 slack variable penalty C값을 찾는다.(cv=3)<jupyter_code>import numpy as np
import pickle
import matplotlib.pyplot as plt
f = open("lfw_people.p", "rb")
lfw_people = pickle.load(f)
f.close()

X = lfw_people.data
y = lfw_people.target
X
y<jupyter_output><empty_output><jupyter_text>1. 컴포넌트가 100개인 PCA와 선형 커널 서포트 벡터 머신을 파이프라인으로 연결하여 분류 모형을 만든다.<jupyter_code>from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.model_selection import validation_curve
model = PCA(n_components=100).fit(X)
X = model.transform(X)
model = Pipeline([("pca", PCA(n_components=100)), ("svc", SVC(kernel="linear"))])
model.fit(X, y)
model.predict(X)
y<jupyter_output><empty_output><jupyter_text>2. Validation_curve를 이용하여 가장 accuracy가 높은 slack variable penalty C값을 찾는다.(cv=3)<jupyter_code>from sklearn.model_selection import validation_curve
train_scores, valid_scores = validation_curve(model, X, y, param_name="svc__C", param_range=np.logspace(-10, 10, 20), scoring="accuracy", cv=3)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
valid_scores_mean = np.mean(valid_scores, axis=1)
valid_scores_std = np.std(valid_scores, axis=1)

plt.title("Validation Curve with SVM")
plt.xlabel("$\gamma$")
plt.ylabel("Score")
plt.ylim(0.0, 1.1)
lw = 3

plt.semilogx(np.logspace(-10, 10, 20), train_scores_mean, label="Training score",
             color="darkorange", lw=lw)
plt.fill_between(np.logspace(-10, 10, 20), train_scores_mean - train_scores_std,
                 train_scores_mean + train_scores_std, alpha=0.2,
                 color="darkorange", lw=lw)
plt.semilogx(np.logspace(-10, 10, 20), valid_scores_mean, label="Cross-validation score",
             color="navy", lw=lw)
plt.fill_between(np.logspace(-10, 10, 20), valid_scores_mean - valid_scores_std,
                 valid_scores_mean + valid_scores_std, alpha=0.2,
                 color="navy", lw=lw)
plt.legend(loc="best")
plt.show()
from sklearn.model_selection import cross_val_score
penalty_list = [1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, \
                1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10]
score_list = []
mean_score_list = []
for penalty in penalty_list:
    model = Pipeline([("pca", PCA(n_components=100)), ("svc", SVC(kernel="linear", C=penalty))])
    print(penalty)
    score = cross_val_score(model, X, y, cv=3)
    score_list.append(score)
    mean_score_list.append(np.mean(score))
score_list
max(mean_score_list)
print(mean_score_list[4])
print(penalty_list[4])<jupyter_output>0.7783329777019098
1e-06
{'fraction_non_alphanumeric': 0.13264945171559958, 'fraction_numerical': 0.04563141139016626, 'mean_word_length': 4.966244725738397, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '227849', 'license': 'MIT', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1312, 'n_words': 238, 'path': 'example/Pipeline_PCA_SVM.ipynb', 'repo_name': 'daesookimds/Machine-Learning', 'size': '2827'}