【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),这里的输出理论上减小

输出:

相关推荐
回眸&啤酒鸭2 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智2 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
AI的探索之旅2 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein2 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu2 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk