def run(self):

        # using gradDescent to get the optimal parameter beta = [w, b] in page-59
        beta = self_def.gradDscent_2(self.X_train, self.y_train)

        # prediction, beta mapping to the model
        y_pred = self_def.predict(self.X_test, beta)

        m_test = np.shape(self.X_test)[0]
        # calculation of confusion_matrix and prediction accuracy
        cfmat = np.zeros((2, 2))
        for i in range(m_test):
            if y_pred[i] == self.y_test[i] == 0:
                cfmat[0, 0] += 1
            elif y_pred[i] == self.y_test[i] == 1:
                cfmat[1, 1] += 1
            elif y_pred[i] == 0:
                cfmat[1, 0] += 1
            elif y_pred[i] == 1:
                cfmat[0, 1] += 1

        print(cfmat)

        pass
Пример #2
0
from sklearn import model_selection

import self_def

# X_train, X_test, y_train, y_test
np.ones(n)
m, n = np.shape(X)
X_ex = np.c_[X, np.ones(m)]  # extend the variable matrix to [x, 1]
#print (X_ex)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X_ex, y, test_size=0.5, random_state=0)

# using gradDescent to get the optimal parameter beta = [w, b] in page-59
beta = self_def.gradDscent_1(X_train, y_train)

# prediction, beta mapping to the model
y_pred = self_def.predict(X_test, beta)

m_test = np.shape(X_test)[0]
# calculation of confusion_matrix and prediction accuracy
cfmat = np.zeros((2, 2))
for i in range(m_test):
    if y_pred[i] == y_test[i] == 0:
        cfmat[0, 0] += 1
    elif y_pred[i] == y_test[i] == 1:
        cfmat[1, 1] += 1
    elif y_pred[i] == 0:
        cfmat[1, 0] += 1
    elif y_pred[i] == 1:
        cfmat[0, 1] += 1

print(cfmat)
from sklearn import model_selection

import self_def

# X_train, X_test, y_train, y_test
np.ones(n)
m,n = np.shape(X)
X_ex = np.c_[X, np.ones(m)]  # extend the variable matrix to [x, 1]
X_train, X_test, y_train, y_test = model_selection.train_test_split(X_ex, y, test_size=0.5, random_state=0)


# using gradDescent to get the optimal parameter beta = [w, b] in page-59 
beta = self_def.gradDscent_2(X_train, y_train)

# prediction, beta mapping to the model
y_pred = self_def.predict(X_test, beta)

m_test = np.shape(X_test)[0]
# calculation of confusion_matrix and prediction accuracy
cfmat = np.zeros((2,2))
for i in range(m_test):   
    if y_pred[i] == y_test[i] == 0: cfmat[0,0] += 1 
    elif y_pred[i] == y_test[i] == 1: cfmat[1,1] += 1 
    elif y_pred[i] == 0: cfmat[1,0] += 1 
    elif y_pred[i] == 1: cfmat[0,1] += 1 
                                
print(cfmat)