class _PLSSVDImpl: def __init__(self, **hyperparams): self._hyperparams = hyperparams self._wrapped_model = Op(**self._hyperparams) def fit(self, X, y=None): if y is not None: self._wrapped_model.fit(X, y) else: self._wrapped_model.fit(X) return self def transform(self, X): return self._wrapped_model.transform(X)
pos_max = np.argmax(acc_val) num_opt_feat = rang_feat[pos_max] test_acc_opt = acc_test[pos_max] print 'Number optimum of features: ' + str(num_opt_feat) print("The optimum test accuracy is %2.2f%%" % (100 * test_acc_opt)) ########################### PLS ##################################3 from sklearn.cross_decomposition import PLSSVD N_feat_max = n_classes # As many new features as classes minus 1 # 1. Obtain PLS projections pls = PLSSVD(n_components=N_feat_max) pls.fit(X_train, Y_train_bin) X_train_pls = pls.transform(X_train) X_val_pls = pls.transform(X_val) X_test_pls = pls.transform(X_test) # 2. Compute and plot accuracy evolution rang_feat = np.arange(1, N_feat_max, 1) [acc_tr, acc_val, acc_test] = SVM_accuracy_evolution(X_train_pls, Y_train, X_val_pls, Y_val, X_test_pls, Y_test, rang_feat, C, gamma) plt.figure() plot_accuracy_evolution(rang_feat, acc_tr, acc_val, acc_test) plt.show() # 3. Find the optimum number of features pos_max = np.argmax(acc_val) num_opt_feat = rang_feat[pos_max]
plt.title('Projected data over the components') plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.show() if (0): #%% PARTIAL LEAST SQUARES #%% PLS SVD nComponents = np.arange(1, nClasses + 1) plsSvdScores = np.zeros((5, np.alen(nComponents))) for i, n in enumerate(nComponents): plssvd = PLSSVD(n_components=n) plssvd.fit(Xtrain, Ytrain) XtrainT = plssvd.transform(Xtrain) XtestT = plssvd.transform(Xtest) plsSvdScores[:, i] = util.classify(XtrainT, XtestT, labelsTrain, labelsTest) plssvd = PLSSVD(n_components=2) xt, yt = plssvd.fit_transform(Xtrain, Ytrain) fig = plt.figure() util.plotData(fig, xt, labelsTrain, classColors) plt.title('First 2 components of projected data') #%% Plot accuracies for PLSSVD plt.figure() for i in range(5): plt.plot(nComponents, plsSvdScores[i, :], lw=3)
dataTestT = pca.transform(dataTest) pcaScores[:,i] = util.classify(dataTrainT,dataTestT,labelsTrain,labelsTest) # Training data with 2 dimensions pca = PCA(n_components=2) xtPCA = pca.fit_transform(dataTrain) uPCA = pca.components_ #%% PARTIAL LEAST SQUARES #%% PLS SVD nComponents = np.arange(1,nClasses+1) plsSvdScores = np.zeros((2,np.alen(nComponents))) for i,n in enumerate(nComponents): plssvd = PLSSVD(n_components=n) plssvd.fit(dataTrain,Ytrain) dataTrainT = plssvd.transform(dataTrain) dataTestT = plssvd.transform(dataTest) plsSvdScores[:,i] = util.classify(dataTrainT,dataTestT,labelsTrain,labelsTest) fig = plt.figure() util.plotAccuracy(fig,nComponents,plsSvdScores) plt.title('PLS SVD accuracy',figure=fig) plssvd = PLSSVD(n_components=2) xt,yt = plssvd.fit_transform(dataTrain,Ytrain) fig = plt.figure() util.plotData(fig,xt,labelsTrain,classColors) u = plssvd.x_weights_ plt.quiver(u[0,0],u[1,0],color='k',edgecolor='k',lw=1,scale=0.1,figure=fig) plt.quiver(-u[1,0],u[0,0],color='k',edgecolor='k',lw=1,scale=0.4,figure=fig)
plt.title('Projected data over the components') plt.xlim([-4,4]) plt.ylim([-4,4]) plt.show() if (0): #%% PARTIAL LEAST SQUARES #%% PLS SVD nComponents = np.arange(1,nClasses+1) plsSvdScores = np.zeros((5,np.alen(nComponents))) for i,n in enumerate(nComponents): plssvd = PLSSVD(n_components=n) plssvd.fit(Xtrain,Ytrain) XtrainT = plssvd.transform(Xtrain) XtestT = plssvd.transform(Xtest) plsSvdScores[:,i] = util.classify(XtrainT,XtestT,labelsTrain,labelsTest) plssvd = PLSSVD(n_components=2) xt,yt = plssvd.fit_transform(Xtrain,Ytrain) fig = plt.figure() util.plotData(fig,xt,labelsTrain,classColors) plt.title('First 2 components of projected data') #%% Plot accuracies for PLSSVD plt.figure() for i in range (5): plt.plot(nComponents,plsSvdScores[i,:],lw=3) plt.xlim(1,np.amax(nComponents))