<jupyter_start><jupyter_text>이 노트북의 코드에 대한 설명은 [분류기 체인: ClassifierChain](https://tensorflow.blog/2018/02/16/%EB%B6%84%EB%A5%98%EA%B8%B0-%EC%B2%B4%EC%9D%B8-classifierchain/) 글을 참고하세요.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import fetch_openml<jupyter_output><empty_output><jupyter_text>fetch yeast dataset (https://www.openml.org/d/40597)사이킷런 0.20 버전에서 `fetch_openml`이 추가되었습니다. `fetch_mldata`는 0.22 버전에서 삭제될 예정입니다.<jupyter_code>yeast = fetch_openml('yeast', version=4)
X = yeast['data']
Y = yeast['target']
X.shape, Y.shape
Y
Y = Y == 'TRUE'
Y
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier<jupyter_output><empty_output><jupyter_text>for OVR model<jupyter_code>ovr = OneVsRestClassifier(LogisticRegression(solver='liblinear'))
ovr.fit(X_train, Y_train)
pred_ovr = ovr.predict(X_test)
from sklearn.metrics import jaccard_similarity_score
ovr_score = jaccard_similarity_score(Y_test, pred_ovr)
ovr_score<jupyter_output><empty_output><jupyter_text>for CC model (https://www.cs.waikato.ac.nz/ml/publications/2009/chains.pdf)<jupyter_code>from sklearn.multioutput import ClassifierChain
cc = ClassifierChain(LogisticRegression(solver='liblinear'), order='random', random_state=42)
cc.fit(X_train, Y_train)
pred_cc = cc.predict(X_test)
cc_score = jaccard_similarity_score(Y_test, pred_cc)
cc_score<jupyter_output><empty_output><jupyter_text>for CC ensemble<jupyter_code>chains = [ClassifierChain(LogisticRegression(solver='liblinear'), order='random', random_state=42+i)
          for i in range(10)]
for chain in chains:
    chain.fit(X_train, Y_train)
pred_chains = np.array([chain.predict(X_test) for chain in chains])
chain_scores = [jaccard_similarity_score(Y_test, pred_chain)
                        for pred_chain in pred_chains]
chain_scores
plt.bar(np.arange(11), [ovr_score]+chain_scores)
plt.show()
proba_chains = np.array([chain.predict_proba(X_test) for chain in chains])
proba_ensemble = proba_chains.mean(axis=0)
ensemble_score = jaccard_similarity_score(Y_test, proba_ensemble >= 0.5)
ensemble_score
plt.bar(np.arange(12), [ovr_score]+chain_scores+[ensemble_score])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12023708721422523, 'fraction_numerical': 0.02243861134631668, 'mean_word_length': 7.984790874524715, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '194988', 'license': 'MIT', 'n_tokens_mistral': 1003, 'n_tokens_neox': 984, 'n_words': 147, 'path': 'Python/scikit-learn/ClassifierChain.ipynb', 'repo_name': 'statKim/TIL', 'size': '2362'}