Exemplo n.º 1
0
def visualizeTrainedModel_v1():
    '''
	3d Surface plot
	'''
    modelName = 'mix-fKDE2_cv'
    modelPath = 'models/{0}'.format(modelName)
    trainedModels = random.sample(os.listdir(modelPath), 10)
    for m in trainedModels:
        mFile = '{0}/{1}'.format(modelPath, m)
        print(mFile)
        pId = mFile.split('/')[-1].replace('.model', '').replace(
            '_{0}'.format(modelName), '')
        print(pId)
        m = comparemethods.mixfKDEModel(pId, modelFile=mFile)
        m.loadModel()
        kde = m.trainedModel

        # xs: advanced day
        # ys: price kpi
        # fixed: seat class, daytype
        daytype = commonoperation.DayType.WORKDAY
        hourtype = 15 / 2
        seat = 1
        discount = 0.8

        fig = plt.figure()
        ax = fig.gca(projection='3d')
        X = np.linspace(0, 30, 61)  #num of advanced days
        Y = np.linspace(0, 1, 51)  #price kpi
        X, Y = np.meshgrid(X, Y)
        old_shape = X.shape
        N = X.shape[0] * X.shape[1]
        X1 = np.reshape(X, (N, ))
        Y1 = np.reshape(Y, (N, ))
        data = []
        for k in range(0, N):
            data.append([X1[k], daytype, hourtype, seat, Y1[k], discount])
        print(len(data))
        Z1 = np.array(kde.computeProbs(data))
        Z = np.reshape(Z1, old_shape)
        print(X.shape, Y.shape, Z.shape)

        surf = ax.plot_surface(X,
                               Y,
                               Z,
                               rstride=1,
                               cstride=1,
                               cmap=cm.coolwarm,
                               linewidth=0,
                               antialiased=False)
        ax.zaxis.set_major_locator(LinearLocator(10))
        ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
        fig.colorbar(surf, shrink=0.5, aspect=5)
        ax.set_xlabel(r'#advanced days')
        ax.set_ylabel(r'price kpi')
        ax.set_zlabel(r'density')

        plt.savefig('plots/{0}_3d.pdf'.format(pId))
        plt.close(fig)
    return
Exemplo n.º 2
0
def visualizeTrainedModel():
    '''
	3d bar plot
	'''
    modelName = 'mix-fKDE2_cv'
    modelPath = 'models/{0}'.format(modelName)
    # 3000939044_mix-fKDE2_1.5.model
    # mFile='models/{0}/{1}_{2}.model'.format(modelName, uId,modelName)
    trainedModels = random.sample(os.listdir(modelPath), 10)
    for m in trainedModels:
        mFile = '{0}/{1}'.format(modelPath, m)
        print(mFile)
        pId = mFile.split('/')[-1].replace('.model', '').replace(
            '_{0}'.format(modelName), '')
        print(pId)
        m = comparemethods.mixfKDEModel(pId, modelFile=mFile)
        m.loadModel()
        kde = m.trainedModel

        # xs: advanced day
        # ys: price kpi
        # fixed: seat class, daytype
        daytype = commonoperation.DayType.WORKDAY
        hourtype = 15 / 2
        seat = 1
        discount = 0.8

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        for c, z in zip(['r', 'g', 'b', 'y'], [0.25, 0.5, 0.75, 1]):
            data = []
            xs = range(0, 20, 2)
            for x in xs:
                data.append([x, daytype, hourtype, seat, z, discount])
            ys = kde.computeProbs(data)
            # You can provide either a single color or an array. To demonstrate this,
            # the first bar of each set will be colored cyan.
            cs = [c] * len(xs)
            # cs[0] = 'c'
            ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
            # print(xs)
            # print(ys)
            # print(z
            # break
        ax.set_xlabel(r'#advanced days')
        ax.set_ylabel(r'price kpi')
        ax.set_zlabel(r'density')
        plt.savefig('plots/{0}_3d.pdf'.format(pId))
        plt.close(fig)
    return
Exemplo n.º 3
0
 def train(self, simiDefine='Euclid'):
     for u in self.userList:
         if simiDefine == 'Euclid':
             m6 = comparemethods.mixfKDEModel(
                 u,
                 modelName=self.modelName,
                 trainData=self.userTrainData[u],
                 testData=self.userTestData[u],
                 trainDataOfNeighbors=self.featureListOfNeighbors[u])
         else:
             m6 = comparemethods.mixfKDEModelDim(
                 u,
                 modelName=self.modelName,
                 trainData=self.userTrainData[u],
                 testData=self.userTestData[u],
                 trainDataOfNeighbors=self.featureListOfNeighborsDim[u])
         m6.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml')
         m6.train()
         self.modelList[u] = m6
     return 0
Exemplo n.º 4
0
def modelUsers(users):  # experiment 1
    # users=usergeneration.generateSamplesOfActiveUsers(10000)
    for u in users:
        print(u)
        datapreparation.prepareData(u)
        traindata = datapreparation.getTraindata(u)
        testdata = datapreparation.getTestdata(u)

        print('only train data')
        # GMM
        m1 = comparemethods.GMMModel(u,
                                     modelName='GMM1_2',
                                     trainData=traindata,
                                     testData=testdata)
        m1.setVariables(nComponents=2)
        m1.run()

        m1 = comparemethods.GMMModel(u,
                                     modelName='GMM1_3',
                                     trainData=traindata,
                                     testData=testdata)
        m1.setVariables(nComponents=3)
        m1.run()

        m2 = comparemethods.fKDEModel(u,
                                      modelName='fKDE1_Silverman',
                                      trainData=traindata,
                                      testData=testdata)
        m2.setVariables(bandwidth='Silverman')
        m2.run()

        neighbors = usergeneration.getSimilarUsers(u)
        featuresListOfNeighbors = []
        for n in neighbors:
            featuresListOfNeighbors += featureextraction.generateFeaturesList(
                n)

        print('with others data')
        print('GMM')
        m3 = comparemethods.GMMModel(u,
                                     modelName='GMM2_2',
                                     trainData=traindata +
                                     featuresListOfNeighbors,
                                     testData=testdata)
        m3.setVariables(nComponents=2)
        m3.run()

        m3 = comparemethods.GMMModel(u,
                                     modelName='GMM2_3',
                                     trainData=traindata +
                                     featuresListOfNeighbors,
                                     testData=testdata)
        m3.setVariables(nComponents=3)
        m3.run()

        print('fKDE2_Silverman')
        m4 = comparemethods.fKDEModel(u,
                                      modelName='fKDE2_Silverman',
                                      trainData=traindata +
                                      featuresListOfNeighbors,
                                      testData=testdata)
        m4.setVariables(bandwidth='Silverman')
        m4.run()

        print('mix-fKDE1_Silverman')
        m5 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE1_Silverman',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m5.setVariables(bandwidth='Silverman', bandwidth1='Silverman')
        m5.run()

        print('mix-fKDE2_cv')
        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml')
        m6.run()
Exemplo n.º 5
0
def modelUsers3(users):
    '''
	Compare mix-fKDE2_cv with different kernels
	'''
    for u in users:
        print(u)
        datapreparation.prepareData(u)
        # train,test data from this user
        traindata = datapreparation.getTraindata(u)
        testdata = datapreparation.getTestdata(u)

        neighbors = usergeneration.getSimilarUsers(u)
        # feature from other users, to make mix KDE
        featuresListOfNeighbors = []
        for n in neighbors:
            featuresListOfNeighbors += featureextraction.generateFeaturesList(
                n)

        m2 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv_tophat',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m2.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml', kernel='tophat')
        m2.run()

        m3 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv_epanechnikov',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m3.setVariables(bandwidth='cv_ml',
                        bandwidth1='cv_ml',
                        kernel='epanechnikov')
        m3.run()

        m4 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv_exponential',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m4.setVariables(bandwidth='cv_ml',
                        bandwidth1='cv_ml',
                        kernel='exponential')
        m4.run()

        m5 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv_linear',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m5.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml', kernel='linear')
        m5.run()

        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv_cosine',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml', kernel='cosine')
        m6.run()

        m1 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv_gaussian',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m1.setVariables(bandwidth='cv_ml',
                        bandwidth1='cv_ml',
                        kernel='gaussian')
        m1.run()
Exemplo n.º 6
0
def modelUsers2(users):  # experiment 2
    '''
	Compare different methods with different bandwidths
	'''
    # users=usergeneration.generateSamplesOfActiveUsers(10000)
    for u in users:
        print(u)
        datapreparation.prepareData(u)
        traindata = datapreparation.getTraindata(u)
        testdata = datapreparation.getTestdata(u)

        print('only train data')
        # GMM
        m1 = comparemethods.GMMModel(u,
                                     modelName='GMM1_2',
                                     trainData=traindata,
                                     testData=testdata)
        m1.setVariables(nComponents=2)
        m1.run()

        m1 = comparemethods.GMMModel(u,
                                     modelName='GMM1_3',
                                     trainData=traindata,
                                     testData=testdata)
        m1.setVariables(nComponents=3)
        m1.run()

        m2 = comparemethods.fKDEModel(u,
                                      modelName='fKDE1_Silverman',
                                      trainData=traindata,
                                      testData=testdata)
        m2.setVariables(bandwidth='Silverman')
        m2.run()

        m2 = comparemethods.fKDEModel(u,
                                      modelName='fKDE1_0.5',
                                      trainData=traindata,
                                      testData=testdata)
        m2.setVariables(bandwidth=0.5)
        m2.run()

        m2 = comparemethods.fKDEModel(u,
                                      modelName='fKDE1_1',
                                      trainData=traindata,
                                      testData=testdata)
        m2.setVariables(bandwidth=1)
        m2.run()

        m2 = comparemethods.fKDEModel(u,
                                      modelName='fKDE1_1.5',
                                      trainData=traindata,
                                      testData=testdata)
        m2.setVariables(bandwidth=1.5)
        m2.run()

        neighbors = usergeneration.getSimilarUsers(u)
        featuresListOfNeighbors = []
        for n in neighbors:
            featuresListOfNeighbors += featureextraction.generateFeaturesList(
                n)

        # try another method: find neighbors on each dimension, each idx represents a dim
        featuresListOfNeighborsDims = []
        for i in range(0, len(basicmining.getProfile(u))):
            neighbors = usergeneration.getSimilarUsersDim(u, i)
            listTemp = []
            for n in neighbors:
                listTemp += (featureextraction.generateFeaturesList(n))
            featuresListOfNeighborsDims.append(listTemp)

        print('with others data')
        m3 = comparemethods.GMMModel(u,
                                     modelName='GMM2_2',
                                     trainData=traindata +
                                     featuresListOfNeighbors,
                                     testData=testdata)
        m3.setVariables(nComponents=2)
        m3.run()

        m3 = comparemethods.GMMModel(u,
                                     modelName='GMM2_3',
                                     trainData=traindata +
                                     featuresListOfNeighbors,
                                     testData=testdata)
        m3.setVariables(nComponents=3)
        m3.run()

        # print('fKDE2_Silverman')
        m4 = comparemethods.fKDEModel(u,
                                      modelName='fKDE2_Silverman',
                                      trainData=traindata +
                                      featuresListOfNeighbors,
                                      testData=testdata)
        m4.setVariables(bandwidth='Silverman')
        m4.run()

        # print('mix-fKDE2_bw')
        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_Silverman',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth='Silverman', bandwidth1='Silverman')
        m6.run()

        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_0.5',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth=0.5, bandwidth1=0.5)
        m6.run()

        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_1',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth=1, bandwidth1=1)
        m6.run()

        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_1.5',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth=1.5, bandwidth1=1.5)
        m6.run()

        m6 = comparemethods.mixfKDEModel(
            u,
            modelName='mix-fKDE2_cv',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighbors)
        m6.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml')
        m6.run()
        # test set components according to the dim
        m7 = comparemethods.mixfKDEModelDim(
            u,
            modelName='mix-fKDE2_cv_moreComponents',
            trainData=traindata,
            testData=testdata,
            trainDataOfNeighbors=featuresListOfNeighborsDims)
        m7.setVariables(bandwidth='cv_ml', bandwidth1='cv_ml')
        m7.run()