TypeError: Expected int32, got None of type 'NoneType' instead

The error is raised by tf.zero() because the shape parameter should be a list of ints.
Indeed, as you build the computation graph, batch size is not (yet) know, unless you explicitly specify if.
Sometimes you need to specify shape information manually when working with Tensorflow Keras API.
One possible fix is to use tf.shape() function instead of shape attribute:
   b=tf.zeros(shape=[tf.shape(inputs)[0] ,self.num_capsule,1,self.input_num_capsule])

This line will run without error, but now the batch dimension of b is defined by a computation. It is dynamic and Keras doesn't know anymore, that it equals the batch size.
Maybe because of this softmax(b, axis=1) does not work anymore. Interestingly softmax() with the default axis setting works. So as a workaround we can juggle dimensions back and forth:
    c=tf.transpose(tf.nn.softmax(tf.transpose(b,perm=[0,2,3,1])),perm=[0,3,1,2])

To make the whole code work, you also need to specify shape information more precisely at the beginning of the code:
    reshape2=L.Reshape([6*6*128//8,8])(conv2)

Also you might notice, that all shape information is lost after the CapsuleLayer. You can check it with digitcaps.shape. But we can re-specify it:
    digitcaps = L.Reshape([3,16])(digitcaps)


How to calculate perplexity of language model?

I believe he meant: you need to identify/predict a sequence of 4 consecutive things. First: an operator, then a sales person, then a technical support person, and finally one name out of 30,000 names. One and only one sequence is correct.

The probability of the correct sequence:

$$(1/4)*(1/4)*(1/4)*(1/120,000) = 0.0000001302083333$$

If you get the 4th root, that gives you the geometric mean (in some sense that's the average per step for four steps)

$$(0.0000001302083333)^.25 = 0.01899589214 ≈ (1/53)$$

So:

$$(1/53)*(1/53)*(1/53)*(1/53) ≈ (1/4)*(1/4)*(1/4)*(1/120,000)$$

It was however, not clear in the slides or the explanation.


Time-series decomposition to a base level and an effect of another feature

You are describing multivariate time series analysis, modeling the interactions and comovements among a group of time series variables.

You can start with a vector autoregression (VAR) model, one of the most successful, flexible, and easy to use models for
the analysis of multivariate time series. Then work through the decomposition of time series.

You'll have to decide if you want assume if all components time series are stationary and ergodic. Each component will have autocovariances and autocorrelations but there are also cross lead-lag covariances and correlations between all possible pairs of components.


Classification for 'not something' Neural Networks

Your problem resembles the learning task of 'one-class classification', otherwise known as anomaly detection. Essentially, you have a set of images all belonging to one class (galaxies), and you want a model to tell you if a new image is a galaxy or not, without providing any counter-examples to train with.

You should try a recently developed method called Deep Support Vector Data Description. In this method, a transformation is learned by a neural network to map the inputs to points contained in a small hypersphere in latent space. Then, examples falling inside the hypersphere can be considered 'galaxies', and outside can be considered 'not galaxies'.

You can read more about it here (quite technical), or just try to use the authors' PyTorch implementation here.


How to avoid overfitting in random forest?

Relative to other models, Random Forests are less likely to overfit but it is still something that you want to make an explicit effort to avoid. Tuning model parameters is definitely one element of avoiding overfitting but it isn't the only one. In fact I would say that your training features are more likely to lead to overfitting than model parameters, especially with a Random Forests. So I think the key is really having a reliable method to evaluate your model to check for overfitting more than anything else, which brings us to your second question.

As alluded to above, running cross validation will allow to you avoid overfitting. Choosing your best model based on CV results will lead to a model that hasn't overfit, which isn't necessarily the case for something like out of the bag error. The easiest way to run CV in R is with the caret package. A simple example is below:

> library(caret)
>
> data(iris)
>
> tr <- trainControl(method = "cv", number = 5)
>
> train(Species ~ .,data=iris,method="rf",trControl= tr)
Random Forest

150 samples
  4 predictor
  3 classes: 'setosa', 'versicolor', 'virginica'

No pre-processing
Resampling: Cross-Validated (5 fold)

Summary of sample sizes: 120, 120, 120, 120, 120

Resampling results across tuning parameters:

  mtry  Accuracy  Kappa  Accuracy SD  Kappa SD
  2     0.96      0.94   0.04346135   0.06519202
  3     0.96      0.94   0.04346135   0.06519202
  4     0.96      0.94   0.04346135   0.06519202

Accuracy was used to select the optimal model using  the largest value.
The final value used for the model was mtry = 2.


What is semantic gap? Why it exists in AI

Any ML model assumes a particular representation of the data, and connects bits of information only within this representation.
For example a linear regression model assumes a linear relation between the features and the target variable. A Naive Bayes model assumes that the features are independent of each other. And these are only the most obvious kind of simplifications made by ML models.
Naturally this results in different models representing data differently. Any semantic interpretation based on some model outcome is potentially biased by the assumptions made by the model.


Many questions training unbalanced and duplicated data

Repeated entry is as good as you are adding weighted samples i.e. those samples will have extra weights in the learning processes.
 So, you should look into the data and decide if that weight is valid for those samples. Otherwise, it might overfit the Model
On Oversample, Undersample
 This is a very broad question. Please do read some relevant blogs/SE posts.
 You should also try
- Both, Over-sampling and Under-sampling together i.e. Hybrid approach
- Adding appropriate class-weight to the learning i.e. Cost-sensitive learning
You may start here -
Python Library
Blog
Blog
Book


There are few ways to create your own dataset or to update already existing one.

By yourself

This way assumes that you have a microphone (at least one). To simplify your recording experience, record the files where you repeat each command. One unique command per one file. Extract the data then. The basic pipeline should be something like this.

Collect data

Record your voice commands in one file for each command. If you use python, pay attention on pyaudio library. While recording do pauses between the words that you repeat. You may try different microphones and different locations. Also, you may ask your friends or relatives to help you with the task.
Read the recording and split it into smaller files with only one word each. For this task you may use VAD - voice audio detector software, like webrtc-vad. There is a wrapper for Python.
To normalize your data, you would want to append your audio files to the same length. In a case of .wav files, append zeros in the beginning and end of the file.

Data augmentation

Add some noise to your data. It may be white noise, randomly generated through the code, or you may just record silence, street noise, quiet conversation etc. Add the noise to your previously recorded data afterwards.

Although, this may not be the best way, but it will work just fine in a case where you.

Extract data

For much bigger dataset, you would probably like to use Simple Audio Indexer to index audio files. It may help you to find timestamps of the specific words or phrases.

Use it on audio recordings, that are available on the internet, like audiobooks (the sound quality is usually good, but the recordings are usually denoised - not like in real life recordings) or open voice datasets (some are listed here).

In the end, you may combine the two ways to solve your problem.


How to find optimal sample weights in binary classification/regression problem

There are a few things which are unclear so I am going to have to make some assumptions.

You say it is binary classification/regression. Are you trying to find the probability of each binary class?

When you say "future data", do you mean future training data or future test data? If it is test data then you do not need to worry at all about his effect. If it is future training data then yes you may have a problem. You do not want to do model building on a training set that is different from the training set you would actually use in the productionized model. I would suggest you produce a number of validation plots for this case.

If you really are doing binary classification. Most models in python have a method (eg predict_proba() ) that will produce a probability and will let you decide on class based on that probability. The default being 50%. For this reason I do not think that you want to have the weighted average be 0.5 but the "probability" represent the true probability. Please refer to this tutorial http://scikit-learn.org/stable/modules/calibration.html

As to your original question. You want the weights to be $M/N_i$ where $N_i$ is the number of samples in each class $i$ and $M$ the total number of samples. This is a way to do some sort of pseudo-upsampleing and have the classes be weighted by the number of samples.


How to estimate the transition probabilities for a Markov Chain when time intervals are non-equally spaced

With the limited information that you have given, it is not possible to judge the trade-off between different approaches of handling time here. I can propose the following alternatives.

Check if you can discretize the time information and then use transition to same state assumption. For example, let us say that transition from A to B took place at 30 minutes. If you have standard time interval of 10 minutes, you can say that for interval 1 & 2 (10 minutes, 20 minutes), the transition was from state A to A whereas in the third interval the transition was from A to B. There will be problems though when multiple transitions take place in a given interval. You can either make the interval small enough or distribute the transition probability equally in such case.
Is the time information significant to whatever that you are trying to model? Otherwise you can ignore the time information.


Time series forecasting dilemma. Could feature engineering overcome time dependency?

They all start from the same assumption: time series forecasting can't be treated as a regression/classification problem. It is time dependent, which means our target y at time t depends on what the value y was at t-1.

Time series forecasting must take into account time dependency, but it doesn't have to be the only source of information. Many complex forecasting tasks take into account external variables and time dependency altogether.

What prevents me to use some basic feature engineering and transform
the example data as follow?
| timestamp | value_at_t_minus_7 | day_of_week | value |

Nothing prevents you from doing that. Moreover, that value_at_t_minus_7 is time dependency. day_of_week can also be thought as a simplification, or summarization of time dependency information - because studying seasonality we concluded that x can be explained with the value of one week ago. It's another way to make time explain the output.
When working with regressions (such as ARIMA or Tree-based models) you have to manually specify what is the shape of time dependency. Therefore the method you proposed is perfectly "legal" and actually very common. Good luck with your task.


BPTT is the process of backpropagating the gradients calculations (chain rule) of a loss function at each time step in a recurrent network. The parameters $U$ (input weights) and $W$ (recurrent connection weights) will have a contributions to the gradient at each time step so the total gradient will be the sum of each contribution.

The vanishing gradient is a generic issue in neural networks of having decaying gradients values over the parameters of the network, meaning that updates for some parameters will be very small and hard to propagate. This gradient-based learning problem may occur with deep networks, vanilla recurrent networks (because of the large product of contributions to the gradient over timesteps) and often raised by the inherent nature of some activation functions.


PCA vs.KernelPCA: which one to use for high dimensional data?

It can be hard to choose - because it's hard to visualize. However, you probably have a specific goal right? Maximizing some kind of score.

Why don't you try a Grid Search applied to your dimensionality reduction decision? See this.

I'm interested in reading other, more theoretical answers to this question, though.


Online vs Batch Learning in Latent Dirichlet Allocation using Scikit Learn

With batch you feed the entire data through each EM iteration. In the online implementation you feed only some of the data through each EM iteration (a "mini-batch"). From the sklearn user guide:

  While the batch method updates variational variables after each full pass through the data, the online method updates variational variables from mini-batch data points.

From the paper about online LDA:

  We then update $\lambda$ using a weighted average of its previous value and $\tilde\lambda$.
  The weight given to $\tilde\lambda$ is given by $\rho_{t} = (\tau_{0} + \tau)^{-\kappa}$, where $\kappa$ ∈ (0.5, 1] controls the rate at which
  old values of $\tilde\lambda$ are forgotten and $τ_{0} \ge 0$ slows down the early iterations of the algorithm.

In the sklearn implementation:

weight = np.power(self.learning_offset + self.n_batch_iter_, -self.learning_decay)

So learning_offset is $\tau_{0}$ which slows down early iterations, and learning_decay is $\kappa$ which controls rate at which old weights are forgotten.


Would keeping all punctuation make any sense in word2vec?

You are correct to keep punctuation if you want to be able to predict it.

Tokenization of your input should actually work for any character, be it a letter or punctation. In fact there have already been exmaples of people modelling mathematics using Word2Vec and then generating very realistic maths, via $\LaTeX$ !  Look at the subsection called Algebraic Geometry in Karpathy's now famous blog post.

There is a good note on the matter here, whcih a specific example given in seq2seq learning (basically translation within the realm of NLP). Be sure to read the comments on the accepted answer there.

to answer your final question, I don't think it would be possible to use your generated model to place to required punctuation back into your model, as something like an LSTM would not have a representation for, say a comma, as it had never seen one.


Filtering detail table based on selection in summary table in Orange 3.11

A workflow like this might do the trick, although it is static (does not adjust to varying actions).

Load the two files, Summary and Details.
I suppose "Details" has columns such as "Action" and some value. In my example, Details will contain "Subject" and "Grade" for 3 highschool subjects.
"Select columns" is optional (slim down unused columns)
You need a "Select rows" for each subject/action, filtering that specific one.
Every "Edit Domain" is used to rename the generic feature name to a specific on (like "Grade" to "Grade_Math").
Results are then merged step-wise to the Summary (which in my example contains students, your summary contains some other people).

If the number of actions is limited, the time needed is reasonable.

If this is a one-time job, you might as well save the output (CSV) and use that in a different project.

If it needs to be more dynamic and adaptive, you'll have to dig into Python and use the Python code widget to create a list of actions, create a list of columns based on that, and then populate those with matching values.


reinforcement learning: Decompose a policy gradient

The Medium post has brackets in the wrong place . . . the second expectation must be inside the sum to make sense, otherwise $t$ is not defined. You can see a couple of steps later that $Q(s_t, a_t)$ gets magically moved back inside the sum.

I cannot see a way to fix the first expectation though that uses $t$ before it is defined!

The expectations are trying to show which parts of the distribution of trajectories each calculation explicitly depends upon. I don't think you need to read into it anything more than that.

However, I would suggest you find another article which does not have these errors. There are similar derivations of Policy Gradient in Reinforcement Learning: An Introduction


does accuracy and performance depends on whole dataset or fraction of that data mimics the whole?

This is actually a pretty complex question that has to do with many facets of how information entropy is carried in a dataset and how your learning algorithms will make use of the information.

Machine Learning is Optimization

Machine learning at its core is just finding the ideal parameter values in order for a specific function to be represented. This means you will use the data in your dataset and some framework in order to tune these parameters. Learning is an iterative process, as you can see providing one instance to your algorithm will not result in very good results. However, there comes a point when you have provided sufficient data that the information contained in that subset is representative of the information contained in the entire dataset.

You can see this in practice, when training your model you will notice that performance starts to converge to a specific value. If you had infinite data this value would not get any better. This is caused by limitations in your data and not your model (machine learning algorithm). This is the best the specific model you are using can do with that kind of data.

How to get better performance?

Assuming you still have more data but performance is no longer improving, you will want to consider a more complex model. This model will likely be able to extract information from your dataset that is too subtle for your simpler model. This is why deep learning outperforms most classical machine learning algorithms when big data is available. Deep learning makes use of very high complexity to approximate functions very accurately, however, the tradeoff is that a lot of data is needed.

Actually my dataset is quite limited, can I improve my performance?

You actually can! This goes back to the core of machine learning, the dataset you are using. The dataset you have constructed has a certain information entropy based on the features you selected. By increasing the information entropy in your dataset, the complexity of your model can be reduced and this will lead to better performance.

Intuitively, if you are trying to classify dog/cat, but your dataset contains information about the weather the day the measurements were taken, this will reduce the entropy of the information for classifying dog/cat, thus obscuring your other features and reducing performance.

Furthermore, sometimes you may overlook the importance of a certain feature, or overlooking the potential of performing transformations to your features to better suit your model. For example, if you are using linear regression to determine points on 2 circles (radius=1, radius=4). Performing linear regression on the Cartesian plane will lead to very poor results, essentially 50%. However, transforming your $x, y$ coordinates into polar coordinates will lead to 100% performance as they will be perfectly separable. This is a simple application of the kernel trick.


Text summarization with limited number of words

You sure can,
for example in latent semantic analysis you can fixate number of topics (which is actually size of the decomposition matrix) beforehand.


Python & Pandas : (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' (timed out)")

The following works for me:

df.to_sql(con="mysql://user:password@localhost/database_name, name='myTable2', if_exists='replace')


Media's explanation is true for regression problems. These are problems where you predict a continuous target variable.

Your image shows a classification problem. Here, the target variable takes only two values (typically -1 and 1 in the Perceptron algorithm).
In that case, an optimal solution $w^*$ is a vector of weights that perfectly separates both classes.
If such a solution exists, the Perceptron algorithm will find it. But: If there is one optimal solution, there are usually infinitely many other optimal solutions. You can easily see this in your image: You can move the line a little to the left or to the right, and you can rotate it a little, and it still perfectly separates the classes.

So while the Perceptron algorithm will find an optimal solution if there is one, you cannot know which one it will find. That depends on the random starting parameters.

This is different e.g. for support vector machines. Here, there is either no optimal solution or exactly one optimal solution.


XGBoost Feature importance - Gain and Cover are high but Frequency is low

I just want to know why it's not considered an important feature based on the frequency metric.

Most likely, the variable gender has much smaller number of possible values (often only two: male/female or 0/1, depending on the representation) compared to other predictors in your dataset.

If gender is just binary in your case, it means that it can be used at most once in each tree, while, let say, age might appear much more often on different levels of the trees.


How does $\chi^2$ feature selection work?

Just go inside the chi2 function

It calculates chi2 values for Y's based on the contingency table then computes p-values for each feature


predicting probability distribution for time series

Yes, you can use scikit-learn for this use case. You may find this tutorial useful.


How do the linear layers in the attention mechanism work?

Your understanding is not correct.
The relevant information is described in the original paper in section 3.2.2:
The three sets of projection matrices you are referring to are $W^Q_i \in \mathbb{R}^{d_{model} \times d_k}$ for the Queries, $W^K_i \in \mathbb{R}^{d_{model}\times d_k}$ for the Keys and $W^V_i \in \mathbb{R}^{d_{model}\times d_v}$ for the Values. Notice that the $i$ subindex in the matrix names refers to the attention head, indicating that there is a different matrix for each attention head.
The final projection matrix is $W^O \in \mathbb{R}^{hd_v \times d_{model}}$.
Given the number of attention heads, $h = 8$, the dimensions of the matrices are defined by $d_k=d_v=d_{model}/h=64$.
The three sets of matrices project the embedding dimensionality $d_{model}$ into a space with 8 times smaller dimensionality ($d_k=d_v=d_{model}/8$). However, note that for each of $W^K$, $W^V$ and $W^Q$ there are 8 matrices (one per attention head) and, analogously, 8 scaled dot products are computed. The results of the dot products are 8 vectors of dimensionality $d_{model}/8$; those 8 vectors are concatenated (see figure below), giving a tensor with the original dimensionality $d_{model}$.
Then, the final matrix multiplication by $W^O$ doesn't change the dimensionality, obtaining again the original one.

About your second question, either of the approaches you describe (averaging and using an RNN) is technically feasible, but what people normally do when using transformers for classification is to use BERT's approach, that is, adding a special token [CLS] at the beginning of the sequence and using the output at that position for the classification, just projecting with a matrix multiplication into a space with dimensionality equal to the number of classes.


I have less than enough karma to leave a comment, but I'd like to support Derek O's assessment but also add 1 more point: if your 50K observations are repeated measures (multiple rows coming from the same individual or unit) - then you'll want to make sure that in your cross-fold setup that you are ensuring that 100% of each individual's observations are falling into the same fold.
An analogy would be that if you were modeling teeth for a probability of developing a cavity - a human head has 32 teeth, and if a particular human's teeth end up in both your testing and training folds, this would be considered a form of leakage. This is because the 32 teeth are not totally independent, but correlated with other teeth within the same head. This particular form of leakage often slips people's minds.


How to interpret KDE distribution graph?

Please find beautiful,  explanation about KDE,  In your graph on X Coordinateif the tail is stretching long towards right side then its positively skewed, it means most of your data points were distributed to left side and vise versa for negative skewness.

Always we needs to ensure that data points on the graph needs to be equally distributed to form Gaussian Normal Curvefor better outcome.
There are various techniques to get rid of skewness , please find top 3 methods to play around with skewed data .

Some prerequisites references for any Distribution Charts

Percentiles, Quartiles, and Interquartile
Range

Percentiles examples

Quartiles examples


Predicting the variable's score using predictive analytics

Can you show us some plots of them ? it really depends on what they look. There is a lot of temporal series forecast.

It will depends on :

Is there any seasonality ?
Is there a trend ?
Is there a high volatility ?
On how long do you want do to your forecast ?


Questions When Advancing from Vanilla Neural Network to Recurrent Neural Network

This question gets at some very important qualities of RNNs and DNNs in general. I'll answer each of your sub-questions, though not in the same order (I'll try to highlight where I do)

Parameter Sharing

First, the most important quality of RNNs is called parameter sharing. Sequential data is typically input into separate layers. For a length 20 input, an RNN network would have 20 layers. The same internal parameters are used for each layer, so all 20 layers use the same weights $W$ and bias $b$. Compare this to a multilayer perceptron which would have 20 separate weights and biases.

Parameter sharing has several benefits:

We now have many fewer parameters. There is 1 repeated block instead of 20 separate layers. A 20x reduction!
This effectively multiplies the training data. The recurrent layer gets to learn from every word in a single example sentence whereas each layer in an MLP learns from a single word per sentence.
Our network is now much more flexible. We can train on sentences up to 20 words and then generalize to a 25 word sentence by adding more steps or by using a dynamic RNN

Network architecture

You ask about tanh and sigmoid activations. To answer this we have to talk about specific RNN architectures. The simple RNN discussed above has a single activation. Simple RNNs tend to create the problem of vanishing gradients (or exploding!) due to the repeated application of the same weights and activation function.

Gated RNN blocks (like GRUs and LSTMs) use gating mechanisms to pass activations into and out of memory states and to combine memory states with input to generate the output of each block. Because of this, gates can stop the gradient from propagating backward. Sigmoid is a common activation function for gates because it squashes activations to (0,1)---0 completely stops the activation and 1 lets it pass through. Any decent activation function with a similar squashing profile works, though. Anecdotally, hard sigmoid is quite common these days.

In addition to the gates, gated RNN blocks have an internal state for which the activation varies quite a bit. Because gating limits gradient backprop, we have a lot of flexibility on this activation. It need not be squashing for instance, and this is where the rectifying activations (relu, elu, islu, etc.) are often seen. Tanh is a perfectly sensible choice as well.

Regarding biases and weights, each activation in an RNN cell typically has its own weight and bias. So a GRU has 3 activations (hidden, update, and reset) and each has its own weight and bias. Though, recall that as an RNN, each of these are re-used for every timestep.

Backward Pass

That covers the forward pass pretty well but you also ask an important question about how error propagates backward. There are two methods for approaching this.

Teacher Forcing

For RNNs that output a prediction at each time step (such as predicting the outcome of following steps, translation, or phoneme recognition), teacher forcing is a method for isolating each step of the RNN. By removing these dependencies, Teacher Forcing allows RNN to use conventional backprop with the chain rule.

But how does it work? Teacher forcing networks have separate train and test architectures. For training, at each timestep $t$, the input $x_t$ is concatenated with the previous target, $y_{t-1}$. Imagine this for a network tasked with predicting the following character. The network has just tried to predict the character for the previous timestep. But we instead use the observed character at that timestep (we know this because we're in the training phase). Thus the error at timestep $t$ depends only on the observed value at $t-1$ and the input at $t$. We've thus removed any connections through time from the network.

At test, we don't know the true value at each timestep so we replace $y_{t-1}$ with the output of the previous layer $o_{t-1}$. In this case, temporal connections have returned but only for the test phase.

Back Propagation Through Time

But we don't have to resort to Teacher Forcing. Back propogation through time allows us to apply the backprop algorithm to RNNs. Consider the network with $n$ timesteps and an input $x$, hidden state $h$, output $o$, and observed value $y$ for each timestep.

BPTT works in the following steps.

Compute the gradient $\nabla_{o_t}$ for each $o_t, y_t$ pair. (This may be done all at once.)
Compute the gradient $\nabla_{h_t}$ for each timestep, beginning with the last timestep and iteratively working backward. (This must be done one at a time.)
This gives us $n$ edges for each internal parameter of our RNN. The trick to updating the parameters is finding the gradient contribution for each timestep (eg $\nabla_{W_t}$ even though we only have one $W$) and then summing those gradients to update the internal parameters.

Further reading

I highly recommend chapter 10 of Goodfellow, Bengio, and Courville's Deep Learning for more information on RNNs. Additionally Graves' RNN book is fantstic for higher-level detail


Implementing "full convolution" to find gradient w.r.t the convolution layer inputs


Does the transformer decoder reuse previous tokens' intermediate states like GPT2?

The past token internal states are reused both in GPT-2 and any other Transformer decoder.

For example, in fairseq's implementation of the transformer, these previous states are received in TransformerDecoder.forward in parameter incremental_state(see the source code).

Remember that there is a mask in the self-attention blocks in the decoder that prevents the predictions and intermediate states to attend to positions equal to or greater than the current one, which means that the internal state won't change even if you recomputed them at every decoding step.

Update: of course it is technically possible to recompute past tokens attending future tokens, but then, what do you do with the future tokens after you re-compute the past ones? Do you recompute them? This is a totally different beast, which has been studied to some degree and is referred to as "iterative refinement". An example can be found in article "Deterministic Non-Autoregressive Neural Sequence Modeling by Iterative Refinement". AFAIK, this kind of approach has not studied in autoregressive models, only in non-autoregressive ones.


If I understand correctly:

Table1 <- data.frame(key = seq(1,100),a.data = rnorm(100))
Table2 <- data.frame(key = c(seq(1,30),rep(NA,30)), b.data = seq(1,60))

##Assuming this is what you want
library(sqldf)
sql.ans <- sqldf("select a.*, b.key from Table1 a LEFT OUTER JOIN Table2 b on a.key = b.key where b.key is null")

## dplyr version
library(dplyr)

dplyr.ans <- Table1 %>% filter(!key %in% Table2$key)

## Regular R version
R.ans <- Table1[which(!Table1$key %in% Table2$key),]

EDIT after dummy data and expected output

dplyr.ans2 <- left_join(df1,df2, by = c("Key1" = "Key2"))
 Key1 BV1  BV2
 1   A1 100  150
 2   A2 200  250
 3   A3 300  350
 4   A4 400 <NA>
 5   A5 500 <NA>


Estimating Variance Reduction Resultant from Additional Data

I would think about starting with a power analysis: i.e. how many data points do you need to measure the effect (or parameter) that you are interested to a specified level of confidence, ceteris paribus? Then, you estimate a cost.


svm.LinearSVC: larger max_iter number doesn't always increase the accuracy/precision/recall

When trying to find the optimum number of iterations it's normally quite useful to visualise how the increasing iteration effect the accuracy (can identify over-fitting and when you should stop fitting).

# Import libraries used
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC

# Create a template lit to store accuracies
acc = []

# Iterate along a logarithmically spaced ranged
for i in np.logspace(0,5, num = 6):
    # Print out the number of iterations to use for the current loop
    print('Training model with iterations: ', i)
    # Create an SVC algorithm with the number of iterations for the current loop
    svc = SVC(solver = 'lbfgs', multi_class = 'auto', max_iter = i, class_weight='balanced')
    # Fit the algorithm to the data
    svc.fit(X_train, Y_train)
    # Append the current accuracy score to the template list
    acc.append(accuracy_score(Y_test, logreg.predict(X_test)) * 100)

# Convert the accuracy list to a series
acc = pd.Series(acc, index = np.logspace(0,5, num = 6))
# Set the plot size
plt.figure(figsize = (15,10))
# Set the plot title
title = 'Graph to show the accuracy of the SVC model as number of iterations increases\nfinal accuracy: ' + str(acc.iloc[-1])
plt.title(title)
# Set the xlabel and ylabel
plt.xlabel('Number of iterations')
plt.ylabel('Accuracy score')
# Plot the graph
acc.plot.line()
plt.show()

This will produce a graph where the number of iterations has been logarithmic increased (note that it might take a bit of time experimenting with np.logspace to create whole number iteration steps).

If the accuracy increases then keep following the trend, if it plateus stop theres probably no point wasting your time, if it drops go back to the highest value (you've over-fitted to the training data).


What would be a good way to use clustering for outlier detection?

A very robust clustering algorithm against outliers is PFCM from Bezdek.
In this paper Bezdek proposes Possibilistic-Fuzzy-C-Means which is an improvement of the different variations of fuzzy posibilistic clustering. This algorithm is particularly good at detecting outliers and avoiding them to influence the clusterization. So using PFCM you could find which points are identified as outliers and at the same time have a very robust fuzzy clustering of your data.


What knowledge do I need in order to write a simple AI program to play a game?

There are multiple ways to approach solving game playing problems. Some games can be solved by search algorithms for example. This works well for card and board games up to some level of complexity. For instance, IBM's Deep Blue was essentially a fast heuristic-driven search for optimal moves.

However, probably the most generic machine learning algorithm for training an agent to perform a task optimally is reinforcement learning. Technically it is not one algorithm, but an extended family of related algorithms that all solve a specific formalisation of the learning problem.

Informally, Reinforcement Learning (RL) is about finding optimal solutions to problems defined in terms of an agent that can observe the state of an environment, take actions in that environment and experience rewards which are somehow related to the state and action. RL solvers need to be designed to cope with situations where rewards are received later than when important actions were taken, and this is usually achieved by the algorithm learning an internal expectation of later rewards associated with state and/or state-action pairs.

Here are some resources for studying Reinforcement Learning:

Reinforcement Learning: An Introduction (Second Edition)
Algorithms for Reinforcement Learning (PDF)
Udacity Reinforcement Learning course
David Silver UCL lectures on Reinforcement Learning

You will find the subject itself is quite large as more and more sophisticated variations of the algorithms are necessary as the problem to solve becomes harder.

Starting games for studying reinforcement learning might include:

Tik-tac-toe (aka Noughts and crosses) - this can be solved easily using search, but it makes for a simple toy problem to solve using basic RL techniques.
Mazes - in the reinforcement learning literature, there are many examples of "grid world" games where an agent moves in single N,E,S,W steps on a small board that can be populated with hazards and goals.
Blackjack (aka 21)

If you want to work with agents for playing video games, you will also want to learn about neural networks and probably in some detail - you will need deep, convolutional neural networks to process screen graphics.

A relatively new resource for RL is OpenAI Universe. They have done a lot of work to package up environments ready to train agents against, meaning you can concentrate on studying the learning algorithms, as opposed to the effort of setting up the environment.

Regarding your list of current skills: None of them are directly relevant to reinforcement learning. However:

If you can understand the maths and theory from your previous course, then you should also be able to understand reinforcement learning theory.
If you have studied any online or batch supervised learning techniques, then these can be used as components inside a RL framework. Typically they can be used to approximate a value function of the game state, based on feedback from successes and failures so far.


Theoretically speaking, more data leads to better model. However, in practice, more features often leads to the difficulty of model training.

Assuming there are 30 "main" features of your dataset. Feature set A contains 20 "main" features, so it might be easy (20 out of 26) for one "main" feature to be "chosen" and "trained", under certain hyperparameters (in your case, 100 trees). When it comes to feature set B, which contains all "main" features, it's hard (30 out of 96) for one "main" feature to be chosen, and it's harder when there's only 100 trees (cause there are 66 "minor" features which should not be trained, relatively). That's what we called "under-fitting".

Back to your question, when the model is under-fitting, if we're lucky that model trained on feature set B (namely model B) contains all these 30 "main" features, the model will be good, and we might not able to find out it's under-fitting. But in most cases, we're not that lucky, model can be ruined with all 66 "minor" features, relatively.

In my ML practice, I'll try more training iterations when more samples come, and try a more complex model when more features come, with the control of over-fitting/under-fitting.


Adding time as a feature with xgboost/random forests

You have already constructed features that indicate relevant events like weekday, holiday and so on, this is good. Create as many such features you can come up with.

As for the time of day, I'd try using the hour only and combination of hour+minute. Probably less important is the minute alone, unless there is something that causes spikes at the same times of every hour.

Since you use tree-based models, you don't need to normalize. Mark your time features as categorical (can you do that in xgboost? I forgot, but I think you can in lightgbm) or as boolean where applicablen. And then check feature importances, which you can with gradient boosted tree type of models.

Finally, make sure your validation splits are good, which can be a challenge whenever you have some kind of time involved. Make sure your don't leak information and that your validation set is representative.


Using Google Translate API to create a Translation Dataset

It depends. Google Translate works very good with some pairs of languages, and not good on others. Based on my personal experience, translating from North-European languages (Dutch, Danish, Swedish) to English worked almost perfectly, while English-Italian translation lead to bad results.

You can find a Spanish-English dataset here, this is a very official source.

Additionally, it seems DeepL is a great German <-> English online translator around.


What are some options to add or remove nodes from a multiclass classification model?

@DPCII, I don't think modifying output nodes at runtime will help you. This is because,

A neural network is trained on a specific dataset and to predict predefined variables only. In backpropogation ( used for optimization ), the gradients are for each weight and bias ( or any other parameters ) are calculated using the loss function ( also the objective function ).

For the changing the number of output nodes, you have remove some the connections coming from the previous layer which is $L_N$, suppose. But during training, the parameters of $L_{N-1}$ were optimized using the gradients of the activation functions of $L_N$.

Hence, removing output nodes at inference time would affect all the previous layers which in turn will affect the final output ( of the remaining nodes ).

Instead, I would suggest, to construct a neural network which predicts all the possible classes. Train this NN on various samples wherein you'll require the predictions of some desired nodes. Let me explain this by an example.

Suppose, we have a neural network which predicts classes $C_1, C_2,C_3,...,C_N$. For a specific sample, we only need the predictions for classes $C_1$ and $C_2$ only, given a sample $X$. For this sample, we set the label as $[ y_1 , y_2 , 0 , 0 , 0 , ... , 0].$ We only set the values for classes $C_1$ and $C_2$ and rest all the classes are set to zero.

Similarly, we assign labels for various samples and set only those values whose predictions are needed. Rest all values are set to zero.

This is actually the common classification we do for images. For a given sample of a cat, I would like to predict an array of $[1,0]$ and for a sample of dog, $[0,1]$.

Hence, we could apply this ideology instead of removing nodes and disturbing the NN.


On the github page of the ESC-50 dataset, there is a list of tried classifiers as well as links to the relevant papers. The best ones are all using some kind of deep learning, mostly CNNs, and currently the best score is 85.5%. A baseline Random forest (notebook available here) achived 44.3% and SVM (available here) achieved 39.6%.

I would recommend you take a look at the approaches linked above and see if you can improve upon them. However, for higher accuracy you probably need some kind of deep neural network.


What fields offer most data science job opporunities?

What difference does it make? Nothing stops you from applying to all of them. The real question is, can you and do you want to write software or do you just want to analyze data? Pay attention to the job descriptions in this regard. Data analysts are typically expected to be strong in classical statistics, while machine engineers are expected to know ... machine learning. Then every field has certain problems that you need to be familiar with before taking the technical interview, such as recommender systems for media companies, and time series forecasting for business/finance. You can find out what they are by reading around and actually applying to companies. Expect several rounds of interviews, including mathematical and programming challenges (real-time or off-line), and questions about your research.

Once again, every field needs data scientists, so this is not a criterion.


Why does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?

Remember that the GradientBoostingRegressor (assuming a squared error loss function) successively fits regression trees to the residuals of the previous stage. Now if the tree in stage i predicts a value larger than the target variable for a particular training example, the residual of stage i for that example is going to be negative, and so the regression tree at stage i+1 will face negative target values (which are the residuals from stage i).
As the boosting algorithm adds up all these trees to make the final prediction, I believe this can explain why you may end up with negative predictions, even though all the target values in the training set were positive, especially as you mentioned that this happens more often when you increase the number of trees.


Get elements from lists in pandas dataframe

You logic is correct, it's just the lambda function that is slightly wrong. .append() adds a single element to the end of the list, so in your case I guess it adds an element which is a list of 20 elements. You could either use .extend() instead, which will do what you want or you can simply write

lambda x: x[:20] + x[-20:]


Autoencoder gets ~0% accuracy / doesn't train at all

A few things. Firstly I don't think what you are trying to do is very meaningful. An autoencoder tries to find patterns in your data and take advantage of them to be able to compress and reconstruct the data. You are trying to use it with random data, which means there will be no patterns for the autoencoder to learn. I suggest you try using MNIST or another dataset.

Another thing is that your last activation function, your loss function, your metric and possibly your data does not work together. It is hard to tell what is right since I don't know what random numbers you generated. Probably your numbers are not binary, and you would want:

model.add(keras.layers.Dense(200))

model.compile(optimizer='adam', loss='mse')

Or if your random numbers are binary then you would want:

model.add(keras.layers.Dense(200), activation='sigmoid')

model.compile(optimizer='adam', loss='binary_crossentropy')

Lastly, using accuracy as a metric does not make sense for an autoencoder. Accuracy is only useful when you are performing classification.


Get how similarity between the training data and the income data?

I think I got your difficulty.  You are trying to detect defective motors whilst there is more than one state in which they are non-defective.  I am assuming that since there are several of those motors even when a motor is OFF the sensors still catch "noise" vibrations.  You do have two distinct known patterns: ON and OFF, i.e. OFF is not just the absence of vibration.  I see two options for this classification (below) but first let us make a small detour:

  and 85% similar to the training data

The vast majority of ML techniques are based on distance measures: the training data is used to define a blobs (e.g. distributions) in (possibly very high dimensional) space, one for each class.  The classes of new samples are predicted by comparing their distance to the class blobs - the closest blob is the predicted class.

Now, this means that the classification probability is the similarity to training data.  Probabilities in the ranges (0.9, 0.1) or (0.8, 0.2) are very similar to training data, whilst probabilities in the ranges (0.6, 0.4) or (0.5, 0.5) are the most likely distinct from training data.  This leads us to one solution you may attempt:

Understand probabilities as distances

The probabilities (.predict_proba) of models such as SVMs in sklearn are literally distances, in the case of SVMs the distances within the support vectors.  In other words, we can understand these as distances between the middle points of the two blobs: ON and OFF.  e.g.

This means that assuming two things:

That the classes ON and OFF are well separated (there's reasonable amount of distance between them), and,
Defective motors will produce less vibrations than motors ON, but more vibrations than motors OFF (I'm a little worried about this assumption but bear with me).

We can say that:

Any probability pair akin of (0.8, 0.2) means that we have an ON or OFF motor.  In other words, if we have at least one of the classes with a probability higher than, say, 80%, we are close enough to the blobs of training data.
If the probability of ON and OFF is somewhere around 50% then we very likely have a defective motor.

One can choose the probability threshold to his liking (be it 80%, 70% or 90%), as a way to adjust for false positives or false negatives.

Issues with this solution

The assumption that a defective motor will have vibrations between the ON and OFF classes.  i.e. if a defect makes the motor vibrate more than the normal ON it will be undetectable with this solution.
Assumes linearity and same scale of distance from ON and from OFF for defective motors.  i.e. classes are of the same size and equally distributed.  Such solution will check distances from each class using the same linear scale (the threshold), it may not detect defects that happen close to the smaller class.

Use Novelty Detection

You do have two known classes: ON and OFF.  But what you are trying to find is whether a motor is defective or not, where we understand non-defective as the fact that the motor is very similar to most ON motors or very similar to most OFF motors.

Forget about the ON and OFF classes and dump both sets of samples of non-defective motors as one single (non-defective) class and into a novelty detection algorithm.  In other words, do not differentiate between ON and OFF motors.  Say that both cases (ON and OFF) are OK, just anything that is quite far away from any known cases is not-OK.  This will look as a bunch of blobs in space and anything far away from these blobs is a defective motor.

I'll suggest starting with an One Class SVM because it has a parameter called nu= which is quite easy to interpret.  It varies between 0 and 1 and the closest to zero it is the more the algorithm is permissive of anomalies (defects) away from the blobs.

Note that we have exactly the same points on this picture as in the previous one.  The only difference is that this time all of them are red.

This solution will cope with defects that are anywhere, not only in between the states of ON and OFF.

Issues with this solution

On Class SVM does not have a .predict_proba.  To get an estimate of how far away from a blob an anomaly marked as an anomaly is, one must evaluate the .decision_function of the model and keep track of the distances oneself.
Even if the classes ON and OFF are well separated one will have false positives and false negatives.  A very low nu= parameter still cannot go beyond the blobs.  Using an Isolation Forest may allow for better tuning but the decision function would be even more complex to get distances from.  Another option is to add marginal versions of ON and OFF to the training data but finding those may not be easy.

P.S. If one needs to do both classify ON and OFF AND classify defective and non-defective motors, then one should build two separate models.


How to use glob() output as input to os.listdir()

Assuming you have a string to pass into glob that does wildcard matching, glob will return a list of matches. So you don't need to make a string out of that list and replace the square-brackets and so on. You can just iterate over that list and do something with each of the values, which are already strings.

results = glob.glob(your_pattern)

Based on your code, the results is actually a list of folder names for which you want to count the number of files in each. We can loop over results and print the counts:

for folder in results:
    n = len(os.listdir(folder))
    print("Folder: {0} --> \t {1} files".format(folder, n))


Chi-square as evaluation metrics for nonlinear machine learning regression models

Use your test data to compare the predictive performance of each model.

In R you could do this like:

linear.predictions <- predict(linear.model, newdata = test.data)
nonlinear.predictions <- predict(nonlinear.model, newdata = test.data)

linear.percent.difference <- (test.data$TARGET_VARIABLE -
                              linear.predictions)  /
                              test.data$TARGET_VARIABLE

nonlinear.percent.difference <- (test.$TARGET_VARIABLE -
                                 nonlinear.predictions) /
                                 test.dtat$TARGET_VARIABLE

linear.grade <- mean(linear.percent.difference)
nonlinear.grade <- mean(nonlinear.percent.difference)

This is a pretty simple way to do it, but it is one that works for me and is easy to understand, especially if your audience is going to eye-glaze as soon as you say "Chi-square..." Get creative!


How does the number of trees effect the prediction time in gradient boost classification trees?

The time it takes to get a prediction from a model of gradient boosted classification trees should be linear in the number of trees. So getting predictions from a model with 1000 trees should take about twice as long as 500 trees, and about half as long as 2000 trees.

You'll need to test it yourself and check if it's fast enough for your use case. Modern libraries like xgboost can handle high numbers of trees with remarkable efficiency. One thing to keep in mind is that it's generally faster to get predictions for a whole block of test examples at once than it is to get the predictions one at a time.


Does using your test set ultimately burn your data set in case of failure?

I would try one more time to split the data & train a new algo. This is similar to p-hacking as you've stated so try to avoid doing this more than once but if the alternative is the project stops there doesn't seem to be much more you can do.
This time when you split the data I would try stratifying it by some feature (the target? Some category of the input variables?). This ensures the training/validation/testing sets have the same proportion of some criteria which might help your performance be a little more consistent when comparing the validation set to the testing set.


Good ideas there. But start with simple things first and then ramp up to a more complex system once you have something already working and some human feedback on the solution.

Simpler solution is just a velocity check - if there is X number of failures per Y number of seconds, then send the notification. From your post it seems like 3 failures over 20 second time window should be a good start. It takes a fair bit of tinkering to get this kind of system running - work out the windowing functions, etc.

Good luck :)


Is GridSearchCV computing SVC with rbf kernel and different degrees?

From what I understand, you'll be able to pass different values of degree even when you're using kernels that are not the polynomial kernel, just that it will not be used. I believe the score will come out to be similar for the other kernels even with different degrees. Will you be able to confirm this?

To avoid additional computation time due to redundant searches, you can fine tune the GridSearchCV by specifying two grids. Try out the below code and passing it into the param_grid argument.

param_grid = [
  {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
  {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
 ]

You can explore the GridSearchCV documentation for the specific examples. Take a look at the example here (look at 3.2.1)


How do I predict survival curves using xgboost?

The proportional hazard model assumes hazard rates of the form: $h(t|X) = h_0(t) \cdot risk(X)$ where usually $risk(X) = exp(X\beta)$. The xgboost predict method returns $risk(X)$ only. What we can do is use the survival::basehaz function to find $h_0(t)$.

Problem is it's not "calibrated" to the actual baseline hazard rate computed in xgboost. What we can do is find some constant $C$ that minimizes the ibrier score between the sample observed death/censorship times and $h_0(t) \cdot risk(X) \cdot C$.

I've implemented this approach in a tiny R package I've written.


How does inception decrease the computational cost?

However, in the overall scheme, sliding this network can be represented by two 3 x 3 convolutional layers which reuses the activation between adjacent tiles.

Since the replacement (two 3x3 instead of one 5x5) share weights, we don't have to calculate them twice. That is where the gain comes from.

Edit:

The gain comes from the sliding: using the following formula (n-filtersize+1) x (n-filtersize+1) that calculates the output of a filter on an n x n input

a more detailed answer can be found here:
Reducing Filter Size in Convolutional Neural Network
Thank you @Thomas W.


You have put nb_epoch in fit.

You have to remove nb_epoch from fit, and let it only in KerasRegressor.


Training Neural network classifier using string inputs

You always avoid feeding direct strings into neural networks. This thread here explains why you should avoid doing this : Neural Network parse string data?

Once you convert the strings you have into vectors or any other form of numerical representation and encoding your labels as categorical, it will solve the problem you have at hand.

If you need me to elaborate more on this, I would be more than happy to do so.


Cross validation for unbalanced dataset using Orange data mining tool

Orange does not have over/undersampling. Our reasoning is that if you model a problem with 10% positive class, than you should not train the model with 50:50 class distribution - it will not reflect the real life. However, there's an option in Orange in LogReg and Random Forest to balance class distribution, which considers class distribution when building a model.


Few clustering algorithms have any kind of loss. That is pretty much only k-means and a few closely related methods that fit this narrow world view of "everything is optimizing a loss".

Now if your features would minimize the same loss, then the result should be close to just doing k-means on the raw pixels - which is known to not work well. You do want features that lose a lot of irrelevant information.


What is the class of this optimization problem?

After some research on this problem I've realised the model I've developed was incorrect. This is because I introduced the weight vectors in a wrong manner.

Essentially, in my first model(the one this question was based on), the weight vectors were applied on the target vectors and on the input vectors that went into the model. Suppose this is right...because the model converges to the target => on the long term this will behave like a linear transformation: $F(w\mathbf{x})=wF(\mathbf{x})$
Introducing this into $E_u$ we get that $w$ doesn't even matter. Diving more deeper I realized this was a property of space. I was computing an $L_2$ norm...by defining an error as the distance of the model vector from the target. No matter how you strech you transform all the points (target and model) in the same manner <=> the relative positions between the points don't change (the errors might be bigger or smaller, but the order relationship between the errors will be preserved).

The solution was to update my model to use instead the weighed euclidean distance. This means:
$$E_u = \dfrac{1}{N_u}\sum_{i=0}^{N_u-1}\left\lVert x(t_{i+1}) - F_D(\{x(t_j)_{j=0,i}\})\right\rVert_D$$

I've written $F_D$, because at some point, my model does a query using a Ball Tree, which depends on D. I don't know theoretically why this works, but I can say that experimentally, it works (in the sense that the model outputs things which I expect and are reasonable).


performances evaluation of image classification with different distribution for train and test set

I'm afraid you have too little data. That's why optimizer can't converge to any reasonable place. I'd suggest you try doing transfer learning first. Few links for you to check out:

https://machinelearningmastery.com/transfer-learning-for-deep-learning/
https://www.pyimagesearch.com/2019/05/20/transfer-learning-with-keras-and-deep-learning/
https://www.pyimagesearch.com/2019/06/24/change-input-shape-dimensions-for-fine-tuning-with-keras/
https://www.pyimagesearch.com/2019/06/03/fine-tuning-with-keras-and-deep-learning/

Consider also heavier augmentation. Albumentations is a nice repo, used in many kaggle competitions.


Train a deep reinforcement learning model using two computers

Distributed RL is very much a thing. Google have created a distributed setup called IMPALA for this, and there are multiple instances of A3C, PPO etc available if you search. I don't know much about IMPALA, but the basic idea of the scalable policy gradient methods is to run multiple environments, collecting gradients on each server, then collating them together to create improved policy and value networks every few steps.

There are a couple of variations in strategy based on which stage of the data is shared - observations or gradients. Gradient calculation is CPU intensive, so above a certain scale it is worth having that occur on the distributed devices, depending on how intensive it is to collect experience in the first place.

  Obs: The computers aren't in the same LAN.

This may prevent you implementing anything with low-level observation or gradient sharing, unless the bandwidth between the machines is high.

The simplest way to use two computers in this case is to perform basic hyper-parameter searches by running different tests on each computer and tracking which computer has done which experiments.

  The first one would execute the game and send requisitions to the second computer which would store and train the model itself

This could work with an off-policy value-based method, such as DQN. You still need a reasonable bandwidth between the two machines, especially if the observation space is large. DQN is a reasonable choice since you don't need the environment-running machine to follow the current optimal policy - although you will still want to update the policy on the first computer some of the time.

The basic algorithms of DQN do not require much changing to support this kind of distribution. Just comment out or make conditional a few parts:

On the first machine:

Comment out or logically block sampling and learning from experience table
Maintain a "behaviour-driving" Q-value network instead of the learning network, in order to run $\epsilon$-greedy policy
Send experience to second machine instead of storing it in local experience-replay table (this is the bandwidth-intensive part)
Asynchronously receive updates to behaviour-driving Q-value network

On the second machine:

Comment out or logically block interactions with the environment
Asynchronously receive experience from first machine and add to experience-replay table
Every so many mini-batches, send updated current network to second machine

You will need to have some experience handling multi-threading or multi-process code in order to cover the asynchronous nature of the updates. If that seems too hard, then you could have the two machines attempt to update each other synchronously, or via a queueing mechanism. The disadvantage of that is one or other machine could end up idle waiting for its partner to complete its part of the work.


It is not a multiclass problem.
It is a multilabel problem. Since, you have the clusters of classes you want to get. You just let the network predict multiple classes and segregate them afterwards. In this case, you will have single classification head.
Other way to do it, is to separately derive multiple classes of article, technique and style. In this case, you will have three classification heads.


How to create an array from the list of arrays in python

This post from stack overflow should give you what you want. The magic code boils down to the following.

flat_list = [item for sublist in l for item in sublist]

You can also loop through the list of lists and merge them.

lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

merged_list = []

for l in lists:
    merged_list += l

print(merged_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]


Face recognition - How to make an image classifier with large number of classes?

How do I download these many different images? Since it's pretty hard to manually download the pictures of all, is there a way to automate it?

First of all, you need to get an idea of how your classifier will be, will it classify variable size images or fixed size images? Will it receive only one face as an input or more than one? Will it classify black and white images or with more than one channel? Will it be a multioutput classifier or a single output classifier?

Before starting you have to answer this and other questions. Based on the later questions you have to get your data from a different source or in a different way. Instead of thinking these questions you can search for a dataset that already has the images collected and organized and adapt to the dataset, Kaggle has lots of public datasets. Collecting the training data by your own can be tedious work, and more if you need thousands of samples, I recommend you to make a data mining application the crawls through different pages and automatically downloads the images, although you have to be careful not to violate any privacy laws.

  I am using Fast.ai and we are required to make a different class for each player. How should I go about making different classes for so many players?

Now that you have all the data clean and tidy you have to classify the images. There are lots of ways to classify images using deep neural networks, you could use simple feedforward neural networks or recurrent neural networks, but the most commonly used and the one I recommend you are convolutional neural networks. If you do not have a lot of experience classifying images I recommend you to start with an easier project such as the MNIST dataset, which consists of black an white images of handwritten digits, this way you can practice with the different hyperparameters and experiment better, rather than starting with such an ambitious classification task. Either way, to classify the images into multiple outputs you will have to add a final layer to your neural network with a number of hidden neurons equal to the number of classes to distinguish, then you should add a softmax activation function to that layer. Finally, for the model to learn you should use a categorial cross entropy loss/cost function.

I don't know if I answered your question or not at all so please don't hesitate into telling me or asking more questions.

Either way, glad to help


Pandas pivot table, creating ad hoc columns per dimension values

The solutions seems to be fairly straight forward. The only thing that is missing in your pivot is, what are the columns you want to put on top to access the pivot. In your case instead of using

mypivot = pd.pivot_table(dftest, values=['Sales'], index=['State', 'City'])

which produces

                  Sales
State City
CT    Hartford     1500
KY    Lexington    1500
      Louisville   3000
ME    Portland     2000
TX    Dallas       2300

you can write

mypivot = pd.pivot_table(dftest, values=['Sales'], index=['State', 'City'], columns=['Product Type'])

which gives you

                    Sales
Product Type     Consumer Educational Industrial Scientific
State City
CT    Hartford        NaN      1500.0        NaN        NaN
KY    Lexington    2000.0         NaN     1000.0        NaN
      Louisville   3000.0         NaN        NaN        NaN
ME    Portland        NaN      2000.0        NaN        NaN
TX    Dallas          NaN         NaN        NaN     2300.0

You can now use .fillna method on chaining to replace non-zero values in your pivot. Hope this helps.

Edit:
If you want to take only a specific set of columns, I can seem to have a not so elegant but workable solution like

mypivot['Sales'][['Consumer', 'Educational']]

Which unfortunately removes the higher column Sales from picture producing something like

Product Type      Consumer  Educational
State City
CT    Hartford         0.0       1500.0
KY    Lexington     2000.0          0.0
      Louisville    3000.0          0.0
ME    Portland         0.0       2000.0
TX    Dallas           0.0          0.0

If I can think of anything, I will add later.


Importance of variables in RandomForest in R

If the features scale of measurement and/or number of categories vary, permutation accuracy is more reliable than the GINI index (Strob et al. 2007).

However, better yet is to use Boruta all relevant variable selection prior to running random forests to determine which variables are relevant to the target property, and which are not. This allows you to trim your dataset to only useful, or relevant variables.

Strobl, Carolin, Anne-Laure Boulesteix, Achim Zeileis, and Torsten Hothorn. “Bias in Random Forest Variable Importance Measures: Illustrations, Sources and a Solution.” BMC Bioinformatics 8 (2007): 25. doi:10.1186/1471-2105-8-25.


Why is random forest an improvement of decision tree?

It comes down to overfitting as you scale. Decision trees tend to overfit as they grow deep. After every split there will be fewer and fewer samples for the next split to work with. Fewer samples means that risk of splitting on noise increases.

Random forest avoids the overfitting problem of decision trees by instead scaling by adding more trees instead of building one big tree. Averaging the outputs of the trees in the forest means that it does not matter as much if the individual trees are overfitting.

Regarding your update. No, they will not score the same. Random forest will not have just one decision tree. It has several and divides the features into random subsets for each tree to be trained on. So even if the size of the decision trees in random forest would be the same as a single decision tree, the features they are trained on would not be.

But if you ask what happens if we take a random forest, only use one tree and train it on the same features as a single decision tree of the same size, then yes they would be one and the same.


You may use gsub function

> c <-  "ce7382"
> gsub("[a-zA-Z ]", "", c)
[1] "7382"

Feel free to add other characters you need to remove to the regexp and / or to cast the result to number with as.numeric.


API to find out how many executors are running my Spark jobs?

Keep in mind that the number of executors is independent of the number of partitions of your dataframe. You set the number of executors when creating SparkConf() object.

https://spark.apache.org/docs/latest/submitting-applications.html

To retrieve the number of executors from code, it depends of the API you use. For python, this is not currently implemented. For Scala, you can. Here is a good stack post :

https://stackoverflow.com/questions/38660907/how-to-get-the-number-of-workersexecutors-in-pyspark

Number of partitions refers to the number of parts your dataframe is composed of. With pyspark API, you can get it by transform your dataframe to a rdd and then use method .getNumPartitions() :

num_partitions = df.rdd.getNumPartitions()


How do I generate a laplacian matrix for a graph dataset?

Well the Laplacian matrix is achieved by:

$degree (v_i) $ for $\space$ i=j

$-1$ for $\space$ if $v_j$ and $v_i$ are not adjacent to each other

$0$ otherwise

First, you need to store your file to a 2d-array
Then you need to define another 2d-array matrix the same size of your first matrix. Then loop over the elements to fill the Laplacian matrix

import pandas as pd
data = pd.read_csv('data.csv')
df = pd.Dataframe(data)
M = df.as_matrix()
L = np.zeros(df.shape[0], df.shape[1]) #shape[0] and shape[1] should be equal

Then for each element $A_{i,j}$ we calculate their corresponding value in L

for i in range(len(df.shape[0])):
    for j in range(len(df.shape[0])): # or shape[1]
        if M[i][j] == 0 and M[j][i]== 0:
            L[i][j] = -1
        if i == j:
            L[i][j] = sum(M[i][:])
        else:
            L[i][j] = 0

I haven't tried the code so consider it much like a pseudo-code.


BERT cannot use GloVe embeddings, simply because it uses a different input segmentation. GloVe works with the traditional word-like tokens, whereas BERT segments its input into subword units called word-pieces. On one hand, it ensures there are no out-of-vocabulary tokens, on the other hand, totally unknown words get split into characters and BERT probably cannot make much sense of them either.

Anyway, BERT learns its custom word-piece embeddings jointly with the entire model. They cannot carry the same type of semantic information as word2vec or GloVe because they are often only word fragments and BERT needs to make sense of them in the later layers.

You might say that inputs are one-hot vectors if you want, but as almost always, it is just a useful didactic abstraction. All modern deep learning frameworks implement embedding lookup just by direct indexing, multiplying the embedding matrix with a one-hot-vector would be just wasteful.


What is the best performance metric used in balancing dataset using SMOTE technique

First of all, just to be clear, you shouldn't evaluate the performance of your models on the balanced data set. What you should do is to split your dataset into a train and a test set with ideally the same degree of imbalance. The evaluation should be performed exclusively on the test set, while the balancing on the training set.

As for your question, any macro averaged metric should do just fine for proving that your balancing technique is effective. To calculate such a metric (let's say accuracy for simplicity), you just need to compute the accuracies of each class individually and then average them.

Example:
We trained two models m1 and m2, the first without balancing the dataset and the second after using SMOTE to balance the dataset.

Actual values :  0, 0, 0, 0, 0, 0, 0, 0, 1, 1
Predicted m1: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  <-- only predicts majority class
Predicted m2: 1, 0, 0, 1, 0, 1, 0, 0, 1, 1

How would we normally calculate accuracy?

$acc = \frac{correct \, predictions}{total \, predictions}$

How do our two models perform on this metric?

$acc_1 = \frac{8}{10} = 80\%$
$acc_2 = \frac{7}{10} = 70\%$

According to this performance metric, m2 is better than m1. However, this isn't necessarily the case as m1 just predicts the majority class! In order to show how m2 is better than m1, we need a metric that treats the two clases as equals.

We' ll now try to calculate a macro-averaged accuracy. How? First we'll calculate the accuracy for each class separately, and then we'll average them:

For m1:
$acc_1^0 = \frac{8}{8} = 100\%$ <-- m1's accuracy on class 0
$acc_1^1 = \frac{0}{2} = 0\%$  <-- m1's accuracy on class 1
$macro\_acc_1 = \frac{acc_1^0 + acc_1^1}{2} = \frac{100\% + 0\%}{2} = 50\%$
For m2:
$acc_2^0 = \frac{5}{8} = 62.5\%$ <-- m2's accuracy on class 0
$acc_2^1 = \frac{2}{2} = 100\%$  <-- m2's accuracy on class 1
$macro\_acc_2 = \frac{acc_2^0 + acc_2^1}{2} = \frac{62.5\% + 100\%}{2} = 81.25\%$

Notes:

Macro averaging can be applied to any metric you want, however it is most common in confusion matrix metrics (e.g precision, recall, f1).
You don't need to implement this by yourself, many libraries already have it (e.g. sklearn's f1_score has a parameter called average, which can be set to "macro")


The two methods you're citing belong to what is called unsupervised binning, including as you said equal width and equal frequency binning. On the other hand, supervised binning broadly tries to make sure bins are made in majority of instances sharing the same class label.
For both types of unsupervised binning, i.e. equal frequency and equal width, the best way is still to give it a try and select based on the observation of the resulting histogram you get. If your data is not properly divided by bins of equal frequency, maybe equal width bins would help, and vice versa.
For what concerns Pandas execution, you can pass a precision argument to qcut, this should return more "comprehensible" bin limits, as shown below
>>> array = np.random.randn(10)

>>> pd.qcut(array, q=4, precision=3)
Categories (4, interval[float64]): [(-1.8889999999999998, -0.732] < (-0.732,
-0.136] < (-0.136, 0.973] < (0.973, 1.543]]

>>> pd.qcut(array, q=4, precision=0)
Categories (4, interval[float64]): [(-3.0, -1.0] < (-1.0, -0.0] < (-0.0, 1.0]
< (1.0, 2.0]]


You can justify your choices by using data.

Treat the anomaly detection like a supervised learning problem where the concept is being anomaly.
Then you'll be able to present - for each method - its confusion matrix.
Not only it will be a good justification, it will enable to understand the expected results.

Many times, we have models and we wonder which confidence threshold we should use for alerting. In the supervised learning framework you'll be able to do trade-off like "increasing the confidence to X will lead to a better precision Y yet a decrease of the recall to Z".


Looking for language and framework for data munging/wrangling

If you are interested in a very high-level (enterprise architecture) framework, I suggest you to take a look at the MIKE2.0 Methodology. Being an information management framework, MIKE2.0 has, certainly, much wider coverage than the domain of your interest, but it is a solid, interesting and open (licensed under the Creative Commons Attribution License) framework. A better fit for your focus is the Extract, transform, load (ETL) framework, which is extremely popular in contexts of Business Intelligence and Data Warehousing. On a more practical note, you might want to check my answer on Quora on open source master data management (MDM) solutions. Pay attention to the Talend solutions (disclaimer: I am not affiliated with this or any company), which cover a wide spectrum of MDM, ETL and data integration domains as open source and commercial offerings.


Please suggest some simple functions that I can use to test my neural network implementation

Without code you could have made to many mistakes to answer your question directly.

However, I suggest two things:

Gradient checking (e.g. this explanation, but there are many more - pretty sure also in the course)
XOR problem with a 2:2:1 network with only sigmoid activations. Plot the error surface. Compare the decision surface with that of this interactive demo.

It should look similar to this:

However, please keep in mind that the problem might not be your implementation, but rather the network architecture / hyperparameters such as the number of epochs you're training or the training data.

Also, very important: I doubt that you will get good results for $y = x^2$, except if $x$ is restricted to $[-1,1]$ or something similar simple. Please keep the domain of your output layer in mind.


What is the best statistical measure tool to measure how close data is to fitted regression line if outliers are not fitted

When you mention that your model does a good job in fitting the data (and not the outliers), I am assuming you are taking a look directly into actual vs predicted (or fitted) values. While doing this, you do not look for comparisons for outlier observations.

Similarly, you could still calculate the R squared by removing the outlier observations from the calculation, and it could still serve as a measure for goodness of fit. There is sufficient documentation available on how to do this manually (by hand) so you can write a function to do this by yourself.


Difference between cell state and hidden state

In short:

Cell state: Long term memory of the model, only part of LSTM models
Hidden state: Working memory, part of LSTM and RNN models

Additional Information
RNN and vanishing/exploding gradients
Traditional Recurrent Neural Networks (RNN) have the ability to model sequential events by propagating through time, i.e. forward and backward propagation. This is achieved by "connecting" these sequential events with the hidden state:
$a_n = f(W_n, a_{n-1}, x_n)$
The hidden state $a_n$ carries past information by applying a linear combination over the previous step and the current input.
Despite being a very successful architecture, RNN have the issue of vanishing/exploding gradients. This means that every previous step is essentially considered in the calculation of the backpropagation (how wrong my prediction has been), due to the chain rule engraved in $a_n$:
$a_n = f(W_n, a_{n-1}, x_n) = f(W_n, f(W_{n-1}, a_{n-2}, x_{n-1}), x_n)$, since $ a_{n-1}=f(W_n, a_{n-2}, x_n)$.
To summarise: RNNs are great, but issues occur with log terms dependencies because of the chain rule in their hidden state.
LSTM and the cell state
To alleviate the issues above, LSTM architectures introduce the cell state, additional to the existing hidden state of RNNs. Cell states give the model longer memory of past events. This long term memory capability is enabled by

the storage of useful beliefs from new inputs
the loading of beliefs into the working memory (i.e. cell state) that are immediately useful.

In case you wonder "how does it know what to store or what's immediately useful?": remember that this a trainable weight that learns with training, consider it as an additional piece of muscle that will learn this new activity storing and loading by training it on examples (i.e. labelled datapoints).
To summarise: LSTMs are usually better at dealing with long term dependencies, because of their capacity to store and load beliefs that are important at different parts of the sequence.

TLDR:
hidden state:

Working memory capability that carries information from immediately previous events and overwrites at every step uncontrollably
-present at RNNs and LSTMs

cell state:

long term memory capability that stores and loads information of not necessarily immediately previous events
present in LSTMs

GRUs are also very relevant but are excluded from the response.


Why the first prediction of neural network in PyTorch is slower than following predictions?

I have seen a similar question several times before. See https://stackoverflow.com/a/55577921/9212382 for a possible explanation.


Why do we use a Gaussian kernel as a similarity metric?

Let's be precise. "Distance" has lots of meanings in data science, I think you're talking about Euclidean distance.

The Gaussian kernel is a non-linear function of Euclidean distance.

The kernel function decreases with distance and ranges between zero and one. In euclidean distance, the value increases with distance. Thus, the kernel function is a more useful metrics for weighting observations.
The fact that it's bounded between zero and one is a nice property, whereas the absolute distance (it can be anything) in Euclidean distance can cause instability and difficulty in modelling.
Euclidean distance (without the negative sign) is not a similarity measure, it's a distance function. The gaussian kernel is a similarity measure.
You can think the Gaussian kernel like a normalization function for Euclidean distance.


What can be the cause of a sudden explosion in the loss when training a CNN (Deeplab)

One possible cause is a high learning rate. High values of this hyperparameter usually cause updates that are too drastic, and therefore divergence from the optimum.

Please keep in mind this is only a suggestion, your problem might be due to completely different reasons. Try different learning rates and schedules, in order to understand if that's the case. You can check this very good post on how to set the learning rate.

Also, consider using early stopping to save some training time.


Best image PPI for computer vision deep learning algorithms

PPI is only relevant for physical objects, like representations on screen or paper. It is pixels per inch, and defines how big an image X by Y pixels appears on that object. So if you have a 256x256 pixel image, and you show it at 256 PPI on a screen it will be 1 inch square if you put a ruler up to it.

Many physical devices have natural PPI characteristics. Screens are often 72 PPI, 100 PPI, or about 300 PPI for "Retina" devices. Laser printers start at 300 PPI and higher.

So if you show a 256x256 image on a 100 PPI screen as a direct mapping of one image pixel to one screen pixel, you'll get an image 2.56" across.

If you zoom into the image you can make it bigger on the screen, and it will now cover more than 256 pixels of the screen. The underlying image data will still be an array of 256x256 values

Scanners are another area where PPI is important. If you scan a 1" image at 300 PPI, you get a 300x300 pixel image. Set your scanner to 1200 DPI and you get a 1200x1200 pixel image, that is much finer detail.

In summary, PPI is irrelevant to CV or ML algorithms because they only care about the number of pixels.


What's the best classification model for this recommendation engine?

Without more information about your dataset, it's impossible to recommend one particular classifier over another.

If you want your classifier to return a vector of probabilities, then if you're using the sklearn library, you could use the predict_proba method.

Here's an example:

from sklearn.datasets import load_digits
digits = load_digits(2)
from sklearn.linear_model import LogisticRegression
preds = LogisticRegression().fit(digits.data, digits.target).predict_proba(digits.data)
print([i[1] for i in preds])


Recurrent Neural Network (LSTM) not converging during optimization

I think the problem lies with your text processing to one-hot vectors.
Try using embeddings instead of one-hot vectors.
An embedding is also a n-dimesional vector that allows words with similar meanings to have similar vectorial representation.
One-hot vectors don't have such information. For them, there's a set of words of say cardinality c , then each vector is cx1 . The 1 in the vector just represents its location. It's like bit manipulation in this sense.
So no semantic meaning is preserved. For e.g., in your corpus, there may be words like adore and love. Both have similar meaning. But one-hot vector for love and adore may be far away, depending upon where in set love and adore are mentioned.

But if you use embeddings, words with similar meanings will have similar representations in predefined vector space.

With this, your LSTM will learn dependencies better and will start converging.

Hope this helps :)


You could do

reshape2::recast(
  data = setNames(strsplit(language, ", ", T), language),
  formula = L1~value,
  fun.aggregate = length
)
#                                                                L1 Assembly Go R Ruby Rust Java Javascript SQL C C++ Haskell Matlab PHP Swift Perl Bash Python
# 1                                                          C, C++        0  0 0    0    0    0          0   0 1   1       0      0   0     0    0    0      0
# 2                                                            Java        0  0 0    0    0    1          0   0 0   0       0      0   0     0    0    0      0
# 3                                                         Java, C        0  0 0    0    0    1          0   0 1   0       0      0   0     0    0    0      0
# 4                                     Java, Javascript, Ruby, SQL        0  0 0    1    0    1          1   1 0   0       0      0   0     0    0    0      0
# 5                                                      Java, Ruby        0  0 0    1    0    1          0   0 0   0       0      0   0     0    0    0      0
# 6                                          Javascript, Ruby, Bash        0  0 0    1    0    0          1   0 0   0       0      0   0     0    0    1      0
# 7                                             Perl, Javascript, R        0  0 1    0    0    0          1   0 0   0       0      0   0     0    1    0      0
# 8  PHP, Javascript, Ruby, Assembly, Swift, R, Matlab, Go, Haskell        1  1 1    1    0    0          1   0 0   0       1      1   1     1    0    0      0
# 9                                         Python, PHP, Javascript        0  0 0    0    0    0          1   0 0   0       0      0   1     0    0    0      1
# 10                                                              R        0  0 1    0    0    0          0   0 0   0       0      0   0     0    0    0      0
# 11                                    Ruby, Assembly, R, Go, Rust        1  1 1    1    1    0          0   0 0   0       0      0   0     0    0    0      0


You should use something like an autoencoder.
Basically. you pass your images through a CNN (the encoder) with decreasing layer size.
The last layer of this network is the one that produce the embeddings (that is, a lower dimensional representation of your input), and the number of neurons you use here is the length of your vector embedding for the input images.

Now, your embeddings are useful only if they actual encode the data in your images.
To achieve this, you need another network (the decoder) that takes as input the image embedding and outputs an image with the same dimension of the input.
Here, you try to minimize a loss function that tells you the distance between the image you generate from the embedding and the initial image (it might be Euclidean distance between pixel values).

Finally, if you need to output the embedding of an image, you just need to pass the image through the encoder network and collect the output.

Here's a simple tutorial.


Algorithms that would benefit from variable transformations?

It's not necessary for all features, but most algorithms would benefit from making highly-skewed or heavy-tailed features more Gaussian-like.
Transforming to Gaussian makes the data symmetric and removes both heavy and long tails. That does a similar job of normalizing to $[0,1]$, without aggressively smashing the data into a bounded interval.

Side note: Transforming to standard Gaussian means that the center scale of every feature is the same. This is very helpful in making sure that your model learns "fairly", rather than putting undue influence on the feature with greater variance. See here for an example of how un-standardized data can harm K-means results. See also the comments in the other answer about regularization.


Can all statistical algorithms be parallelized using a Map Reduce framework

Indeed there are:

Gradient Boosting is by construction sequential, so parallelization is not really possible

Generalized Linear Models need all data at the same time, although technically you can parallelize some of the inner linear algebra nuts and bolts
Support Vector Machines


Feature Engineering - correlation with binary outcome - Titanic Dataset - Ticket feature

The thing is that you are looking at correlation with binary outcome. There as some counter intuitive things when doing that (see for example: https://stats.stackexchange.com/questions/103801/is-it-meaningful-to-calculate-pearson-or-spearman-correlation-between-two-boolea).
Outside than that, the features do not appears so much predictive.
More specifically for Is_group: Individual tickets (Is_group = 0), yields à 30% mean, while individuals with group tickets have around 50%. This is not really predictive.
Similarly, the average probability doesn't evolve monotonically of group size (from 0.3, to 0.6 then 0.7, then droping to 0.5 followed by 0 twice then back to 0.23). It doesn't seems really appropriate to use linear correlation to measure the relation between group_size and output here.


How to fill missing numeric if any value in a subset is missing, all other columns with the same subset are missing

I usually use mice for missing data imputation. It relies on chained equations and performs very well. It also has a random forest method, but typing methods(mice) you will find a list of available options,  that can overcome the issues encountered by standard approaches such as logistic regression techniques.
Another option is using the na.impute option available in caret package.


What does pandas describe() percentiles values tell about our data?

It describes the distribution of your data: 50 should be a value that describes „the middle“ of the data, also known as median. 25, 75 is the border of the upper/lower quarter of the data. You can get an idea of how skew your data is. Note that the mean is higher than the median, which means your data is right skewed.
Try:
import pandas as pd
x=[1,2,3,4,5]
x=pd.DataFrame(x)
x.describe()


Algorithm for document retrieval in QA system

I am still not quite sure how you are solving in the problem with tf-idf for a QA system. However, there have been lots of improvements that has been done in the QA domain over the years, with the usage of deep learning for natural language processing.

I would urge you to look at the following approaches that might help you to reach the accuracies you are looking for:

Bidaf Model that was used to solve SQuAD dataset.
BERT model that was used to solve the SQuAD dataset.

You have multiple open source implementations to train the network and use it for prediction. You could  also learn about them in the papers published by the creators of these networks.


Sub topics with Latent Dirichlet Allocation

I actually do not think your method is a good way to find subtopics.  Consider a document X with a distribution of topics z.  X is made up of a mixed model distribution of topic Z.  If you just give a document the most domiant distribution, and then run lda again, you might find subtopics but you'll also refind the topics that should should perhaps not be considered subtopics.

For example, let's consider a document that talks about food and exercising and why certain foods and cardo is good for heart health.  Let's assume that we find topics 1 and 2 such that the main topics are food, and exercise.  Suppose that the domiant distribution is topic 1.  Then running LDA on that particular document again, you're going to refine food, exercise and perhaps nutrition, viatims, etc.  However, there's no real reasons what should be considered the topic without inspecting each one and making an inference regarding it.  Also you'll have no real way to discern how deep you should go down this tree.

The original paper on hLDA can prove useful and exploits the chinese restraurant process and its relationship to dirichlet distribution to form the topic relationship and tree depth problem.  Also on David Blei github (bleilab) has an implentation in c++.  You can essentially setup a little shell script after processing the data in a langauge you may be more familar with and use his code.


Which between random forest or extra tree is best in a unbalance dataset?

Both Random Forest Classifier and Extra Trees randomly sample the features at each split point, but because Random Forest is greedy it will try to find the optimal split point at each node whereas Extra trees selects the split point randomly.
I would choose Random Forest because it's more likely to create a split point that accounts for the imbalanced class, whereas Extra Trees might keep splitting over and over again on a subset of the data without separating out class 3 due to the random split point.


Explanation about i//2 in positional encoding in tensorflow tutorial about transformers

It's not a bug, although they added some confusion with this trick. They should better call their argument $j$ instead of $i$, cos what they actually do is they take all values $0 \leq j \leq d_{model} - 1$ and compute $PE(pos, j)$. $j$ сan be either even or odd, but in the right side of the equation it's even, that's why they compute i//2 and multiply back by 2.


You don't need to change the way optimizer works. You just need to define your loss function in some standard way.

Checkout the answer in this post to have an idea on an example of customizing loss function in Keras.

https://stackoverflow.com/questions/45961428/make-a-custom-loss-function-in-keras