198

You could simply implement the class_weight from sklearn: Let's import the module first from sklearn.utils import class_weight In order to calculate the class weight do the following class_weights = class_weight.compute_class_weight('balanced', np.unique(y_train), ...


183

If you are talking about the regular case, where your network produces only one output, then your assumption is correct. In order to force your algorithm to treat every instance of class 1 as 50 instances of class 0 you have to: Define a dictionary with your labels and their associated weights class_weight = {0: 1., 1: 50., ...


39

Just add weights based on your time labels to your xgb.DMatrix. The following example is written in R but the same principle applies to xgboost on Python or Julia. data <- data.frame(feature = rep(5, 5), year = seq(2011, 2015), target = c(1, 0, 1, 0, 0)) weightsData <- 1 + (data$year - max(data$year)) * 5 * 0.01 ...


35

I use this kind of rule for class_weight : import numpy as np import math # labels_dict : {ind_label: count_label} # mu : parameter to tune def create_class_weight(labels_dict,mu=0.15): total = np.sum(list(labels_dict.values())) keys = labels_dict.keys() class_weight = dict() for key in keys: score = math.log(mu*total/float(...


21

On Python you have a nice scikit-learn wrapper, so you can write just like this: import xgboost as xgb exgb_classifier = xgb.XGBClassifier() exgb_classifier.fit(X, y, sample_weight=sample_weights_data) More information you can receive from this: http://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.XGBClassifier.fit


15

Class weights do help with the imbalance problem ("resolve" seems too much), but upsampling has a certain advantage on it. If you think about it, downsampling/upsampling the number of samples in each class to balance the dataset is almost exactly the same as using class weights. For example, say you have a dataset containing 3 samples divided into 2 ...


11

You could try building multiple xgboost models, with some of them being limited to more recent data, then weighting those results together. Another idea would be to make a customized evaluation metric that penalizes recent points more heavily which would give them more importance.


10

class_weight is fine but as @Aalok said this won't work if you are one-hot encoding multilabeled classes. In this case, use sample_weight: sample_weight: optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to ...


10

Is this approach better than the mere augmentation or just the use of class weights ? Note that data augmentation is the process of changing the training samples (e.g. for images, flipping them, changing their luminosity, adding noise, etc.) and adding them back into the set. It is used for enriching the diversity of training samples, thus, in this aspect ...


9

Adding to the solution at https://github.com/keras-team/keras/issues/2115. If you need more than class weighting where you want different costs for false positives and false negatives. With the new keras version now you can just override the respective loss function as given below. Note that weights is a square matrix. from tensorflow.python import keras ...


8

I'm not sure I completely understand what you want but it looks like you're trying to find an implementation for sample_weight. Well, I had tried something similar. Before I go into it, I want to mention that my error actually went up - I haven't had the time to go into why that happened. For simplicity, lets say you know the weights you need for each ...


7

The reason for weights in machine learning is actually a lot easier than it seems. It's the way by which our model learns some underlining function and performs the classification or regression. We tune these weights in order to model some underlining function which can map our input to a desired output. Either a class in classification, or a range of values ...


7

PCA is unsupervised method for finding the most important components. I don't see a reason why you should want add a weight. If you know what features are important, why use PCA at all? Or perform PCA on the features where you are unsure about the importance. Further, components are created in directions with highest variance and the importance is measured ...


6

Yes it is definitely possible to calculate optimised weightings provided you have some training examples where you know the document fields, the query, and either the outcome (relevant/not-relevant) or the desired score. I think your training feature set should be the query score in range [0.0,1.0] for each field of each example. The training label should ...


5

The final range of emotion is completely arbitrary. No matter the interval [a, b], you can adjust the emotions to fit inside. [-100, 100] is perfectly reasonable and is common. An example of use is from GDELT, which provides this interval for average tone of news documents. Asking if equally distancing the emotions is statistically correct does not make ...


5

As I understand it, this option only calculates the loss function differently without training the model with weights (sample importance) so how do I train a Keras model with different importance (weights) for different samples. when the loss function is calculated differently that means the backprop will behave differently (more emphasis to important ...


4

In general, if you want to automate fine tuning a model's hyper parameters, its best to use a well tested package such as caret or MLR. I've used the caret package extensively. Here is a reference of the parameters supported by caret for tuning a xgboost model. To automatically select parameters using caret, do the following: First define a range of ...


4

After standardizing your data you can multiply the features with weights to assign weights before the principal component analysis. Giving higher weights means the variance within the feature goes up, which makes it more important. Standardizing (mean 0 and variance 1) is important for PCA because it is looking for a new orthogonal basis where the origin ...


4

Predicting and scoring are two different tasks. And according to your answers and comments you are not solving prediction problem. You just want to set to each student a number in range [1,100] according to some rule. This is ranking (or scoring, whatever). Therefore, the terms #prediction_model, #accuracy, #validation, #training_set are out of this scope. ...


4

Here's a one-liner using scikit-learn: from sklearn.utils import class_weight class_weights = dict(zip(np.unique(y_train), class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)))


3

Let $v_i$ be the updated $w_i$, then $$v_i = \frac{w_i}{\sum_{j\neq 4}w_j}=\frac{w_i}{1-w_4}$$ In general, if $w_j$ is removed, then $$v_i =\frac{w_i}{1-w_j}$$ Notice that if $w_j=1$, then all the remaining $w_i=0$, $i \neq j$.


3

Yes, this is a well-studied problem: rank aggregation. Here is a solution with code. The problem is that the quantity you are trying to estimate, the "score" of the item, is subject to noise. The fewer votes you have the greater the noise. Therefore you want to consider the variance of your estimates when ranking them.


3

You can try to compute class weights and assign these values to model via weight classes function. One more reminder about weights; probably major classes weight will be less than 1 so you need to round it to 1 otherwise model won't learn major class this time.


3

I wansn't able to use the class_weight parameter yet, but in the mean time i've found another way to apply class weighting to each output layer. Current solution In this keras issue they have supplied an easy method to apply class weights via a custom loss that implements the required class weighing. def weighted_categorical_crossentropy(y_true, y_pred, ...


3

If you assign to each instance the weight of the corresponding class the effect will be similar and sometimes exact. I say similar because there are methods which uses sample weighting intrinsically, for example AdaBoost or almost all the types of decision trees, but they do perform also some arithmetics with those weights which can lead to different results....


2

There is an application of tf-idf on the sklearn website. sklearn handles sparse matrices for you, so I wouldn't worry about it too much: Fortunately, most values in X will be zeros since for a given document less than a couple thousands of distinct words will be used. For this reason we say that bags of words are typically high-dimensional sparse ...


2

Have a look at the example on "How to order Reddit comments" using their up- & down-votes in Cam Davidson Pilon's book. $$ \frac{a}{a+b} - 1.65\sqrt{\frac{a b}{(a+b)^2(a+b+1)}} $$ where $$ a = 1 + u $$ $$ b = 1 + d $$ $u$ is the number of yes votes and $d$ is the number of no votes. Sorting your data using the score obtained from that formula results ...


2

Here the function used for $p_{i}$ is sigmoid function. So, $p_{i}$ = $\frac{1}{1+e^{-\Sigma w_{j} ∗ x_{i}^j}}$ $\frac{∂p_{i}} {w_{j}}$= $\frac{-1}{(1+e^{-\Sigma w_{j} ∗ x_{i}^j})^2}$ ∗ $e^{-\Sigma w_{j} ∗ x_{i}^j}$ ∗ $(-x_{i}^j)$ $\frac{∂p_{i}} {w_{j}}$ = $\frac{1}{(1+e^{-\Sigma w_{j} ∗ x_{i}^j})}$ ∗ $\frac{e^{-\Sigma w_{j} ∗ x_{i}^j}}{...


1

grid_result = grid.fit(X_train, y_train, clf__class_weight={0:0.95, 1:0.05}) FYI, per the docs fit_params should no longer be passed to the GridSearchCV constructor as a dict, but should be passed directly to fit as above. http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html


1

If you have a date variable (or something similar), you can create a weight using this. If you're using XGBoost, there is an option to specify a weight for each instance when creating the DMatrix - feed your observation weighting in here.


Only top voted, non community-wiki answers of a minimum length are eligible