Exemplo n.º 1
0
def sigmoid_classifier(data, Mu, Sigma):
    checksig = logGaussPdf(data, Mu, Sigma)
    df = pd.DataFrame(checksig)
    df.to_csv('checksig.csv')
    normalizer = 1e10
    valuereturn = act.sigmoid(checksig / normalizer)
    return valuereturn
Exemplo n.º 2
0
def mixofg_bin_classifier(y, mean1, mean2, sig1, sig2, pri1, pri2, th=0):
    temp1 = np.ndarray((y.shape[1], pri1.shape[1]))
    temp2 = np.ndarray((y.shape[1], pri1.shape[1]))

    for i in range(pri1.shape[1]):
        temp1[:, i] = np.nan_to_num(
            np.log(pri1[0, i] + realmin) +
            gpdf.logGaussPdf(y, mean1[:, i].reshape(mean1.shape[0], 1),
                             sig1[:, :, i])).reshape((temp1.shape[0]))
    for i in range(pri2.shape[1]):
        temp2[:, i] = np.nan_to_num(
            np.log(pri2[0, i] + realmin) +
            gpdf.logGaussPdf(y, mean2[:, i].reshape(mean2.shape[0], 1),
                             sig2[:, :, i])).reshape((temp2.shape[0]))

    pred = []

    temp1 = np.nan_to_num(sp.logsumexp(temp1, axis=1))
    temp2 = np.nan_to_num(sp.logsumexp(temp2, axis=1))

    Prob1 = temp1 / (temp1 + temp2)
    Prob2 = temp2 / (temp1 + temp2)
    Prob1 = Prob1.reshape((temp1.shape[0]))
    Prob2 = Prob2.reshape((temp2.shape[0]))

    for i in range(Prob1.shape[0]):
        if (Prob2[i] - Prob1[i]) > 0:
            pred.append(1)
        else:
            pred.append(0)

    return pred, act.sigmoid(100 * (Prob1 - Prob2))
def factorclassifier(x, m1, m2, s1, s2, theta1, theta2, threshold=0.5):
    '''
        threshold =0.5
        x: the input data=array(featurelength=1200,number of datapoints=1000)
        m1,m2: Mean of class face and non face, shape =(featurelength,)
        s1,s2 : Variance of the calss face and non face diagonal mat, shape=(1200,)
        theta1,theta2: factor matrix, shape=(1200,3)
    '''
    predict = []
    mu1 = m1.reshape(m1.shape[0], 1)
    mu2 = m2.reshape(m2.shape[0], 1)
    sigma1 = np.matmul(theta1, np.transpose(theta1)) + np.diag(s1)
    sigma2 = np.matmul(theta2, np.transpose(theta2)) + np.diag(s2)

    p1 = gpdf.logGaussPdf(x, mu1, sigma1)
    p2 = gpdf.logGaussPdf(x, mu2, sigma2)
    p1 = sp.softmax(p1)
    p2 = sp.softmax(p2)
    Prob1 = p1 / (p1 + p2)
    Prob2 = p2 / (p1 + p2)

    Prob1 = Prob1.reshape(p1.shape[0])
    Prob2 = Prob2.reshape(p2.shape[0])

    #

    for i in range(p1.shape[0]):
        if (Prob2[i] - Prob1[i]) < 0:
            predict.append(1)
        else:
            predict.append(0)
    return predict, act.sigmoid(1000 * (Prob2 - Prob1))
    def classifier(self, featurevector, a1, t1):

        temp1 = np.matmul(
            np.transpose(featurevector - self.mean_class_f),
            np.linalg.inv(self.var_class_f)
            @ (featurevector - self.mean_class_f))
        temp2 = np.matmul(
            np.transpose(featurevector - self.mean_class_nf),
            np.linalg.inv(self.var_class_f)
            @ (featurevector - self.mean_class_nf))
        normalization_constant = 1e9  # a normalization constant to handle data overflow and underflow
        temp3 = (act.sigmoid((a1 + temp1 - temp2) / normalization_constant))
        print(temp3)
        #print(temp2)
        #print(type(temp2))
        #print(a1)
        #print(type(a1))
        if temp3 <= t1:
            return 1, temp3
        else:
            return 0, temp3
def studt():
    
    trainface,testface,trainnonface,testnonface=load_data()
    pca_face1=PCA(30)
    pca_face2=PCA(30)
    pca_face3=PCA(30)
    temp1=trainface.Feature.to_numpy()
    temp2=trainnonface.Feature.to_numpy()
    array_trainf = np.zeros((len(temp1),temp1[0].shape[0]))
    array_trainnf = np.zeros((len(temp2),temp2[0].shape[0]))
    for i in range(len(temp1)):
        array_trainf[i,:] = temp1[i].flatten()
        array_trainnf[i,:] = temp2[i].flatten()
        
    f_train_pca=pca_face1.fit_transform(array_trainf)
    nf_train_pca=pca_face2.fit_transform(array_trainnf)
    
    temp3=testface.Feature.to_numpy()
    temp4=testnonface.Feature.to_numpy()
    array_testf = np.zeros((len(temp3),temp3[0].shape[0]))
    array_testnf = np.zeros((len(temp4),temp4[0].shape[0]))
    for i in range(len(temp3)):
        array_testf[i,:] = temp3[i].flatten()
        array_testnf[i,:] = temp4[i].flatten()
        
    f_test_pca=pca_face3.fit_transform(array_testf)
    nf_test_pca=pca_face3.fit_transform(array_testnf)
    
    (mu_f,sig_f,nu_f)=tfit.fit_t(f_train_pca,0.01)
    (mu_nf,sig_nf,nu_nf)=tfit.fit_t(nf_train_pca,0.01)
                               
    px_face_pf = pdf_tm.pdf_tm(f_test_pca,mu_f,sig_f,nu_f)
    px_nonface_pf = pdf_tm.pdf_tm(nf_test_pca,mu_f,sig_f,nu_f)
    px_face_pnf = pdf_tm.pdf_tm(f_test_pca,mu_nf,sig_nf,nu_nf)
    px_nonface_pnf = pdf_tm.pdf_tm(nf_test_pca,mu_nf,sig_nf,nu_nf)
    
    Prob_face = np.concatenate((px_face_pf,px_nonface_pf))
    Prob_nonface = np.concatenate((px_face_pnf,px_nonface_pnf))
    
    
    
    df = pd.concat([testface,testnonface],ignore_index = True)
    #print(Prob_face.shape)
    pred = []
    for i in range(Prob_face.shape[0]):
        if Prob_face[i]-Prob_nonface[i] > 0:
            pred.append(1)
        else:
            pred.append(0)
    Prob_faced = Prob_face/(Prob_face+Prob_nonface)
    Prob_nonfaced = Prob_nonface/(Prob_face+Prob_nonface)
    df['CheckProbFace'] = Prob_faced
    df['CheckProbNonface'] = Prob_nonfaced        
    df['Predictions'] = pred
    df['Probability'] = act.sigmoid(5*(Prob_nonfaced-Prob_faced))
    
    df.to_csv('studentT.csv')
    r_o_c = roc.roc(1200)
    r_o_c.evaluation_criteria(df)
    r_o_c.plot_ROC(df)
    
    mu_f = pca_face1.inverse_transform(mu_f)
    mu_f=mu_f.flatten()
    mu_nf=pca_face2.inverse_transform(mu_nf)
    mu_nf=mu_nf.flatten()
    sig_f = pca_face1.inverse_transform(sig_f)
    sig_nf = pca_face2.inverse_transform(sig_nf)

    
    mvp.mean_variance_plot(mu_f,sig_f,'studfacemean','studfacevar','RGB',20,'BOTH')
    mvp.mean_variance_plot(mu_nf,sig_nf,'studnonfacemean','studnonfacevar','RGB',20,'BOTH')
    

##################################################################################################
   
    '''mixture of T'''
    '''Uncomment mix_tfit import file from top to run this part'''