Пример #1
0
    def constructSimilartyMatrixITML(self, k=5):
        print 'Now doing itml'
        num_constraints = 100
        itml = ITML()
        C = ITML.prepare_constraints(self.y_train,
                                     self.trainVectorsPCA.shape[0],
                                     num_constraints)
        itml.fit(self.trainVectorsPCA, C, verbose=True)
        self.L_itml = itml.transformer()

        name = 'itml/ITML transformer matrix with dataset shape ' + str(
            self.trainVectorsPCA.shape)
        print 'L itml shape  is ', self.L_itml.shape
        np.save(name, self.L_itml)

        # Input data transformed to the metric space by X*L.T

        self.transformedTrainITML = copy(itml.transform(self.trainVectorsPCA))
        self.transformedTestITML = copy(itml.transform(self.testVectorsPCA))
        self.transformedAllITML = copy(itml.transform(self.allDataPCA))
        # now we can simply calculate the eucledian distances on the above transformed dataset

        #Visualizing the dataset by TSNE
        projectedDigits = TSNE(random_state=randomState).fit_transform(
            self.transformedTrainITML)

        plt.scatter(projectedDigits[:, 0],
                    projectedDigits[:, 1],
                    c=self.y_train)
        plt.title(
            'ITML Transformed Train set projected to 2 Dimensions by TSNE with k='
            + str(k) + ' and num_constraints = ' + str(num_constraints))
        plt.savefig(pp, format='pdf')
        self.pwdis = copy(
            pairwise_distances(self.transformedAllITML, metric='euclidean'))
        self.D = np.zeros(self.pwdis.shape)
        for i in range(0, self.pwdis.shape[0]):
            l1 = self.pwdis[i].tolist()
            #print 'l1 is ',l1,'\n\n'
            allnearestNeighbours = sorted(range(len(l1)), key=lambda i: l1[i])
            #now set the all the weights except for k+1 to 0
            self.pwdis[i, allnearestNeighbours[k:]] = 0
            self.D[i, i] = sum(self.pwdis[i])

        print 'accuracy for ITML\n'
        self.labelPropogation()
 def constructSimilartyMatrixITML(self,k=5):
     print 'Now doing itml'
     num_constraints=100
     itml=ITML()
     C = ITML.prepare_constraints(self.y_train, self.trainVectorsPCA.shape[0], num_constraints)
     itml.fit(self.trainVectorsPCA, C, verbose=True)
     self.L_itml=itml.transformer()
     
     name='itml/ITML transformer matrix with dataset shape '+str(self.trainVectorsPCA.shape)
     print 'L itml shape  is ',self.L_itml.shape
     np.save(name,self.L_itml)
     
     # Input data transformed to the metric space by X*L.T
     
     self.transformedTrainITML=copy(itml.transform(self.trainVectorsPCA))
     self.transformedTestITML=copy(itml.transform(self.testVectorsPCA))
     self.transformedAllITML=copy(itml.transform(self.allDataPCA))
     # now we can simply calculate the eucledian distances on the above transformed dataset
     
     #Visualizing the dataset by TSNE
     projectedDigits = TSNE(random_state=randomState).fit_transform(self.transformedTrainITML)
     
     plt.scatter(projectedDigits[:,0],projectedDigits[:,1],c=self.y_train)
     plt.title('ITML Transformed Train set projected to 2 Dimensions by TSNE with k='+str(k)+' and num_constraints = '+str(num_constraints))
     plt.savefig(pp,format='pdf')
     self.pwdis=copy(pairwise_distances(self.transformedAllITML,metric='euclidean'))
     self.D=np.zeros(self.pwdis.shape)
     for i in range(0,self.pwdis.shape[0]):
         l1=self.pwdis[i].tolist()
         #print 'l1 is ',l1,'\n\n'
         allnearestNeighbours=sorted(range(len(l1)),key=lambda i : l1[i])
         #now set the all the weights except for k+1 to 0
         self.pwdis[i,allnearestNeighbours[k:]]=0
         self.D[i,i]=sum(self.pwdis[i])
     
     print 'accuracy for ITML\n'
     self.labelPropogation()
Пример #3
0
    def constructSimilartyMatrixITML(self):

        print 'Now doing itml'
        counter = 1
        ks = [
            3, 5, 7, 10, 12, 15, 20, 22, 25, 27, 30, 33, 35, 37, 40, 43, 45,
            47, 50, 53, 55, 57, 60, 65
        ]

        constraints = [80, 100, 120, 150, 180, 200]
        constraints = [100]
        for k in ks:
            for num_constraints in constraints:

                itml = ITML()
                self.y_train = self.y_train.reshape(-1, )
                C = ITML.prepare_constraints(self.y_train,
                                             self.trainVectorsPCA.shape[0],
                                             num_constraints)
                itml.fit(self.trainVectorsPCA, C, verbose=True)
                self.L_itml = copy(itml.transformer())

                name = 'itml/ITML transformer matrix with dataset shape ' + str(
                    self.trainVectorsPCA.shape) + ' and k=' + str(
                        k) + ' and num_constraints=' + str(num_constraints)
                #print 'L itml shape  is ',self.L_itml.shape
                np.save(name, self.L_itml)

                # Input data transformed to the metric space by X*L.T

                self.transformedTrainITML = copy(
                    itml.transform(self.trainVectorsPCA))
                self.transformedTestITML = copy(
                    itml.transform(self.testVectorsPCA))
                self.transformedAllITML = copy(itml.transform(self.allDataPCA))
                # now we can simply calculate the eucledian distances on the above transformed dataset

                #Visualizing the dataset by TSNE
                projectedDigits = TSNE(random_state=randomState).fit_transform(
                    self.transformedAllITML)

                print 'projectedDigits is ', projectedDigits.shape
                plt.figure()
                plt.scatter(projectedDigits[:, 0],
                            projectedDigits[:, 1],
                            c=self.labels)
                plt.title(
                    'ITML Transformed ALL set projected to 2 Dimensions by TSNE with'
                    + str(k) + ' and num_constraints=' + str(num_constraints))
                plt.savefig(pp, format='pdf')
                #plt.show()
                plt.close()

                self.pwdis = copy(
                    pairwise_distances(self.transformedAllITML,
                                       metric='euclidean'))
                #sigmas=[1,2.5,2,2.5,3,3.5,4,4.5,5]
                #for sigma in sigmas:

                self.D = np.zeros(self.pwdis.shape)
                for i in range(0, self.pwdis.shape[0]):
                    l1 = self.pwdis[i].tolist()
                    #print 'l1 is ',l1,'\n\n'
                    allnearestNeighbours = sorted(range(len(l1)),
                                                  key=lambda i: l1[i])
                    #now set the all the weights except for k+1 to 0
                    #since we exponentiated the distances with the minus sign we need to set the lowest weights to 0, so everything except for the last k go to 0
                    self.pwdis[i, allnearestNeighbours[k:]] = 0
                    self.D[i, i] = sum(self.pwdis[i])

                print 'accuracy for ITML for k= ', k, ' and num_constraints=' + str(
                    num_constraints), '\n'
                self.labelPropogation()
import numpy as np
from metric_learn import ITML
from sklearn.datasets import load_iris

iris_data = load_iris()
X = iris_data['data']
Y = iris_data['target']

print 'Y is ',Y.shape
print 'X.shape is ',X.shape
itml = ITML()

num_constraints = 200
C = ITML.prepare_constraints(Y, X.shape[0], num_constraints)
itml.fit(X, C, verbose=False)
x2=itml.transform(X)

print 'x2 is ',x2
l=itml.transformer()
print '\n\n\nafter transforming is ',np.dot(X,l.T)
import numpy as np
from metric_learn import ITML
from sklearn.datasets import load_iris

iris_data = load_iris()
X = iris_data['data']
Y = iris_data['target']

print 'Y is ', Y.shape
print 'X.shape is ', X.shape
itml = ITML()

num_constraints = 200
C = ITML.prepare_constraints(Y, X.shape[0], num_constraints)
itml.fit(X, C, verbose=False)
x2 = itml.transform(X)

print 'x2 is ', x2
l = itml.transformer()
print '\n\n\nafter transforming is ', np.dot(X, l.T)