def display_data(self):
        # Randomly select 100 data points to display
        m = self.y.shape[0]
        rand_indices = np.random.choice(m, 100, replace=False)
        sel = self.X[rand_indices, :]

        utils.displayData(sel)
# MATLAB where there is no index 0
y[y == 10] = 0

m = y.size
# -

# ### 1.2 Visualizing the data
#
# You will begin by visualizing a subset of the training set. In the following cell, the code randomly selects selects 100 rows from `X` and passes those rows to the `displayData` function. This function maps each row to a 20 pixel by 20 pixel grayscale image and displays the images together. We have provided the `displayData` function in the file `utils.py`. You are encouraged to examine the code to see how it works. Run the following cell to visualize the data.

# +
# Randomly select 100 data points to display
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]

utils.displayData(sel)
# -

# ### 1.3 Vectorizing Logistic Regression
#
# You will be using multiple one-vs-all logistic regression models to build a multi-class classifier. Since there are 10 classes, you will need to train 10 separate logistic regression classifiers. To make this training efficient, it is important to ensure that your code is well vectorized. In this section, you will implement a vectorized version of logistic regression that does not employ any `for` loops. You can use your code in the previous exercise as a starting point for this exercise.
#
# To test your vectorized logistic regression, we will use custom data as defined in the following cell.

# +
# test values for the parameters theta
theta_t = np.array([-2, -1, 1, 2], dtype=float)

# test values for the inputs
X_t = np.concatenate(
    [np.ones((5, 1)),
Пример #3
0
# - \left(x^{(2)} \right)^T - \\
# \vdots \\
# - \left(x^{(m)} \right)^T - \\
# \end{bmatrix}
# $$
#
# The second part of the training set is a 5000-dimensional vector `y` that contains labels for the training set.
# The following cell randomly selects 100 images from the dataset and plots them.

# In[4]:

# Randomly select 100 data points to display
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]

utils.displayData(sel)

# ### 1.2 Model representation
#
# Our neural network is shown in the following figure.
#
# ![](Figures/neural_network.png)
#
# It has 3 layers - an input layer, a hidden layer and an output layer. Recall that our inputs are pixel values
# of digit images. Since the images are of size $20 \times 20$, this gives us 400 input layer units (not counting the extra bias unit which always outputs +1). The training data was loaded into the variables `X` and `y` above.
#
# You have been provided with a set of network parameters ($\Theta^{(1)}, \Theta^{(2)}$) already trained by us. These are stored in `ex4weights.mat` and will be loaded in the next cell of this notebook into `Theta1` and `Theta2`. The parameters have dimensions that are sized for a neural network with 25 units in the second layer and 10 output units (corresponding to the 10 digit classes).

# In[5]:

# Setup the parameters you will use for this exercise
Пример #4
0
from scipy import optimize
from scipy.io import loadmat
import utils

#======================================= Loading Data ================================
input_layer_size  = 400
num_labels = 10
data = loadmat(os.path.join('Data', 'ex3data1.mat'))
X, y = data['X'], data['y'].ravel()
y[y == 10] = 0
y=np.reshape(y,(5000,1))
m = y.size
#====================================== Visualising Data =============================
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]
utils.displayData(sel)
#=================================== Testing Cost Function ===========================
theta_t = np.array([-2, -1, 1, 2], dtype=float)
X_t = np.concatenate([np.ones((5, 1)), np.arange(1, 16).reshape(5, 3, order='F')/10.0], axis=1)
y_t = np.array([1, 0, 1, 0, 1])
lambda_t = 3
#================================== Cost Function ====================================
def lrCostFunction(theta, X, y, lambda_):
    m = y.size
    if y.dtype == bool:
        y = y.astype(int)
    J = 0

    n=np.shape(theta)[0]
    theta=np.reshape(theta,(n,1))
    grad = np.zeros(theta.shape)
Пример #5
0
for xnorm, xrec in zip(X_norm, X_rec):
    ax.plot([xnorm[0], xrec[0]], [xnorm[1], xrec[1]], '--k', lw=1)
pyplot.show()

## =============== Part 4: Loading and Visualizing Face Data =============
#  We start the exercise by first loading and visualizing the dataset.
#  The following code will load the dataset into your environment
#
print('\nLoading face dataset.\n\n')

#  Load Face dataset
data = loadmat('ex7faces.mat')
X = data['X']

#  Display the first 100 faces in the dataset
utils.displayData(X[:100, :], figsize=(8, 8))
pyplot.show()

## =========== Part 5: PCA on Face Data: Eigenfaces  ===================
#  Run PCA and visualize the eigenvectors which are in this case eigenfaces
#  We display the first 36 eigenfaces.
#
print(
    ['\nRunning PCA on face dataset. This mght take a minute or two ...\n\n'])

#  Before running PCA, it is important to first normalize X by subtracting
#  the mean value from each feature
X_norm, mu, sigma = utils.featureNormalize(X)

#  Run PCA
U, S = pca.pca(X_norm)
Пример #6
0
                                        hidden_layer_size,
                                        output_layer_size,
                                        X,
                                        y,
                                        lambda_=lamda_)

res = optimize.minimize(costFunction,
                        initial_nn_params,
                        jac=True,
                        method="TNC",
                        options=options)
optimized_nn_params = res.x
optimized_Theta1 = optimized_nn_params[:(input_layer_size + 1) *
                                       hidden_layer_size].reshape(
                                           hidden_layer_size,
                                           input_layer_size + 1)
optimized_Theta2 = optimized_nn_params[(input_layer_size + 1) *
                                       hidden_layer_size:].reshape(
                                           output_layer_size,
                                           hidden_layer_size + 1)

pred = utils.predict(optimized_Theta1, optimized_Theta2, X)
print('Training Set Accuracy: %f' %
      (np.mean(pred == y) *
       100))  #####Training Set Accuracy: 96.420000 passed!!!

utils.displayData(optimized_Theta1[:, 1:])
plt.show()

# nnCostFunction(initial_nn_params,input_layer_size,hidden_layer_size,output_layer_size,X,y)
Пример #7
0
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy.io import loadmat
import utils
from scipy import optimize

###===================Part 1: Load and  Visualizing Data ============================
data = loadmat(
    "D:/TJH/ML03/machine-learning-ex3/machine-learning-ex3/ex3/ex3data1.mat")
X = data["X"]
y = data["y"].ravel()
y[y == 10] = 0
m = len(y)
sel = X[np.random.choice(m, 100)]
utils.displayData(sel)
plt.show()


##====================Part 2a: Vectorize Logistic Regression ========================
def h(theta, x):
    z = np.matmul(x, theta)
    y = 1. / (1. + np.exp(-z))
    return y


def costFunction(theta, X, y, lamda):
    H = h(theta, X).T
    theta1 = theta.copy()
    theta1[0] = 0