Exemple #1
0
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))
    plt.title('PLS SVD accuracy')
    plt.xlabel('Number of components')
    plt.ylabel('accuracy')
    plt.legend(['LR', 'LDA', 'GNB', 'Linear SVM', 'rbf SVM'],
               loc='lower right')
#%% 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)

#%% PLS mode-A
lda = LDA()
nComponents = np.arange(1,nClasses+1)
plsCanScores = np.zeros((2,np.alen(nComponents)))
for i,n in enumerate(nComponents):
    plscan = PLSCanonical(n_components=n)
    plscan.fit(dataTrain,Ytrain)
    dataTrainT = plscan.transform(dataTrain)
Exemple #3
0
Y_expanded = neuron_types.map(embed_map)
Y_expanded = np.stack(Y_expanded.values)
Y_expanded


# %%
Y_expanded = StandardScaler(with_mean=True, with_std=True, copy=False).fit_transform(
    Y_expanded
)
#%%
from sklearn.cross_decomposition import PLSSVD, CCA

currtime = time.time()
n_components = 5
model = PLSSVD(n_components=n_components)
X_scores, Y_scores = model.fit_transform(X_train, Y_expanded)
print(f"{time.time() - currtime} elapsed")

import matplotlib.pyplot as plt

for i in range(n_components):
    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    sns.scatterplot(
        x=X_scores[:, i],
        y=Y_scores[:, i],
        hue=neuron_types,
        palette=neuron_type_palette,
        ax=ax,
        legend=False,
    )
    ax.set(ylabel="Connectivity score", xlabel="Gene score")
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))
    plt.title('PLS SVD accuracy')
    plt.xlabel('Number of components')
    plt.ylabel('accuracy')
    plt.legend (['LR','LDA','GNB','Linear SVM','rbf SVM'],loc='lower right')
    plt.grid(True)