def output(part_id): # Random Test Cases X = np.column_stack((np.ones(20), (np.exp(1) * np.sin(np.linspace(1, 20, 20))), (np.exp(0.5) * np.cos(np.linspace(1, 20, 20))))) y = np.sin(X[:,0] + X[:,1]) > 0 Xm = np.array([[-1,-1],[-1,-2],[-2,-1],[-2,-2],[1,1],[1,2],[2,1],[2,2],[-1,1], [-1,2],[-2,1],[-2,2],[1,-1],[1,-2],[-2,-1],[-2,-2]]).reshape((16,2)) ym = np.array([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]).reshape(16,1) t1 = np.sin(np.array(range(1,24,2)).reshape(3,4).T) t2 = np.cos(np.array(range(1,40,2)).reshape(5,4).T) fname = srcs[part_id-1].rsplit('.',1)[0] mod = __import__(fname, fromlist=[fname], level=1) func = getattr(mod, fname) if part_id == 1: J = lrCostFunction(np.array([0.25, 0.5, -0.5]), X, y, 0.1) grad = gradientFunctionReg(np.array([0.25, 0.5, -0.5]), X, y, 0.1) return sprintf('%0.5f ', np.hstack((J, grad)).tolist()) elif part_id == 2: return sprintf('%0.5f ', oneVsAll(Xm, ym, 4, 0.1)) elif part_id == 3: return sprintf('%0.5f ', predictOneVsAll(t1, Xm)) elif part_id == 4: return sprintf('%0.5f ', predict(t1, t2, Xm))
def output(partId): # Random Test Cases X = np.stack([ np.ones(20), np.exp(1) * np.sin(np.arange(1, 21)), np.exp(0.5) * np.cos(np.arange(1, 21)) ], axis=1) y = (np.sin(X[:, 0] + X[:, 1]) > 0).astype(float) Xm = np.array([[-1, -1], [-1, -2], [-2, -1], [-2, -2], [1, 1], [1, 2], [2, 1], [2, 2], [-1, 1], [-1, 2], [-2, 1], [-2, 2], [1, -1], [1, -2], [-2, -1], [-2, -2]]) ym = np.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]) t1 = np.sin(np.reshape(np.arange(1, 25, 2), (4, 3), order='F')) t2 = np.cos(np.reshape(np.arange(1, 41, 2), (4, 5), order='F')) if partId == '1': J, grad = lrCostFunction(np.array([0.25, 0.5, -0.5]), X, y, 0.1) out = formatter('%0.5f ', J) out += formatter('%0.5f ', grad) elif partId == '2': out = formatter('%0.5f ', oneVsAll(Xm, ym, 4, 0.1)) elif partId == '3': out = formatter('%0.5f ', predictOneVsAll(t1, Xm)) elif partId == '4': out = formatter('%0.5f ', predict(t1, t2, Xm)) return out
def output(part_id): # Random Test Cases X = np.column_stack( (np.ones(20), (np.exp(1) * np.sin(np.linspace(1, 20, 20))), (np.exp(0.5) * np.cos(np.linspace(1, 20, 20))))) y = np.sin(X[:, 0] + X[:, 1]) > 0 Xm = np.array([[-1, -1], [-1, -2], [-2, -1], [-2, -2], [1, 1], [1, 2], [2, 1], [2, 2], [-1, 1], [-1, 2], [-2, 1], [-2, 2], [1, -1], [1, -2], [-2, -1], [-2, -2]]).reshape((16, 2)) ym = np.array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]).reshape(16, 1) t1 = np.sin(np.array(range(1, 24, 2)).reshape(3, 4).T) t2 = np.cos(np.array(range(1, 40, 2)).reshape(5, 4).T) fname = srcs[part_id - 1].rsplit('.', 1)[0] mod = __import__(fname, fromlist=[fname], level=1) func = getattr(mod, fname) if part_id == 1: J = lrCostFunction(np.array([0.25, 0.5, -0.5]), X, y, 0.1) grad = gradientFunctionReg(np.array([0.25, 0.5, -0.5]), X, y, 0.1) return sprintf('%0.5f ', np.hstack((J, grad)).tolist()) elif part_id == 2: return sprintf('%0.5f ', oneVsAll(Xm, ym, 4, 0.1)) elif part_id == 3: return sprintf('%0.5f ', predictOneVsAll(t1, Xm)) elif part_id == 4: return sprintf('%0.5f ', predict(t1, t2, Xm))
def train(): num_labels = 34 print '... Training' X = matrix(loadtxt('X.dat')) / 255.0 y = matrix(loadtxt('y.dat')).transpose() the_lambda = 0.1 all_theta = oneVsAll(X, y, num_labels, the_lambda) savetxt('theta.dat', all_theta)
def output(partId): # Random Test Cases X = column_stack((ones(20), exp(1) * sin(arange(1, 21, 1)), exp(0.5) * cos(arange(1, 21, 1)))) y = (sin(X[:,0] + X[:,1]) > 0).astype(int) Xm = array([[-1, -1], [-1, -2], [-2, -1], [-2, -2], [1, 1], [1, 2], [2, 1], [2, 2], [-1, 1], [-1, 2], [-2, 1], [-2, 2], [1, -1], [1, -2], [-2, -1], [-2, -2]]) ym = array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]) t1 = sin(arange(1, 25, 2).reshape(4, 3, order='F')) t2 = cos(arange(1, 41, 2).reshape(4, 5, order='F')) if partId == '1': J, grad = lrCostFunction(array([0.25, 0.5, -0.5]), X, y, 0.1) out = sprintf('%0.5f ', J) return out + sprintf('%0.5f ', grad) elif partId == '2': return sprintf('%0.5f ', oneVsAll(Xm, ym, 4, 0.1)) elif partId == '3': return sprintf('%0.5f ', predictOneVsAll(t1, Xm)) elif partId == '4': return sprintf('%0.5f ', predict(t1, t2, Xm))
def calcularThetas(): print("Calculando thetas para predecir nùmeros....") # 20x20 imagenes de Entrada input_layer_size = 400 # 10 labels num_labels = 10 # traigo las imagenes de MNIST almacenadas data = loadmat(os.path.join('Data', 'ex3data1.mat')) X, y = data['X'], data['y'].ravel() y[y == 10] = 0 m = y.size lambda_ = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_) pred = predictOneVsAll(all_theta, X) print('Training Set Accuracy: {:.2f}%'.format(np.mean(pred == y) * 100)) np.savetxt("thetas.txt", all_theta) indices = np.random.permutation(m) i, indices = indices[0], indices[1:] pred = predictOneVsAll(all_theta, X[i, :]) print('Prediccion aleatoria para control: {}'.format(*pred))
# ============ Part 2a: Vectorize Logistic Regression ============ # Test case for lrCostFunction print('\nTesting lrCostFunction() with regularization') theta_t = np.array([-2, -1, 1, 2]).reshape(4, 1) #print(theta_t) X_t = np.column_stack((np.ones( (5, 1)), (np.reshape(np.arange(1, 16), (5, 3), order="F") / 10))) #print(X_t) y_t = np.array([1, 0, 1, 0, 1]).reshape(5, 1) lambda_t = 3 J, grad = lrCostFunction(theta_t, X_t, y_t, lambda_t, return_grad=True) print('\nCost: \n', J) print('Expected cost: 2.534819\n') print('Gradients:\n') print(' \n', grad) print('Expected gradients:\n') print(' 0.146561\n -0.548558\n 0.724722\n 1.398003\n') # ============ Part 2b: One-vs-All Training ============ print('\nTraining One-vs-All Logistic Regression...\n') _lambda = 0.1 all_theta = oneVsAll(X, y, 10, _lambda) #print(all_theta.shape) # ================ Part 3: Predict for One-Vs-All ================ p = predictOneVsAll(all_theta, X) #print(p.shape) print("Train Accuracy: ", np.mean(p == y) * 100)
# After that, you will implement one-vs-all classification for the handwritten digit dataset. # Test case for lrCostFunction print('Testing lrCostFunction() with regularization') theta_t = np.array([-2, -1, 1, 2]) X_t = np.c_[(np.ones([5,1]),np.arange(1,16).reshape(3,5).T/10)] y_t = np.array([[1],[0],[1],[0],[1]]) >= 0.5 lambda_t = 3 [J,grad] = lrCostFunction.lrCostFunction(theta_t, X_t, y_t, lambda_t) #[J,grad] = lrCostFunction.lrCostFunction(initial_theta, X, y, xlambda) print('Cost: %.7f\nExpected cost: 2.534819\nGradients:'%J) for grad_i in grad: print(grad_i) print('Expected gradients:\n0.146561\n -0.548558\n 0.724722\n 1.398003') input('Program paused. Press enter to continue.') ## ============ Part 2b: One-vs-All Training ============ print('Training One-vs-All Logistic Regression...') xlambda = 0.1 all_theta = oneVsAll.oneVsAll(X, y, num_labels, xlambda) input('Program paused. Press enter to continue.') ## ================ Part 3: Predict for One-Vs-All ================ pred = predictOneVsAll.predictOneVsAll(all_theta, X) acc = np.mean(np.mean((pred == y))*100) print('Training Set Accuracy: %.4f'%acc)
% % Load Training Data """ print("visualizing training data"); data=scipy.io.loadmat('ex3data1.mat');# % training data stored in arrays X, y #n=np.shape(X) X=data['X'] y=data['y'] m=np.size(X,0) rand_indices=np.random.permutation(m) sel=X[rand_indices[0:100],:] print(np.size(sel)) lamda=0.1 all_theta=oneVsAll.oneVsAll(X,y,num_labels,lamda) import predictOneVsAll as pD pred = pD.predictOneVsAll(all_theta, X); accuracy=(np.mean(np.double(np.equal( y,pred).astype(int)))*100); print('training accuracy is %f\n' %accuracy)
dd.displayData(sel) raw_input('Program paused. Press enter to continue.\n') ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print('Training One-vs-All Logistic Regression...') lambda_reg = 0.1 all_theta = ova.oneVsAll(X, y, num_labels, lambda_reg) raw_input('Program paused. Press enter to continue.\n') ## ================ Part 3: Predict for One-Vs-All ================ # After ... pred = pova.predictOneVsAll(all_theta, X) print('Training Set Accuracy: {:f}'.format((np.mean(pred == y%10)*100))) print('Training Set Accuracy for 1: {:f}'.format(np.mean(pred[500:1000] == y.flatten()[500:1000]%10) * 100)) print('Training Set Accuracy for 2: {:f}'.format(np.mean(pred[1000:1500] == y.flatten()[1000:1500]%10) * 100)) print('Training Set Accuracy for 3: {:f}'.format(np.mean(pred[1500:2000] == y.flatten()[1500:2000]%10) * 100)) print('Training Set Accuracy for 4: {:f}'.format(np.mean(pred[2000:2500] == y.flatten()[2000:2500]%10) * 100)) print('Training Set Accuracy for 5: {:f}'.format(np.mean(pred[2500:3000] == y.flatten()[2500:3000]%10) * 100)) print('Training Set Accuracy for 6: {:f}'.format(np.mean(pred[3000:3500] == y.flatten()[3000:3500]%10) * 100)) print('Training Set Accuracy for 7: {:f}'.format(np.mean(pred[3500:4000] == y.flatten()[3500:4000]%10) * 100))
''' %% ============ Part 2: Vectorize Logistic Regression ============ % In this part of the exercise, you will reuse your logistic regression % code from the last exercise. You task here is to make sure that your % regularized logistic regression implementation is vectorized. After % that, you will implement one-vs-all classification for the handwritten % digit dataset. % ''' #fprintf('\nTraining One-vs-All Logistic Regression...\n') lambda_ = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_) #%% ================ Part 3: Predict for One-Vs-All ================ #% After ... pred = predictOneVsAll(all_theta, X) pred = pred.reshape(-1,1) print('Training Set Accuracy is : ', np.mean(pred == y) * 100.) # Randomly select 100 data points to display rand_indices = np.random.permutation(m); X_train = X[rand_indices[:4500], :] y_train = y[rand_indices[:4500], :] X_test = X[rand_indices[4501:], :]
import numpy as np import oneVsAll as ova import scipy.io as sio data = sio.loadmat(r'G:\2014斯坦福大学机器学习mkv视频\机器学习课程2014源代码\mlclass-ex3-jin\ex3data1.mat') X = data['X'] y = data['y'] y[np.where(y==10)]=0 all_theta=ova.oneVsAll(X,y,10,100) def sigma(z): return 1/(1+np.e**-z) def predictOneVsAll(all_theta,X): m=X.shape[0] X=np.c_[np.ones(m),X] return np.argmax(sigma(np.dot(all_theta,X.reshape(-1,1))),0),(sigma(np.dot(all_theta,X.reshape(-1,1))),0) predictOneVsAll(all_theta,X[[1],:])
J = lrCostFunction(theta_s, X_s, y_s, lambda_t) grad = gradFind(theta_s, X_s, y_s, lambda_t) print('\nCost:', J) print('Expected cost: 2.534819\n') print('Gradients:\n') print(grad) print('Expected gradients:') print(' 0.146561\n -0.548558\n 0.724722\n 1.398003\n') input('Program paused. Press enter to continue.\n') ## ============ Part 2b: One-vs-All Training ============ print('\nTraining One-vs-All Logistic Regression...\n') # Not WORKING from oneVsAll import oneVsAll lambda_n = 0.1 theta_opt = oneVsAll(X_t, y_t, 10, lambda_n) input('Program paused. Press enter to continue.\n') ## ================ Part 3: Predict for One-Vs-All ================ from predictOneVsAll import predictOneVsAll pred = predictOneVsAll(theta_opt, X) print('\nTraining Set Accuracy:\n', np.mean(pred == y) * 100)
# ============ Part 2a: Vectorize Logistic Regression ============ print('===========Testing CostFunctionReg with regularization===========') test_theta = np.array([-2, -1, 1, 2]) X_test = np.concatenate((np.ones([5, 1]), np.arange(1, 16, dtype=float).reshape(5, 3, order='F') / 10), axis=1) y_test = np.array([1, 0, 1, 0, 1]) reg_factor_test = 3 J, grad = costFunctionReg(test_theta, X_test, y_test, reg_factor_test, requires_grad=True) print('Cost: %f' % J) print('Expected cost: 2.534819\n') print('Gradients:\n', grad) print('Expected gradients:\n0.146561\n -0.548558\n 0.724722\n 1.398003\n') input("Program paused. Press enter to continue.\n") # ============ Part 2b: One-vs-All Training ============ print('===========Training One-vs-All Logistic Regression...===========\n') reg_factor = 0.1 num_labels = 10 all_theta = oneVsAll(X, y, num_labels, reg_factor) input("Program paused. Press enter to continue.\n") # ================ Part 3: Predict for One-Vs-All ================ h = sigmoid(X @ all_theta.T) pre = np.argmax(h, axis=1) accuracy = np.mean((pre == y).astype(np.float)) * 100 print('Training Set Accuracy: %f' % accuracy)
"""Handwriting recognition using one vs all logistic regression.""" __author__="Jesse Lord" __date__="January 9, 2015" from readData import readData from oneVsAll import oneVsAll from predictOneVsAll import predictOneVsAll import pickle firstrun = 0 if __name__=="__main__": (X,y) = readData() num_labels = 10 # number of labels for one vs all lam = 0.1 # regularization parameter if firstrun: all_thetas = oneVsAll(X,y,num_labels,lam) with open('thetas.pickle','w') as f: pickle.dump([all_thetas],f) else: with open('thetas.pickle') as f: [all_thetas] = pickle.load(f) prediction = predictOneVsAll(X,y,all_thetas,num_labels) print "One vs All determines the handwriting correctly on the training set "+str(100*prediction)+"% of the time."
# Нормализация свойств и добавление единичного признака m, n = X.shape X = np.concatenate((np.ones((m, 1)), X), axis = 1) # Задание общего числа классов (меток) num_labels = 10 # Задание начальных параметров модели initial_theta = np.zeros([n + 1, num_labels]) # Задание параметров градиентного спуска iterations = 1500 alpha = 1 # Визуализация процесса сходимости для i-го классифкатора flag = False # Выполнение процедуры обучения параметров модели all_theta = oneVsAll(X, y, num_labels, initial_theta, alpha, iterations, flag) input('Программа остановлена. Нажмите Enter для продолжения ... \n') # == Часть 3. Вычисление доли правильных ответов классификатора == print('Часть 3. Вычисление доли правильных ответов классификатора') # Вычисление доли правильных ответов классификатора p = predictOneVsAll(X, all_theta) acc = np.sum((p == y).astype('float64')) / len(y) * 100 print('Доля правильных ответов обученного классификатора = {:.4f}'.format(acc))
rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:99], :] #displayData(sel) raw_input("Program paused. Press Enter to continue...") ## ============ Part 2a: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # theta = oneVsAll(X, y, 10, 0.1) print theta ## ============ Part 2b: One-vs-All Training ============ print('\nTraining One-vs-All Logistic Regression...\n') lambda_reg = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_reg) raw_input("Program paused. Press Enter to continue...") ## ================ Part 3: Predict for One-Vs-All ================
dd.displayData(sel) input('Program paused. Press enter to continue.\n') ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print('Training One-vs-All Logistic Regression...') lambda_reg = 0.1 all_theta = ova.oneVsAll(X, y, num_labels, lambda_reg) input('Program paused. Press enter to continue.\n') ## ================ Part 3: Predict for One-Vs-All ================ # After ... pred = pova.predictOneVsAll(all_theta, X) print('Training Set Accuracy: {:f}'.format((np.mean(pred == y % 10) * 100))) print('Training Set Accuracy for 1: {:f}'.format( np.mean(pred[500:1000] == y.flatten()[500:1000] % 10) * 100)) print('Training Set Accuracy for 2: {:f}'.format( np.mean(pred[1000:1500] == y.flatten()[1000:1500] % 10) * 100)) print('Training Set Accuracy for 3: {:f}'.format( np.mean(pred[1500:2000] == y.flatten()[1500:2000] % 10) * 100)) print('Training Set Accuracy for 4: {:f}'.format(
"""Handwriting recognition using one vs all logistic regression.""" __author__ = "Jesse Lord" __date__ = "January 9, 2015" from readData import readData from oneVsAll import oneVsAll from predictOneVsAll import predictOneVsAll import pickle firstrun = 0 if __name__ == "__main__": (X, y) = readData() num_labels = 10 # number of labels for one vs all lam = 0.1 # regularization parameter if firstrun: all_thetas = oneVsAll(X, y, num_labels, lam) with open('thetas.pickle', 'w') as f: pickle.dump([all_thetas], f) else: with open('thetas.pickle') as f: [all_thetas] = pickle.load(f) prediction = predictOneVsAll(X, y, all_thetas, num_labels) print "One vs All determines the handwriting correctly on the training set " + str( 100 * prediction) + "% of the time."
# Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel) raw_input("Program paused. Press Enter to continue...") ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print "Training One-vs-All Logistic Regression..." Lambda = 0.1 all_theta = oneVsAll(X, y, num_labels, Lambda) raw_input("Program paused. Press Enter to continue...") ## ================ Part 3: Predict for One-Vs-All ================ # After ... pred = predictOneVsAll(all_theta, X) accuracy = np.mean(np.double(pred == np.squeeze(y))) * 100 print "\nTraining Set Accuracy: %f\n" % accuracy
displayData(sel, sel_Y) ''' %% ============ Part 2: Vectorize Logistic Regression ============ % In this part of the exercise, you will reuse your logistic regression % code from the last exercise. You task here is to make sure that your % regularized logistic regression implementation is vectorized. After % that, you will implement one-vs-all classification for the handwritten % digit dataset. % ''' #fprintf('\nTraining One-vs-All Logistic Regression...\n') lambda_ = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_) #%% ================ Part 3: Predict for One-Vs-All ================ #% After ... pred = predictOneVsAll(all_theta, X) pred = pred.reshape(-1, 1) print('Training Set Accuracy is : ', np.mean(pred == y) * 100.) # Randomly select 100 data points to display rand_indices = np.random.permutation(m) X_train = X[rand_indices[:4500], :] y_train = y[rand_indices[:4500], :] X_test = X[rand_indices[4501:], :] y_test = y[rand_indices[4501:], :]
y = data['y'] # Add constant for intercept X = np.c_[np.ones((data['X'].shape[0], 1)), data['X']] #print('X: {} (with intercept)'.format(X.shape)) #print('y: {}'.format(y.shape)) theta1, theta2 = weights['Theta1'], weights['Theta2'] #print('theta1: {}'.format(theta1.shape)) #print('theta2: {}'.format(theta2.shape)) sample = np.random.choice(X.shape[0], 20) plt.imshow(X[sample, 1:].reshape(-1, 20).T) plt.show() theta = oneVsAll(X, y, 10, 0.1) pred = predictOneVsAll(theta, X) print('Training set accuracy: {} %'.format(np.mean(pred == y.ravel()) * 100)) clf = LogisticRegression(C=10, penalty='l2', solver='liblinear') # Scikit-learn fits intercept automatically, so we exclude first column with 'ones' from X when fitting. clf.fit(X[:, 1:], y.ravel()) pred2 = clf.predict(X[:, 1:]) print('Training set accuracy: {} %'.format(np.mean(pred2 == y.ravel()) * 100)) pred = predict(theta1, theta2, X) print('Training set accuracy: {} %'.format(np.mean(pred == y.ravel()) * 100))
axis=1) y_t = np.array([1, 0, 1, 0, 1]) lambda_t = 3 [J, grad] = lrCostFunction(theta_t, X_t, y_t, lambda_t) print('\nCost: %f\n' % J) print('Expected cost: 2.534819\n') print('Gradients:\n') for i in range(np.size(grad)): print(" grad%i = %f" % (i, grad[i])) print('\nExpected gradients:\n') print(' 0.146561\n -0.548558\n 0.724722\n 1.398003\n') input('Program paused. Press enter to continue.\n') # ============ Part 2b: One-vs-All Training ============ print('\nTraining One-vs-All Logistic Regression...\n') lambda_par = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_par) print('Program paused. Press enter to continue.\n') # ================ Part 3: Predict for One-Vs-All ================ pred = predictOneVsAll(all_theta, X) print('Training Set Accuracy: %.1f' % (np.mean(pred == y) * 100))
# -*- coding: utf-8 -*- import oneVsAll as ova import scipy.io as scio import numpy as np data = scio.loadmat("ex3data1.mat") X = data['X'] * 255 y = data['y'] #print(X.shape) allt = ova.oneVsAll(X, y, 2, 0) count = 0 for i in range(5000): isone = np.dot(allt, np.array(np.append(1, X[i]))) if isone > 0: count += 1 print(count)
def ex3(): ## Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all # Instructions # ------------ # # This file contains code that helps you get started on the # linear exercise. You will need to complete the following functions # in this exericse: # # lrCostFunction.m (logistic regression cost function) # oneVsAll.m # predictOneVsAll.m # predict.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## Setup the parameters you will use for this part of the exercise input_layer_size = 400 # 20x20 Input Images of Digits num_labels = 10 # 10 labels, from 1 to 10 # (note that we have mapped "0" to label 10) ## =========== Part 1: Loading and Visualizing Data ============= # We start the exercise by first loading and visualizing the dataset. # You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') mat = scipy.io.loadmat( 'ex3data1.mat') # training data stored in arrays X, y X = mat['X'] y = mat['y'].ravel() % 10 m = y.size # Randomly select 100 data points to display rand_indices = np.random.choice(m, 100, replace=False) sel = X[rand_indices, :] displayData(sel) plt.savefig('figure1.png') print('Program paused. Press enter to continue.') #pause; ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print('\nTraining One-vs-All Logistic Regression...') lambda_value = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_value) print('Program paused. Press enter to continue.') #pause; ## ================ Part 3: Predict for One-Vs-All ================ # After ... pred = predictOneVsAll(all_theta, X) print('\nTraining Set Accuracy: %f' % (np.mean( (pred - 1 == y).astype(int)) * 100))
fig = figure() displayData(X[sel[:100], :]) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print '\nTraining One-vs-All Logistic Regression...' lambda_ = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_) print 'Program paused. Press enter to continue.' raw_input() ## ================ Part 3: Predict for One-Vs-All ================ # After ... pred = predictOneVsAll(all_theta, X) print 'Training Set Accuracy: %f' % (mean(pred == y) * 100)
def predictX(index): script_dir = os.path.dirname(__file__) rel_path = 'ex3data1.mat' abs_path = os.path.join(script_dir, rel_path) mat_contents = sio.loadmat(abs_path) X = mat_contents['X'] y = mat_contents['y'] X = abs(X) #arr = np.reshape(X[2], (20,20)) #plt.imshow(arr, cmap='gray', vmin=0, vmax=1) # plt.show() #theta_t = np.matrix('-2.;-1.;1.;2.') #X_t = np.ones((5,1)) #X_t_2 = [] #for i in range(1,16): # X_t_2.append(float(i)) #X_t_2 = np.arange(1, 16) #X_t_2 = np.reshape(X_t_2, (3, 5)) / 10 #X_t_2 = np.transpose(X_t_2) #X_t = np.concatenate((X_t, X_t_2), axis=1) #y_t = np.matrix('1.;0.;1.;0.;1.') #lambda_t = 3.; #J, grad = lrCostFunction(theta_t, X_t, y_t, lambda_t) #print(J) #print(grad) lambda_t = 0.1 num_labels = 10 all_theta = oneVsAll(X, y, num_labels, lambda_t) print(all_theta) p = predictOneVsAll(all_theta, X) p = np.reshape(p, (p.size, 1)) print(np.average(p == y) * 100) #path = 'D:\\dev\\machine-learning\\machine-learning-ex3\\ex3\\test.bmp' #oneimage = cv2.imread(path, cv2.IMREAD_GRAYSCALE) #oneimage = oneimage / 255 print(index) oneimage = X[index] oneimage = np.reshape(oneimage, (1, oneimage.size)) r = predictOneVsAll(all_theta, oneimage) return r
#1.1 data = loadmat('ex3data1.mat') X = data['X'] y = data['y'] #1.2 displayData(X) #1.4 m = len(y) ones = np.ones((m, 1)) X = np.hstack((ones, X)) (m, n) = X.shape theta_optimized = oneVsAll(X, y) #print(theta_optimized.shape) pred = np.argmax(X @ theta_optimized.T, axis=1) pred = [e if e else 10 for e in pred] acc = np.mean(pred == y.flatten()) * 100 print(acc, '%') #2.1 weights = loadmat('ex3weights.mat') Theta1 = weights['Theta1'] Theta2 = weights['Theta2'] Theta2 = np.roll(Theta2, 1, axis=0) input_layer_size = 400 hidden_layer_size = 25
data = scio.loadmat('ex3data1.mat') X = data['X'] y = data['y'] m = np.size(y) for i in range(m): y[i] -= 1 #Randomly select: 100 data points to display rand_indices = np.arange(m) np.random.shuffle(rand_indices) sel = X[rand_indices[0:100], :] displayData(sel) #============ Part 2a: Vectorize Logistic Regression ============ #Test case for lrCostFunction print('Testing lrCostFunction() with regularization') theta_t = np.array([[-2], [-1], [1], [2]]) X_t = np.arange(1, 16) / 10 X_t = X_t.reshape(3, 5) X_t = np.c_[np.ones([5, 1]), X_t.T] y_t = np.array([[1], [0], [1], [0], [1]]) lambda_t = 3 [J, grad] = lrCostFunction(theta_t, X_t, y_t, lambda_t) #============ Part 2b: One-vs-All Training ============ print('\nTraining One-vs-All Logistic Regression...') lamda = 0.1 all_theta = oneVsAll(X, y, num_labels, lamda) #================ Part 3: Predict for One-Vs-All ================ pred = predictOneVsAll(all_theta, X, y) print('Training Set Accuracy: ', pred)
try: assert( np.around( lrCostFunction(X, y, theta, l), decimals=4).tolist() == [7.6832] ) assert( np.around( lrGrad(X, y, theta, l), decimals =5).tolist() == [[0.31722], [-0.12768], [2.64812], [4.23787]] ) except: sys.exit("Unit test failed") ''' oneVsAll check ''' np.set_printoptions(suppress=True) np.set_printoptions(precision=6) X = np.r_[ magic3 , np.sin(range(1, 4))[np.newaxis], np.cos(range(1, 4))[np.newaxis]] y = np.array([[0, 1, 1, 0, 2]]).T num_labels = 3 l = 0.1 print oneVsAll(X, y, num_labels, l) print "\n" print oneVsAll_fmin_cg(X, y, num_labels, l) ''' nn Predict check ''' Theta1 = np.array([[0.00000, 0.90930, -0.75680], [0.47943, 0.59847, -0.97753], [0.84147, 0.14112, -0.95892], [0.99749, -0.35078, -0.70554] ]) Theta2 = np.array([[0.00000, 0.93204, 0.67546, -0.44252, -0.99616], [0.29552, 0.99749, 0.42738, -0.68777, -0.92581], [0.56464, 0.97385, 0.14112, -0.87158, -0.77276], [0.78333, 0.86321, -0.15775, -0.97753, -0.55069]])
displayData(sel) input("Program paused. Press Enter to continue...") ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print('Training One-vs-All Logistic Regression...') Lambda = 0. all_theta = oneVsAll(X, y, num_labels, Lambda) input("Program paused. Press Enter to continue...") ## ================ Part 3: Predict for One-Vs-All ================ # After ... pred = predictOneVsAll(all_theta, X) print('pred.shape', pred.shape) #print('pred', pred) n_total, n_correct = 0., 0. for irow in range(X.shape[0]): n_total += 1 if pred[irow] == y[irow]:
import readData from oneVsAll import oneVsAll import pickle from predictOneVsAll import predictOneVsAll firstRun = 0 if __name__ == "__main__": (X, y) = readData.readData() numOfLabels = 10 lam = 0.1 if firstRun: all_theta = oneVsAll(X, y, numOfLabels, lam) with open('thetas.pickle', 'w') as f: pickle.dump([all_theta], f) else: with open('thetas.pickle') as f: [all_theta] = pickle.load(f) prediction = predictOneVsAll(X, y, all_theta, numOfLabels) print "One vs All determines the handwriting correctly on the training set " + str( 100 * prediction) + "% of the time."