コード例 #1
0
num_labels = 10          # 10 labels, from 1 to 10 (note that we have mapped "0" to label 10)


# =========== Part 1: Loading and Visualizing Data =============
print 'Loading and Visualizing Data...'

# Load Training Data
mat_data = sio.loadmat('ex4data1.mat')
X = mat_data['X']
y = mat_data['y'].ravel()
m, n = X.shape

# Randomly select 100 data points to display
rand_indices = np.random.permutation(m)
plt.figure()
display_data(X[rand_indices[0:100], :], padding=1)
plt.show()


# ================ Part 2: Loading Parameters ================
# In this part of the exercise, we load some pre-initialized neural network parameters.
print 'Loading Saved Neural Network Parameters...'

# Load the weights into variables theta_1 and theta_2
mat_param = sio.loadmat('ex4weights.mat')
theta_1 = mat_param['Theta1']
theta_2 = mat_param['Theta2']

# Unroll parameters
params_trained = np.hstack((theta_1.flatten(), theta_2.flatten()))
コード例 #2
0
axes = plt.gca()
axes.set_xlim([-4, 3])
axes.set_ylim([-4, 3])
axes.set_aspect('equal', adjustable='box')
plt.show()


# =============== Part 4: Loading and Visualizing Face Data =============
print 'Loading face dataset.'

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

plt.figure()
display_data(X[0:100, :])
plt.show()


# =========== Part 5: PCA on Face Data: Eigenfaces  ===================
print 'Running PCA on face dataset.'

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

# Run PCA
U, S, V = pca(X_norm)

# Visualize the top 36 eigenvectors found
plt.figure()
display_data(U[:, 0:36].T)
コード例 #3
0
ファイル: simple_NN.py プロジェクト: andrex-naranjas/test
import matplotlib.pyplot as plt
from display_data import display_data

input_layer_size = 400  # 20x20 Imagenes de entrada
hidden_layer_size = 25  # 25 unidades escondidas
num_labels = 10  # 10 etiquetas del 1 al 10

mat_data = sio.loadmat('data/numeros_escritos.mat')
X = mat_data['X']
y = mat_data['y'].ravel()
m, n = X.shape

rand_indices = np.random.permutation(m)
sel = X[rand_indices[0:100], :]
plt.figure()
display_data(sel, padding=1)
plt.savefig('numeros_random.png')

#Cargar los parametros entrenados de neural network
mat_param = sio.loadmat('data/trained_NN.mat')
Theta_1 = mat_param['Theta1']
Theta_2 = mat_param['Theta2']


#La funcion predict() predice la etiqueta de un unput dada una neural network entrenada
def predict(Theta_1, Theta_2, X):
    """
    Predict the label of an input given a trained neural network.

    Parameters
    ----------
コード例 #4
0
ファイル: ex4.py プロジェクト: indraastra/ml-sandbox
# Load Training Data
print('> Loading and Visualizing Data ...\n')

data = sio.loadmat('../data/fontdata_small.mat');
X = data['X']
# This correction is for going from labels in Octave to zero-indexed labels
# in python.
y = data['y'].flatten()
y[y == 10] = 0

m = X.shape[0]

# Randomly select 100 data points to display
sel = random.sample(range(m), 100)

display_data(X[sel, :]);

input('\nProgram paused. Press enter to continue.\n');

## ================ Part 2: Loading Parameters ================
# In this part of the exercise, we load some pre-initialized 
# neural network parameters.

print('> Loading Saved Neural Network Parameters ...')

# Load the weights into variables Theta1 and Theta2
weights = sio.loadmat('../data/ex4weights.mat');
Theta1 = weights['Theta1']
Theta2 = weights['Theta2']

# Unroll parameters 
コード例 #5
0
num_labels = 10  # 10 labels, from 1 to 10

# =========== Part 1: Loading and Visualizing Data =============
print('Loading and Visualizing Data...')

# Load Training Data
mat_data = sio.loadmat('ex3data1.mat')
X = mat_data['X']
y = mat_data['y'].ravel()
m, n = X.shape

# Randomly select 100 data points to display
rand_indices = np.random.permutation(m)
sel = X[rand_indices[0:100], :]
plt.figure()
display_data(sel, padding=1)
# plt.show()

# ============ Part 2a: Vectorize Logistic Regression ============
# Test case for lrCostFunction
theta_t = np.array([-2, -1, 1, 2])
X_t = np.hstack((np.ones(
    (5, 1)), np.arange(1, 16).reshape(5, 3, order='F') / 10.0))
y_t = np.array([1, 0, 1, 0, 1])
lambda_t = 3

cost, grad = lr_cost_function(theta_t, X_t, y_t, lambda_t)

print('Cost: ', cost)
print('Expected cost: 2.534819')
print('Gradients: \n', grad)
コード例 #6
0
# Load Training Data
print('> Loading and Visualizing Data ...\n')

data = sio.loadmat('../data/fontdata_small.mat')
X = data['X']
# This correction is for going from labels in Octave to zero-indexed labels
# in python.
y = data['y'].flatten()
y[y == 10] = 0

m = X.shape[0]

# Randomly select 100 data points to display
sel = random.sample(range(m), 100)

display_data(X[sel, :])

input('\nProgram paused. Press enter to continue.\n')

## ================ Part 2: Loading Parameters ================
# In this part of the exercise, we load some pre-initialized
# neural network parameters.

print('> Loading Saved Neural Network Parameters ...')

# Load the weights into variables Theta1 and Theta2
weights = sio.loadmat('../data/ex4weights.mat')
Theta1 = weights['Theta1']
Theta2 = weights['Theta2']

# Unroll parameters
コード例 #7
0
def main():
    global X, y

    try:
        df = pd.read_csv("./datasets/digits.csv", header=None)
        X = df.values
        df = pd.read_csv("./datasets/labels.csv", header=None)
        y = df.values
    except:
        X = np.array([[]])
        y = np.array([[]])

    sel = np.random.permutation(X)
    sel = sel[0:100]

    if len(X[0]) != 0:
        display_data(sel)

    input_layer_size = 400
    hidden_layer_size = 25
    num_labels = 10

    shape = (input_layer_size, hidden_layer_size, num_labels)

    for i in range(1, 11):
        prevLen = len(y)

        print(f'Write example for {i if i < 10 else 0}')
        f = lambda x: addTrainingExample(x, [[i]])
        Paint(func=f)

        if len(y) == prevLen:
            break

    if len(X[0]) == 0:
        print("Not enough data... Exiting")
        return

    nn = NeuralNetwork(shape)
    nn.Lambda = 1

    initial_Theta1 = nn.rand_initialize_weights(input_layer_size,
                                                hidden_layer_size)
    initial_Theta2 = nn.rand_initialize_weights(hidden_layer_size, num_labels)
    initial_nn_params = np.array(initial_Theta1.flatten().tolist() +
                                 initial_Theta2.flatten().tolist())

    f = lambda p: nn.cost_function(p, X, y)[0]
    gradf = lambda p: nn.cost_function(p, X, y)[1]
    print('Training Neural Network... (This can take a while)')
    nn_params = optimize.fmin_cg(f,
                                 initial_nn_params,
                                 fprime=gradf,
                                 maxiter=250)

    shape = hidden_layer_size, (input_layer_size + 1)
    idx = hidden_layer_size * (input_layer_size + 1)
    Theta1 = np.reshape(nn_params[0:idx], shape)

    display_data(Theta1[:, 1:])

    pred = nn.predict(nn_params, X)
    print(f'Training Set Accuracy: {np.mean(pred == y.T) * 100}%')

    p = lambda x: nn.predict(nn_params, x)
    Paint(predict=p)

    file = open("./datasets/digits.csv", 'w+')
    for i in range(len(X)):
        file.write(f'{", ".join(str(x) for x in X[i])}\n')
    file.close()

    file = open("./datasets/labels.csv", 'w+')
    for l in y:
        file.write(f'{l[0]}\n')
    file.close()
コード例 #8
0
file_path_X = 'C:/Users/uran-desktop/Documents/Octave/mlclass-ex3/mlclass-ex3/ex3data1X.npy'
file_path_Y = 'C:/Users/uran-desktop/Documents/Octave/mlclass-ex3/mlclass-ex3/ex3data1Y.npy'

# ----------------- load input data  -----------
X = np.load(file_path_X)
Y = np.load(file_path_Y)

numExamples = X.shape[0]  # 5000 examples
numFeatures = X.shape[1]  # 400 features
numLabels = 10            # digits from 0 to 9
X1 = np.append(np.ones((numExamples, 1)), X, axis=1)

# ------------- select random 100 rows from original data and display it -----------
select = X[np.random.permutation(numExamples)[0:100], :]
display_data(select)

# ----------------------------------- train model ----------------------------------
_lambda = 0.1
success, all_theta = ova(X1, Y, numLabels, _lambda)
if success:
    print("Result of optimisation successful")

# ---------------------------- compute training accuracy ---------------------------
predictions = h(X1 @ all_theta.T).argmax(axis=1).reshape((numExamples, 1))
# argmax() returns the indices of the maximum values along an axis.
accuracy = float(np.mean(predictions == Y)*100)
print("Train Accuracy: %5.2f" % accuracy)

# ------------------------ test arbitrary training set -----------------------------
if h(X1[519, :] @ all_theta[1, :].T) >= 0.5: