【问题标题】:PCA prediction and errors using sklearn使用 sklearn 进行 PCA 预测和错误
【发布时间】:2018-12-20 00:30:17
【问题描述】:

我想使用 sklearn 在 Python 中使用 PCA 预测一些值。
我首先从数据中提取相关列,并将它们命名为 X 表示特征,Y 表示需要预测的特征。

Y = DF['Predict'].values
X = pd.DataFrame(data=scale(DF[X_cols]), columns=X_cols)

pca = PCA(n_components=NCOMPS)  #NCOMPS=min(len(X_cols, Num_samples)

X_reduced = pd.DataFrame(pca.fit_transform(X),
                         columns=['PC%i' % i for i in range(NCOMPS)])

我已经绘制了 PC 数量对方差的解释程度,所以我知道我提取了 PC 没问题。 我想继续根据 PC 的数量绘制预测 Y 的误差。
我如何使用我所拥有的进行预测?

最重要的是,我还想添加 LOOCV,但我想如果我再次卡住,我会保留它以用于另一个问题。

稍后编辑: 我试过这个,但后来我搞砸了十几个撤消/重做,Spyder 的编辑历史无法再让我摆脱这种痛苦。

classifier = LogisticRegression()   
total_err = []   
for num_comps in range(1, NCOMPS):
    classifier.fit(X_reduced, Y)

    ypred = np.array(classifier.predict(X_reduced[:,:num_comps))
    Y = np.array(Y)
    total_err.append(abs(np.subtract(Y, ypred)))

哪里出错了?控制台说'X 每个样本有 2 个特征;预计 30'

【问题讨论】:

    标签: pandas scikit-learn data-science pca prediction


    【解决方案1】:

    您只需要选择一个分类器/估计器并适合您的数据。

    from sklearn.datasets import load_iris
    from sklearn.dimensionality_reduction import PCA
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import train_test_split
    
    iris = load_iris()
    X = iris.data
    y = iris.target
    
    pca = PCA()
    
    X_reduced = pca.fit_transform(X)
    
    rf = RandomForestClassifier()
    X_train, X_test, y_train, y_test = train_test_split(X_reduced, y)
    rf.fit(X_train, y_train)
    rf.predict_proba(X_test)
    array([[0. , 0.9, 0.1],
           [0. , 0.8, 0.2],
           [0.9, 0. , 0.1],
           [0. , 0.2, 0.8],
           [0. , 1. , 0. ],
           [1. , 0. , 0. ],
           [0. , 0.1, 0.9],
           [0. , 0.3, 0.7],
           [1. , 0. , 0. ],
           [0. , 0. , 1. ],
           [1. , 0. , 0. ],
           [0.9, 0.1, 0. ],
           [1. , 0. , 0. ],
           [0. , 1. , 0. ],
           [1. , 0. , 0. ],
           [0. , 0.9, 0.1],
           [0.9, 0. , 0.1],
           [1. , 0. , 0. ],
           [0. , 0.8, 0.2],
           [1. , 0. , 0. ],
           [0. , 0. , 1. ],
           [0. , 0. , 1. ],
           [0.1, 0.8, 0.1],
           [0. , 0.7, 0.3],
           [1. , 0. , 0. ],
           [0.9, 0.1, 0. ],
           [0. , 0.7, 0.3],
           [0. , 0.1, 0.9],
           [0. , 0.9, 0.1],
           [0. , 0.9, 0.1],
           [0. , 0. , 1. ],
           [0. , 0. , 1. ],
           [0. , 0.7, 0.3],
           [0. , 0. , 1. ],
           [0. , 0. , 1. ],
           [1. , 0. , 0. ],
           [1. , 0. , 0. ],
           [1. , 0. , 0. ]])
    rf.score(X_test, y_test)
    0.9736842105263158
    pca.inverse_transform(X_test)
    array([[5.7, 2.8, 4.5, 1.3],
           [5.7, 3. , 4.2, 1.2],
           [5.4, 3.9, 1.3, 0.4],
           [7.1, 3. , 5.9, 2.1],
           [6.4, 2.9, 4.3, 1.3],
           [5.7, 3.8, 1.7, 0.3],
           [6.4, 3.1, 5.5, 1.8],
           [7.7, 3. , 6.1, 2.3],
           [4.8, 3. , 1.4, 0.3],
           [6.9, 3.2, 5.7, 2.3],
           [5.2, 4.1, 1.5, 0.1],
           [4.6, 3.1, 1.5, 0.2],
           [4.9, 3.1, 1.5, 0.1],
           [6. , 2.2, 4. , 1. ],
           [5. , 3.4, 1.5, 0.2],
           [5.5, 2.4, 3.7, 1. ],
           [5. , 3.5, 1.3, 0.3],
           [5.5, 3.5, 1.3, 0.2],
           [6. , 2.2, 5. , 1.5],
           [4.8, 3. , 1.4, 0.1],
           [6.9, 3.1, 5.4, 2.1],
           [6.8, 3.2, 5.9, 2.3],
           [5.6, 3. , 4.5, 1.5],
           [5.6, 2.9, 3.6, 1.3],
           [5.1, 3.8, 1.6, 0.2],
           [4.3, 3. , 1.1, 0.1],
           [6.6, 2.9, 4.6, 1.3],
           [7.4, 2.8, 6.1, 1.9],
           [5.6, 3. , 4.1, 1.3],
           [5.8, 2.7, 4.1, 1. ],
           [6.5, 3. , 5.2, 2. ],
           [6.3, 2.9, 5.6, 1.8],
           [6.9, 3.1, 4.9, 1.5],
           [7.2, 3.2, 6. , 1.8],
           [7.2, 3.6, 6.1, 2.5],
           [5.4, 3.9, 1.7, 0.4],
           [5.1, 3.5, 1.4, 0.2],
           [5.8, 4. , 1.2, 0.2]])
    y_test
    array([1, 1, 0, 2, 1, 0, 2, 2, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 2, 0, 2, 2,
           1, 1, 0, 0, 1, 2, 1, 1, 2, 2, 1, 2, 2, 0, 0, 0])
    

    【讨论】:

    • 还有一个问题:pca.inverse_transform(X_test) 不应该使用y_test 吗?也许我还没有完全理解这个概念。
    • 您可能应该复习一下 PCA 和 SVD。 PCA 对y_test 或如何使用它一无所知。如果您在 100x10 矩阵上运行 PCA 并选择 n_components=5。输出 components_ 属性将是一个 5x10 矩阵。 inverse_transform 接受转换后的输入并返回与components_ 的点积。因此输入必须是 nx5 矩阵,而不是预测向量。总体 PCA 与预测值没有任何关系。它只是一种压缩数据的方式,同时保持尽可能多的信息。我可以变得富有诗意,但我没有角色。
    猜你喜欢
    • 2011-09-14
    • 2018-03-23
    • 2015-06-19
    • 2018-10-15
    • 1970-01-01
    • 2017-06-24
    • 2016-01-12
    • 2018-12-01
    • 2018-12-27
    相关资源
    最近更新 更多