<jupyter_start><jupyter_text>监督学习基础套路- Train vs Test：隔离训练集，测试集以验证训练效果    把已有数据分为两部分，训练数据（Train）和测试数据（Test），训练数据来训练模型，测试数据来测试模型在新数据上运行的准确性    - 建立一个管道将x,y关联起来：f(x) = y    feature: x, label: y, classifier其实就是一个feature到label的函数 可以从sklearn中import各种分类器进行训练，各种分类器有类似的接口，这些不同分类器都可以解决类似的问题 让算法从数据中学习到底是什么？- 拒绝手工写分类规则代码    本质上是学习feature到label，从输入到输出的函数，从一个模型开始，用规则来定义函数- 根据训练数据调整函数参数    从我们发现规律的方法中，找到model        比如一条划分两类点的线就是一个分类器的model，调整参数就能得到我们想要的分类器：         ![classify.png](https://i.loli.net/2018/02/25/5a92d8f1b7d76.png) Example of Neural Network：[TensorFlow PlayGround](http://playground.tensorflow.org/)<jupyter_code># import a dataset
from sklearn import datasets

iris = datasets.load_iris()<jupyter_output><empty_output><jupyter_text>创建特征X和标签Y，将X、Y看做一个函数f(x)=y，x是输入，y是输出<jupyter_code>X = iris.data
y = iris.target<jupyter_output><empty_output><jupyter_text>分离训练数据和测试数据，各占原数据集的一半*PS.*`sklearn.cross_validation`模块在0.18版本中被弃用，支持所有重构的类和函数都被移动到`sklearn.model_selection`模块。<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5)<jupyter_output><empty_output><jupyter_text>建立决策树分类器<jupyter_code># decision tree
from sklearn import tree
my_classifier_tree = tree.DecisionTreeClassifier()<jupyter_output><empty_output><jupyter_text>建立KNN分类器<jupyter_code># KNN
from sklearn.neighbors import KNeighborsClassifier
my_classifier_knn = KNeighborsClassifier()<jupyter_output><empty_output><jupyter_text>分别对两种分类器进行训练<jupyter_code>my_classifier_tree.fit(X_train, y_train)
my_classifier_knn.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>分别对两种分类器调用预测方法，并用它来分类测试数据<jupyter_code>predictions_tree = my_classifier_tree.predict(X_test)
predictions_knn = my_classifier_knn.predict(X_test)<jupyter_output><empty_output><jupyter_text>分别计算两种分类器的准确性<jupyter_code>from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, predictions_tree))
print(accuracy_score(y_test, predictions_knn))<jupyter_output>0.9466666666666667
0.96
{'fraction_non_alphanumeric': 0.10014873574615767, 'fraction_numerical': 0.01983143282102132, 'mean_word_length': 12.103896103896103, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '469595', 'license': 'Apache-2.0', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1128, 'n_words': 76, 'path': 'Ch04_pipline/pipline.ipynb', 'repo_name': 'AngusMonroe/GoogleMachineLearning', 'size': '2017'}