机器学习:BootStrapping(Python)

import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.decomposition import PCA  # 主成分分析
from sklearn.preprocessing import LabelEncoder, StandardScaler  # 类别标签编码,标准化处理
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score   # 分类报告,正确率
import seaborn as sns

wdbc = pd.read_csv("breast+cancer+wisconsin+diagnostic/wdbc.data", header=None)
X, y = wdbc.loc[:, 2:].values, wdbc.loc[:, 1]  # 提取特征数据和样本标签集
X = StandardScaler().fit_transform(X)  # 对样本特征数据进行标准化
lab_en = LabelEncoder()  # 对目标值进行编码,创建对象
y = lab_en.fit_transform(y)  # 拟合和转换
lab_en.classes_, lab_en.transform(["B", "M"])
pca = PCA(n_components=6).fit(X)  # 选取6个主成分, 30维-->6维,信息损失了约11%
X_pca = pca.transform(X)
def bootstrapping(m):
    """
    自助法
    :param m:
    :return:
    """
    bootstrap = []  # 存储每次采样的样本索引编号
    for i in range(m):
        bootstrap.append(np.random.randint(0, m, 1))    # 随机产生一个样本的索引编号
    return np.asarray(bootstrap).reshape(-1)

print("样本总体正例与反例的比:%d : %d = %.2f" % (len(y[y == 0]), len(y[y == 1]), len(y[y == 0])/len(y[y == 1])))

n_samples = X_pca.shape[0]  # 样本量
ratio_bs = []   # 存储每次未划分到训练集中的样本比例
for i in range(15000):
    train_idx = bootstrapping(n_samples)    # 一次自助采样获得训练集样本索引
    idx_all = np.linspace(0, n_samples - 1, n_samples, dtype=np.int64)  # 总体样本的索引编号
    test_idx = np.setdiff1d(idx_all, train_idx)     # 测试样本的索引编号
    ratio_bs.append(len(test_idx) / n_samples)      # 测试样本占所有样本的比例

y_train = y[train_idx]  #其中一次自助采样后的训练样本目标集
print("抽样后,正例与反例的比例:%.5f" % (len(y_train[y_train == 0]) / len(y_train[y_train == 1])))
print("自主采样后,未出现在训练集中的数据比例:%.5f" % np.mean(ratio_bs))   # 15000次自助采样的均值


sns.displot(ratio_bs, kind="hist", color="purple")
plt.show()

X_train, y_train = X_pca[train_idx, :], y[train_idx]
X_test, y_test = X_pca[train_idx, :], y[train_idx]

KNeighborsClassifier()
knn = KNeighborsClassifier(n_neighbors=9)
knn.fit(X_train, y_train)
y_test_pred = knn.predict(X_test)
print("Test score is %.5f" % accuracy_score(y_test, y_test_pred))

相关推荐

  1. 学习笔记:机器学习

    2024-01-22 14:04:02       43 阅读
  2. opencv学习 机器学习

    2024-01-22 14:04:02       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 14:04:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 14:04:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 14:04:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 14:04:02       18 阅读

热门阅读

  1. [Optimization] For matlab and cvx

    2024-01-22 14:04:02       30 阅读
  2. hot100:08无重复字符的最长子串

    2024-01-22 14:04:02       37 阅读
  3. python定义函数和写循环批量处理数据集

    2024-01-22 14:04:02       36 阅读
  4. RepLKNet 学习笔记

    2024-01-22 14:04:02       37 阅读
  5. C语言中malloc的用法和意义(附带源码)

    2024-01-22 14:04:02       34 阅读
  6. Spark在降本增效中的一些思考

    2024-01-22 14:04:02       28 阅读
  7. brpc负载均衡load balance和服务发现name servicing

    2024-01-22 14:04:02       29 阅读