初心者データ科学ここに。PCA値を持つK最近傍分類器を構築する
私は高次元のデータセットを持っています。 2308の寸法を持つ83のサンプルがあり、その形状は(83,2308)である。さらに、長さ83のサンプルタイプの配列を持ち、その形状は(83、)です。
私は元のデータセットのサブセットでKNNクラシファイア(2人の隣人)を訓練し、それを使って残りのデータポイントのサンプルタイプ(テストサブセット)を予測しようとしています。私の訓練データは形状(66,2308)を持っていて、それを形状のサンプル型配列(63、)に訓練しています。
私の目標は、次元が小さくなったトレーニングセットを使用してKNNクラシファイアをトレーニングすることです。そのため、PCAを実行しました。私は最初の10台のPCしか保管していません。トレーニングセットを変換した後、その形状は(63、10)です。
残念ながら、この縮小トレーニングセットを使用して未還元テストセットの予測を行うことはできません。コードを実行すると、「クエリデータのディメンションはトレーニングデータのディメンションと一致する必要があります」というエラーが表示されます。
私は最初の10台のPCをKNNモデルに組み込むことができます。これを行うための助けとなることはありますか?
ここに参照のための私のコードです:
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
# creates my training and testing partitions
train_ind, test_ind = test_train_id(cancer_types, 0.8)
# create the train partition
genes_train = genes[train_ind, :]
# perform PCA on the train partition
gene_pca = PCA(10)
gene_pca.fit(genes_train)
# transform the gene partition with the PCA
genes_train_red = gene_pca.transform(genes_train)
# the KNN model
model = KNeighborsClassifier(2)
model.fit(genes_train_red, cancer_types[train_ind])
predict = model.predict(genes[train_ind])
np.mean(predict == cancer_types[test_ind])
print('The unreduced train set has shape',genes[train_ind, :].shape)
print('The label set being trained to has shape', cancer_types[train_ind].shape)
print('------', '\n', 'After PCA, the reduced train set has shape', genes_train_red.shape ,'\n')
print('The unreduced test set has shape', genes[test_ind].shape)
は、お返事ありがとうございます!これは、私が探していたものです:削減されたトレーニングデータを使用しながら、元のデータセットについての予測を行う方法。 –