python - 选择 n 个分量后应用 PCA 约简
问题描述
我有一个数据集,我想用 PCA 方法降低维度。
我导入数据集,将其拆分为训练集和验证集,然后应用 PCA 来查看累积方差和。
df = read_csv("train.csv")
X = df.drop(['label'], axis = 1)
y = df['label']
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size = 0.2, random_state = 42)
model = PCA()
model.fit_transform(X_train_scaled)
variance = model.explained_variance_
cum_var = np.cumsum(variance)/np.sum(variance)
plt.figure(figsize = (15,15))
plt.bar(range(1,26), cum_var*100, alpha = 0.5, align = 'center', label = 'cummulative variance')
plt.legend()
plt.ylabel('Variance')
plt.xlabel('Principal components')
for x,y in zip(range(1,26),cum_var):
label = "{:.2f}".format(y)
plt.annotate(label, # this is the text
(x,y), # this is the point to label
textcoords = "offset points", # how to position the text
xytext = (0,10), # distance from text to points (x,y)
ha = 'center')
plt.show()
我选择 n_components = 16。
如何在 X_train 和 X_validation 上应用 n_components = 16?
解决方案
一旦您知道适当数量的组件,您就可以PCA
再次运行,设置n_components=16
,并将新的缩减功能保留为您的X_train
:
model = PCA(n_components=16)
model.fit(X_train)
X_train = model.transform(X_train)
X_validation = model.transform(X_validation)
推荐阅读
- tensorflow - 如何使用张量板查找输出节点?
- c# - 如何从下拉列表中拆分值并将其存储在会话中?
- xamarin - 如何在 xamarin 表单中调整 MicroCharts-Barchart 的宽度和间距
- python-3.x - 我无法在方法中设置默认值以传递给字典
- java - 在不同的线程上运行 AsynchronousSocketChannel
- dart - Flutter Renderboxes 传递无限高度?
- c# - 如何将图像从 windows 窗体 picturbox 上传到 FTP 服务器?
- coldfusion - 将下拉值绑定到 cfgrid 中的绑定时出现 CF 升级问题
- react-native - 经纬度的当前位置在 react-native 中返回为 [object object]
- python-3.x - caffe 导入错误:即使在成功安装后,在 ubuntu 18.04 上