【sklearn练习】preprocessing的使用

介绍

scikit-learn 中的 preprocessing 模块提供了多种数据预处理工具,用于准备和转换数据以供机器学习模型使用。这些工具可以帮助您处理数据中的缺失值、标准化特征、编码分类变量、降维等。以下是一些常见的 preprocessing 模块中的功能和用法示例:

  1. 标准化特征(Feature Scaling)

    • 使用 StandardScaler 类可以对特征进行标准化,使其具有零均值和单位方差。这对于许多机器学习算法来说是必要的。

    示例使用方法:

    python 复制代码
    from sklearn.preprocessing import StandardScaler
    
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)
  2. 最小-最大缩放(Min-Max Scaling)

    • 使用 MinMaxScaler 类可以将特征缩放到指定的最小值和最大值之间,通常在0到1之间。

    示例使用方法:

    python 复制代码
    from sklearn.preprocessing import MinMaxScaler
    
    scaler = MinMaxScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)
  3. 编码分类变量

    • 使用 LabelEncoder 类可以将分类变量编码为整数标签。

    示例使用方法:

    python 复制代码
    from sklearn.preprocessing import LabelEncoder
    
    encoder = LabelEncoder()
    y_encoded = encoder.fit_transform(y)
  4. 独热编码(One-Hot Encoding)

    • 使用 OneHotEncoder 类可以将分类变量转换为独热编码形式,创建虚拟变量。

    示例使用方法:

    python 复制代码
    from sklearn.preprocessing import OneHotEncoder
    
    encoder = OneHotEncoder()
    X_encoded = encoder.fit_transform(X_categorical).toarray()
  5. 处理缺失值

    • 使用 SimpleImputer 类可以填充数据中的缺失值,可以选择使用均值、中位数、众数等填充策略。

    示例使用方法:

    python 复制代码
    from sklearn.impute import SimpleImputer
    
    imputer = SimpleImputer(strategy="mean")
    X_imputed = imputer.fit_transform(X_missing)
  6. 降维

    • 使用 PCA 类可以进行主成分分析(PCA)降维,将高维数据投影到低维空间。

    示例使用方法:

    python 复制代码
    from sklearn.decomposition import PCA
    
    pca = PCA(n_components=2)
    X_pca = pca.fit_transform(X)

以上是一些 preprocessing 模块中常见功能的示例用法。数据预处理是机器学习中非常重要的一步,它有助于提高模型的性能和稳定性。您可以根据您的数据和任务选择适当的预处理方法,并将其应用于您的数据,以确保数据准备得当。

实例

例1:

python 复制代码
from sklearn import preprocessing
import numpy as np

a = np.array([[10,   2.7, 3.6],
             [-100, 5,   -2],
             [120,  20,  40]])
print(a)
print(preprocessing.scale(a))

输出:

python 复制代码
[[  10.     2.7    3.6]
 [-100.     5.    -2. ]
 [ 120.    20.    40. ]]
[[ 0.         -0.85170713 -0.55138018]
 [-1.22474487 -0.55187146 -0.852133  ]
 [ 1.22474487  1.40357859  1.40351318]]

例2:

python 复制代码
from sklearn import preprocessing                      #预处理的模块
import numpy as np
from sklearn.model_selection import train_test_split   #将数据打乱随机分为训练集和测试集的类train_test_split 
from sklearn.datasets import make_classification       #datasets中make开头的创建数据集的类make_classification 
from sklearn.svm import SVC                            #训练模型的类SVC
import matplotlib.pyplot as plt

X, y = make_classification(n_samples=300, n_features=2,
                           n_redundant=0, n_informative=2,
                           random_state=22,
                           n_clusters_per_class=1,
                           scale=100)
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()

X = preprocessing.scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3)
clf = SVC()
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))
#输出为0.9555555555555556,
#当删去对X的预处理语句X = preprocessing.scale(X),这里的输出理论上减小

输出:

相关推荐
运维行者_3 小时前
Applications Manager中的Redis监控
大数据·服务器·数据库·人工智能·网络协议
吃好睡好便好3 小时前
提取矩阵某一行或某一列元素
开发语言·人工智能·线性代数·算法·matlab·矩阵
AI数字化笔记5 小时前
【无标题】
人工智能
悦数图数据库5 小时前
图数据库选型指南 2026:从架构、性能、AI 适配三个维度看 悦数科技
数据库·人工智能·架构
小江的记录本6 小时前
【JVM虚拟机】垃圾回收GC:四种引用类型:强引用、软引用、弱引用、虚引用(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试
北京耐用通信6 小时前
自动化工程师必修课:耐达讯自动化Modbus TCP转PROFIBUS协议转换的核心逻辑与应用
人工智能·物联网·网络协议·自动化·信息与通信
无忧智库6 小时前
某AI漫剧超级工厂AI绘画与分镜自动化生成流水线详细设计方案(WORD)
人工智能·ai作画·自动化
火山引擎开发者社区6 小时前
ArkClaw 全新升级,从 UI 到 Agent 协作全面进化
人工智能
Mininglamp_27186 小时前
会中 AI Skill 架构设计解析:3 种人设 × 7 种能力的技术实现
人工智能·语音识别·硬件·ai agent·skill
墨神谕6 小时前
人工智能(三)— 神经网络的训练
人工智能·神经网络·机器学习