机器学习一般的数据集会划分为两个部分:
sklearn.model_selection.train_test_split
from sklearn.datasets import load_iris
li = load_iris()
print('获取特征值')
print(li.data)
print('获取目标值')
print(li.target)
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
li = load_iris()
x_train,x_test,y_train,y_test = train_test_split(li.data,li.target,test_size = 0.25)
print('训练集特征值和目标值:',x_train,y_train)
print('测试集特征值和目标值:',x_test,y_test)
fit_transform():输⼊入数据直接转换,fit() + transform()功能相同。fit只是对输入的数据做一些前提准备,比如求取平均值或者方差之类的。
from sklearn.preprocessing import StandardScaler
s=StandardScaler()
a = s.fit_transform([[1,2,3],[4,5,6]])
print(a)
结果
[[-1. -1. -1.]
[ 1. 1. 1.]]
from sklearn.preprocessing import StandardScaler
ss =StandardScaler()
a = ss.fit([[1,2,3],[4,5,6]])
print(a)
print()
b = ss.transform([[1,2,3],[4,5,6]])
print(b)
结果
StandardScaler(copy=True, with_mean=True, with_std=True)
[[-1. -1. -1.]
[ 1. 1. 1.]]
两者结果是相同的,但如果是如果fit输入的数组变了,如下:
from sklearn.preprocessing import StandardScaler
ss =StandardScaler()
a = ss.fit([[2,3,4],[4,5,7]])
print(a)
print()
b = ss.transform([[1,2,3],[4,5,6]])
print(b)
结果
StandardScaler(copy=True, with_mean=True, with_std=True)
[[-2. -2. -1.66666667]
[ 1. 1. 0.33333333]]
可以看到虽然转换的都是数组[[1,2,3],[4,5,6]],但是fit的内容不一样,最后转换的结果就变了,其实fit的改变也就是转换的标准改变了。