concept weight in category machine learning

appears as: weight, weight, weights, weights, The weights, The weight
Grokking Machine Learning

This is an excerpt from Manning's book Grokking Machine Learning.

Features are simply the columns of the table. In our pet example, the features may be size, name, type, weight, etc. This is what describes our data. Some features are special, though, and we call them labels.

def lr_trick(weights, bias, features, label, learning_rate = 0.01):
    pred = lr_prediction(weights, bias, features)
    for i in range(len(weights)):
        weights[i] += (label-pred)*features[i]*learning_rate
        bias += (label-pred)*learning_rate
    return weights, bias
 
def lr_algorithm(features, labels, learning_rate = 0.01, epochs = 200):
    weights = [1.0 for i in range(len(features.loc[0]))]
    bias = 0.0
    errors = []
    for i in range(epochs):
        draw_line(weights[0], weights[1], bias, color='grey', linewidth=1.0, linestyle='dotted')
        errors.append(total_error(weights, bias, features, labels))
        j = random.randint(0, len(features)-1)
        weights, bias = perceptron_trick(weights, bias, features.loc[j], labels[j])
    draw_line(weights[0], weights[1], bias)
    plot_points(features, labels)
    plt.show()
    plt.scatter(range(epochs), errors)
    plt.xlabel('epochs')
    plt.ylabel('error')
    return weights, bias

In Chapters 4 and 5 we managed to infer things about the language based on the equations in the classifiers. For example, if the weight of the word ‘aack’ was positive, we concluded that it was likely a happy word. What about now? Could we infer anything about the language in this classifier that contains two equations?

Both of these notations are used in the literature, but the one in the right is more common.
Right: The same perceptron with a bias unit. The value of the bias comes in as the weight of the bias node.
Left: A perceptron with a threshold. The value of the threshold goes inside the node.
Figure 8.14.
Machine Learning Bookcamp

This is an excerpt from Manning's book Machine Learning Bookcamp.

Let's consider the first feature: horsepower. The weight for this feature is 0.01, which means that for each extra unit of horsepower, we adjust the baseline by adding 0.01. Because we have 453 horses in the engine, we add 4.53 to the baseline: 453 horses ⋅ 0.01 = 4.53.

{'contract=month-to-month': 0.563,
 'contract=one_year': -0.086,
 'contract=two_year': -0.599,
 'dependents=no': -0.03,
 'dependents=yes': -0.092,
 ... # the rest of the weights is omitted
 'tenure': -0.069,
 'totalcharges': 0.0}

We also know the weights w1, w2 and w3 that correspond to contract=month-to-month, contract=one_year, and contract=two_years (figure 3.33).

Figure 3.33 The weights of the contract=month-to-month, contract=one_year, and contract=two_years features

To make a prediction, we perform the dot product between the feature vector and the weights, which is multiplication of the values in each position and then summation. The result of the multiplication is 0.91: which turns out to be the same as the weight of the contract=month-to-month feature (figure 3.34).

As we see, during the prediction, only the weight of the hot feature is taken into account, and the rest of the weights are not considered in calculating the score. This makes sense: the cold features have values of 0, and when we multiply by 0, we get 0 again (figure 3.36).

Figure 3.36 When we multiply the one-hot encoding representation of a variable by the weight vector from the model, the result is the weight corresponding to the hot feature.

The interpretation of the signs of the weights for one-hot encoded features follows the same intuition as the bias term. If a weight is positive, the respective feature is an indicator of churn, and vice versa. If it's negative, it's more likely to belong to a non-churning customer.

Let's look again at the weights of the contract variable. The first weight for contract=month-to-month is positive, so customers with this type of contract are more likely to churn than not. The other two features, contract=one_year and contract=two_years have negative signs, so such clients are more likely to remain loyal to the company (figure 3.37).

Figure 3.37 The sign of the weight matters. If it's positive, it's a good indicator of churn; if it's negative, it indicates a loyal customer.

The magnitude of the weights also matters. For two_year, the weight is -1.404, which is greater in magnitude than -0.144 — the weight for one_year. So, a two-year contract is a stronger indicator of not churning than a one-year one. It confirms the feature importance analysis we did previously. The risk ratios (the risk of churning) for this set of features are 1.55 for monthly, 0.44 for one-year, and 0.10 for two-year (figure 3.38).

Figure 3.38 The weights for the contract features and their translation to probabilities. For contract=two-year, the weight is -1.404, which translates to very low probability of churn. For contract=one-year, the weight is -0.144, so the probability is moderate. And for contract=month-to-month, the weight is 0.910, and the probability is quite high.

Now let's have a look at the numerical features. We have two of them: tenure and total charges. The weight of the tenure feature is -0.097, which has a negative sign. This means the same thing: the feature is an indicator of no churn. We already know from the feature importance analysis that the longer clients stay with us, the less likely they are to churn. The correlation between tenure and churn is -0.35, which is also a negative number. The weight of this feature confirms it: for every month that the client spends with us, the total score gets lower by 0.097.

The other numerical feature, total changes, has weight 0. Because it's 0, no matter what the value of this feature is, the model will never consider it, so this feature is not really important for making the predictions.

Ensemble Methods for Machine Learning

This is an excerpt from Manning's book Ensemble Methods for Machine Learning.

For instance, when classifying abalone (sea snails), the features size and weight are highly correlated (unsurprising, since bigger snails will be heavier). This means that including them in a decision tree will add roughly the same amount of information, and cause the overall impurity to decrease by roughly the same amount. Thus, we expect that their mean impurity decrease scores will be the same.

However, say we select weight first as a splitting variable. Adding this feature to the tree removes information contained in both size and weight features. This means that the feature importance of size is reduced because any impurity that size could decrease was already previously decreased by adding weight. This results in imbalanced feature importances. Random feature selection mitigates this problem a little, but not consistently.

Intuitively, weights reflect the relative importance of training examples. Misclassified examples have higher weights, while correctly classified examples have lower weights.

When we train the next base estimator sequentially, the weights will allow the learning algorithm to prioritize (and hopefully fix) mistakes from the previous iteration. This is the “adaptive” component of AdaBoost, which ultimately leads to a powerful ensemble.

The base estimator weight (αt) can also be used to update the weights of each training example. AdaBoost updates example weights as

When examples are correctly classified, the new weight is decreased by . Stronger base estimators will decrease the weight more because they are more confident in their correct classification.

Machine Learning with R, the tidyverse, and mlr

This is an excerpt from Manning's book Machine Learning with R, the tidyverse, and mlr.

diabetesTask

Supervised task: diabetesTib
Type: classif
Target: class
Observations: 145
Features:
   numerics     factors     ordered functionals
          3           0           0           0
Missings: FALSE
Has weights: FALSE
Has blocking: FALSE
Has coordinates: FALSE
Classes: 3
Chemical   Normal    Overt
      36       76       33
Positive class: NA

After the creation of the map, the next thing the algorithm does is randomly assign each node a set of weights: one weight for each variable in the dataset. So for our example, each node has three weights, because we have three variables. These weights are just random numbers, and you can think of them as guesses for the value of each of the variables. If this is hard to visualize, take a look at figure 15.4. We have a dataset containing three variables, and we are looking at three nodes from a map. Each node has three numbers written under it: one corresponding to each variable in the dataset. For example, the weights for node 1 are 3 (for var 1), 9 (for var 2), and 1 (for var 3). Remember, at this point these are just random guesses for the value of each variable.

Next, the algorithm chooses a case at random from the dataset and calculates which node’s weights are the closest match to this case’s values for each of the variables. For example, if there were a case in the dataset whose values for var 1, var 2, and var 3 were 3, 9, and 1, respectively, this case would perfectly match the weights of node 1. To find which node’s weights are most similar to the case in question, the distance is calculated between each case and the weights of each node in the map. This distance is usually the squared Euclidean distance. Remember that Euclidean distance is just the straight-line distance between two points, so the squared Euclidean distance just omits the square root step to make the computation faster.

In figure 15.4, you can see the distances calculated between the first case and each of the node’s weights. This case is most similar to the weights of node 1, because it has the smallest squared Euclidean distance to them (93.09).

Figure 15.4. How distances between each case to each node are calculated. The arrows pointing from each variable to each node represent the weight for that variable on that particular node (for example, the weights of node 1 are 3, 9, and 1). Distance is calculated by finding the difference between a node’s weights and a case’s value for each variable, squaring these differences, and summing them.