<jupyter_start><jupyter_text>決策樹學習 - 分類樹 (以RR Lyrae變星資料集為例)* [程式碼來源](http://www.astroml.org/book_figures/chapter9/fig_rrlyrae_decisiontree.htmlbook-fig-chapter9-fig-rrlyrae-decisiontree)<jupyter_code>import numpy as np
from matplotlib import pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from astroML.datasets import fetch_rrlyrae_combined
from astroML.utils import split_samples
from astroML.utils import completeness_contamination


#fetch_rrlyrae_combined?
X, y = fetch_rrlyrae_combined() # 合併RR Lyrae變星和標準星的colors資訊
print('Features (u-g, g-r, r-i, i-z colors): ')
print(X)
print('Labels (標準星-0; RR Lyrae變星-1): ')
print(y)
X = X[:, [1, 0, 2, 3]]  # rearrange columns for better 1-color results
(X_train, X_test), (y_train, y_test) = split_samples(X, y, [0.75, 0.25],
                                                     random_state=0)
N_tot = len(y)
N_st = np.sum(y == 0)
N_rr = N_tot - N_st
N_train = len(y_train)
N_test = len(y_test)
N_plot = 5000 + N_rr
%matplotlib notebook
# plot the results
fig = plt.figure(figsize=(5, 2.5))
fig.subplots_adjust(bottom=0.15, top=0.95, hspace=0.0,
                    left=0.1, right=0.95, wspace=0.2)

# left plot: data and decision boundary
ax = fig.add_subplot(121)
im = ax.scatter(X[-N_plot:, 1], X[-N_plot:, 0], c=y[-N_plot:],
                s=4, lw=0, cmap=plt.cm.binary, zorder=2)
im.set_clim(-0.5, 1)

#ax.contour(xx, yy, Z, [0.5], colors='k')

# ax.set_xlim(xlim)
# ax.set_ylim(ylim)

ax.set_xlabel('$u-g$')
ax.set_ylabel('$g-r$')
plt.show()
# ax.text(0.02, 0.02, "depth = %i" % depths[1],
#         transform=ax.transAxes)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15721331689272502, 'fraction_numerical': 0.036374845869297165, 'mean_word_length': 3.963302752293578, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '667691', 'license': 'MIT', 'n_tokens_mistral': 751, 'n_tokens_neox': 724, 'n_words': 134, 'path': 'notebooks/notebooks4ML/DecisionTreeClassifier_RRLyraeExample.ipynb', 'repo_name': 'Astrohackers-TW/IANCUPythonMeetup', 'size': '1622'}