Exemple #1
0
from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from nfold import nFold


mat = loadmat('Assignment_2_datasets/dataset_2.mat')
samples = mat['samples']
labels = mat['labels'][0]
train_samples,train_labels,test_samples,test_labels = nFold(samples,labels,5,4)

clf = LogisticRegression(random_state=0).fit(samples, labels)
pred = clf.predict(samples)
print(accuracy_score(pred,labels))
print(clf.coef_)
print(clf.intercept_)
Exemple #2
0
def mse(pred, test_l):
    k = pred - test_l
    k = np.multiply(k, k)
    return np.mean(k)


samples, labels = load_data()

fold = []
tr_err = []
te_err = []
sk_tr_err = []
sk_te_err = []

for i in range(5):  #FOR 5 FOLDS
    train_s, train_l, test_s, test_l = nFold(samples, labels, 5, i)
    model = Regression()
    model.fit(train_s, train_l)

    train_pred = model.predict(train_s)
    test_pred = model.predict(test_s)

    fold.append(i)
    tr_err.append(mse(train_pred, train_l))
    te_err.append(mse(test_pred, test_l))
    sk_tr_err.append(mean_squared_error(train_pred, train_l))
    sk_te_err.append(mean_squared_error(test_pred, test_l))

with open('Q1_b.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerow([
Exemple #3
0
    plt.plot(lam_list, acc)
    plt.title('Grid Search')
    plt.xlabel('Lambda')
    plt.ylabel('Accuracy')
    plt.show()

    return lam_list[np.argmax(acc)]


mat = loadmat('Assignment_2_datasets/dataset_1.mat')
samples = mat['samples']
labels = mat['labels'][0]
samples = np.append(samples, np.ones((len(labels), 1)),
                    axis=1)  #Adding a column of 1 for constant
train_s, train_l, test_s, test_l = nFold(samples, labels, 5,
                                         4)  #Grid search for lambda value

#lam = perform_grid_search(train_s,train_l,test_s,test_l)
lam = 5
tr_acc = []
te_acc = []
tr_loss = []
te_loss = []

for i in range(5):

    train_s, train_l, test_s, test_l = nFold(samples, labels, 5, i)
    model = LogRegression(lam)
    iter, train_acc, train_loss, test_acc, test_loss = model.fit(
        train_s, train_l, test_s, test_l)