1.DictVectorize特征提取¶
DictVectorize的处理对象是符号化(非数字化)的但是具有一定结构的特征数据,如字典等,将符号转成数字0/1表示。¶
In [62]:
from sklearn.feature_extraction import DictVectorizer
myonehot = DictVectorizer() # 如果结果不用toarray,请开启sparse=False
instances = [{'city': '北京','temperature':100},{'city': '上海','temperature':60}, {'city': '深圳','temperature':30}]
X = myonehot.fit_transform(instances).toarray()
X
myonehot.inverse_transform(X)
Out[62]:
Out[62]:
2.sklearn的train_test_split¶
In [4]:
import numpy as np
from sklearn.model_selection import train_test_split
x, y = np.arange(10).reshape((5, 2)), list(range(5))
print("x:",x)
print("y:",y)
In [51]:
#x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
# random_state=0 保证程序每次运行都分割一样的训练集和测试集。否则,同样的算法模型在不同的训练集和测试集上的效果不一样。
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=0)
x_train
y_train
Out[51]:
Out[51]:
In [52]:
x_test
y_test
Out[52]:
Out[52]:
3.train_test_split--数据集切分¶
In [38]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn import svm
iris = datasets.load_iris()
In [53]:
iris.data.shape
iris.target
Out[53]:
Out[53]:
In [58]:
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=0)
X_train.shape,y_train.shape
X_test.shape, y_test.shape
Out[58]:
Out[58]:
In [61]:
clf = svm.SVC(kernel='linear', C=0.1).fit(X_train, y_train)
clf.score(X_test, y_test)
Out[61]: