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]:
array([[   0.,    1.,    0.,  100.],
       [   1.,    0.,    0.,   60.],
       [   0.,    0.,    1.,   30.]])
Out[62]:
[{'city=北京': 1.0, 'temperature': 100.0},
 {'city=上海': 1.0, 'temperature': 60.0},
 {'city=深圳': 1.0, 'temperature': 30.0}]
 

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)
 
x: [[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
y: [0, 1, 2, 3, 4]
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]:
array([[2, 3],
       [6, 7],
       [8, 9]])
Out[51]:
[1, 3, 4]
In [52]:
x_test
y_test
Out[52]:
array([[4, 5],
       [0, 1]])
Out[52]:
[2, 0]
 

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]:
(150, 4)
Out[53]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
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]:
((120, 4), (120,))
Out[58]:
((30, 4), (30,))
In [61]:
clf = svm.SVC(kernel='linear', C=0.1).fit(X_train, y_train)
clf.score(X_test, y_test)
Out[61]:
1.0