import numpy as npfrom sklearn import decomposition# Define individual featuresx1 = np.random.normal(size=250)x2 = np.random.normal(size=250)x3 = 2*x1 + 3*x2x4 = 4*x1 - x2x5 = x3 + 2*x4# Create dataset with the above featuresX = np.c_[x1, x3, x2, x5, x4]# Perform Principal Components Analysispca = decomposition.PCA()pca.fit(X)# Print variancesvariances = pca.explained_variance_print '\nVariances in decreasing order:\n', variances# Find the number of useful dimensionsthresh_variance = 0.8num_useful_dims = len(np.where(variances > thresh_variance)[0])print '\nNumber of useful dimensions:', num_useful_dims# As we can see, only the 2 first components are usefulpca.n_components = num_useful_dimsX_new = pca.fit_transform(X)print '\nShape before:', X.shapeprint 'Shape after:', X_new.shape
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。