问:
Deep Neural Network dimension issue
I'm facing a problem with the dimensions of my deep neural network, which throws the error:

```<code>line 104, in linear_backward
dW = (dz @ a.T) * W * lambd / m
ValueError: operands could not be broadcast together with shapes (8,8) (8,32)
</code>```

It seems clear to me that the problem lies in the back-propagation, but I can't find my mistake.
I apologise for the messy code in advance and would be really grateful for any assistance since I am a high-schooler with no help around me.

The program is as follows:

```<code>import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Neural Network Build

def sigmoid(Z):
    # activation function used to classify the output
    # range of the function is between 0 and 1.
    a = (1 + np.exp(-Z)) ** -1
    return a

def relu(Z):
    # activation function used in hidden layers

    A = np.maximum(0, Z)  # outputs Z if Z is positive, otherwise returns 0

    assert (A.shape == Z.shape) #stops program if this isn't true

    return A

def relu_back(dA, Z):
    # function finding the derivative of the relu function
    dZ = np.array(dA, copy=True)

    dZ[Z <= 0] = 0

    return dZ

def sigmoid_back(dA, Z):
    # function finding the derivative of the sigmoid function
    s = sigmoid(Z)

    dZ = dA * s * (1 - s)

    return dZ

def initialize_parameters(layer_dims):
    parameters = {}
    L = len(layer_dims)
    for layer in range(1, L):
        parameters["W" + str(layer)] = np.random.randn(layer_dims[layer], layer_dims[layer - 1]) * np.sqrt(
            2 / layer_dims[layer - 1])
        parameters["b" + str(layer)] = np.zeros((layer_dims[layer], 1))

        print(parameters["W" + str(layer)].shape)

        assert (parameters['W' + str(layer)].shape == (layer_dims[layer], layer_dims[layer - 1]))
        assert (parameters['b' + str(layer)].shape == (layer_dims[layer], 1))

    return parameters

def forward_prop(X, parameters):
    L = len(parameters) // 2

    forward_cache = {}

    forward_cache["z1"] = parameters["W1"] @ X + parameters["b1"]
    forward_cache["a1"] = relu(forward_cache["z1"])

    for layer in range(2, L):
        forward_cache["z" + str(layer)] = parameters["W" + str(layer)] @ forward_cache["a" + str(layer - 1)] + \
                                          parameters["b" + str(layer)]
        forward_cache["a" + str(layer)] = relu(forward_cache["z" + str(layer)])

    # Output neuron will have sigmoid activation function applied to classify the output according to a probability.
    # This needs to be handled separately.
    forward_cache["z" + str(L)] = parameters["W" + str(L)] @ forward_cache["a" + str(L - 1)] + parameters["b" + str(L)]
    forward_cache["a" + str(L)] = sigmoid(forward_cache["z" + str(L)])

    AL = forward_cache["a" + str(L)]

    assert (AL.shape == (1, X.shape[1]))

    return AL, forward_cache # Return y_hat (the model's prediction) and forward_cache (used in back-prop)

def compute_loss(A, Y, parameters, lambd):
    L = len(parameters) // 2
    m = Y.shape[1]
    log_function = -(np.multiply(Y, np.log(A)) + np.multiply((1 - Y), np.log(1 - A)))
    L2_regularisation_cost = 0
    for weight in range(1, L):
        L2_regularisation_cost += np.sum(np.square(parameters["W" + str(weight)]))

    loss = 1. / m * (np.nansum(log_function) + (L2_regularisation_cost * lambd / 2))

    loss = np.squeeze(loss)

    assert (loss.shape == ())

    return loss

def linear_backward(dz, W, b, a, lambd):
    m = a.shape[1]
    dW = (dz @ a.T) * W * lambd / m
    db = np.sum(dz, axis=1, keepdims=True)
    dA_prev = W.T @ dz

    print("W:", W.shape,"dZ:", dz.shape)
    print(dA_prev.shape, a.shape)

#    assert (dA_prev.shape == a.shape)
    assert (dW.shape == W.shape)
    assert (db.shape == b.shape)

    return dW, db, dA_prev

def activation_backward(dA, a, z, W, b, activation_function, lambd):
    if activation_function == "sigmoid":
        dz = sigmoid_back(dA, z)
    elif activation_function == "relu":
        dz = relu_back(dA, z)

    dW, db, dA_prev = linear_backward(dz, W, b, a, lambd)

    return dW, db, dA_prev

def backward_prop(AL, Y, cache, parameters, lambd):
    L = len(parameters) // 2
    print(AL)
    gradients = {}
    dAL = -(np.divide(Y, AL)) + np.divide(1 - Y, 1 - AL)
    dz = sigmoid_back(dAL, cache["z" + str(L)])
    gradients["dW" + str(L)], gradients["db" + str(L)], dA_prev = \
        linear_backward(dz, parameters["W" + str(L)], parameters["b" + str(L)], cache["a" + str(L)], lambd)

    for l in reversed(range(1, L)):
        gradients["dW" + str(l)], gradients["db" + str(l)], dA_prev = \
            activation_backward(dA_prev, cache["a" + str(l)], cache["z" + str(l)],
                                parameters["W" + str(l)], parameters["b" + str(l)],
                                "relu", lambd)

    return gradients

def update_parameters(parameters, gradients, learning_rate):
    L = len(parameters) // 2

    for l in range(1, L + 1):
        parameters["W" + str(l)] = parameters["W" + str(l)] - (gradients["dW" + str(l)] * learning_rate)
        parameters["b" + str(l)] = parameters["b" + str(l)] - (gradients["db" + str(l)] * learning_rate)

    return parameters

def predict(X, Y, parameters):
    """
        This function is used to predict the results of a  L-layer neural network.

        Arguments:
        X -- data set of examples you would like to label
        parameters -- parameters of the trained model

        Returns:
        p -- predictions for the given dataset X
        """

    m = X.shape[1]
    # n = len(parameters) // 2 number of layers in the neural network
    binary_outcome = np.zeros((1, m))

    # Forward propagation
    probabilities = deep_neural_network(X, Y, parameters, )

    # convert probabilities to 0/1 predictions
    for i in range(0, probabilities.shape[1]):
        if probabilities[0, i] > 0.5:
            binary_outcome[0, i] = 1
        else:
            binary_outcome[0, i] = 0

    # print results
    # print ("predictions: " + str(p))
    # print ("true labels: " + str(y))
    print("Accuracy: " + str(np.sum((binary_outcome == Y) / m)))

    return binary_outcome

def deep_neural_network(X, Y, layer_dims, number_of_iterations, learning_rate, print_cost, lambd):
    losses = []

    for epoch in range(number_of_iterations):

        parameters = initialize_parameters(layer_dims)

        AL, forward_cache = forward_prop(X, parameters)

        loss = compute_loss(AL, Y, parameters, lambd)

        losses.append(loss)

        gradients = backward_prop(AL, Y, forward_cache, parameters, lambd)

        parameters = update_parameters(parameters, gradients, learning_rate)

        if (epoch % 100 == 0) and print_cost:
            print(loss)

    return parameters

    return

cancer = load_breast_cancer()
data = cancer.data
labels = cancer.target

xtrain, xtest, ytrain, ytest = train_test_split(data, labels)

xtrain = xtrain.T
ytrain = ytrain.reshape((1, 426))

parameters, costs = deep_neural_network(xtrain, ytrain, [30, 64, 32, 8, 1], learning_rate=0.045,
                                        number_of_iterations=2000, print_cost=True, lambd=0.01)
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(0.075))
plt.show()
predictions = predict(X=xtrain, parameters=parameters)
</code>```

答:
SOLVED:

The following code facilitates a customizable Deep Neural Network. My DNN is trained on the Extended MNIST.

```<code>import numpy as np
import matplotlib.pyplot as plt
from emnist import extract_training_samples
from emnist import extract_test_samples
import keras
import time
import os

# Converts data from the emnist database into the required format
# for the neural network and outputs the X and Y datasets
def getData(size, images, labels):
    training_samples = images.shape[0]

    # Converts each matrix of pixels (28x28) into feature vectors
    xtrain = images.reshape(training_samples, 784)

    # Arranges each training example into a column
    xtrain = xtrain.T

    #normalises the database to prevent the exploding gradients problem
    xtrain = xtrain / 255

    ytrain = labels.reshape(1, training_samples)

    # Converts the numerical labels into one-hot arrays with 47 elements.
    # For example a label of 10 will be converted into an array of 0s with
    # a 1 in the 10th position. This can be abstracted as the 10th neuron
    # firing in the neural network.
    y_train = keras.utils.to_categorical(ytrain, 47)

    y_train = y_train[0].T

    # Replaces 1s and 0s with approximate probabilities to prevent NaN errors in
    # the cost entropy function
    y_train[y_train == 0] = 0.01
    y_train[y_train == 1] = 0.99

    # Allows choice of the number of training examples input into the neural network.
    xsmall = np.hsplit(xtrain, [size, (training_samples - size)])[0]
    ysmall = np.hsplit(y_train, [size, (training_samples - size)])[0]

    # sanity check
    print(xsmall.shape, ysmall.shape)

    return xsmall, ysmall

# Activation function that replaces all negative values with 0
def relu(Z):
    A = np.maximum(0, Z)

    assert (A.shape == Z.shape)

    return A

# Derivative of the relu function: 0 if Z is negative and Z if Z is positive
def relu_backward(dA, cache, layer):

    # Accesses Z from the cache set up in the feed_forward function
    Z = cache["Z" + str(layer)]

    dZ = np.array(dA, copy=True)

    # At 0, I've set the derivative to be 0 as well, although it isn't differentiable at that point
    dZ[Z <= 0] = 0

    assert (dZ.shape == Z.shape)

    return dZ

# Activation function on the final layer that converts the linear transformation
# from the layer into probabilities that sum to 1. This is the neural net's estimate of how
# likely it is that the input example is each label in the dataset: the highest probability is
# what we choose as the neural net's output
def softmax(Z):

    exp_Z = np.exp(Z - np.max(Z))

    A = exp_Z / np.sum(exp_Z, axis=0)

    assert (A.shape == Z.shape)

    return A

# Derivative of the softmax function works very well with the cross-entropy cost function,
# which simplifies to the difference between the neural net's guess and the actual label.
def softmax_backward(cache, layer):

    # fetching values from the cache
    Z = cache["Z" + str(layer)]
    AL = cache["A" + str(layer)]
    Y = cache["Y"]

    dZ = AL - Y

    assert (dZ.shape == Z.shape)

    return dZ

# This sets up our parameters depending on the required dimensions of the neural network.
def initialise_parameters(layer_dims):
    parameters = {}

    np.random.seed(2)
            # He initialisation allows for symmetry breaking and efficient gradient descent
            parameters[W_layer] = np.random.randn(layer_dims[l], layer_dims[l - 1]) * np.sqrt(2 / layer_dims[l - 1])
            parameters[b_layer] = np.zeros((layer_dims[l], 1))

            assert (parameters[W_layer].shape == (layer_dims[l], layer_dims[l - 1]))
            assert (parameters[b_layer].shape == (layer_dims[l], 1))

    return parameters

# Outputs Z as the linear transformation of the previous layer's output using the parameters: Weight and bias.
def linear_forward(W, A_previous, b):
    Z = W @ A_previous + b

    assert (Z.shape == (W.shape[0], A_previous.shape[1]))

    return Z

# chooses the activation function that Z passes through depending on the activation_type requested.
def activation_forward(Z, activation_type):
    if activation_type == "relu":
        A = relu(Z)

    elif activation_type == "softmax":
        A = softmax(Z)

    assert (A.shape == Z.shape)

    return A

def feed_forward(X, parameters, Y):
    cache = {}
    A = X

    # Fetches number of layers in the neural network.
    L = len(parameters) // 2

    # considers the feature vector input as the 0th layer, initialising the cache
    cache["A0"] = X
    cache["Y"] = Y

    # Uses the relu for all the hidden layers
    for l in range(1, L + 1):

        # Uses softmax for the output layer
        activation_type = "relu"
        if l == L:
            activation_type = "softmax"

        # Combines all the helper functions above to propagate through the neural network
        A_previous = A
        W = parameters["W" + str(l)]
        b = parameters["b" + str(l)]
        Z = linear_forward(W, A_previous, b)
        A = activation_forward(Z, activation_type)

        # We store these parameters for use in the back propagation, since we use the element inside
        # when differentiating according to the chain rule. Saving these here improves efficiency and speed.
        cache["W" + str(l)] = W
        cache["b" + str(l)] = b
        cache["Z" + str(l)] = Z
        cache["A" + str(l)] = A

    # Returns the final output layer and the cache
    return cache["A" + str(L)], cache

def compute_cost(AL, Y):

    training_samples = Y.shape[1]

    # Zeroes can introduce NaN errors in the cost function, so we approximate them closely
    # to prevent log(0) from occurring and messing up our cost.
    AL_nonzero = np.maximum(AL, 1.0e-15)
    cost = (-1. / training_samples) * (np.sum(np.multiply(Y, np.log(AL_nonzero))) + np.sum(np.multiply(1 - Y, np.log(1 - AL_nonzero))))

    # Removes unnecessary dimensions from the cost value, so [[0.15]] is converted into 0.15
    cost = np.squeeze(cost)
    assert (cost.shape == ())

    return cost

# Performs backward propagation from one layer to the previous depending on
# the activation function specified.
def activation_backward(dAL, cache, layer, activation_type):
    if activation_type == "relu":
        dZ = relu_backward(dAL, cache, layer)

    elif activation_type == "softmax":
        dZ = softmax_backward(cache, layer)

    # Accesses values from cache
    A_prev = cache["A" + str(layer - 1)]
    W = cache["W" + str(layer)]
    b = cache["b" + str(layer)]
    training_samples = A_prev.shape[1]

    dW = (1. / training_samples) * np.dot(dZ, A_prev.T)
    db = (1. / training_samples) * np.sum(dZ, axis=1, keepdims=True)
    dA_prev = np.dot(W.T, dZ)

    assert (dA_prev.shape == A_prev.shape)
    assert (dW.shape == W.shape)
    assert (db.shape == b.shape)

    return dA_prev, dW, db

# Performs backward propagation on the whole neural network to find the derivatives of the
# cost function with respect to the parameters Weight and bias of each layer. These derivatives will
# allow us to perform gradient descent on the the cost function, tuning the parameters to improve accuracy.
def back_propagation(cache, Y, L):
    gradients = {}

    # Accesses output layer matrix from cache
    AL = cache["A" + str(L)]

    # Derivative of the cross entropy cost function
    gradients["dA" + str(L)] = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))

    # Uses relu_backward for all the hidden layers
    for l in reversed(range(1, L + 1)):
        activation_type = "relu"

        # Uses softmax_backward for the output layer
        if l == L:
            activation_type = "softmax"

        # finds gradients of each layer
        dA_previous, dW_current, db_current = activation_backward(gradients["dA" + str(l)], cache, l, activation_type)

        # Stores these gradients in the gradients dictionary
        gradients["dA" + str(l - 1)] = dA_previous
        gradients["dW" + str(l)] = dW_current
        gradients["db" + str(l)] = db_current

    return gradients

# Updates all of the parameters by subtracting their gradients from them
def update_parameters(gradients, parameters, learning_rate):
    L = len(parameters) // 2

    for l in range(1, L):
        W_layer = "W" + str(l)
        b_layer = "b" + str(l)
        dW_layer = "dW" + str(l)
        db_layer = "db" + str(l)

        # The learning rate needs to be tuned to approach the minimum of the cost accurately and reliably
        # while optimising time taken to train the neural net
        parameters[W_layer] = parameters[W_layer] - (gradients[dW_layer] * learning_rate)
        parameters[b_layer] = parameters[b_layer] - (gradients[db_layer] * learning_rate)

    return parameters

# Calculates accuracy of the neural net's prediction against the true labels
def calculate_accuracy(parameters, x, y_onehot, size):

    # Uses parameters to get the neural net's guess
    AL, cache = feed_forward(x, parameters, y_onehot)

    # Finds the highest probability character for each training example
    AL_max = np.argmax(AL, axis=0)

    AL_max = AL_max.reshape(1, AL_max.shape[0])

    # Converts the labels from one-hot matrices back into a numerical vector
    y_int = np.argmax(y_onehot, axis=0)
    y_int = y_int.reshape(1, y_int.shape[0])

    # Compares the two
    accuracy = np.mean(AL_max == y_int)

    return accuracy

# Brings everything together to train the model
def NN_model(X, Y, layer_dims, learning_rate, num_iterations, size, print_values):

    # Accesses initial parameters
    parameters = initialise_parameters(layer_dims)

    # Used to calculate the training time over the iteration
    times = []

    # Used to plot the accuracies at the end
    accuracies = []

    times.append(time.time())

    for i in range(num_iterations):

        # Accesses the output and cache using forward propagation through the parameters
        AL, cache = feed_forward(X, parameters, Y)

        # Calculates the gradients using the previouslt generated cache
        gradients = back_propagation(cache, Y, len(layer_dims) - 1)

        # Tunes the parameters to achieve greater accuracy
        parameters = update_parameters(gradients, parameters, learning_rate)

        # Used to track progress during training
        if i % 10 == 0 and print_values:
            time_difference = time.time() - times[-1]

            times.append(time.time())

            print("Time:", time_difference)

            cost = compute_cost(AL, Y)

            accuracy = calculate_accuracy(parameters, x_test, y_test, size)

            print("Cost on test dataset after iteration %i: %f, accuracy: %f" % (i, cost, accuracy))

            #           costs.append(cost)

            accuracies.append(accuracy)

        if i % 1000 == 0:
            learning_rate = learning_rate * 0.98

    # Plots graph of accuracy to mark improvement
    plt.xlabel('iterations')
    plt.title("Learning rate =" + str(learning_rate))

    line, = plt.plot(np.squeeze(accuracies), label='accuracies')

    plt.xlabel('iterations x10')

    plt.legend()

    plt.show()

    return parameters

size = 30000

images, labels = extract_training_samples('balanced')

x_train, y_train = getData(size, images, labels)

images, labels = extract_test_samples('balanced')

global x_test
global y_test

x_test, y_test = getData(size, images, labels)

layer_dims = [784, 30, 20, 47]

parameters = initialise_parameters(layer_dims)

print(calculate_accuracy(parameters, x_train, y_train, size))

parameters = NN_model(x_train, y_train, layer_dims, 0.03, 40000, size, print_values=True)

print(calculate_accuracy(parameters, x_train, y_train, size))
</code>```

Title: Deep Neural Network dimension issue

Body:
I'm facing a problem with the dimensions of my deep neural network, which throws the error:

line 104, in linear_backward
dW = (dz @ a.T) * W * lambd / m
ValueError: operands could not be broadcast together with shapes (8,8) (8,32)

It seems clear to me that the problem lies in the back-propagation, but I can't find my mistake.
I apologise for the messy code in advance and would be really grateful for any assistance since I am a high-schooler with no help around me.

The program is as follows:

import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Neural Network Build

def sigmoid(Z):
    # activation function used to classify the output
    # range of the function is between 0 and 1.
    a = (1 + np.exp(-Z)) ** -1
    return a

def relu(Z):
    # activation function used in hidden layers

    A = np.maximum(0, Z)  # outputs Z if Z is positive, otherwise returns 0

    assert (A.shape == Z.shape) #stops program if this isn't true

    return A

def relu_back(dA, Z):
    # function finding the derivative of the relu function
    dZ = np.array(dA, copy=True)

    dZ[Z <= 0] = 0

    return dZ

def sigmoid_back(dA, Z):
    # function finding the derivative of the sigmoid function
    s = sigmoid(Z)

    dZ = dA * s * (1 - s)

    return dZ

def initialize_parameters(layer_dims):
    parameters = {}
    L = len(layer_dims)
    for layer in range(1, L):
        parameters["W" + str(layer)] = np.random.randn(layer_dims[layer], layer_dims[layer - 1]) * np.sqrt(
            2 / layer_dims[layer - 1])
        parameters["b" + str(layer)] = np.zeros((layer_dims[layer], 1))

        print(parameters["W" + str(layer)].shape)

        assert (parameters['W' + str(layer)].shape == (layer_dims[layer], layer_dims[layer - 1]))
        assert (parameters['b' + str(layer)].shape == (layer_dims[layer], 1))

    return parameters

def forward_prop(X, parameters):
    L = len(parameters) // 2

    forward_cache = {}

    forward_cache["z1"] = parameters["W1"] @ X + parameters["b1"]
    forward_cache["a1"] = relu(forward_cache["z1"])

    for layer in range(2, L):
        forward_cache["z" + str(layer)] = parameters["W" + str(layer)] @ forward_cache["a" + str(layer - 1)] + \
                                          parameters["b" + str(layer)]
        forward_cache["a" + str(layer)] = relu(forward_cache["z" + str(layer)])

    # Output neuron will have sigmoid activation function applied to classify the output according to a probability.
    # This needs to be handled separately.
    forward_cache["z" + str(L)] = parameters["W" + str(L)] @ forward_cache["a" + str(L - 1)] + parameters["b" + str(L)]
    forward_cache["a" + str(L)] = sigmoid(forward_cache["z" + str(L)])

    AL = forward_cache["a" + str(L)]

    assert (AL.shape == (1, X.shape[1]))

    return AL, forward_cache # Return y_hat (the model's prediction) and forward_cache (used in back-prop)

def compute_loss(A, Y, parameters, lambd):
    L = len(parameters) // 2
    m = Y.shape[1]
    log_function = -(np.multiply(Y, np.log(A)) + np.multiply((1 - Y), np.log(1 - A)))
    L2_regularisation_cost = 0
    for weight in range(1, L):
        L2_regularisation_cost += np.sum(np.square(parameters["W" + str(weight)]))

    loss = 1. / m * (np.nansum(log_function) + (L2_regularisation_cost * lambd / 2))

    loss = np.squeeze(loss)

    assert (loss.shape == ())

    return loss

def linear_backward(dz, W, b, a, lambd):
    m = a.shape[1]
    dW = (dz @ a.T) * W * lambd / m
    db = np.sum(dz, axis=1, keepdims=True)
    dA_prev = W.T @ dz

    print("W:", W.shape,"dZ:", dz.shape)
    print(dA_prev.shape, a.shape)

#    assert (dA_prev.shape == a.shape)
    assert (dW.shape == W.shape)
    assert (db.shape == b.shape)

    return dW, db, dA_prev

def activation_backward(dA, a, z, W, b, activation_function, lambd):
    if activation_function == "sigmoid":
        dz = sigmoid_back(dA, z)
    elif activation_function == "relu":
        dz = relu_back(dA, z)

    dW, db, dA_prev = linear_backward(dz, W, b, a, lambd)

    return dW, db, dA_prev

def backward_prop(AL, Y, cache, parameters, lambd):
    L = len(parameters) // 2
    print(AL)
    gradients = {}
    dAL = -(np.divide(Y, AL)) + np.divide(1 - Y, 1 - AL)
    dz = sigmoid_back(dAL, cache["z" + str(L)])
    gradients["dW" + str(L)], gradients["db" + str(L)], dA_prev = \
        linear_backward(dz, parameters["W" + str(L)], parameters["b" + str(L)], cache["a" + str(L)], lambd)

    for l in reversed(range(1, L)):
        gradients["dW" + str(l)], gradients["db" + str(l)], dA_prev = \
            activation_backward(dA_prev, cache["a" + str(l)], cache["z" + str(l)],
                                parameters["W" + str(l)], parameters["b" + str(l)],
                                "relu", lambd)

    return gradients

def update_parameters(parameters, gradients, learning_rate):
    L = len(parameters) // 2

    for l in range(1, L + 1):
        parameters["W" + str(l)] = parameters["W" + str(l)] - (gradients["dW" + str(l)] * learning_rate)
        parameters["b" + str(l)] = parameters["b" + str(l)] - (gradients["db" + str(l)] * learning_rate)

    return parameters

def predict(X, Y, parameters):
    """
        This function is used to predict the results of a  L-layer neural network.

        Arguments:
        X -- data set of examples you would like to label
        parameters -- parameters of the trained model

        Returns:
        p -- predictions for the given dataset X
        """

    m = X.shape[1]
    # n = len(parameters) // 2 number of layers in the neural network
    binary_outcome = np.zeros((1, m))

    # Forward propagation
    probabilities = deep_neural_network(X, Y, parameters, )

    # convert probabilities to 0/1 predictions
    for i in range(0, probabilities.shape[1]):
        if probabilities[0, i] > 0.5:
            binary_outcome[0, i] = 1
        else:
            binary_outcome[0, i] = 0

    # print results
    # print ("predictions: " + str(p))
    # print ("true labels: " + str(y))
    print("Accuracy: " + str(np.sum((binary_outcome == Y) / m)))

    return binary_outcome

def deep_neural_network(X, Y, layer_dims, number_of_iterations, learning_rate, print_cost, lambd):
    losses = []

    for epoch in range(number_of_iterations):

        parameters = initialize_parameters(layer_dims)

        AL, forward_cache = forward_prop(X, parameters)

        loss = compute_loss(AL, Y, parameters, lambd)

        losses.append(loss)

        gradients = backward_prop(AL, Y, forward_cache, parameters, lambd)

        parameters = update_parameters(parameters, gradients, learning_rate)

        if (epoch % 100 == 0) and print_cost:
            print(loss)

    return parameters

    return

cancer = load_breast_cancer()
data = cancer.data
labels = cancer.target

xtrain, xtest, ytrain, ytest = train_test_split(data, labels)

xtrain = xtrain.T
ytrain = ytrain.reshape((1, 426))

parameters, costs = deep_neural_network(xtrain, ytrain, [30, 64, 32, 8, 1], learning_rate=0.045,
                                        number_of_iterations=2000, print_cost=True, lambd=0.01)
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(0.075))
plt.show()
predictions = predict(X=xtrain, parameters=parameters)

Body:
SOLVED:

The following code facilitates a customizable Deep Neural Network. My DNN is trained on the Extended MNIST.

import numpy as np
import matplotlib.pyplot as plt
from emnist import extract_training_samples
from emnist import extract_test_samples
import keras
import time
import os

# Converts data from the emnist database into the required format
# for the neural network and outputs the X and Y datasets
def getData(size, images, labels):
    training_samples = images.shape[0]

    # Converts each matrix of pixels (28x28) into feature vectors
    xtrain = images.reshape(training_samples, 784)

    # Arranges each training example into a column
    xtrain = xtrain.T

    #normalises the database to prevent the exploding gradients problem
    xtrain = xtrain / 255

    ytrain = labels.reshape(1, training_samples)

    # Converts the numerical labels into one-hot arrays with 47 elements.
    # For example a label of 10 will be converted into an array of 0s with
    # a 1 in the 10th position. This can be abstracted as the 10th neuron
    # firing in the neural network.
    y_train = keras.utils.to_categorical(ytrain, 47)

    y_train = y_train[0].T

    # Replaces 1s and 0s with approximate probabilities to prevent NaN errors in
    # the cost entropy function
    y_train[y_train == 0] = 0.01
    y_train[y_train == 1] = 0.99

    # Allows choice of the number of training examples input into the neural network.
    xsmall = np.hsplit(xtrain, [size, (training_samples - size)])[0]
    ysmall = np.hsplit(y_train, [size, (training_samples - size)])[0]

    # sanity check
    print(xsmall.shape, ysmall.shape)

    return xsmall, ysmall

# Activation function that replaces all negative values with 0
def relu(Z):
    A = np.maximum(0, Z)

    assert (A.shape == Z.shape)

    return A

# Derivative of the relu function: 0 if Z is negative and Z if Z is positive
def relu_backward(dA, cache, layer):

    # Accesses Z from the cache set up in the feed_forward function
    Z = cache["Z" + str(layer)]

    dZ = np.array(dA, copy=True)

    # At 0, I've set the derivative to be 0 as well, although it isn't differentiable at that point
    dZ[Z <= 0] = 0

    assert (dZ.shape == Z.shape)

    return dZ

# Activation function on the final layer that converts the linear transformation
# from the layer into probabilities that sum to 1. This is the neural net's estimate of how
# likely it is that the input example is each label in the dataset: the highest probability is
# what we choose as the neural net's output
def softmax(Z):

    exp_Z = np.exp(Z - np.max(Z))

    A = exp_Z / np.sum(exp_Z, axis=0)

    assert (A.shape == Z.shape)

    return A

# Derivative of the softmax function works very well with the cross-entropy cost function,
# which simplifies to the difference between the neural net's guess and the actual label.
def softmax_backward(cache, layer):

    # fetching values from the cache
    Z = cache["Z" + str(layer)]
    AL = cache["A" + str(layer)]
    Y = cache["Y"]

    dZ = AL - Y

    assert (dZ.shape == Z.shape)

    return dZ

# This sets up our parameters depending on the required dimensions of the neural network.
def initialise_parameters(layer_dims):
    parameters = {}

    np.random.seed(2)
            # He initialisation allows for symmetry breaking and efficient gradient descent
            parameters[W_layer] = np.random.randn(layer_dims[l], layer_dims[l - 1]) * np.sqrt(2 / layer_dims[l - 1])
            parameters[b_layer] = np.zeros((layer_dims[l], 1))

            assert (parameters[W_layer].shape == (layer_dims[l], layer_dims[l - 1]))
            assert (parameters[b_layer].shape == (layer_dims[l], 1))

    return parameters

# Outputs Z as the linear transformation of the previous layer's output using the parameters: Weight and bias.
def linear_forward(W, A_previous, b):
    Z = W @ A_previous + b

    assert (Z.shape == (W.shape[0], A_previous.shape[1]))

    return Z

# chooses the activation function that Z passes through depending on the activation_type requested.
def activation_forward(Z, activation_type):
    if activation_type == "relu":
        A = relu(Z)

    elif activation_type == "softmax":
        A = softmax(Z)

    assert (A.shape == Z.shape)

    return A

def feed_forward(X, parameters, Y):
    cache = {}
    A = X

    # Fetches number of layers in the neural network.
    L = len(parameters) // 2

    # considers the feature vector input as the 0th layer, initialising the cache
    cache["A0"] = X
    cache["Y"] = Y

    # Uses the relu for all the hidden layers
    for l in range(1, L + 1):

        # Uses softmax for the output layer
        activation_type = "relu"
        if l == L:
            activation_type = "softmax"

        # Combines all the helper functions above to propagate through the neural network
        A_previous = A
        W = parameters["W" + str(l)]
        b = parameters["b" + str(l)]
        Z = linear_forward(W, A_previous, b)
        A = activation_forward(Z, activation_type)

        # We store these parameters for use in the back propagation, since we use the element inside
        # when differentiating according to the chain rule. Saving these here improves efficiency and speed.
        cache["W" + str(l)] = W
        cache["b" + str(l)] = b
        cache["Z" + str(l)] = Z
        cache["A" + str(l)] = A

    # Returns the final output layer and the cache
    return cache["A" + str(L)], cache

def compute_cost(AL, Y):

    training_samples = Y.shape[1]

    # Zeroes can introduce NaN errors in the cost function, so we approximate them closely
    # to prevent log(0) from occurring and messing up our cost.
    AL_nonzero = np.maximum(AL, 1.0e-15)
    cost = (-1. / training_samples) * (np.sum(np.multiply(Y, np.log(AL_nonzero))) + np.sum(np.multiply(1 - Y, np.log(1 - AL_nonzero))))

    # Removes unnecessary dimensions from the cost value, so [[0.15]] is converted into 0.15
    cost = np.squeeze(cost)
    assert (cost.shape == ())

    return cost

# Performs backward propagation from one layer to the previous depending on
# the activation function specified.
def activation_backward(dAL, cache, layer, activation_type):
    if activation_type == "relu":
        dZ = relu_backward(dAL, cache, layer)

    elif activation_type == "softmax":
        dZ = softmax_backward(cache, layer)

    # Accesses values from cache
    A_prev = cache["A" + str(layer - 1)]
    W = cache["W" + str(layer)]
    b = cache["b" + str(layer)]
    training_samples = A_prev.shape[1]

    dW = (1. / training_samples) * np.dot(dZ, A_prev.T)
    db = (1. / training_samples) * np.sum(dZ, axis=1, keepdims=True)
    dA_prev = np.dot(W.T, dZ)

    assert (dA_prev.shape == A_prev.shape)
    assert (dW.shape == W.shape)
    assert (db.shape == b.shape)

    return dA_prev, dW, db

# Performs backward propagation on the whole neural network to find the derivatives of the
# cost function with respect to the parameters Weight and bias of each layer. These derivatives will
# allow us to perform gradient descent on the the cost function, tuning the parameters to improve accuracy.
def back_propagation(cache, Y, L):
    gradients = {}

    # Accesses output layer matrix from cache
    AL = cache["A" + str(L)]

    # Derivative of the cross entropy cost function
    gradients["dA" + str(L)] = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))

    # Uses relu_backward for all the hidden layers
    for l in reversed(range(1, L + 1)):
        activation_type = "relu"

        # Uses softmax_backward for the output layer
        if l == L:
            activation_type = "softmax"

        # finds gradients of each layer
        dA_previous, dW_current, db_current = activation_backward(gradients["dA" + str(l)], cache, l, activation_type)

        # Stores these gradients in the gradients dictionary
        gradients["dA" + str(l - 1)] = dA_previous
        gradients["dW" + str(l)] = dW_current
        gradients["db" + str(l)] = db_current

    return gradients

# Updates all of the parameters by subtracting their gradients from them
def update_parameters(gradients, parameters, learning_rate):
    L = len(parameters) // 2

    for l in range(1, L):
        W_layer = "W" + str(l)
        b_layer = "b" + str(l)
        dW_layer = "dW" + str(l)
        db_layer = "db" + str(l)

        # The learning rate needs to be tuned to approach the minimum of the cost accurately and reliably
        # while optimising time taken to train the neural net
        parameters[W_layer] = parameters[W_layer] - (gradients[dW_layer] * learning_rate)
        parameters[b_layer] = parameters[b_layer] - (gradients[db_layer] * learning_rate)

    return parameters

# Calculates accuracy of the neural net's prediction against the true labels
def calculate_accuracy(parameters, x, y_onehot, size):

    # Uses parameters to get the neural net's guess
    AL, cache = feed_forward(x, parameters, y_onehot)

    # Finds the highest probability character for each training example
    AL_max = np.argmax(AL, axis=0)

    AL_max = AL_max.reshape(1, AL_max.shape[0])

    # Converts the labels from one-hot matrices back into a numerical vector
    y_int = np.argmax(y_onehot, axis=0)
    y_int = y_int.reshape(1, y_int.shape[0])

    # Compares the two
    accuracy = np.mean(AL_max == y_int)

    return accuracy

# Brings everything together to train the model
def NN_model(X, Y, layer_dims, learning_rate, num_iterations, size, print_values):

    # Accesses initial parameters
    parameters = initialise_parameters(layer_dims)

    # Used to calculate the training time over the iteration
    times = []

    # Used to plot the accuracies at the end
    accuracies = []

    times.append(time.time())

    for i in range(num_iterations):

        # Accesses the output and cache using forward propagation through the parameters
        AL, cache = feed_forward(X, parameters, Y)

        # Calculates the gradients using the previouslt generated cache
        gradients = back_propagation(cache, Y, len(layer_dims) - 1)

        # Tunes the parameters to achieve greater accuracy
        parameters = update_parameters(gradients, parameters, learning_rate)

        # Used to track progress during training
        if i % 10 == 0 and print_values:
            time_difference = time.time() - times[-1]

            times.append(time.time())

            print("Time:", time_difference)

            cost = compute_cost(AL, Y)

            accuracy = calculate_accuracy(parameters, x_test, y_test, size)

            print("Cost on test dataset after iteration %i: %f, accuracy: %f" % (i, cost, accuracy))

            #           costs.append(cost)

            accuracies.append(accuracy)

        if i % 1000 == 0:
            learning_rate = learning_rate * 0.98

    # Plots graph of accuracy to mark improvement
    plt.xlabel('iterations')
    plt.title("Learning rate =" + str(learning_rate))

    line, = plt.plot(np.squeeze(accuracies), label='accuracies')

    plt.xlabel('iterations x10')

    plt.legend()

    plt.show()

    return parameters

size = 30000

images, labels = extract_training_samples('balanced')

x_train, y_train = getData(size, images, labels)

images, labels = extract_test_samples('balanced')

global x_test
global y_test

x_test, y_test = getData(size, images, labels)

layer_dims = [784, 30, 20, 47]

parameters = initialise_parameters(layer_dims)

print(calculate_accuracy(parameters, x_train, y_train, size))

parameters = NN_model(x_train, y_train, layer_dims, 0.03, 40000, size, print_values=True)

print(calculate_accuracy(parameters, x_train, y_train, size))


来源: stackexchange

问:
Entity Framework includes the unexpected child if the child is ready called in another statement
I am using ASP.NET Core 3.1.2, Pomelo.EntityFramework 3.1.1 to link with a MySQL database. Here is a minimum example to demonstrate my problem.

Two model classes are defined. The model Field includes multiple zones:

public class Field
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<Zone> Zones { get; set; }

    public Field()
    {
        Zones = new HashSet<Zone>();
    }
}

public class Zone
{
    [Key]
    public int Id { get; set; }
    public int FieldId { get; set; }
    public virtual Field Field { get; set; }
}
```

<p>Then an API is created to get the data for a zone. </p>

<pre class="lang-cs prettyprint-override"><code>[Route("api/field/{field}/zone/{id}")]
[HttpGet]
public async Task<ActionResult<List<Field>>> GetZone(
    [FromRoute] int field,
    [FromRoute] int id)
{
    Zone Zone = await _context.Zone
                              .Where(m => m.FieldId == field && m.Id == id)
                              .SingleOrDefaultAsync();
    return Ok(_context.Field);
}
</code>```

In the demo code, I expect to return the list of Field without Zone. However, the results include the contents for Zone equals id if calling /api/field/16/zone/15.

```<code>[
    {
        "zone": [],
        "id": 7
    },
    {
        "zone": [
            {
                "id": 15
            }
        ],
        "id": 16
    }
]
</code>```

It seems the contents are kept if the child is called in another place. I would not want to return child. Any suggestions?

答:
The problem is related with Tracking behavior in Entity Framework Core. See this link for more information: https://learn.microsoft.com/en-us/ef/core/querying/tracking

Using function AsNoTracking() will solve my problem.

return Ok(_context.Field.AsNoTracking());
```

ContentLicense: CC BY-SA 4.0

Body:
I am using ASP.NET Core 3.1.2, Pomelo.EntityFramework 3.1.1 to link with a MySQL database. Here is a minimum example to demonstrate my problem.

Two model classes are defined. The model Field includes multiple zones:

public class Field
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<Zone> Zones { get; set; }

    public Field()
    {
        Zones = new HashSet<Zone>();
    }
}

public class Zone
{
    [Key]
    public int Id { get; set; }
    public int FieldId { get; set; }
    public virtual Field Field { get; set; }
}

Then an API is created to get the data for a zone.

[Route("api/field/{field}/zone/{id}")]
[HttpGet]
public async Task<ActionResult<List<Field>>> GetZone(
    [FromRoute] int field,
    [FromRoute] int id)
{
    Zone Zone = await _context.Zone
                              .Where(m => m.FieldId == field && m.Id == id)
                              .SingleOrDefaultAsync();
    return Ok(_context.Field);
}

In the demo code, I expect to return the list of Field without Zone. However, the results include the contents for Zone equals id if calling /api/field/16/zone/15.

[
    {
        "zone": [],
        "id": 7
    },
    {
        "zone": [
            {
                "id": 15
            }
        ],
        "id": 16
    }
]

It seems the contents are kept if the child is called in another place. I would not want to return child. Any suggestions?


问:
Recursion for generating subsets of a list
I would like to know for this function, why does after it runs and returns [[],[1]] which is the last line of the function? Then it would run the line smaller = genSubset(L[:-1]) again and again. I visualized the code from pythontutor. However, i do not understand why it works this way. Someone please enlighten me. Thank You.

This function generates all the possible subsets of a given list, so, for example, input list is [1,2,3]. It would return [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]].

```<code>def genSubsets(L):
    res = []
    if len(L) == 0:
        return [[]] #list of empty list
    smaller = genSubsets(L[:-1]) # all subsets without last element
    extra = L[-1:] # create a list of just last element
    new = []
    for small in smaller:
        new.append(small+extra)  # for all smaller solutions, add one with last element
    return smaller+new  # combine those with last element and those without

print(genSubsets([1,2,3]))
</code>```

答:
This function is recursive, that means - it calls itself. when it finishes one recursion iteration - it returns to where it stopped. This is exactly at the line smaller = getSubsets(L[:-1]) but notice that it wont run it again, but instead continue until it finishes the call. This recursion happens n times (where n is the list's length), and you will see this behavior exactly n times before the function finally returns the subsets.

I hope i understood correctly the question and managed to answer it :)

Body:
I would like to know for this function, why does after it runs and returns [[],[1]] which is the last line of the function? Then it would run the line smaller = genSubset(L[:-1]) again and again. I visualized the code from pythontutor. However, i do not understand why it works this way. Someone please enlighten me. Thank You.

This function generates all the possible subsets of a given list, so, for example, input list is [1,2,3]. It would return [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]].

def genSubsets(L):
    res = []
    if len(L) == 0:
        return [[]] #list of empty list
    smaller = genSubsets(L[:-1]) # all subsets without last element
    extra = L[-1:] # create a list of just last element
    new = []
    for small in smaller:
        new.append(small+extra)  # for all smaller solutions, add one with last element
    return smaller+new  # combine those with last element and those without

print(genSubsets([1,2,3]))


问:
Set a String to TextView and wait for a while and set another String to same TextView
I want to set "First" word to TextView (textTT) and wait for a while set to "Second" to same TextView
but result is directly set "second" to TextView
but I need firstly set "First". How can I overcome this?
<></p>

```<code>private void applicationTest()  {
    textTT.setText("First");
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    textTT.setText("Second");
}
</code>```

答:
If you call Thread.sleep(1000) it means UI thread  will be blocking and UI not update First. If you want to wait and set Second you should use Handler. Try this

```<code>private void applicationTest()  {
    textTT.setText("First");
    new Handler().postDelayed({
           textTT.setText("Second");
    }, 1000)
}
</code>```

Body:
I want to set "First" word to TextView (textTT) and wait for a while set to "Second" to same TextView
but result is directly set "second" to TextView
but I need firstly set "First". How can I overcome this?
<>

private void applicationTest()  {
    textTT.setText("First");
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    textTT.setText("Second");
}

Body:
If you call Thread.sleep(1000) it means UI thread  will be blocking and UI not update First. If you want to wait and set Second you should use Handler. Try this

private void applicationTest()  {
    textTT.setText("First");
    new Handler().postDelayed({
           textTT.setText("Second");
    }, 1000)
}


问:
How do i keep track of the time at which something happened in my code?
i am making a program that reads input from a user, allows them to view the books available in my library (which is imported from an excel sheet), picking an item and choosing to borrow it, but the thing is that i want to put borrowing and return dates, how do i do that exactly? for example: if a user confirms the borrowing of a book, after the operation happens the date of borrowing should be added in the excel sheet, and also a little side request with this; if it is possible to somehow neglect the first row in a for loop that is looping across an array of loops and proceed with other rows then please help me out with that as well (this loop is at line 111 in the code, the one after the list "filteredBooks") here is my code so far:
`public class BookStudentMerge {

```<code> public static List<Row> getStudentInfo(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
      List<Row> result = new ArrayList<Row>();
      String cellValue = "";
      String searchV = searchValue.toLowerCase();
      for (Row row : sheet) {
       for (Cell cell : row) {
        cellValue = formatter.formatCellValue(cell, evaluator);
        String cellV = cellValue.toLowerCase();
        if (cellV.contains(searchV)) {
         result.add(row);
         break;
        }
       }
      }
      return result;
     }
 public static List<Row> bookSearch(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
     List<Row> none = new ArrayList<Row>();
     String searchM = searchValue.toLowerCase();
     List<Row> result = new ArrayList<Row>();
      String cellValue = "";
      for (Row row : sheet) {
       for (Cell cell : row) {
        cellValue = formatter.formatCellValue(cell, evaluator);
        String cellM = cellValue.toLowerCase();
        if (cellM.contains(searchM)) {
            result.add(row);
         break;
        }
       }
      }
      return result;
 }
 public static boolean checkAvailability (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
     String cellValue = "";
     for (Cell c : r) {
         cellValue = formatter.formatCellValue(c, evaluator);
         String cellM = cellValue.toLowerCase();
         if (cellM.equalsIgnoreCase("Available")) {
             return true;
     }
    }
     return false;
 }
 public static void changeStatus (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
     String cellValue = "";
     for (Cell c : r) {
         cellValue = formatter.formatCellValue(c, evaluator);
         String cellM = cellValue.toLowerCase();
     if (cellM.equalsIgnoreCase("Available")) {
         c.setCellValue("Unavailable");
     }
     else if (cellM.equalsIgnoreCase("Unavailable")) {
         c.setCellValue("Available");
     }
     }
 }

public static void main(String[] args) throws IOException, InvalidFormatException {

    Workbook Books = WorkbookFactory.create(new FileInputStream("C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\Books & DDC.xlsx"));
    Sheet SB = Books.getSheetAt(0);
    Workbook Students = WorkbookFactory.create(new FileInputStream("C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\StudentsInfo.xlsx"));
    Sheet SS = Students.getSheetAt(0);
    DataFormatter FB = new DataFormatter();
    FormulaEvaluator EB =  Books.getCreationHelper().createFormulaEvaluator();
    DataFormatter FS = new DataFormatter();
    FormulaEvaluator ES =  Students.getCreationHelper().createFormulaEvaluator();
    Scanner s = new Scanner (System.in);

         System.out.println("Welcome to our BiblioTheque library program!");
         System.out.println("Enter a valid student ID number: ");
         String inp = s.nextLine();
          Row headers = SS.getRow(0);
          for (Cell HS : headers) {
              System.out.print(HS.getStringCellValue() + "\t\t  ");
          }
         System.out.println();
         List<Row> filteredStudents = getStudentInfo(SS, FS, ES, inp);
         for (Row row : filteredStudents) {
               for (Cell cell : row) {
                   System.out.print(FS.formatCellValue(cell, ES));
                   System.out.print("\t     ");
               }
               System.out.println();
         }
         System.out.println();
         System.out.println("Search for a book by title, author, DDC, or ISBN: ");
         System.out.println("enter the name, author, ddc, isbn of your book: ");
         String search = s.nextLine();
          List<Row> filteredBooks = bookSearch(SB, FB, EB, search);
          Row HB = SB.getRow(0);
          for (Cell CB : HB) {
              System.out.print(CB.getStringCellValue() + "\t\t\t");
          }
          System.out.println();
          for (Row row : filteredBooks) {
              for (Cell cell : row) {
            System.out.print(FB.formatCellValue(cell, EB));
            System.out.print("\t\t");
           }
              System.out.println();
          }
          if (filteredBooks.isEmpty()) {
              System.out.println("no results found for the entered keyword/number");
          }
          else {
              System.out.println("do you wish to borrow any of the displayed books?");
          }
          String ans = s.nextLine();
          if (ans.equalsIgnoreCase("yes")) {
              System.out.println("choose the desired book by typing the item's list number: ");
          }
          int borrow = s.nextInt() - 1;
          Row Result = filteredBooks.get(borrow);
          if (checkAvailability(Result, FB, EB)) {
              System.out.println("You are going to borrow the book " + Result.getCell(0) + ", please proceed to the help desk to complete the operation.");
              changeStatus(Result, FB, EB);
          }
}
</code>```

}`

答:
One possibility is java.time.LocalDateTime:

```<code>import java.time.LocalDateTime;

public class Time {
  public static void main(String[] args) {
   LocalDateTime time = LocalDateTime.now();
   System.out.println(time);
  }
}
</code>```

This will give an output like this: 2020-05-30T13:29:14.566.

If you want to change the format, you will have to use a DateTimeFormatter:

```<code>import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Time {
  public static void main(String[] args) {
   LocalDateTime time = LocalDateTime.now();

   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
   String timeDate = time.format(formatter);
   System.out.println(timeDate);
  }
}
</code>```

This will give an output like 30-05-2020 13:59:13.

So now we have a String with the wanted date. We now just have to write this to a csv-file (my recommendation is to use a csv and not a excel-file!). One example on how this is possible is provided in this answer. If you really want to use excel, I suggest reading this question and its answers.

More information can be found here and here.

With the information and examples provided above, you should be able to implement it by yourself in your code.

Body:
i am making a program that reads input from a user, allows them to view the books available in my library (which is imported from an excel sheet), picking an item and choosing to borrow it, but the thing is that i want to put borrowing and return dates, how do i do that exactly? for example: if a user confirms the borrowing of a book, after the operation happens the date of borrowing should be added in the excel sheet, and also a little side request with this; if it is possible to somehow neglect the first row in a for loop that is looping across an array of loops and proceed with other rows then please help me out with that as well (this loop is at line 111 in the code, the one after the list "filteredBooks") here is my code so far:
`public class BookStudentMerge {

 public static List<Row> getStudentInfo(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
      List<Row> result = new ArrayList<Row>();
      String cellValue = "";
      String searchV = searchValue.toLowerCase();
      for (Row row : sheet) {
       for (Cell cell : row) {
        cellValue = formatter.formatCellValue(cell, evaluator);
        String cellV = cellValue.toLowerCase();
        if (cellV.contains(searchV)) {
         result.add(row);
         break;
        }
       }
      }
      return result;
     }
 public static List<Row> bookSearch(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
     List<Row> none = new ArrayList<Row>();
     String searchM = searchValue.toLowerCase();
     List<Row> result = new ArrayList<Row>();
      String cellValue = "";
      for (Row row : sheet) {
       for (Cell cell : row) {
        cellValue = formatter.formatCellValue(cell, evaluator);
        String cellM = cellValue.toLowerCase();
        if (cellM.contains(searchM)) {
            result.add(row);
         break;
        }
       }
      }
      return result;
 }
 public static boolean checkAvailability (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
     String cellValue = "";
     for (Cell c : r) {
         cellValue = formatter.formatCellValue(c, evaluator);
         String cellM = cellValue.toLowerCase();
         if (cellM.equalsIgnoreCase("Available")) {
             return true;
     }
    }
     return false;
 }
 public static void changeStatus (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
     String cellValue = "";
     for (Cell c : r) {
         cellValue = formatter.formatCellValue(c, evaluator);
         String cellM = cellValue.toLowerCase();
     if (cellM.equalsIgnoreCase("Available")) {
         c.setCellValue("Unavailable");
     }
     else if (cellM.equalsIgnoreCase("Unavailable")) {
         c.setCellValue("Available");
     }
     }
 }

public static void main(String[] args) throws IOException, InvalidFormatException {

    Workbook Books = WorkbookFactory.create(new FileInputStream("C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\Books & DDC.xlsx"));
    Sheet SB = Books.getSheetAt(0);
    Workbook Students = WorkbookFactory.create(new FileInputStream("C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\StudentsInfo.xlsx"));
    Sheet SS = Students.getSheetAt(0);
    DataFormatter FB = new DataFormatter();
    FormulaEvaluator EB =  Books.getCreationHelper().createFormulaEvaluator();
    DataFormatter FS = new DataFormatter();
    FormulaEvaluator ES =  Students.getCreationHelper().createFormulaEvaluator();
    Scanner s = new Scanner (System.in);

         System.out.println("Welcome to our BiblioTheque library program!");
         System.out.println("Enter a valid student ID number: ");
         String inp = s.nextLine();
          Row headers = SS.getRow(0);
          for (Cell HS : headers) {
              System.out.print(HS.getStringCellValue() + "\t\t  ");
          }
         System.out.println();
         List<Row> filteredStudents = getStudentInfo(SS, FS, ES, inp);
         for (Row row : filteredStudents) {
               for (Cell cell : row) {
                   System.out.print(FS.formatCellValue(cell, ES));
                   System.out.print("\t     ");
               }
               System.out.println();
         }
         System.out.println();
         System.out.println("Search for a book by title, author, DDC, or ISBN: ");
         System.out.println("enter the name, author, ddc, isbn of your book: ");
         String search = s.nextLine();
          List<Row> filteredBooks = bookSearch(SB, FB, EB, search);
          Row HB = SB.getRow(0);
          for (Cell CB : HB) {
              System.out.print(CB.getStringCellValue() + "\t\t\t");
          }
          System.out.println();
          for (Row row : filteredBooks) {
              for (Cell cell : row) {
            System.out.print(FB.formatCellValue(cell, EB));
            System.out.print("\t\t");
           }
              System.out.println();
          }
          if (filteredBooks.isEmpty()) {
              System.out.println("no results found for the entered keyword/number");
          }
          else {
              System.out.println("do you wish to borrow any of the displayed books?");
          }
          String ans = s.nextLine();
          if (ans.equalsIgnoreCase("yes")) {
              System.out.println("choose the desired book by typing the item's list number: ");
          }
          int borrow = s.nextInt() - 1;
          Row Result = filteredBooks.get(borrow);
          if (checkAvailability(Result, FB, EB)) {
              System.out.println("You are going to borrow the book " + Result.getCell(0) + ", please proceed to the help desk to complete the operation.");
              changeStatus(Result, FB, EB);
          }
}

}`

Body:
One possibility is java.time.LocalDateTime:

import java.time.LocalDateTime;

public class Time {
  public static void main(String[] args) {
   LocalDateTime time = LocalDateTime.now();
   System.out.println(time);
  }
}

This will give an output like this: 2020-05-30T13:29:14.566.

If you want to change the format, you will have to use a DateTimeFormatter:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Time {
  public static void main(String[] args) {
   LocalDateTime time = LocalDateTime.now();

   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
   String timeDate = time.format(formatter);
   System.out.println(timeDate);
  }
}

This will give an output like 30-05-2020 13:59:13.

So now we have a String with the wanted date. We now just have to write this to a csv-file (my recommendation is to use a csv and not a excel-file!). One example on how this is possible is provided in this answer. If you really want to use excel, I suggest reading this question and its answers.

More information can be found here and here.

With the information and examples provided above, you should be able to implement it by yourself in your code.


问:
jquery countdown clock lag/jump
I have a countdown clock that I have been working on. If you watch it for several seconds you will start to notice that it will occasionally hang or jump a bit when counting. Its subtle but... obvious. We have tested on lot of browsers and os's and the problem seems to be consistent and we just arent sure what to do on this. Oringially we had image sprites for the font but changed that thinking that we had some http request leaking by but now that we have changed to cufon we still see the same exact problems.

Another update we made is to set the interval time to 500ms but we still seem to be stuck

http://dl.dropbox.com/u/2982102/Moto/Countdown/Cufon/markup-new/countdownclock/index.html

Any help would be awesome on this.

答:
My guess is each 'tick' of the clock is too intensive to stay in sync correctly. For this to work, you need the action performed by each 'tick' to be as close to instant as possible.

Looking at the DOM as the countdown runs shows that the counter rebuilds the DOM fragment with the updated value with every 'tick'. This on its own wouldn't cause a problem (I think), but adding Cufon into the mix adds a LOT of overhead - Cufon needs to re-index the new elements, read their values, and re-render the fonts.

There could be reasons that the countdown plugin rebuilds the DOM every time it ticks, but on first glance, it seems like an inefficient way to do it.

If you really need fancy fonts, I would either use a @font-face solution, or like you said, use a set of image sprites. Manipulating a sprite-map by adjusting classes would allow you to update the counter without touching the DOM structure at all. This would require building your own version of the plugin, though.

UPDATE:

I think I might have some bad news for you, mate. I've been tinkering with the foundation of a nice javascript timer, and I think I've run into the root cause of your problems with the clocks you've been using. Basically, it comes down to the fact that Javascript's timing functions are not very accurate, for a couple of reasons:
The resolution of the browser-level timer is only down to 10 or 15ms, depending on the browser.
Javascript is single-threaded, meaning that timer events go into a queue, throwing their timing off.
I've set up a baseline test which does everything 'right' - keeps everything that needs to run on time small and fast, and self-corrects for any inaccuracies in the timing, due to the queueing of the 'tick' events. Essentially, the tick checks every 20ms to find the most accurate point to update the display. Each update takes 1ms or less on my machine (it's below the limit of Javascript's timing resolution).

Now, 90% of the time, this produces a clock that updates within 20ms of the exact time it should, which looks nice and precise. The problem is when the browser decides to do something else - with another tab, or something internal to the browser. Then, we see exactly the lag you've been noticing: the display can hang for a second or more before it updates. Being self-correcting, the next update jumps back to the right time, but it is definitely noticeable, just like with the others. The user doesn't even have to be doing anything - I can sit and watch the accuracy readout jump from 5(ms), 5, 8, 10, 5, 1300, 5, 5, 800... without touching anything at all.

So, it looks like I might have given you a bum steer - the problem isn't that your update code takes too long (though there are inefficiencies we could improve on), it's that browsers aren't set up to give accurate timings at accurate intervals. At this stage, I don't see that there's a way to work around this fundamental limitation.

Here's some more reading, if you're interested: http://ejohn.org/blog/how-javascript-timers-work/, http://old.nabble.com/Proposal%3A-High-resolution-%28and-otherwise-improved%29-timer-API-td19791635.html

ContentLicense: CC BY-SA 3.0

Title: jquery countdown clock lag/jump


问:
IndexError: list assignment index out of range?
How to fix this error ?!
The code is

```<code>from datetime import datetime
with open("1.txt") as f:
    lines = f.readlines()
lines[1] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
with open("1.txt", "w") as f:
    f.writelines(lines)
</code>```

The error

```<code>  File "test.py", in <module>
    lines[1] = datetime.today().strftime('%A %d %B %Y')
IndexError: list assignment index out of range```
```

答:
What is the content of 1.txt? Assuming there is only one line in the file and you want to update the line, you have to do so with the first index which is [0] therefore:

```<code>lines[0] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
</code>```

If you want to add a line you have to use

```<code>lines.append(datetime.today().strftime('%A %d %B %Y at %I:%M %p'))
</code>```

Body:
How to fix this error ?!
The code is

from datetime import datetime
with open("1.txt") as f:
    lines = f.readlines()
lines[1] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
with open("1.txt", "w") as f:
    f.writelines(lines)

The error

  File "test.py", in <module>
    lines[1] = datetime.today().strftime('%A %d %B %Y')
IndexError: list assignment index out of range```

Body:
What is the content of 1.txt? Assuming there is only one line in the file and you want to update the line, you have to do so with the first index which is [0] therefore:

lines[0] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')

If you want to add a line you have to use

lines.append(datetime.today().strftime('%A %d %B %Y at %I:%M %p'))


问:
Conditional row read of csv in pandas
I have large CSVs where I'm only interested in a subset of the rows.  In particular, I'd like to read in all the rows which occur before a particular condition is met.

For example, if read_csv would yield the dataframe:

```<code>     A    B      C
1   34   3.20   'b'
2   24   9.21   'b'
3   34   3.32   'c'
4   24   24.3   'c'
5   35   1.12   'a'
...
1e9 42   2.15   'd'
</code>```

is there some way to read all the rows in the csv until col B exceeds 10.  In the above example, I'd like to read in:

```<code>     A    B      C
1   34   3.20   'b'
2   24   9.21   'b'
3   34   3.32   'c'
4   24   24.3   'c'
</code>```

I know how to throw these rows out once I've read the dataframe in, but at this point I've already spent all that computation reading them in.  I do not have access to the index of the final row before reading the csv (no skipfooter please)

答:
Building on @joanwa answer:

df = (pd.read_csv("filename.csv")
      [lambda x: x['B'] > 10])
```

<hr>

<p>From Wes McKinney's "Python for Data Analysis" chapter
on "Advanced pandas":</p>

<p>We cannot refer to the result of load_data until it has been assigned to the temporary variable df. To help with this, assign and many other pandas functions accept function-like arguments, also known as callables.</p>

<p>To show callables in action, consider ...</p>

<pre class="lang-py prettyprint-override"><code>df = load_data()
df2 = df[df['col2'] < 0]
</code>```

Can be rewritten as:

df = (load_data()
      [lambda x: x['col2'] < 0])
```

Title: Conditional row read of csv in pandas

Body:
I have large CSVs where I'm only interested in a subset of the rows.  In particular, I'd like to read in all the rows which occur before a particular condition is met.

For example, if read_csv would yield the dataframe:

     A    B      C
1   34   3.20   'b'
2   24   9.21   'b'
3   34   3.32   'c'
4   24   24.3   'c'
5   35   1.12   'a'
...
1e9 42   2.15   'd'

is there some way to read all the rows in the csv until col B exceeds 10.  In the above example, I'd like to read in:

     A    B      C
1   34   3.20   'b'
2   24   9.21   'b'
3   34   3.32   'c'
4   24   24.3   'c'

I know how to throw these rows out once I've read the dataframe in, but at this point I've already spent all that computation reading them in.  I do not have access to the index of the final row before reading the csv (no skipfooter please)

Body:
Building on @joanwa answer:

df = (pd.read_csv("filename.csv")
      [lambda x: x['B'] > 10])

From Wes McKinney's "Python for Data Analysis" chapter
on "Advanced pandas":

We cannot refer to the result of load_data until it has been assigned to the temporary variable df. To help with this, assign and many other pandas functions accept function-like arguments, also known as callables.

To show callables in action, consider ...

df = load_data()
df2 = df[df['col2'] < 0]

Can be rewritten as:

df = (load_data()
      [lambda x: x['col2'] < 0])


答:
You should use Handler to achieve that:

```<code>private void applicationTest()  {
    textTT.setText("First");
    new Handler().postDelayed(new Runnable{
      @Override
      public void run() {
         textTT.setText("Second");
   }
    },1000)
}
</code>```

Title: Set a String to TextView and wait for a while and set another String to same TextView

Body:
You should use Handler to achieve that:

private void applicationTest()  {
    textTT.setText("First");
    new Handler().postDelayed(new Runnable{
      @Override
      public void run() {
         textTT.setText("Second");
   }
    },1000)
}


问:
Spring @RequestMapping for everything except /api/ or /rest/ (negate specific word in regex)
I need a Spring @RequestMapping that I will use to match all non-/api/... paths like /products to forward them to my / (by return "forward:/";) view controller which actually serves index.html contents and causes frontend (React) application to be served so that frontend application can handle this path to render a page.

Please note, that implicitly this @RequestMapping cannot match / as it would end up in infinite recurence and StackOverflowError. It may be worth to exclude index.html also.

In other words, I want my /api/products to be handled "locally" by Spring Boot application, where /products/ should be forwarded to render React app. There is not Apache or Nginx proxy - React app is served by Spring thanks to static resource mappings.

I have studied many similar Stackoverflow questions in this area and none of them is working.

Important note: I saw cases on Stackoverflow where the problem only supposed to be resolved, just because initially it looked like working (/products/ forwarded and /api/products handled by API Controller), but it actually wasn't, because proposed regex was matching everything and /api/products were handled only by Spring mapping precedences (more specific "wins"), but the same path would actually match the pattern if no other Controller would exist.

This results in inappropriate 404 (or mapping not found) error handling - calling any, even not existing /api/something endpoint ends up with forward to index.html in all answers that I have found, which I want to avoid. Accessing not existing /api/something endpoint should rather end up with no handler found Spring error, not in forward. An example of such solution, is the most popular one, like: @RequestMapping(value = "{_:^(?!api).*$}")

The problem is, whatever pattern I try, it ends up with forwarding all of my test cases (like /api /api/ /api/x /page/ /page/2 or none.

Just some examples of patterns I have tried:

```<code>@RequestMapping(value = "{_:^(?!index\\.html|api).*$}")
@RequestMapping(value = "{x:^(?!api).*$}")
@RequestMapping(value = "/{path:^(?!api/).*$}")
@RequestMapping(value = "/{dataType:^.*(?!api).*$}")
@RequestMapping(value = "/{arg:(?!sitemap.xml|api|index.html).*$}")
@RequestMapping(value = "{arg:^(?!api|!index.html).*$}/**")
@RequestMapping(value = "{_:^(?!index\\.html|api).*$}")
</code>```

答:
Looks like the original regex actually works (e.g. "{_:^(?!index\\.html|api).*$}").

The problem is, Jetty, to display 404 was internally forwarding to /error page which also was subject for that controller mapping! Effectively forwarding to frontend app from public resources instead of rendering error page

Body:
I need a Spring @RequestMapping that I will use to match all non-/api/... paths like /products to forward them to my / (by return "forward:/";) view controller which actually serves index.html contents and causes frontend (React) application to be served so that frontend application can handle this path to render a page.

Please note, that implicitly this @RequestMapping cannot match / as it would end up in infinite recurence and StackOverflowError. It may be worth to exclude index.html also.

In other words, I want my /api/products to be handled "locally" by Spring Boot application, where /products/ should be forwarded to render React app. There is not Apache or Nginx proxy - React app is served by Spring thanks to static resource mappings.

I have studied many similar Stackoverflow questions in this area and none of them is working.

Important note: I saw cases on Stackoverflow where the problem only supposed to be resolved, just because initially it looked like working (/products/ forwarded and /api/products handled by API Controller), but it actually wasn't, because proposed regex was matching everything and /api/products were handled only by Spring mapping precedences (more specific "wins"), but the same path would actually match the pattern if no other Controller would exist.

This results in inappropriate 404 (or mapping not found) error handling - calling any, even not existing /api/something endpoint ends up with forward to index.html in all answers that I have found, which I want to avoid. Accessing not existing /api/something endpoint should rather end up with no handler found Spring error, not in forward. An example of such solution, is the most popular one, like: @RequestMapping(value = "{_:^(?!api).*$}")

The problem is, whatever pattern I try, it ends up with forwarding all of my test cases (like /api /api/ /api/x /page/ /page/2 or none.

Just some examples of patterns I have tried:

@RequestMapping(value = "{_:^(?!index\\.html|api).*$}")
@RequestMapping(value = "{x:^(?!api).*$}")
@RequestMapping(value = "/{path:^(?!api/).*$}")
@RequestMapping(value = "/{dataType:^.*(?!api).*$}")
@RequestMapping(value = "/{arg:(?!sitemap.xml|api|index.html).*$}")
@RequestMapping(value = "{arg:^(?!api|!index.html).*$}/**")
@RequestMapping(value = "{_:^(?!index\\.html|api).*$}")


问:
is there any way to read "select mock location app" from Android developer settings?
I know the recomended way to know in Android if a location is a fake point is something like this:

```<code>boolean isMock = false;
if (android.os.Build.VERSION.SDK_INT >= 18) {
    isMock = location.isFromMockProvider();
} else { // Old Android versions (<6)
    isMock = !Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}
</code>```

So, for current Androids I have to request location permission to know if this is a mock location. But I think it is not necessary if I can read "select mock location app".

Also, if you want to show an alert message you can custom this message. e.g.: You could say something like this: "Please remove com.lexa.fakegps app". And when user clicks on "Accept", you could redirect to next link: https://play.google.com/store/apps/details?id=com.lexa.fakegps

Also, there are fake gps apps that return false in the  isFromMockProvider method.

Also, you can do stats with apps more use for yours users.

And finally, I don't understood why you can read every setting of developer settings except "select mock location app":

https://developer.android.com/reference/android/provider/Settings

Is there any solution? Or simply is it a stupid impossible thing?

答:
I haven't tested this but this looks legit. I spent an hour or so trying to come up with my own solution and it was right there on GitHub.

https://github.com/GantMan/jail-monkey/blob/master/android/src/main/java/com/gantix/JailMonkey/MockLocation/MockLocationCheck.java

Body:
I know the recomended way to know in Android if a location is a fake point is something like this:

boolean isMock = false;
if (android.os.Build.VERSION.SDK_INT >= 18) {
    isMock = location.isFromMockProvider();
} else { // Old Android versions (<6)
    isMock = !Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}

So, for current Androids I have to request location permission to know if this is a mock location. But I think it is not necessary if I can read "select mock location app".

Also, if you want to show an alert message you can custom this message. e.g.: You could say something like this: "Please remove com.lexa.fakegps app". And when user clicks on "Accept", you could redirect to next link: https://play.google.com/store/apps/details?id=com.lexa.fakegps

Also, there are fake gps apps that return false in the  isFromMockProvider method.

Also, you can do stats with apps more use for yours users.

And finally, I don't understood why you can read every setting of developer settings except "select mock location app":

https://developer.android.com/reference/android/provider/Settings

Is there any solution? Or simply is it a stupid impossible thing?


问:
How do I have to use extern variables?
I'm having some problems with extern variables. It's the first time I use it, so I don't know how it works. I have this code:

```<code>#include <stdlib.h>
#include <stdio.h>
#include <time.h>

extern double *x, *y, a;
extern unsigned int N;

inline void f1 ()
{
    int i;

    for (i=0; i<N; i++)
    {
        y[i] = a*x[i] + y[i];
    }
}
</code>```

And the error that I get when I compile it it's that x,y,a and N are not defined. So, could someone help me please?

答:
```<code>extern double *x, *y, a;
extern unsigned int N;
</code>```

The extern qualifier for variables at global space tells the compiler/linker that the respective variable is defined in another preprocessing translation unit (another source file) instead of the actual one.

You have no other source file, so the compiler can´t find the definition of the variables anywhere. Thus, it throws an appropriate warning.

If you don´t have multiple source files, you don´t need to use the extern qualifier by *x, *y, a and N. Remove them.

Have a look at here:

How do I use extern to share variables between source files?

Title: How do I have to use extern variables?

Body:
I'm having some problems with extern variables. It's the first time I use it, so I don't know how it works. I have this code:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

extern double *x, *y, a;
extern unsigned int N;

inline void f1 ()
{
    int i;

    for (i=0; i<N; i++)
    {
        y[i] = a*x[i] + y[i];
    }
}

And the error that I get when I compile it it's that x,y,a and N are not defined. So, could someone help me please?

Body:
extern double *x, *y, a;
extern unsigned int N;

The extern qualifier for variables at global space tells the compiler/linker that the respective variable is defined in another preprocessing translation unit (another source file) instead of the actual one.

You have no other source file, so the compiler can´t find the definition of the variables anywhere. Thus, it throws an appropriate warning.

If you don´t have multiple source files, you don´t need to use the extern qualifier by *x, *y, a and N. Remove them.

Have a look at here:

How do I use extern to share variables between source files?


问:
Is it okay to use functions to stay organized in C?
I'm a relatively new C programmer, and I've noticed that many conventions from other higher-level OOP languages don't exactly hold true on C.

Is it okay to use short functions to have your coding stay organized (even though it will likely be called only once)? An example of this would be 10-15 lines in something like void init_file(void), then calling it first in main().

答:
I would have to say, not only is it OK, but it's generally encouraged.  Just don't overly fragment the train of thought by creating myriads of tiny functions.  Try to ensure that each function performs a single cohesive, well... function, with a clean interface (too many parameters can be a hint that the function is performing work which is not sufficiently separate from it's caller).

Furthermore, well-named functions can serve to replace comments that would otherwise be needed.  As well as providing re-use, functions can also (or instead) provide a means to organize the code and break it down into smaller units which can be more readily understood.  Using functions in this way is very much like creating packages and classes/modules, though at a more fine-grained level.


问:
codeigniter 3 URI routing - geting right hand side from given url
I need to simulate routing in Codeigniter 3, so my question is how to get the right-hand side from any URL programmatically?

for example, some routes that I have:

```<code>$route["blog"] = "Main/blog/en";
$route["blog/(:any)"] = "Main/blog/en/$1";
$route["novosti"] = "Main/blog/sr";
$route["novosti/(:any)"] = "Main/blog/sr/$1";
$route["contact"] = "Main/contact/en";
$route["kontakt"] = "Main/contact/sr";
</code>```

Now I need a function that can return right-hand side for a given URL part something like this:

```<code>echo $this->route->item("novosti/petar")
</code>```

should print then Main/blog/sr/$1 or Main/blog/sr/petar

Is there such a function in Codeigniter, because I can't find it in documentation?

UPDATE:
I am looking throughout system/router class and I see that protected function _parse_routes is doing something similar so if there is no function that can give me what I need I will create one based on this one.

答:
Codeigniter is simple, too simple... And because it is not obvious for me where that function is (if exists at all) I've just adopted _parse_routes to parse URL (slug) into the right-hand side from which I can find the corresponding file much easier.

Here it is (if someone gets in the same situation I was).

```<code>  function parseRoute($uri) {

    // Get HTTP verb
    $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';

    // Loop through the route array looking for wildcards
    foreach ($this->router->routes as $key => $val) {
      // Check if route format is using HTTP verbs
      if (is_array($val)) {
        $val = array_change_key_case($val, CASE_LOWER);
        if (isset($val[$http_verb])) {
          $val = $val[$http_verb];
        } else {
          continue;
        }
      }

      // Convert wildcards to RegEx
      $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

      // Does the RegEx match?
      if (preg_match('#^' . $key . '$#', $uri, $matches)) {
        // Are we using callbacks to process back-references?
        if (!is_string($val) && is_callable($val)) {
          // Remove the original string from the matches array.
          array_shift($matches);

          // Execute the callback using the values in matches as its parameters.
          $val = call_user_func_array($val, $matches);
        }
        // Are we using the default routing method for back-references?
        elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) {
          $val = preg_replace('#^' . $key . '$#', $val, $uri);
        }

        return $val;
      }
    }

    // If we got this far it means we didn't encounter a
    // matching route so we'll set the site default route
    return null;
  }
</code>```

Now, this:

```<code>echo parseRoute("novosti/petar")
</code>```

will produce:

```<code>Main/blog/sr/petar
</code>```

AKA: Controler class / function inside that controller / language param / blog article

Body:
I need to simulate routing in Codeigniter 3, so my question is how to get the right-hand side from any URL programmatically?

for example, some routes that I have:

$route["blog"] = "Main/blog/en";
$route["blog/(:any)"] = "Main/blog/en/$1";
$route["novosti"] = "Main/blog/sr";
$route["novosti/(:any)"] = "Main/blog/sr/$1";
$route["contact"] = "Main/contact/en";
$route["kontakt"] = "Main/contact/sr";

Now I need a function that can return right-hand side for a given URL part something like this:

echo $this->route->item("novosti/petar")

should print then Main/blog/sr/$1 or Main/blog/sr/petar

Is there such a function in Codeigniter, because I can't find it in documentation?

UPDATE:
I am looking throughout system/router class and I see that protected function _parse_routes is doing something similar so if there is no function that can give me what I need I will create one based on this one.

Body:
Codeigniter is simple, too simple... And because it is not obvious for me where that function is (if exists at all) I've just adopted _parse_routes to parse URL (slug) into the right-hand side from which I can find the corresponding file much easier.

Here it is (if someone gets in the same situation I was).

  function parseRoute($uri) {

    // Get HTTP verb
    $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';

    // Loop through the route array looking for wildcards
    foreach ($this->router->routes as $key => $val) {
      // Check if route format is using HTTP verbs
      if (is_array($val)) {
        $val = array_change_key_case($val, CASE_LOWER);
        if (isset($val[$http_verb])) {
          $val = $val[$http_verb];
        } else {
          continue;
        }
      }

      // Convert wildcards to RegEx
      $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

      // Does the RegEx match?
      if (preg_match('#^' . $key . '$#', $uri, $matches)) {
        // Are we using callbacks to process back-references?
        if (!is_string($val) && is_callable($val)) {
          // Remove the original string from the matches array.
          array_shift($matches);

          // Execute the callback using the values in matches as its parameters.
          $val = call_user_func_array($val, $matches);
        }
        // Are we using the default routing method for back-references?
        elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) {
          $val = preg_replace('#^' . $key . '$#', $val, $uri);
        }

        return $val;
      }
    }

    // If we got this far it means we didn't encounter a
    // matching route so we'll set the site default route
    return null;
  }

Now, this:

echo parseRoute("novosti/petar")

will produce:

Main/blog/sr/petar

AKA: Controler class / function inside that controller / language param / blog article


问:
How to install Homebrew Portable Ruby when your system version is too old
I'm trying to install Homebrew on macOS. Once the installation finished, when I typed help I got:

```<code>Already downloaded: /.../Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
Error: Checksum mismatch.
Expected: ab81211a2052ccaa6d050741c433b728d0641523d8742eef23a5b450811e5104
  Actual: f80fd4cc877a64b0ac1c105fdc212ca6be08bb034118900f2c2f58edcfe0dafd
 Archive: /.../Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
To retry an incomplete download, remove the file above.
Error: Failed to install Homebrew Portable Ruby (and your system version is too old)!
</code>```

答:
I fixed it.

First, I deleted the brew file that stores in the Library folder when using macOS.

The failure before was due to the connection. When I make sure the connection worked, it worked.

Body:
I'm trying to install Homebrew on macOS. Once the installation finished, when I typed help I got:

Already downloaded: /.../Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
Error: Checksum mismatch.
Expected: ab81211a2052ccaa6d050741c433b728d0641523d8742eef23a5b450811e5104
  Actual: f80fd4cc877a64b0ac1c105fdc212ca6be08bb034118900f2c2f58edcfe0dafd
 Archive: /.../Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
To retry an incomplete download, remove the file above.
Error: Failed to install Homebrew Portable Ruby (and your system version is too old)!


问:
SQL query to find minimum difference between row values of a column
I am trying to find the minimum difference between years for each ID. For example,

I have this data:

```<code>ID     year
1      2001
1      2001
2      2003
2      2004
2      2010
3      2000
</code>```

and I want this output:

```<code>ID    year
1      0
2      1
3      0
</code>```

答:
You could use a LEFT JOIN

```<code>SELECT Id,
       ISNULL(MIN(Def), 0) [Year]
FROM
(
  SELECT T.Id,
         TT.Year - T.Year Def
  FROM Data T
  LEFT JOIN Data TT ON T.Id = TT.Id AND T.Year < TT.Year
) T
GROUP BY Id;
</code>```

Online Demo

Body:
I am trying to find the minimum difference between years for each ID. For example,

I have this data:

ID     year
1      2001
1      2001
2      2003
2      2004
2      2010
3      2000

and I want this output:

ID    year
1      0
2      1
3      0

Body:
You could use a LEFT JOIN

SELECT Id,
       ISNULL(MIN(Def), 0) [Year]
FROM
(
  SELECT T.Id,
         TT.Year - T.Year Def
  FROM Data T
  LEFT JOIN Data TT ON T.Id = TT.Id AND T.Year < TT.Year
) T
GROUP BY Id;

Online Demo


问:
Should one table reference every other table
I'm working on an application that routinely gathers information from a large number of websites and saves it to a mysql database with a table for each site. The idea is to create a sort of customizable news feed.

stackoverflow_table(id, url, title, date)

reddit_table(id, title, url, author, date)

github_commit_table(id,
commit_message, author, repository,
branch, date)

twitter_table(id, tweet, author, url, date)

etc...

I want the ability to request any number of news items and filter out certain sites too. As an example:

Show newest 100 items but exclude items from Twitter and GitHub.

It seems like the best way to handle this is to create a table that just has foreign keys and website names.

master_table(id, website, date, foreign_key)

and I can just query the foreign ids I need from this table.

Am I going about this horribly wrong?

答:
A table for each site is the issue. As more sites come online you will end up with too many links.

Get a DBA/Architect involved to fix your data model and your links issue will disappear.


问:
Create a shape with libvips ruby
How can I convert a picture with any color and form to its shape? As an example I attached an input image and desired output. I need to make this conversion with ruby and ruby-vips gem. Generally speaking, I need to fill anything that is not alpha channel with single color.

答:
You can use [ ] to get bands out of an image. Your image is RGBA, so 0, 1 and 2 are R, G and B, and 3 is A.

alpha = image[3]
```

<p><img src="https://i.stack.imgur.com/kmylD.png" alt="alpha channel of sample image"></p>

<p>You can use ranges as well, so eg.:</p>

<pre class="lang-rb prettyprint-override"><code>rgb = image[0..2]
</code>```

Gets the first three bands. bandsplit breaks an image into an array of one-band images:

r, g, b, a = image.bandsplit
```

<p>The alpha has 0 for transparent and 255 for solid, and it sounds like you'd prefer the inverse of that. Therefore:</p>

<pre class="lang-rb prettyprint-override"><code>mask = image[3].invert
</code>```

Title: Create a shape with libvips ruby

Body:
You can use [ ] to get bands out of an image. Your image is RGBA, so 0, 1 and 2 are R, G and B, and 3 is A.

alpha = image[3]

You can use ranges as well, so eg.:

rgb = image[0..2]

Gets the first three bands. bandsplit breaks an image into an array of one-band images:

r, g, b, a = image.bandsplit

The alpha has 0 for transparent and 255 for solid, and it sounds like you'd prefer the inverse of that. Therefore:

mask = image[3].invert


问:
Running a Java Program from a Browser without Plugins
Some weeks ago I wrote a little Java program showing a real time plot chart (i.e. records with tracking of current time), with a modest 25 kbps streaming rate, 20fps framing rate, and no relevant user interaction. Thinking on its eventual deployment as a Mobile App, a Web Server chart or simply as a PC as a program, being Java a perfect selection for me.

Before starting now the Web version of this program, i started to investigate and according the 2016 Oracle White Paper Migrating from Java Applets to Plugin Free Java Technologies:

The deprecated plugin technology will be completely removed from the
  Oracle Java Development Kit (JDK) and Java Runtime Environment (JRE) in a
  future Java release (TBD). Java Web Start applications do not rely on a
  browser plugin and will not be affected by these changes.

And according 2019 OpenWebStart:

Java Web Start (JWS) was deprecated in Java 9, and starting with Java
  11, Oracle removed JWS from their JDK distributions

And then I get lost.

Hence, which is the way to go with a Java program like this? Which is the proper sustainable Java way to do this (and not subject to something like what happened to Java Web Start)?.

Or should I simply move to another way to do it, now that the program is in its infancy, considering I am not precisely a fan of Phyton, considering PHP is not precisely the way for programming these kind of numerical things, and considering I am not so willing to give Microsoft technologies a chance?

It is not a problem for me to adapt and rewrite my current code if necessary, but possibly not doing this every 5 years.

Which article would you recommend me to get back to the proper path?

Thanks in advance.

答:
I've missed the Java on browser too. Because of curiosity I just developed an "JVM-Server", which precompiles the Java-Classes to native JavaScript and serves them to a WebBrowser.
Look here is a Hello World example application which is running on Browser:
https://github.com/neo-expert/jsjvm_helloworld

There is also a Library which implements WebGL bindings. In the above Example is a WebGL demo included, where a 3D cube will be rendered. The code is written in pure Java.


问:
Clear FileUpload Content in ASP.NET
How to clear the textbox content in a FileUpload control in ASP.NET?</p>

答:
You can clear the FileUpload control with response redirect back to page itself and you can store the file in a session variable you can use on the next round trip.

```<code>protected void PageLoad(EventArgs e){

   if(FileUpLoadControlId.HasFile)
    {
        Session["data"]=FileUpLoadControlId.PostedFile.InputStream;

        Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri, true);
     }

    else if( Session["data"]!=null)
    {

         //massage data here

         //and post back
         Session["data"]=null;
         Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri, true);

    }

 }
</code>```

The response redirect reinitializes the view state

ContentLicense: CC BY-SA 2.5

Title: Clear FileUpload Content in ASP.NET

Body:
You can clear the FileUpload control with response redirect back to page itself and you can store the file in a session variable you can use on the next round trip.

protected void PageLoad(EventArgs e){

   if(FileUpLoadControlId.HasFile)
    {
        Session["data"]=FileUpLoadControlId.PostedFile.InputStream;

        Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri, true);
     }

    else if( Session["data"]!=null)
    {

         //massage data here

         //and post back
         Session["data"]=null;
         Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri, true);

    }

 }

The response redirect reinitializes the view state


答: If a group of statements can be thought of as a thing - then make them a function</p>

Title: Is it okay to use functions to stay organized in C?

Body:
I'm a relatively new C programmer, and I've noticed that many conventions from other higher-level OOP languages don't exactly hold true on C.

Is it okay to use short functions to have your coding stay organized (even though it will likely be called only once)? An example of this would be 10-15 lines in something like void init_file(void), then calling it first in main().


问:
Git keeps prompting me for a password
I've been using Git for a while now, but the constant requests for a password are starting to drive me up the wall.

I'm using Mac OS X and GitHub, and I set up Git and my SSH keys as instructed by GitHub's Set Up Git page.

I've also added the github SSH key to my Mac OS X keychain, as mentioned on GitHub's SSH key passphrases page. My public key is registered with Git.

Nevertheless, every time I try to Git pull, I have to enter my username and password. Is there something other than an SSH key that I need to set up for this?

答:
If you have SSH agent set up, you can also add this to your ~/.gitconfig to force git to use SSH for all GitHub repos rather than HTTPS:

```<code>[url "ssh://git@github.com/"]
    insteadOf = git://github.com/
    insteadOf = https://github.com/
</code>```

(If you're mostly working with public repos, you can also use pushInsteadOf rather than insteadOf, as reading from a public repo can be done without authentication).

Title: Git keeps prompting me for a password

Body:
If you have SSH agent set up, you can also add this to your ~/.gitconfig to force git to use SSH for all GitHub repos rather than HTTPS:

[url "ssh://git@github.com/"]
    insteadOf = git://github.com/
    insteadOf = https://github.com/

(If you're mostly working with public repos, you can also use pushInsteadOf rather than insteadOf, as reading from a public repo can be done without authentication).