程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

机器学习-初级进阶(Kernel PCA)

发布于2020-02-25 14:51     阅读(1365)     评论(0)     点赞(23)     收藏(4)


一、Kernel PCA(将线性不可分转化为线性可分)

  1. 原理

    在这里插入图片描述

  2. 代码实现

    数据:

     User ID  Gender   Age  EstimatedSalary  Purchased
    15624510    Male  19.0          19000.0          0
    15810944    Male  35.0          20000.0          0
    15668575  Female  26.0          43000.0          0
    ...
    对于不同用户信息,是否会对投放广告进行点击
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    代码:

    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    from sklearn.preprocessing import StandardScaler
    from sklearn.metrics import confusion_matrix
    from matplotlib.colors import ListedColormap
    from sklearn.decomposition import KernelPCA
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    # Importing the dataset
    dataset = pd.read_csv('Social_Network_Ads.csv')
    X = dataset.iloc[:, [2, 3]].values
    y = dataset.iloc[:, 4].values
    
    # Splitting the dataset into the Training set and Test set
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
    
    # Feature Scaling
    sc_X = StandardScaler()
    X_train = sc_X.fit_transform(X_train)
    X_test = sc_X.transform(X_test)
    
    
    # 构建kernel PCA
    kpca = KernelPCA(n_components=2, kernel="rbf")  # kernel="rbf": 高斯核函数
    X_train = kpca.fit_transform(X_train)
    X_test = kpca.transform(X_test)
    
    
    # 逻辑回归拟合数据
    classifier = LogisticRegression(random_state=0)
    classifier.fit(X_train, y_train)
    
    # 预测测试集
    y_pred = classifier.predict(X_test)
    
    # 构建混淆矩阵
    cm = confusion_matrix(y_test, y_pred)
    
    # 画图
    X_set, y_set = X_train, y_train
    X1, X2 = np.meshgrid(np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01),
                         np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01))
    plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
                 alpha=0.75, cmap=ListedColormap(('red', 'green', 'black')))
    plt.xlim(X1.min(), X1.max())
    plt.ylim(X2.min(), X2.max())
    for i, j in enumerate(np.unique(y_set)):
        plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                    c=ListedColormap(('orange', 'blue', 'grey'))(i), label=j)
    plt.title('Logistic Regression (Training set)')
    plt.xlabel('pc1')
    plt.ylabel('pc2')
    plt.legend()
    plt.show()
    
    
    X_set, y_set = X_test, y_test
    X1, X2 = np.meshgrid(np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01),
                         np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01))
    plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
                 alpha=0.75, cmap=ListedColormap(('red', 'green', 'black')))
    plt.xlim(X1.min(), X1.max())
    plt.ylim(X2.min(), X2.max())
    for i, j in enumerate(np.unique(y_set)):
        plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                    c=ListedColormap(('orange', 'blue', 'grey'))(i), label=j)
    plt.title('Logistic Regression (Test set)')
    plt.xlabel('pc1')
    plt.ylabel('pc2')
    plt.legend()
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    输出结果:
    训练结果:
    在这里插入图片描述
    测试结果:
    在这里插入图片描述

发布了159 篇原创文章 · 获赞 13 · 访问量 2万+


所属网站分类: 技术文章 > 博客

作者:我下面给你吃

链接:https://www.pythonheidong.com/blog/article/233410/20d701b7c3000aec4d1f/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)