python:用 sklearn 转换器处理数据
pip install scikit-learn 或者 直接用 Anaconda3
sklearn 提供了 model_selection 模型选择模块、preprocessing 数据预处理模块、decompisition 特征分解模块。
编写 test_sklearn_2.py 如下
# -*- coding: utf-8 -*-
""" 乳腺癌数据集 """
import numpy as np
from sklearn import datasets# 1.加载 datasets 中的数据集
cancer = datasets.load_breast_cancer()
print(len(cancer))
print(type(cancer))
# 数据集的数据
data = cancer['data']
print('breast_cancer_data:','\n', data)
# 数据集的标签
target = cancer['target']
print('breast_cancer_target:','\n', target)
# 数据集的特征名称
feature_names = cancer['feature_names']
print('breast_cancer_feature_names:','\n', feature_names)
# 数据集的描述信息
Descr = cancer['DESCR']
print('breast_cancer_DESCR:','\n', Descr)
print('-----------------------')
# 2.将数据集划分为训练集和测试集
# 原始数据集数据的形状
print('cancer_data_shape:', data.shape)
print('cancer_target_shape:', target.shape)
# 使用 train_test_split 划分数据集
from sklearn import model_selection
data_train,data_test,target_train,target_test = \model_selection.train_test_split(data,target, test_size=0.2, random_state=42)
print('-----------------------')
print('cancer_data_train_shape:', data_train.shape)
print('cancer_target_train_shape:', target_train.shape)
print('cancer_data_test_shape:', data_test.shape)
print('cancer_target_test_shape:', target_test.shape)# 3.使用 sklearn 转换器进行数据预处理与降维
# 离差标准化
from sklearn import preprocessing
# 生成规则
Scaler = preprocessing.MinMaxScaler().fit(data_train)
# 将规则应用于训练集
train_Scaler = Scaler.transform(data_train)
# 将规则应用于测试集
test_Scaler = Scaler.transform(data_test)
print('-----------------------')
print('data_train_min:', np.min(data_train))
print('train_Scaler_min:', np.min(train_Scaler))
print('data_train_max:', np.max(data_train))
print('train_Scaler_max:', np.max(train_Scaler))
print('data_test_min:', np.min(data_test))
print('test_Scaler_min:', np.min(test_Scaler))
print('data_test_max:', np.max(data_test))
print('test_Scaler_max:', np.max(test_Scaler))# 对 breast_cancer 数据集 PCA 降维
from sklearn.decomposition import PCA
# 生成 PCA 规则
pca_model = PCA(n_components=10).fit(train_Scaler)
# 将规则应用于训练集
train_pca = pca_model.transform(train_Scaler)
# 将规则应用于测试集
test_pca = pca_model.transform(test_Scaler)
print('-----------------------')
print('train_Scaler_shape:', train_Scaler.shape)
print('train_pca_shape:', train_pca.shape)
print('test_Scaler_shape:', test_Scaler.shape)
print('test_pca_shape:', test_pca.shape)
运行 python test_sklearn_2.py
参考书:【Python 数据分析与应用】第6章 使用 scikit-learn 构建模型