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

输出:

相关推荐
小淮AI2 小时前
从“刷题”到“追问”:AI课堂正在重塑哪些学习旧习惯?
人工智能·学习
阿童木写作2 小时前
跨境电商图片翻译工具推荐:批量图片翻译+视频字幕翻译+智能抠图
python·macos·音视频·xcode
cui_ruicheng3 小时前
LangChain 应用开发(十四):Agent 上下文与记忆机制
服务器·人工智能·python·langchain
飞哥数智坊3 小时前
直播半小时,我聊了聊 Agent 最常见的 3 个疑问
人工智能
circuitsosk3 小时前
任务规划器的三种范式对比:ReAct、Plan-and-Execute 与 Tree-of-Thought 在真实业务中的取舍
前端·javascript·python·react.js·llm·ai agent
shehuiyuelaiyuehao3 小时前
算法31,前缀和,可被k整除的子数组
数据结构·python·算法
ggb喔3 小时前
AI 原生攻防时代:2026 年渗透测试与逆向开发的范式重构
人工智能·重构
宸津-代码粉碎机3 小时前
AI攻防战升级!基于Spring AI构建Java应用自动免疫安全体系
java·大数据·开发语言·人工智能·python·安全·spring
测试19984 小时前
如何用appium搭建Android自动化测试框架?
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
程序员大雄学编程4 小时前
微积分46. 无穷级数四大核心性质:从理论到实践的全方位解析
python·学习·微积分·学习工具