How to merge two CNN deep learning model using weighted sum and weighted product in Keras?

I think the most elegant way is to write a layer that does this. For example for the case of the weighted sum:

class WeightedSum(Layer):

    def __init__(self, a, **kwargs):
        self.a = a  # "weight" of the weighted sum
        super(WeightedSum, self).__init__(**kwargs)

    def call(self, model_outputs):
        return self.a * model_outputs[0] + (1 - self.a) * model_outputs[1]

    def compute_output_shape(self, input_shape):
        return input_shape[0]

Suppose you have two models model1 and model2 with outputs out1 and out2 respectively. This layer simply performs the operation:

$$
out = a \cdot out_1 + (1-a) \cdot out_2
$$

You can compute a weighted product the same way, just change the call method.

Example

from keras.layers import Layer, Input, Dense
from keras.models import Model
import keras.backend as K
import tensorflow as tf

# Define the custom layer
class WeightedSum(Layer):
    def __init__(self, a, **kwargs):
        self.a = a
        super(WeightedSum, self).__init__(**kwargs)
    def call(self, model_outputs):
        return self.a * model_outputs[0] + (1 - self.a) * model_outputs[1]
    def compute_output_shape(self, input_shape):
        return input_shape[0]

# Create model1
inp1 = Input((5,))
d1 = Dense(100)(inp1)
out1 = Dense(10)(d1)
model1 = Model(inp1, out1)

# Create model2
inp2 = Input((7,))
d2 = Dense(70)(inp2)
out2 = Dense(10)(d2)
model2 = Model(inp2, out2)

# Weighed sum of the two models' outputs with a = 0.1
out = WeightedSum(0.1)([model1.output, model2.output])

# Create the merged model
model = Model(inputs=[inp1, inp2], outputs=[out])

Let's check the summary:

>>> model.summary()
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_5 (InputLayer)            (None, 5)            0
__________________________________________________________________________________________________
input_6 (InputLayer)            (None, 7)            0
__________________________________________________________________________________________________
dense_9 (Dense)                 (None, 100)          600         input_5[0][0]
__________________________________________________________________________________________________
dense_11 (Dense)                (None, 70)           560         input_6[0][0]
__________________________________________________________________________________________________
dense_10 (Dense)                (None, 10)           1010        dense_9[0][0]
__________________________________________________________________________________________________
dense_12 (Dense)                (None, 10)           710         dense_11[0][0]
__________________________________________________________________________________________________
weighted_sum_10 (WeightedSum)   (None, 10)           0           dense_10[0][0]
                                                                 dense_12[0][0]
==================================================================================================
Total params: 2,880
Trainable params: 2,880
Non-trainable params: 0
__________________________________________________________________________________________________

Let's see if it works:

import numpy as np

a = np.random.random(size=(32, 5))  # input for model1 (batch size 32)
b = np.random.random(size=(32, 7))  # input for model2 (batch size 32)

pred = model.predict([a, b])

Let's see if it has the right shape:

>>> model.shape
(32, 10)

Let's see if it's the correct thing:

# Generate model outputs manually:
o1 = model1.predict(a)  # model1's output for array a
o2 = model2.predict(b)  # model2's output for array b

# Compute their weighted sum manually:
o = 0.1 * o1 + 0.9 * o2

Now if we're correct, o should be equal to pred:

>>> np.array_equal(o, pred)
True


How to Determine Specific Activation Function from keras' .summary()

You can check the type of activation in layer config.

model.layers[idx].get_config(), where idx is the index of desired layer.


Why does my loss value start at approximately -10,000 and my accuracy not improve?

You are using the sigmoid activation function on the output layer that squashes the values between 0 and 1, but you are setting the targets outside of that range. I suggest scaling your outputs between 0 and 1 for each output unit. By the way, using a library like conx that is built on Keras would give you a warning about this very issue. For more, see: http://conx.readthedocs.io/en/latest/Getting%20Started%20with%20conx.html#A-Simple-Network


How to find if any variable which is normally distributed in pandas dataframe

You can do like so:
from scipy.stats import normaltest

def normal_test(frame, significance = .01, plot = False):
    """
    Function to perform ks test and test against normal distribution using
    D’Agostino, R. B. (1971), “An omnibus test of normality for moderate and large sample size”

    frame: a pandas dataframe
    significance: float. Alpha level for which the null hypotesis will be rejected (H0: series comes from a normal distribution)
    plot: Boolean, whether or not plot a histogram for resulting columns

    returns a dataframe with only those columns that follow a normal distribution according to test.
"""

    columns = frame.columns.tolist()

    for col in columns:
        aux = frame[col]

        _, p = normaltest(aux)

    if p <= significance:
        frame.drop(col, axis = 1, inplace = True)

    if plot:
        fig, ax = plt.subplots(1,1, figsize = (12,7))

    frame.hist(ax = ax, color = "darkred", edgecolor = "black");

    return frame

Test data:
 A = pd.DataFrame({"normal":np.random.normal(size = 1000),
                  "exponential":np.random.exponential(size = 1000),
                  "poisson":np.random.poisson(size = 1000),
                  "normal_2":np.random.normal(size = 1000)})

result = normal_test(A)


See sections 3.2 and 3.3 of the geojson standard.
It looks like value_geojson is a FeatureCollection object, which have a single member "features" which is a list of Feature objects.  So
value_geojson["features"][0]

is now just the first Feature object.  Those have a few members, including "properties", which is an arbitrary JSON object.  At that point we're outside of the geojson standard, so whatever "title" means is more context-specific.


I don't know what kind of image you have, 16 channels?! oh boy :)
Anyway, if they are images, the first one is better. The reason is that in the second approach you are somehow unrolling the input signal. By doing so, you are removing the information about the locality. You are removing the information of near adjacent inputs. Convolutional neural nets attempt to find these kinds of features. As an example, consider the MNIST dataset. You can learn using CNN and MLP but the former is used due to the fact that CNNs care about patterns which are somehow replicated in different parts of inputs.
If they are not images and you are aware that adjacent pixels or inputs are related, again you should exploit CNNs. Consider that the convolutional layers are CNNs are for extracting appropriate features. The classification task is done using dense layers in CNNs.

About efficiency, consider two points. Graphic cards are called SIMD computers. It stands for single instruction multiple data. Matrix operations are done using GPUs very efficiently as the name of Graphic cards implies. Consequently, Dense layers are very fast in GPUs compared to CPUs. The other point is about parallel programming. Each filter in convolutional layers are independent; consequently, they can be applied using paralyzed instructions. Again appropriate GPUs are very good at this too. I know I've said two things but there are actually three things to be kept in mind. Forget all of the above mentioned points! The most important thing to consider is the memory and the bus. There are situations that you don't have a graphic card with more than 6 Gig of memory. In those situations, I really prefer the last generations of CPUs instead of GPUS. The reason is that you have to deal with the limitations of memory.

In your case consider that if you use dense layers exactly after inputs, the number of parameters will be science fictional although You are supposed to use CNNs.


Adding batch normalization layer to VGG16 network

The main purpose of batch normalisation is not for dealing with overfitting but if you have small batches while training it can have regularization effect. In the paper that it was introduced for dealing with covariat shift, it was mentioned that it should be used before activation function. Consequently, you can use it both in convolutional layers and dense layers, after employing weights and before activation functions. But I've seen people using that after activation function. It can also be used there.


Dealing with irrelevant features in dataset (Homework)

This could be many things.

Since you want to calssify the data into three classes, I would use one-hot-encoding, rather than the binary enumeration, because you kind of introduce the information to the model that [1,0] and [1,1] are "closer" than [0,0] and [1,1] for example. Unless this is actually the case, better encode the classes like [1,0,0], [0,1,0] and [0,0,1].
That the eigenvalues in your PCA are large can have two reasons: a) you need to divide XX^T by the number of data points, as the PCA is computed on an approximation of the covariance matrix, which in turn is based on an expectation value
b) not sure if you did, but make sure to center your data first
I think you discovered this yourself already, but if the PCA is useful on your problem depends actually on your data distribution and how you computet the Z. It sounds a bit as if there was no covariance between the features, so there are no primary components to discover. And in case you picked the features to compute Z randomly, you have no guarantee that the primary components tell you anything about Z.
Maybe do a PCA on the data and append Z to X. This might help with dimensionality reduction or identifying relevent features.
Since you have many features and use saturating activation functions, it is important that you normalize the data before passing it to the model and that you initialize the weights properly, otherwise the neurons will saturate and the gradient will be almost zero. Also training might be very slow anyway, as with all the "useless" features you introduce a lot of noise.

I hope this helps. If not, feel free to post some more information on your training data and the neural network you wanted to use.


1) Those extracted features are "combination" of original features. Yes, it's additional information. And yes, it could be redundant, but I'd check that after creating those features by doing standard feature selection operations. Here's a great guide from Kaggle. At the same time, it might be a very good feature engineering decision - completely depends on a dataset.

2) Yes, it matters - redundant information can negatively influence model performance. But you find out to understand whether it's redundant or not. The minimum bad thing you can face - is how much time does training takes (more features - more computation resources). The more serious thing is overfitting.

3) Yes, and it depends on how your model is able to process categorical features. If it cannot do anything specific with them, you should remove the original categorical features. XGboost or CatBoost (those are tree-based both) are able to process categorical features from the box. Generally, tree-based models work good with a lot of features, from my experience. And linear models require right feature selection.

Also, here's a link to a great example on Target Feature encoding, it might be also useful, however, you must be very attentive to train/test splits and overfitting using it.


Extracting keywords from pdf file with python

This is an interesting question!  Let's convert all your PDF files to Word Documents, so you can loop through each one, and read all the contents of each, and then report everything in MS Excel.

Sub OpenAndReadWordDoc()

Rows("2:1000000").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A1").Select

    ' assumes that the previous procedure has been executed
    Dim oWordApp As Word.Application
    Dim oWordDoc As Word.Document
    Dim blnStart As Boolean
    Dim r As Long
    Dim sFolder As String
    Dim strFilePattern As String
    Dim strFileName As String
    Dim sFileName As String
    Dim ws As Worksheet
    Dim c As Long
    Dim n As Long
    Dim iCount As Long
    Dim strSearch As String

    '~~> Establish an Word application object
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")
    If Err Then
        Set oWordApp = CreateObject("Word.Application")
        ' We started Word for this macro
        blnStart = True
    End If
    On Error GoTo ErrHandler

    Set ws = ActiveSheet
    r = 1 ' startrow for the copied text from the Word document
    ' Last column
    n = ws.Range("A1").End(xlToRight).Column

    sFolder = "C:\Users\Excel\Desktop\test\"

    '~~> This is the extension you want to go in for
    strFilePattern = "*.doc*"
    '~~> Loop through the folder to get the word files
    strFileName = Dir(sFolder & strFilePattern)
    Do Until strFileName = ""
        sFileName = sFolder & strFileName

        '~~> Open the word doc
        Set oWordDoc = oWordApp.Documents.Open(sFileName)
        ' Increase row number
        r = r + 1
        ' Enter file name in column A
        ws.Cells(r, 1).Value = sFileName

        ActiveCell.Offset(1, 0).Select
        ActiveSheet.Hyperlinks.Add Anchor:=Sheets("Sheet1").Range("A" & r), Address:=sFileName, _
        SubAddress:="A" & r, TextToDisplay:=sFileName

        ' Loop through the columns
        For c = 2 To n
            If oWordDoc.Content.Find.Execute(FindText:=Trim(ws.Cells(1, c).Value), _
                    MatchWholeWord:=True, MatchCase:=False) Then

                    strSearch = ws.Cells(1, c).Value
                    iCount = 0

                    With ActiveDocument.Content.Find
                        .Text = strSearch
                        .Format = False
                        .Wrap = wdFindStop
                        Do While .Execute
                            iCount = iCount + 1
                        Loop
                    End With

            ws.Cells(r, c).Value = iCount
            End If
        Next c
        oWordDoc.Close SaveChanges:=False

        '~~> Find next file
        strFileName = Dir
    Loop

ExitHandler:
    On Error Resume Next
    ' close the Word application
    Set oWordDoc = Nothing
    If blnStart Then
        ' We started Word, so we close it
        oWordApp.Quit
    End If
    Set oWordApp = Nothing
    Exit Sub

ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub

Function GetDirectory(path)
   GetDirectory = Left(path, InStrRev(path, "\"))
End Function

Test the code above and feed back.  My setup looks like the image below.


Is there a disadvantage to letting a model train for a large number of epochs?

If you can be sure that the model is not seeing the same instances repeatedly then there is very good chances that your model is not overfitting and that is precisely what you can measure from your validation set, you should see continuously downward loss which will eventually plateau at the local optimum that has been eventually reached by your model, that is the best possible solution attained given the starting conditions. If your model would be overfitting then you should see the loss function start increasing for the validation.

The best way to know if you are overfitting is to take your model and then apply it to a completely new dataset and measure the performance. If the performance is good, then you are fine.

p.s. To overcome falling inside a bad optimum you can train multiple models each with different starting conditions for the model parameters and then make an ensemble classifier.


What are the main distribution semantics based algorithms?

The main idea here is: "birds of a feather flock together" That is, words that appear near each other inform the "function" of a word.

More importantly, I think of the techniques you mentioned as "methods," rather than "models." The reason why is it seems possible to violate the definition of what a distributional semantic model is without appropriate data preprocessing.

SVD for example, is typically a dimensionality reduction technique, or the pre-cursor to a clustering technique depending on feature engineering and usage of the methodology. In this case, if you were counting the co-occurrence of words across documents -- rows = documents, cols = words, cells = # of times that word appeared in that particular document -- and then you ran SVD on that, you might have the pre-cursor to a "model" that may eventually be something you can call distributional semantic.

Another example could be Word2Vec, where one typically uses a neural network to train a shallow layer, and extract the weights. Word2Vec can be trained via Skip-gram, or continuous bag of words. Since the "meaning" of a word is derived from the co-occurrence and/or proximity to neighboring words, it may be considered a distributional semantic model. FastText is probably even more so, since it explicitly uses the distribution of words in documents to perform a similar vector operation.

Latent Dirichlet Allocation may be another example. Perhaps even Naive Bayes, if used in the appropriate manner.

So ultimately, the answer is, it depends on data pre-processing/feature-engineering and usage, rather than the technique.


How to replace NA values with another value in factors in R?

You can use this function :

forcats::fct_explicit_na

library(forcats)
fct_explicit_na(DF$col, na_level = "None")

Usage

It can be used within the mutate function and piped to edit DF directly:

library(tidyverse) # for tidy data packages, automatically loads dplyr
library(magrittr) # for piping
DF %<>% mutate(cols = fct_explicit_na(col, na_level = "None"))

Note that "col" needs to be a factor for this to work.


How to supress previous results in a generative network?

I need a way to supress previous answers.

The answer to the question depends on the particular network that you are using. What kind of generative model is it? GAN? Autoencoder? Seq2Seq RNN?

Having said that, if your network keeps outputting the same result, this is usually referred to as "mode collapse", which is typical for GANs. If your network is a GAN, it may be worth exploring the Wasserstein Loss that effectively helps you deal with mode collapse.

Generally, a standard path to achieve diversity (and non-repetitive results) in the output of a generative network is to augment your training dataset. Augmentation refers to representing the same data in different ways. In the context of animal pictures, imagine it as providing different pictures of zebras or horses, from different angles or under different light conditions etc.

Also make sure that your training dataset is balanced, i.e. all classes are represented equally in the training set, otherwise the result will be biased.

If you are using a seq2seq network which generates string names of animals, you usually sample those names character by character. There are different sampling strategies in this case, such as greedy sampling (always the most probable character), multinomial sampling (beam search) which can also have temperature scaling. These strategies yield different results and are worth exploring. A very important metric that enhances the diversity of the output of such a network, is the likelihood of a sampled sequence (or its negative log-likelihood). You want to include this likelihood in your loss function to make sure you "push" the network to sample everything in its reach with equal probability. This is explained nicely in this publication and this publication, which use string representations of molecules that are analogous to your animal names task.

If you are using an autoencoder as a generative network, you can combine it with an optimization algorithm, such as particle swarm optimizer, to navigate within its latent space and automatically sample diverse solutions. The key here is the cost function of the optimization algorithm, because inside it you need to penalize repetitive answers. Similar performance can be achieved using reinforcement learning and a likewise penalizing scoring function.

  I'm not sure if this is because the previous animal names are stored in the short term memory or the synapses for the previous animals are supressed somehow

If you are using stateless LSTM units, the memory is reset between independent predictions. If you use stateful LSTM units, previous predictions affect next predictions.

I realize that my answer covered many different approaches, however it may provide you with different directions to explore.


The lubridate package provides functions to easily convert character strings into dates in a sensible way. For example, the dmy function can parse strings in a variety of formats so long as the string is in the order of day-month-year. The lubridate functions handle all sorts of separators and extraneous characters.
As for your dummy variable, after you convert your v584 column into dates, just create a new column:
new_column <- as.integer(v584 > dmy("Sunday 29th May 2005"))


How to compare performance of Cosine Similarity and Manhatten Distance?

Comparing distance values of different distances is nonsense.

Consider this distance function:

$$d(x,y)=0$$

Clearly, this gives smaller values, but it also is useless.

Consider this trivial variation of Manhattan distance:

$$d(x,y) = \sum_i |x_i-y_i|/100$$

Clearly, this distance is equivalent to Manhattan, but will yield much smaller values.

Choose the distance because it is "the right thing to use", not because of some number. Cosine is good if you desire length normalization and have sparse vectors.


Find periodicity of a signal using python

One option could be applying the Fourier transform which transforms time to the frequency domain. It is often easy to find recurring patterns when signals are represented in the frequency domain.


How to understand Inconsistent and ambiguous dimensions of matrices used in the Attention layer?

First to equation (7): $s_{i-1}$ is a vector, not a matrix. When you multiply it with a matrix $W$, you get another vector of which as the length of the intermediate attention projection, let us call it $d_a$. The shape of $W$ is thus $1024 \times d_a$. Similarly, the shape of $V$ is $512 \times d_a$ and bias $b$ is a vector of length $d_a$. The vector $w$ has $d_a$ dimensions and projects the intermediate projection into a scalar number.

You would probably like to compute everything with large matrix multiplications, which is possible only at training time, and which might the reason for your confusion about the dimensions.

If you have all decoder states in a matrix $S$ of shape $n \times 1024$. You can project it using $W$ into the shape $n \times d_a$.
You can also project all encoder states $H$ of shape $m \times 512$  using $V$ and get a matrix of shape $m \times d_a$ for input and output lengths $n$ and $m$.*
Now, you need to get a tensor of size $n \times m \times d_a$ for the values of $e_{i,j}$.

There is a simple way to implement it, you just reshape the first matrix to $n \times 1 \times d_a$, the second one to $1 \times m \times d_a$ and sum them together. The deep learning/linear algebra libraries will broadcast the tensor (i.e., copy along the singleton dimension), so it will give you a tensor of shape $n \times m \times d_a$. When you multiply this with $w$, you get a table of attention energies of shape $n \times m$, after applying softmax on the last dimension, you get $\alpha_{i,j}$ There is no unknown dimension anywhere.

Equation (8) is just a fancy formulation of the weighted average. In $F$, you have $k$ vectors of dimension $r$, the distribution $\alpha_i$ has length $k$:

$$f_i = \sum_{j=0}^k f_{i,j} \cdot \alpha_{i-1,j}$$

Note that $\alpha_{i-1,j}$ is scalar. This is the context vector form the previous decoding step, i.e., a weighted average of $h_j$ based on the probability distribution  $a_{i-1,j}$.

Equation (9) is basically the same as (7).


Machine Learning algorithms and Cross Validation, the best practice

Cross-validation (CV) is a method meant essentially to accurately evaluate a model using some training data. As a consequence CV doesn't have to be used when training the final model, usually one simply uses the whole training data for that. Note that using one of the CV models would have two disadvantages: there's no reason to select one or the other (the performance on one split is not relevant), and it doesn't exploit the whole training set.
I also notice that you didn't mention any test set evaluation in your steps. A common mistake is to select a model based on parameter tuning (step 3) using CV, and just assume that the CV performance is reliable. It is not because the fact that a choice was made (whether about parameters, models, features...) means that this step is still part of the training process, therefore there needs to be evaluation on a different test set afterwards. In other words: the fact that some parameters perform better than others could be due to chance, so it's important to evaluate the selected model (and only this model) on a fresh test set.


Threshold Value for Random Forest Classifier

Yes and the most important parameter is the tree depth. It's a pre-pruning technique that allows to prevent overfitting. Specifically for sklearn is:

estimator.tree_.max_depth

I suggest you to perform GridSearch on max_depth:

params = {'max_depth':[1,50]}
gs = GridSearchCV(DecisionTreeClassifier(), params)
gs.fit(X,y)

where X is you training set containing instances
and y are the labels.

There are packages that also support post-pruning like this one https://svaante.github.io/decision-tree-id3/


Local minima in Stochastic Gradient Descent

One of the major advantage of stochastic gradient descent (SGD) over batch descent is the ability to explore different local minima's. But due to stochastic nature, you might end up with different results every time you repeat.

In all regression the most important task is to perform regularization to achieve a generalized fit for better cross validation scores. Your issue might be of over fitting rather than stochastic nature of optimizer. In your model.add method, add an argument for kernel_regularizer=L1L2(l1=0.0, l2=0.15). Try with different values for shrinkage of L1, L2 norms.

Also, If you are looking for stability in accuracy you can try batch/ vanilla gradient descent. Also, I would recommend you to try different learning rates and other optimizer like Adam that make use of adaptive learning rates.


model.score and r2_score giving different values for a regression model

Both functions are the same r2 metric and should produce the same results.

Your usage of the r2_score function is wrong. The first argument should be the ground truth values and not the predicted values, so in your case it should be:

score_DT = r2_score(y_test, y_pred_DT)


What is the best Data Mining algorithm for prediction based on a single variable?

Common rule in machine learning is to try simple things first. For predicting continuous variables there's nothing more basic than simple linear regression. "Simple" in the name means that there's only one predictor variable used (+ intercept, of course):

y = b0 + x*b1

where b0 is an intercept and b1 is a slope. For example, you may want to predict lemonade consumption in a park based on temperature:

cons = b0 + temp * b1

Temperature is in well-defined continuous variable. But if we talk about something more abstract like "weather", then it's harder to understand how we measure and encode it. It's ok if we say that the weather takes values {terrible, bad, normal, good, excellent} and assign values numbers from -2 to +2 (implying that "excellent" weather is twice as good as "good"). But what if the weather is given by words {shiny, rainy, cool, ...}? We can't give an order to these variables. We call such variables categorical. Since there's no natural order between different categories, we can't encode them as a single numerical variable (and linear regression expects numbers only), but we can use so-called dummy encoding:  instead of a single variable weather we use 3 variables - [weather_shiny, weather_rainy, weather_cool], only one of which can take value 1, and others should take value 0. In fact, we will have to drop one variable because of collinearity. So model for predicting traffic from weather may look like this:

traffic = b0 + weather_shiny * b1 + weather_rainy * b2  # weather_cool dropped

where either b1 or b2 is 1, or both are 0.

Note that you can also encounter non-linear dependency between predictor and predicted variables (you can easily check it by plotting (x,y) pairs). Simplest way to deal with it without refusing linear model is to use polynomial features - simply add polynomials of your feature as new features. E.g. for temperature example (for dummy variables it doesn't make sense, cause 1^n and 0^n are still 1 and 0 for any n):

traffic = b0 + temp * b1 + temp^2 * b2 [+ temp^3 * b3 + ...]


predict_proba to print specific class probablity

Once you fit your sklearn classifier, it will generally have a classes_ attribute. This attribute contains your class labels (as strings). So you could do something as follows:

probas = model.predict_proba(dataframe)
classes = model.classes_

for class_name, proba in zip(classes, probas):
    print(f"{class_name}: {proba}")

And to find a specific index, you can use numpy's where function:

import numpy as np

class_label = "XYZ"
class_index = np.where(model.classes_ == class_label)

proba = model.predict_proba(dataframe)[class_index]


Why is there such a mismatch between the Model's predicted probability and theoretical probability in logistic regression?

It's probably due to the effect coding of categorical predictors. Eg, the regression coefficient for CPR = 7 is not zero but -(sum of regression coefficients for the other 6 levels). I guess EM should have an option to switch it to reference coding, then your way of computing the predicted probability should work.


Should I create metafeatures for my XGBoost training set?

Yes.

You should think about the physical meaning of each proposed metafeature, and whether it's relevant to the problem at hand. For example, suppose you're interested in patient temperature. You might add in $height^2$ and $height^3$, as they are roughly proportional to surface area (through which patient loses heat to a cool environment) and proportional to volume (tissue respirating to generate heat). Of course, you might choose to discard that last one as silly if patient $weight$ is part of your dataset. If your subjects are mammals ranging from mouse up to elephant, that cubic entry might still hold some predictive power.

Multiplying a pair of prices to obtain a column with units of dollars squared would not be a good idea.

In the familiar iris dataset, multiplying length by width to get sepal area or petal area definitely makes sense, as does dividing them, to obtain an aspect ratio.

If a metafeature doesn't improve training error, then of course discard it and move on.


Best way to visualize huge amount of data

I have three suggestions that may help.

Reduce the point size
Make the points highly transparent
Downsample the points

Since you do not provide any sample data, I will use some random data to illustrate.

## The purpose of S1 is to intermix the two classes at random
S1 = sample(3000000)
x = c(rnorm(2000000, 0, 1), rnorm(1000000, 3,1))[S1]
y = c(rnorm(2000000, 0, 1), rnorm(1000000, 3,1))[S1]
z = c(rep(1,2000000), rep(2,1000000))[S1]

plot(x,y, pch=20, col=rainbow(3)[z])

The base plot without any adjustments is not very nice. Let's apply suggestions 1 and 2.

plot(x,y, pch=20, cex=0.4, col=rainbow(3, alpha=0.01)[z])

Reducing the point size and making the points highly transparent helps some. This gives a better idea of the overlap between the two distributions.

If we downsample, we don't need quite as much transparency.

## The purpose of S2 is to downsample the data
S2 = sample(3000000, 100000)
plot(x[S2],y[S2], pch=20, cex=0.4, col=rainbow(3, alpha=0.1)[z[S2]])

This gives a different view that provides a similar, but not identical understanding of the two distributions.

These are not magic, but I think that they are helpful.


Tensorflow neural network TypeError: Fetch argument has invalid type

The problem lay in using the name 'cost' on two occasions, the problem was solved by changing this:

_, cost = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output})

to this:

_, c = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output})

This way the name of the variable 'c' doesn't clash anymore with the [optimizer, cost] part of the code.


Creating a single number from a numpy array - Python

Welcome to Data Science! You're question needs a little more detail... there are many many ways to make an array into a single number. You should say a little more about what you mean by meaningful.

Here are a few examples, using your example array, which may seem outrageously simple, but do indeed form the basis to many of the techniques used in modern research:

In [1]: import numpy as np

In [2]: x = np.array([[129, 155, 191], [123, 150, 185], [120, 149, 183]], dtype=np.float32)

In [3]: x
Out[3]:
array([[129., 155., 191.],
       [123., 150., 185.],
       [120., 149., 183.]], dtype=float32)

Now here are a few (randomly selected) ways to create a single number from your array:

In [4]: np.mean(x)    # the mean / average
Out[4]: 153.88889

In [5]: np.sum(x)    # the sum
Out[5]: 1385.0

In [6]: np.std(x)    # the standard deviation
Out[6]: 25.722641

In [7]: np.linalg.norm(x)    # the Frobius norm - a distance measure
Out[7]: 468.07156

In [8]: np.max(x)
Out[8]: 191.0

These might seem stupidly simple, but if we were to treat your array as a single block from a larger image-array, then these might represent pooling-layers that are used to down-sample arrays as they are passed through a neural network. Just have a look at the available pooling layers within the Keras library.

Which method you might want to use will heavily depend on your use case, your dataset and your model.


optimizing a linear optimization function with linear constarints and binary variables

Use scipy.optimize.minimize of the negative objective function.
Use the link to play and modify as you wish but here is application in the nutshell.
Lets say you have your data
> array([[ 0.00749589,  0.01255155,  0.02396251,  0.04750988, 0.09495377]
>        [ 0.01255155,  0.02510441,  0.04794055,  0.09502834,  0.18996269],
>        [ 0.02396251,  0.04794055,  0.09631614,  0.19092151,  0.38165151],
>        [ 0.04750988,  0.09502834,  0.19092151,  0.38341252,  0.7664427 ],
>        [ 0.09495377,  0.18996269,  0.38165151,  0.7664427,   1.53713523]])

You should define your constraints dictionary in the following way (for example):
> cons = ({'type': 'eq', 'fun': lambda x:  x[0] - 2 * x[1] + 2},
> {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
> {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})

(accesing array/columns with indexing (x[0]) thats exactly your bats, bowls etc columns).
Next just pass your negative objective function and you maximized it.


When should i use fit(x_train) and when should i fit( x_train,y_train)?

When you are fitting a supervised learning ML model (such as linear regression) you need to feed it both the features and labels for training. The features are your X_train, and the labels are your y_train. In your case:
from sklearn.linear_model import LinearRegression
LinReg = LinearRegression()
LinReg.fit(X_train, y_train)

If you are performing some unsupervised learning approach, or simply some data transformation (such as PolynomialFeatures) you simply fit on your feature space (X_train) since there are no labels required for such an approach. Like so:
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(2)
poly.fit(X_train)
X_train_transformed = poly.transform(X_train)

For your second point - depending on your approach you might need to transform your X_train or your y_train. It's entirely dependent on what you're trying to do.
As for your last point - never ever fit on testing data. It defeats the purpose of a train/test split. Usually what is done is your pipeline step is fit either with X_train and y_train or just X_train alone. This fit transformer can then be applied to your testing data (X_test) using the .transform() method but never use this data for .fit()


How to write a generator for keras fit_generator?

Ill supply two tutorials I used when I first started using fit_generator. That being said the first thing to remember is that a generator is essentially like any other function your write that returns something with the exception is that the function runs a continuous loop that is designed not to exit. For example, in a normal function, you would use return to return some chunk of data every time that function is called. In a generator, the function returns data in chunks continuously until there is no data to return. This is what the yield statement does. On my project, I have a couple of terabytes of signal data. Even on a powerful server that is a little bit too much data for hardware I use. So the generator function serves up chunks of data in batches, it can be run in parallel as well to increase speed. This approach also greatly reduces your memory usage and usually, your system's memory will hit some point and not really change until all data has been fed to the DNN. I apologize for the poor explanation but I am pretty new to this as well :-) I am sure someone else will provide a better answer but this should get you started.

Here is one good tutorial https://www.pyimagesearch.com/2018/12/24/how-to-use-keras-fit-and-fit_generator-a-hands-on-tutorial/

and a very short one https://medium.com/@fromtheast/implement-fit-generator-in-keras-61aa2786ce98

also, look at the docs
https://keras.io/models/sequential/

and finally here is one I use on a current project. It is not very pretty but it works well

def batch_generator(X_train, Y_train):

    while True:
         # samples_counter = 0
        for fl, lb in zip(X_train, Y_train):
            sam, lam = get_IQsamples(fl, lb)
            max_iter = sam.shape[0]
            sample = []  # store all the generated data batches
            label = [] # store all the generated label batches
            i = 0
            for d, l in zip(sam, lam):
                sample.append(d)
                label.append(l)
                i += 1
                if i == max_iter:
                    break
            sample = np.asarray(sample)
            label = np.asarray(label)
            yield sample, label


Why is logistic regression not sigmoidal?

if would suggest you implement the following formulas:

Sigmoid activation function

$$\sigma(x) = \frac{1}{1+e^{-x}}$$

Output (prediction) formula

$$\hat{y} = \sigma(w_1 x_1 + w_2 x_2 + b)$$

Error function

$$\text{Error}(y, \hat{y}) = - y \log(\hat{y}) - (1-y) \log(1-\hat{y})$$

The function that updates the weights

$$ w_i \longrightarrow w_i + \alpha (y - \hat{y}) x_i$$

$$ b \longrightarrow b + \alpha (y - \hat{y})$$

If you want the corresponding Python code for the above just let me know. I also have it but I guess it would be more useful for you (from the point of view of learning) to implement it yourself. In any case, if you want it, I can update my answer with the Python code.

Source code:

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def output_formula(features, weights, bias):
    return sigmoid(np.dot(features, weights) + bias)

def error_formula(y, output):
    return - y*np.log(output) - (1 - y) * np.log(1-output)

def update_weights(x, y, weights, bias, learnrate):
    output = output_formula(x, weights, bias)
    d_error = y - output
    weights += learnrate * d_error * x
    bias += learnrate * d_error
    return weights, bias


References for longitudinal data analysis

In data science, people normally refer to “longitudinal data” as one or more time series. So you can try searching for that. A classic is to use ARIMA (available in many languages including R). Personally I also like Facebook’s Prophet library as a starting point, because it’s easier to use and works surprisingly well. Also available in R.


Why did sampling boost the performance of my model?

As you mentioned in a comment, you are upsampling before splitting the test set, which leads to data leakage; your scores are not to be trusted.  The problem is that a given positive sample may be duplicated and then put into both the training and the test set.  Especially with tree models, this is very likely to correctly predict that sample in the test set.  The story with SMOTE is similar, but as you pointed out, not quite so severe.  In SMOTE you're interpolating between positive samples (see image from imb-learn docs), so if some of those points are in the training set and some in the testing set you're still more likely to correctly identify those points.

Instead, you should split first, upsample the training set second.  Alternatively, set class weights (this has the benefit of being independent of the split).  Now your test set has a different distribution that the training set, so you'll need to adjust the class prediction threshold, or adjust the probability predictions.  See e.g. "Convert predicted probabilities after downsampling to actual probabilities in classification?".  Part of the question here is whether you want actual estimates of the probabilities, or just care about the class predictions.

There's a serious question about whether resampling techniques are helpful at all.  See e.g.
"What is the root cause of the class imbalance problem?"
"When is unbalanced data really a problem in Machine Learning?"
As a first attempt, I would stick with the original data, fit the random forest, and have a look at different thresholds.

In your case, I would worry that 88 positive samples may just not be enough to see a meaningful pattern.  (It might be; it depends on how separated the classes are.)


Your question 1:

You are close but not exactly.

Each time you take the mean, you include only the samples with the given value, say, A, of the categorical feature.

You partition #2-#20 folds into 10 equal folds; however, once you consider only the samples with the category value A, their numbers may be different in these 10 folds.

Say, these numbers are n1,...,n10.

Then the mean of all but the first fold assigns the weight of 0 to the samples in the first fold and the weight of 1/(n2+...+n10) to each sample in all other folds.

The mean of all but the second fold assigns the weight of 0 to the samples in the second fold and the weight of 1/(n1+n3+n4+...+n10) to each sample in all other folds, etc.

Taking the average of these means yields the weighted average of the target with the weights of the samples being the averages of the above weights.

These weights will be slightly different for samples in different folds.

Thus, we get the weighted average of #2-#20 folds but with slightly different weights obtained by this random procedure.

You probably want to understand what is the point of this: how is it better than just taking the plain average over folds #2-#20?

This process adds randomality to the values of the encoding.  (The randomality can be measured, e.g., by the standard deviation of the encoding values for the category value A.)

This randomality of the encoding values may prevent the algorithm from learning the relationship between the encoding and the target in case this relationship holds only in the training set, i.e., reduce overfitting.

This randomality depends on two parameters:

The width of the distribution of the target value for the samples having the category value A.  (The width may be measured, e.g., by the standard deviation.)  Wider distribution in these target values results in greater randomality (wider distribution) of the encoding values.
The number of samples having the category value A: more samples result in smaller randomality of the encoding values.

One can compare this way with other ways of increasing the randomality of the encoding values.  The simplest way is to do single cross-validation but with less than 20 folds.  This will increase the randomality of the encoding values but it will mainly depend on the width of the distribution of the target values and less on the the number of samples in the category A.  This could be the reason for this double cross-validation.

Your question 2:

As you said, the point is to use for each sample the average of the target values that does not include this sample.

For a sample in the fold #1 with the category value A, the value of the mean target encoding is the (weighted) mean of the target values over all samples in the folds #2-#20 with the category value A (calculated as above).

This means that the values of the encoding of A will be different in different folds.

The post you quoted, does not mention the test set.  Other posts suggest that on the test set you take the average of the target values of all samples that have category A in the entire train set.


The shape of the labels array was ( 25 , 1 ). The shape of your previous model was ( 25 , 20 ). The dimension 20 was because of this line.

model.add(TimeDistributed(Dense(20)))

The Dense layer had an output dimension in the form of units= argument  as 20. 25 was the length of the sequence.

  So, on each of the 25 timesteps, a Dense layer operation was done and
  each produced a array of shape ( 20 , ). This process happened for 25
  timesteps which resulted in a final output of shape ( 25, 20 ).

RepeatVector : It repeats the input vector given to it n number of times.

Timedistributed: Executes the layer operation which is wrapped in it ( in your case it was Dense layer ) for every timestep in the given input sequence.

To know more about the above layers, refer to this answer of mine.

Hence, In the last layer changing Dense( 20 ) to Dense( 1 ) will correct the vector dimension.


Classification problem with many images per instance

This might be a hack, but, have you considered repeating some images to make every instance contain a fix number of images? Let's say you have images A, B, C, D, E, F, G, H, and I. Your dataset might look like this:

A , B
C
D, E, F
G
H, I

Make a function that will randomly select one of the image in an instance and then use that as placeholder. From our example, we now modify our dataset into:

A, B, A
C, C, C
D, E, F
G, G, G
H, I, I

Then, from here, you can use a convolutional neural network with 3 inputs. Here is an example of a CNN with multiple inputs: Multi-Input Convolutional Neural Network for Flower Grading - images of same flowers taken at different angles.

Also, you can shuffle the images in each instance to produce more training samples.


In order to fix the problem of vanishing gradients, you can use Xavier Initilization. Also, the implementation of Xavier Initialization in tensorflow can be done by following this thread.


I can't say for sure but I think the issue here is you're not subtracting the mean of the rewards.

The idea is that actions with above average reward are positive after mean normalization, while actions with below average reward are negative after mean normalization.

Your update step is -log(P(action))*reward, which you then minimize with your optimizer.

P(action)<1 therefore log(P(action))<0, -log(P(action))>0

If reward>0, -log(P(action))*reward>0. Minimizing this value is the same maximizing log(P(action))*reward<0, which is maximized when P(action)=1.

Conversely, if reward<0, -log(P(action))*reward<0. This has the opposite effect, where P(action) is driven to 0.

The important part is the different sign on above average/below average rewards causes actions associated with good rewards to have their probability increased, while actions associated with bad rewards have their probability decreased.


Categorization of Natural Language Processing Tasks

Basically, you need to know about the unsupervised learning tasks in NLP. For this, we mostly vectorize the input sentences using an embedding matrix.

Text Summarization:

Text Summarization not based on Neural Networks is used in a number of systems. These systems vectorize the input and then rank the sentences using a ranking algorithm ( like TextRank ) and cosine similarity based on their importance. The most important sentences are then given as output. You can read more here.

Similarity between two documents:

If you have a powerful document vectorizer like Doc2Vec, then the vectors of similar documents are similar to each other with some score. This score can be derived using cosine similarity.

  A technology blog will have a higher score of similarity with a computer science based blog rather than a holiday/tourism blog.

Such systems are useful in many use cases. Clustering algorithms have also been applied here.

Sentiment Analysis is not an unsupervised learning task. It is a classification task:

For training a sentiment analysis model, you need a dataset which consists of text and its corresponding sentiment ( categorical or binary ). Such a model could not be achieved through clustering or ranking methods. But, yes, these models use word embedding like the other models.

Some links which describe the basic tasks categorized under NLP:

https://natural-language-understanding.fandom.com/wiki/List_of_natural_language_processing_tasks
https://www.analyticsvidhya.com/blog/2017/10/essential-nlp-guide-data-scientists-top-10-nlp-tasks/
“Deep Learning for NLP: An Overview of Recent Trends” by Elvis https://link.medium.com/iItIayc0NW
https://blog.algorithmia.com/introduction-natural-language-processing-nlp/


Varying the number of neighbors in nearest-neighbors regression

"A good algorithm for varying the number of neighbors" is probably quite data-dependent. But there is, for example, the fixed radius approach.


No, there aren't.  I wish there were.  It would make life a lot easier.

A categorical variable is just that:  a category.  It might be represented by a number but it cannot be treated like one.  For example, if you have a list of cars with their colors, and the colors are denoted this way:

1 = red
2 = blue
3 = green

you can't really work with them as numbers.  You can't say that blue is the average color, for instance, because the average of 1 + 2 + 3 is 2.

What you can do is create dummy variables, usually a 0 and 1, which then describe the category.  In our example:

0 = not red
1 = red

You can then create dummies for the other colors along similar lines:

0 = not blue
1 = blue

and

0 = not green
1 = green

This can be done across different columns to make things easier.  It's all part of the mungification of data, getting it into shape so you can run an analysis.


You are missing one argument in your code. Following will fix your problem.
model.fit(training_set,
          training_steps= training_set.samples//training_set.batch_size,
          validation_data=validation_set,
          epochs=5,
          verbose=1,
          validation_steps=validation_set.samples//validation_set.batch_size)


Why an eigenvector might be reasonable notion of centrality

Let the adjacency matrix of our network be $A∈\{0,1\}^{n×n}$ with an empty diagonal ($A_{ii} = 0 ∀i$).

Direct approach

Let’s start with the approach that a node’s centrality ($C_i$) shall be proportional to the sum of the centralities of its neighbours with a proportionality constant $\frac{1}{λ}$ (chosen thusly with some foresight):

$$ C_i = \frac{1}{λ} \sum_{j=1}^{n} A_{ij} C_j.$$

This is nothing but a line-wise formulation of the matrix–vector multiplication:

$$ λ \vec{C} = A·\vec{C},$$

which is exactly the definition of an eigenvector. Now, to see why the largest eigenvector is chosen, we can turn to the Perron–Frobenius theorem, which tells us that for this eigenvalue (and in case of a connected network only for this eigenvalue), we can find an eigenvector all of whose components, i.e., the eigenvector centralities are positive.

Iterative approach

Alternatively, we can interpret the above ansatz iteratively:

Assign random positive values to the components of $\vec{C}$.
Update these values according to:

$$\vec{C} ← \frac{A·\vec{C}}{\left| A·\vec{C} \right|}.$$

This means that each component is updated according to:

$$C_i ← \frac{1}{\left| A·\vec{C} \right|} \sum_{j=1}^{n} A_{ij} C_j,$$

i.e., you say the new centrality of a node is the sum of the centralities of its neighbours – times some normalisation to avoid values getting very big.
Repeat Step 2 until the centralities converge. The idea is that if this converges to a unique result, this result does not only fulfil $C_i \propto \sum_{j=1}^{n} A_{ij} C_j$ but is also robust in that respect.

For almost all initial choices of $\vec{C}$, this will converge to the positive, length-1 eigenvector to the largest eigenvalue (which exists and is unique for a connected network, see above). The reason for this is that components along the eigenvector to the largest eigenvalue will be most enlarged by the multiplication and thus dominate the others over the iterations. (Having no components along the eigenvector to the largest eigenvalue does not happen in reality and is the reason why it’s almost all above.)

Note that such an iteration can also be used to numerically determine the largest eigenvalue.


SVM model classifying into one class only, after standardization

I can't add a comment so I'll post this here.

My main question is why are your feature values so wildly different different between you your test and training sets?

For example the raw values of feature 1.

Training set: All values > 20, looks like they average about 35-40.
Test set: All values around 2, looks like they all fall between 2-3.

What is the reason for this. Also have you shuffled you data before performing a train  test split? The you should plot a distribution of the feature values for both your training and test sets sets, and make sure they are somewhat similar.

It looks like you you have a bad covariate shift between training and test sets. Don't worry about this yet as I think it's a data preprocessing problem. First answer the question if the distribution of values of each feature are similar between training and test sets.


Pandas, make the bars of a bar graph have equal widths

Looking at the documentation, it seems there is no obvious way.

Looking at the source code of the plt.bar method (held on Axes objects), and searching for uses of the width parameter, it starts getting quite complicated and I don't think it is meant to be used for this purpose explicitly! Lots of internal methods, starting with a _, like self._convert_dx is a clue.

I would just offer a nasty hack for your toy example, hoping it is applicable to your actual data:

In [1] : import pandas as pd
    ...: import matplotlib.pyplot as plt
    ...:
    ...: fig, (ax1, ax2) = plt.subplots(2)
    ...:
    ...: pd.Series([0, 1, 0]).plot(kind='bar', ax=ax1)
    ...: pd.Series([1, 2, 3]).plot(kind='bar', ax=ax2)

Gives

So the hack is to make the length of your datasets the same, adding zeros where needed.


The callback you are using isn't for displaying the desired metrics, just recording them. For example if you want to access the F1-score you need to type: metrics.f1s. This is useful, let's say, if you want to make a graph on how the F1-score reduced during training.

To use the EarlyStopping callback, however, f1-score needs to be a metric not a callback like you have it!

You need to write (or find) a function that calculates the F1-score through keras' backend functions. You might want to check if this works for you.


Derivative of activation function used in gradient descent algorithms

As the name suggests, Gradient Descent ( GD ) optimization works on the principle of gradients which basically is a vector of all partial derivatives of a particular function. According to Wikipedia,

  In vector calculus, the gradient is a multi-variable generalization of the derivative.

At its core, GD computes derivatives ( in terms of Neural Networks ) of a composite function ( a neural network is itself a composite function ) because of the gradient descent update rule, which is,

$\Large \theta = \theta - \alpha \frac{\partial J}{\partial \theta}$

Where $\theta$ is the parameter which needs to be optimized. In a neural network, this parameter could be a weight or a bias. $J$ is the objective function ( loss function in NN ) which needs to be minimized. So for $\frac{\partial J}{\partial \theta}$, we need to repeatedly apply the chain rule till we have a derivative of the loss function with respect to that parameter.

Intuition:

Sorry for the weird image. When GD is far away from the function minima ( where it tends to reach ) the value of $\frac{\partial J}{\partial \theta}$ is greater and therefore the updated value of $\theta$ is smaller than the previous one. This updated value is scaled by the learning rate ( $\alpha$ ). The negative sign indicates that we are moving in the opposite direction of the gradient.


I would say that trying out the resources online such as the Explore Data Science course and Andrew Ng's ML Course on Coursera (mentioned in the notes above) are great. However, nothing beats true data that you understand inside and out. After picking up some skills from Explore Data Science, I propose you collect your fuel usage data continuously. Collect the basics: city, state, gallons, total cost, and your vehicles mileage. When you forget to enter it, then you have to learn how to work with missing data. As you feel more comfortable, enter in kaggle contests. The data science community is growing so quickly that the online resources are vast and will help you identify any areas where a text book is what you need. Good Luck!! Have fun!


What are the activation functions in Convolutional Layers for?

1 - Activation functions are non-linear functions. These are added in between layers which are simply Linear transformations.
Example without activation function:
ConvLayer1(Input) -> ConvMaps1
ConvLayer2(ConvMaps2) -> ConvMaps2

Mathematically, this would be $I_{nput} \circledast K_{ernel_1} \circledast K_{ernel_2} $,
which is equivalent to $I_{nput} \circledast K_{ernel_3} $,
where $K_{ernel_3} = K_{ernel_1} \circledast K_{ernel_2} $
That means that creating two layers would simply waste computational effort and give no gain whatsoever.
2 - Yes, if you keep the same image Width and Heigh and just stack more channels, there convolution maps will grow out of control, this is why you compensate this with Pooling Operations and use of large strides. Also, increasing the number of parameters without proper care and architecture design usually leads to overfitting.
3 - Backpropagation is simply a computationally efficient and convenient way of expressing the Partial Derivatives of the function and acts exactly as it would if you just took the derivative of the whole function at once. There is plenty of content online on it, you probably didn't understand the process of weight updating.


convert single index pandas data frame to multi-index

One way to pull the embedded dataframe up into the main dataframe and build a multi index is like:

Code:

def expand_tweets(tweets_df):
    tweets = []
    for uid, sub_df in tweets_df.set_index('uid').tweets.iteritems():
        sub_df['uid'] = uid
        tweets.append(sub_df)
    return pd.concat(tweets).merge(
        tweets_df.drop('tweets', axis=1).reset_index(),
        how='outer', on='uid').set_index(['uid', 'id'])

How:

Pull all of the tweet dataframes out of main dataframe using uid as an index, and concat() them together with their uid.
Then merge the main dataframe into the concatenated tweets dataframe.
Set the desired index.

Test Code:

import json
import pandas as pd
with open('test.json') as f:
    df = pd.DataFrame(json.load(f))
df['tweets'] = df.tweets.apply(lambda x: pd.DataFrame(x))

print(expand_tweets(df).text.head())

Results:

uid         id
1153859336  655060275025047552    Article on my new Haunted Stevenage book Paran...
            653912439940120576    Big thank you to @bobfmuk for interviewing me ...
            643709869908996096    Another interesting non-toadstool tweet today,...
            547107275681579008    @sisax67 Thanks, Simon. All the best to you &a...
            546693940024733696    Paul Adams @SkySportsDarts The Wanderer from W...
Name: text, dtype: object


How to transform an imbalanced attribute to make it more suitable for linear regression?

You have multiple options and you may choose the best by seeing the performance.

First one is to use a guess. For positive-only values the log-transform is a hot candidate to make sense (maybe correct for small values here to avoid exceedingly large negative transformed values).

Log-transform is natural if percentage increases have a particular real-world meaning. This is often the case for financial data. Why is log a hot candidate? You probably know that when there are addititve real world effects, the normal distribution often appears. Now, when you have multiplicative effects, you get the https://en.wikipedia.org/wiki/Log-normal_distribution .

Other common transformations are power transforms, where you take some power of the values. I don't think there are many more which are very common. Theoretically, your perfect transform would make the noise on the linear regression Gaussian, but no-one can tell what that would be and most likely reality isn't perfectly linear anyway.

A transform is more interesting when the transformed values follow a Gaussian distribution. But that is just a guess and in the end only final performance evaluation can tell more.

For a second option, be aware that you can force the transformed values to be any distribution you want. For example, if you take ranks of the values you get a uniform transformed distribution. You could even force it to be a Gaussian by a suitable mapping. However, in your case, this will lose the interesting bump on the right.

I think these are the most common options. In data science, nothing is ever obvious and most of the time you can only decide by performance evaluation (cross-validation with the whole model).

Conclusion:

Set up a performance test (cross-validation; not on final test set though if you like a fair, final evaluation) and try all of the following

Try untransformed. It might already be what has most information.
Try log-transform (while adding a small offset if you have some very small values)
Try power-transform if you like


A fundamental question about the gradient descent equation

You are looking for a local minimum, instead of a global minimum value.

The local minimum has $\frac{dL}{dW} = 0$, there is no direction of $W$ in which you can further reduce the loss.

If you are near the local minimum, you want to take small steps, not to 'overshoot'. If you are further away, you can take larger steps to speed up things, because you do have an idea about the direction of the minimum, namely the gradient.


Good chromosome representation in a VRPTW genetic algorithm

Given you want to maximize fitness function $\phi:\Bbb{R}^n\rightarrow\Bbb{R}$, make $\phi(\vec{x})=-\infty\ \forall \vec{x}\ \notin X$ where the feasible region is $X\subset \Bbb{R}^n$. Then you can initialize the population $P=\{p\ |\ p\in X\}$ and if the GA tries any $p\notin X$, that solution would be killed off. This forces the GA to "learn" the feasible region.


Homemade deep learning library: numerical issue with relu activation

I don't know exactly what the problem is, but maybe you could try checking the value of your gradients and see if they change a lot around the 8th epoch.


Is it possible to train probabilistic model to return several distributions?

That data can be modeled as a statistical process, where the distributions and parameters change as a function of x. This is in contrast to modeling it as a typical statistical distribution which assumes the same distribution and parameters throughout a range.
The "a" range could be modeled as a bimodal distribution and the "b" range could be modeled as a unimodal distribution.


Binary predication from binary variables

This is really a job for Logistic Regression. Input variables can be categorical/boolean and the prediction can be categorical/boolean as well.

However, if your target variable contains multiple categories, you should use multinomial logistic regression or Ordinal Logistic Regression (if there is an order in your categories). It also changes the way you interpret the output.

The another approach would be to take any nonparametric method, Decision Tree for example. Where input and target can be boolean/categorical/continuous. Note that in case of a categorical variable, it is often recommended to transform it into dummy variables.


Shaping data for ConvLSTM for many-to-one image model

The problem was for each ConvLSTM layer I was using keep_dims = True which means that the number of dimensions in the input is reflected in the ouput. To obtain a many to one model I just needed to set keep_dims = False. I was then able to manipulate the size of the output using dense layers and convolutions. In this way I could have a video sequence as an input and a single 2D image as the output and I was able to obtain the relationship:

input shape : (10, 50, 256, 400, 1)
output shape:     (10, 256, 400, 1)

Where I was able to get a 2D segmentation for each fragment in the video sequence.

It is not exactlly what I needed, but was much better than getting a segmentation result for every frame in the video sequence.


predicting next observation using HMMLearn.multinomialhmm(discrete hmm)

i found out that there is no function to do this just can use model.predict() to get hidden states probabilities and then find out the next state(and observation) using Viterbi algorithm.


TypeError: unhashable type: 'numpy.ndarray'

The problem is that you're passing a list of numpy arrays to the mode function.
It requires either a single list of values, or a single numpy array with values (basically any single container will do, but seemingly not a list of arrays).

This is because it must make a hash map of some kind in order to determine the most common occurences, hence the mode.  It is unable to hash a list of arrays.

One solution would be to simple index the value out of each array (which then means mode gets a list of integers). Just changing the main line to:

max_voting_pred = np.append(max_voting_pred, mode([a[i][0], b[i][0]]))

Let me know if that doesn't fix things.

If you want something that is perhaps easier than fixing your orignal code, try using the mode function from the scipy module: scipy.stats.mode.

This version allows you to pass the whole array and simply specify an axis along which to compute the mode. Given you have the full vectors of predictions from both models:

Combine both arrays to be the two columns of one single (200, 2) matrix

results = np.concatenate((y_pred, vgg16_y_pred), axis=1)

Now you can perform the mode on that matrix across the single rows, but all in one single operation (no need for a loop):

max_votes = scipy.stats.mode(results, axis=1)

The results contain two things.

the mode values for each row
the counts of that mode within that row.

So to get the results you want (that would match your original max_voters_pred, you must take the first element from max_votes:

max_voters_pred = max_votes[0]


Where can I find out about the "Helvetica scenario"?

Placing too much probability mass on a single $x$ is indeed a major problem with GANs. Usually it is referred to as mode collapse. Most GAN papers will discuss it.

  I was a bit confused as to how training  for a shorter amount of time before running  can fix it.

The idea is to keep the discriminator $D$ up to date with the generator $G$. By training $G$ less, you are training $D$ more. Usually, an alternating optimization is being done with GANs, i.e. train $D$ for $n$ iterations, then $G$ for $m$ iterations, and so on. The "job" of $D$ is to be able to discern fake $G$ outputs from real data points. But, if you don't train $D$ enough (i.e., you train $G$ too much, so $m>n$), then $G$ will quickly "run off" and exploit local minima in $D$. From $G$'s point of view, if $D$ "likes" some output $x=G(z)$, then the easiest thing to do is just only output that $x$! Training $G$ less (and thus $D$ more) allows this minima to move, and prevents $G$ from exploiting them.

Notice that overtraining $D$ doesn't seem to be a bad thing a priori.
Indeed, with more modern GAN losses (e.g., Wasserstein ones), one wants to $n$ to be as large as possible.
It just means that $D$ converges to being a great discriminator at every step, so it should be informative for $G$. (Indeed, proofs of convergence hinge on doing this!) But in practice, for the original GAN, overtraining $D$ tends to lead to useless gradients (see below): nothing $G$ can do will please the discriminator, so all weight updates are useless, so the GAN just futilely flails around parameter space). This flailing is called unstable training by others. :)

In other words, for the vanilla GAN, undertraining $D$ can lead to mode collapse, while overtraining it can cause instability. People seemingly have focused on mitigating instability so that one simply overtrains $D$ and avoids mode collapse.

  how it can be prevented?

One common way to mitigate it is to use a different loss function, such as a Wasserstein loss. One problem with the original GAN formulation is that (by minimizing the JS divergence) can have very uninformative gradients because the loss does not handle distributions with very little overlap very well (i.e., when the shared support is small). In some sense, it is too harsh when the generator is doing poorly. In contrast, an optimal transport loss smoothly decreases even when the the two distributions are far from each other. See e.g. the Wasserstein GAN paper and its followups.

  And why is it called "the Helvetica scenario"?

It refers to the show you mentioned, where too much Calcium is put in one place and causes a disaster. Here, a disaster occurs when too much generator probability density is placed in a small area of the data space. :)


Binary classification of every time series step based on past and future values

You are facing a very common problem: handling imbalanced data. For neural networks, typical procedures are:

Having the proper metrics: global accuracy should not be used.
Oversampling the minority class: randomly generate replicas of the minority class until the imbalance disappears.You can also perform data augmentation on the minority class. Synthetic data can be generated from the feature space, using SMOTE algorithm, but I don't know how it applies to neural networks.
Under-sampling the majority class: randomly remove instances of the majority class. It can deteriorate the performance on the majority class
Include class-weights in the loss function: the idea is to penalize the misclassification of the minority class. The weights are usually inversely proportional to the occurrence frequency of each class.
Using different learning rates per class: you can use a bigger learning rate for the majority class, thus the net stops learning  earlier from the majority class than from the minority class

I'd recommend a combination of 1 (always), 2 and 4. For a higher insight in this topic, which is of very importance, I recommend reading:

https://dl.acm.org/citation.cfm?id=1592322
https://arxiv.org/pdf/1710.05381.pdf


Convolutional neural network giving high confidence on wrong classification

Since your model seems to be doing fine on the correct hand signs, I don't think there is anything wrong. It's a common misconception that you can interpret the model output as a confidence measure. Consider the following example.

Let's say you have a binary classifer and you learn to distinguish numbers between -1 and 0 in one class labelled as 0 and numbers between 0 and 1 in the other labelled as 1. Your training data is in these intervals and is labeled correctly. Your classifier in the end will be something like

$$ y = \sigma(wx)$$

with $\sigma$ as the sigmoid function. When you train, you will quickly learn that $w > 0$. The more you train, the larger $|w|$ will become untill you get something close to 0 for all $x \in [-1,0)$ and something close to 1 for $x \in (0,1]$. All perfect and normal so far.

Now you pass the number $x=10$ to the model. The query does not make sense, as this $x$ does not belong to any of the classes the model was trained on and has nothing to do with the training data. There is no reason to assume that the model would give any useful output for $x=10$. But still, the model will the super confident that $10 \in (0,1]$ since for $x=10$ the model will output a value closer to 1 than for any of the training examples. This is clearly not correct.

I hope this clarifies that the interpretation of the classifier output as confidence can be very misleading. If the classes are close together in feature space and you query with an example that is somewhat between the training data, then you could take it as such, but for input that is not represented in the training data, the numbers you will get don't have any meaning at all.

In your application you could introduce the counterexamples to the training data and label them as "other", but that would be risky as well. If you really need a confidence measure, look into Bayesian Neural Networks (e.g. Bayes-By-Backprop, Dropout or Batch norm), other Bayesian methods or bootstrapping.


Multi-Class classification with CNN using keras - trained model predicts object even in a fully white picture

As I mentioned in my question post, the post is a bit silly even for a new learner. In this case, the world has only 2 classes which are dogs and cats so the output must be either a dog or a cat.


Using Keras how and what do I need to export to use my classifier independently?

In keras you have the option to save the entire model state including the optimizer parameters or simply the model weights. In the first case all you need to do is:

model.save(model_path)
model = load_model(model_path)

In the second case you have to first create your model and then load the weights:

model.save_weights(model_weight_path)

In case model is not already specified:

model = Sequential()
model.add()
...

model.load_weights(model_weight_path)

https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model


In which CRAN mirror is the quadprog package available?

The quadprog package is available on all CRAN mirrors, but it requires the latest R version: R 3.6.0 (released on Apr 26, 2019), see also https://cran.r-project.org/web/packages/quadprog/index.html

> quadprog: Functions to Solve Quadratic Programming Problems
>
> This package contains routines and documentation for solving quadratic
> programming problems.
> Version:    1.5-6
> Depends:    R (≥ 3.6.0)
> Published:  2019-04-26


Can you explain the difference between SVC and LinearSVC in scikit-learn?

A regular SVM with default values uses a radial basis function as the SVM kernel.  This is basically a Gaussian kernel aka bell-curve.  Meaning that the no man's land between different classes is created with a Gaussian function.  The linear-SVM uses a linear kernel for the basis function, so you can think of this as a ^ shaped function.  It is much less tunable and is basically just a linear interpolation.

People are kind of hammering this question because you didn't provide a lot of information, but looking deeply into what you posted... this hits on some fundamental aspects of really understanding the details of bias and variance and the difference between linear and nonlinear basis functions in SVM.

Check out this image describing the four regions of high and low bias and high and low variance.  Obviously the best place to be is  low variance and low bias.

First lets assess variance -

Now take a look at your plots:

The nonlinear basis function has higher variance.  See how it is noisier than the linear kernel!  The linear kernel has lower variance.  See how it is less noisy!

Now lets assess bias -

Which kernel is more accurate?  We can add the errors that you provided.  The nonlinear kernel has a total error of ~550+325=~875.  The linear kernel has an error of ~690+~50=~740.  So the linear kernel seems to do better overall, but they are pretty close overall.  This is were things get tricky!

Putting it all together

See how the linear kernel did a poor job on 1's and a really great job on 0's.  This is pretty unbalanced.  Where as the nonlinear kernel is more balanced.  It kind of seems like the sweet spot might be to create a balanced model that doesn't have such high variance.  How do we control for high variance?  Bingo - regularization.  We can add regularization to the nonlinear model and we will probably see much better results.  This is the C parameter in scikit learn SVMs, which you will want to increase from the default.  We could also play with the gamma parameter.  Gamma controlls the width of the Gaussian.  Maybe try increasing that one slightly to get less noisy results e.g. a larger no-man's land between classes.

Hope this helps!


This is quite standard for the training time. It depends on how much optimization you did on your code. The speed of your processing unit, it's often better to use a GPU as opposed to a CPU. GPUs do mathematical operations much faster. Also, you should use parallel computing when you can, in the case of NN you definitely can.

Training a machine learning algorithm only needs to be done once. Let it run all night and then you will be ready to do some pretty good predictions.


To detect unauthorized access using outlier detection

This question is quite broad. I'll try to set you on the right path, more so than providing a truly complete answer.

Theoretical background

As others have mentioned, the task you're trying to do is usually known as anomaly detection, also known as novelty detection.

There's many possible ways to approach this kind of task, depending on the assumptions you are willing to make about your data.

Before we start exploring alternatives, it's important to mention here that all your variables  are categorical (country, service_key, system, app_type). This is important, since usually algorithms usually handle either continuous variables or discrete variables, and categorical ones fall into the latter type.

There's some tricks you can do, however, to handle them interchangeably. One-hot encoding is simple and popular, and enables you to transform a categorical variable into N binary variables. For some classes of algorithms, you can then use these binary variables as if they were continuous. You can even mix binary and truly continuous variables.

Now, there's usually two families of methods to approach this sort of problem: parametric vs non-parametric.

Parametric methods require that you do assumptions on the underlying probability distributions of your data. You can then estimate these parameters using your data.

Non-parametric methods, on the other hand, are simpler to use, as they don't require assuming a underlying distribution. They might also be less powerful / exact, especially if you don't have a lot of data and your data very closely follows a known probability distribution.

Practical advise

If you're willing to do some statistical work, look into bayesian inference.

For a easier non-parametric method, look into into One-Class SVM. Here is a code example using python and scikit-learn and more practical information on anomaly detection

But, honestly, you're giving us a important clue here:

  Here to be note is that logins can have different country and system
  but it cannot have different username, service_key and app_type.

If you already know all this, you might want to completely skip the complicated parts. Just compare those values directly, summarize them into a distance measure (you can give different weights to service_key and app_type, for example) and see if that's good enough


Making predictions / Loading model in TensorFlow 2.0

I had the same problem in TF 2.1. It has something to do with the shape/ type of the input, namely the query. In my case, I solved the problem as follows: (My model takes 3 inputs)

x = [[test_X[0][0]], [test_X[1][0]], [test_X[2][0]]]
MODEL.predict(x)

Output:

  WARNING:tensorflow:Your input ran out of data; interrupting training.
  Make sure that your dataset or generator can generate at least
  steps_per_epoch * epochs batches (in this case, 7 batches). You may
  need to use the repeat() function when building your dataset.

array([[2.053718]], dtype=float32)

Solution:

x = [np.array([test_X[0][0]]), np.array([test_X[1][0]]), np.array([test_X[2][0]])]
MODEL.predict(x)

Output:

array([[2.053718]], dtype=float32)


It's usually not that clear cut; there's typically not one universally best approach.

Having said that, there are some prototyped ensemble approaches that are supposed to always be better than their underlying component algorithms, notably Erin LeDell's binary ensemble classifier for H2O. However, even in those cases you still need to optimize the first stage algorithms for the ensemble to be universally better.

Thus if you're willing to spend a lot of extra time, let's say 2 weeks for an ensemble instead of the 1 week it might take you for your single-stage algorithm, then it's possible (especially for binary classification) to find an ensemble that will definitely be better than your single-stage classifier.

However this is rarely the case and the way you framed the question implies there's a choice between

building 1 really good single-stage model, selected from many candidate models (and by the way be sure to avoid overfitting while making those selections)  and
throwing an ensemble at the problem without completing #1 above for each component of the ensemble (or completing #1 but not also optimizing the 2nd stage of the ensemble)

If that's the decision then -- while there's no 1 universally right answer -- I'd say that in the vast majority of the cases it's better to stick with #1.


Use regression instead of classification for hard labeled ranking datasets

An issue with regression is that the distance between negative, neutral and positive are not necessarily the same. This transition/uncertainty you mention can be modeled by using a probability distribution over the classes as opposed to a hard argmax. If you use Neural Networks for example the last softmax layer gives you this information for free (For example: 0.1 on Negative, 0.6 Neutral and 0.3 Positive).


To start with chatbot first of all you to decide which type of chatbot are you trying to build.

Order placing bot
Chitchat bot

Both required different approach to solve the problem.

Order Placing Bot required multiple model for different task like intent identification, named entity recognition, state machine.

Chitchat bot required only 2 person conversation dataset which is available easily on kaggle.com

But if you are looking for specific language dataset then it difficult to find it in both type of bots. For that either you use any translation api which you to pay for it or use web scrapping techniques to do same task at free of cost.


Can parallel computing be utilized for boosting?

You can estimate in parallel each of the weak learners. For example, searching for optimal splits in 'weak' decision trees can be streamlined by utilizing large number of cores.


Both AdaBoost and Gradient Boosting build weak learners in a sequential fashion.
Originally, AdaBoost was designed in such a way that at every step the sample distribution was adapted to put more weight on misclassified samples and less weight on correctly classified samples. The final prediction is a weighted average of all the weak learners, where more weight is placed on stronger learners.
Later, it was discovered that AdaBoost can also be expressed in terms of the more general framework of additive models with a particular loss function (the exponential loss). See e.g. Chapter 10 in (Hastie) ESL.
Additive modeling tries to solve the following problem for a given loss function $L$:
$ \min_{\alpha_{n=1:N},\beta_{n=1:N}} L\left(y, \sum_{n=1}^N \alpha_n f(x,\beta_n) \right)$
where $f$ could be decision tree stumps. Since the sum inside the loss function makes life difficult, the expression can be approximated in a linear fashion, effectively allowing to move the sum in front of the loss function iteratively minimizing one subproblem at a time:
$ \min_{\alpha_n,\beta_n} L\left(y, f_{n-1}((x) + \alpha_n f_n(x,\beta_n) \right)$
For arbitrary loss functions this is still a tricky problem, so we can further approximate this by applying a steepest descent with line search, i.e. we update $f_n$ by taking a step into the direction of the negative gradient.
In order to avoid overfitting on the gradient, the gradient is approximated with a new weak learner. This gives you the gradient boosting algorithm:

Start with a constant model $f_0$
Fit a weak learner $h_n$ to the negative gradient of the loss function w.r.t. $f_{n-1}$
Take a step $\gamma$ so that $f_n= f_{n-1} + \gamma h_n$  minimizes the loss $L\left(y, f_n(x) \right)$

The main differences, therefore, are that Gradient Boosting is a generic algorithm to find approximate solutions to the additive modeling problem, while AdaBoost can be seen as a special case with a particular loss function. Hence, Gradient Boosting is much more flexible.
On the other hand, AdaBoost can be interpreted from a much more intuitive perspective and can be implemented without the reference to gradients by reweighting the training samples based on classifications from previous learners.
See also this question for some further references (quote):

In Gradient Boosting, ‘shortcomings’ (of existing weak learners) are identified by gradients.
In Adaboost, ‘shortcomings’ are identified by high-weight data points.


How to detect cardboard boxes using Neural Network

As you said:

  My suspicion is that box is too simple of an object, and the neural
  network has a hard time detecting it because there are too little
  features to extract from the object.

... and that is exactly the problem with this task. I suggest you to train your Network using a whole lot of image augmentation. If you are using Keras/TensorFlow 2.0, they have built-in functions that do that.

I also suggest you to train a first model, then study accurately the images that your Network thinks are boxes but aren't (the false positives). At that point, you select the "correct" cases, together with the false positive cases, and build a subset of the dataset that you have. That sub-dataset can be useful to train the Network to distinguish between actual boxes and objects that look like boxes.

Once again: use a massive load of image augmentation. That's my main point, and it's what I would do.


What is the difference between Slow Feature Analysis (SFA) and a Moving Average?

Mathematically, SFA differs from moving average by the fact that the instant output $y_t$ can only be a function of instant input $x_t$. On the other hand, the output of moving $y_t$ is a function of the past history of $x_{t'}$ with $t' \leq t$.

One immediate result due to this difference, is that if you have a flat constant input, with only a pulse at time $t$, then the pulse will be smoothed if you use a moving average, but its shape will be kept better if you use SFA.

You can always try both moving average and SFA for your task, although the moving average requires some input parameters such as window width while SFA does not.


What I am trying to understand is, the reward calculation does not take an action as a parameter. How does it choose an action properly.

The reward function does not choose the action. It gives the immediate consequences for your previous action - and in most cases is a consequence of all previous actions, states and random factors in the environment. Although it should only depend on most recent state, previous action plus a random factor at most.

In your case reward depends directly on the resulting state = how many cars at each junction after taking an action, and not directly on the previous action = which traffic flow to enable. It does still depend on the action indirectly, via however the environment works. But that does not need to be expressed mathematically (as a model of the system) for Q learning to work. Although not stated, the reward probably also depends on a random factor outside of your control = how many cars turn up at each junction.

It is the Q value which eventually ranks the different actions and allows you to select the best action. The value $Q^t(s, a)$ gives you the current best estimate for future rewards (for a continuous problem, such as yours, it is common to have a discount factor, $\gamma$, to give more weight to immediate rewards).

There are multiple ways to select the next action in reinforcement learning, depending on the problem, your learning algorithm etc. In the example pseudo-code you copied, the selection process is using Upper Confidence Bound (or UCB), which is a relatively sophisticated way of balancing exploration of actions that might be better than your current best estimate, versus simply using the "best" action so far. If you always took the "best" action, in many cases this would cause a problem because you would never update your knowledge of what the other actions do.

One of the standard textbooks for all this is Sutton & Barto, Reinforcement Learning: An Introduction. The notation in that book is slightly different to what you have used, but it explains the concepts involved in your problem in easy-to-understand detail.


The output of softmax is a probability distribution, It gives the probability of each class of being correct. So, you have to find out
the array index of the max value in array.

predicted_class = np.argmax(predicted)


Why do we double the number in a quadratic cost function or MSE?

This is really just like a convention that appears in some places because we normallt want to take the derivative of the cost function (i.e. compute gradients), which means the power of 2 would be taken to the front.

If we put the $\frac{1}{2}$ at the front to begin with, it just looks nicer once we have finished. I have seen this written somewhere before in a paper, but can't find a reference right now.

Because the nominal values of cost themselves (the scale of the values) is not of importance, we can scale it as we like really. Multiplying by a constance of $0.5$ does not change the algebraic behaviour.


Algorithm for generating classification rules

C45 made by Quinlan is able to produce rule for prediction. Check this Wikipedia page. I know that in Weka its name is J48. I have no idea which are implementations in R or Python. Anyway, from this kind of decision tree  you should be able to infer rules for prediction.

Later edit

Also you might be interested in algorithms for directly inferring rules for classification. RIPPER is one, which again in Weka it received a different name JRip. See the original paper for RIPPER: Fast Effective Rule Induction, W.W. Cohen 1995


An optimization problem involving perceptron

The perceptron receives a $n$ number of $m$-dimensional vectors, so, $x^{i} \in \ R^{m}\  \forall\ i \in [1,n]$.

To iterate, the dataset has $n$ different examples and each of which has $m$ dimensions. It's not a power notation, it just denotes the $i$th example.


Policy gradient on data only, without emulators

I think your best approach is to use Imitation Learning. Many techniques in imitation learning use Supervised Learning so you do not need to use emulator. Check DAGGER which is used in continuous action scenarios or the recent AggreVated algorithm (just ignore the theoretical parts of the paper).

As a start you can use Supervised Learning just for experimentation and then use the above algorithms. I would suggest though to use even a poor simulator just to have an idea of how your implementations behave after the. Bare in mind that RL tries to solve an optimization problem (maximize an utility/cost function) whereas the Supervised Learning methods try to optimize the difference between model's prediction and ground truth. Just be cautious of the algorithm's behavior before you go live.


Context Based Embeddings vs character based embeddings vs word based embeddings

Context-based or contextual means that the vector contains information about the use of the word in a context of a sentence (or rarely a document). It thus does not make sense to talk about the word embeddings outside of the context of the sentence.
Models such as BERT use input segmentation into so-called subwords which is basically a statistical heuristic. Frequent words are kept intact, whereas infrequent words get segmented into smaller units (which often resemble stemming or morphological analysis, and often seem pretty random), ultimately keeping some parts of the input segmented into characters (this is typically the case of rare proper names). As a result, you get contextual vectors of subwords rather than words.
Character-based embeddings usually mean word-level embeddings inferred by from character input. For instance, ELMo used character-level inputs to get word embeddings that were further contextualized using a bi-directional LSTM. ELMo embeddings are thus both character-based and contextual.
Both when using sub-words and when using embeddings derived from characters, there are technically no OOV words. With subwords, the input breaks to characters (and all characters are always in the vocabulary). With character-level methods, you always get a vector from the characters. There is of course no guarantee that the characters are processed reasonably, but in most cases they are.
Models that use static word embeddings (such as Universal Sentence Encoder) typically reserve a special token for all unknown words (typically <unk>), so the model is not surprised by a random vector at the inference time. If you limit the vocabulary size in advance, the OOV tokens will naturally occur in the training data.


How do I train Xgboost classifier for ECG Signal data?

I might be a little bit late to the party, but I did exactly what you need in the follow-up challenge of CinC2017, where our algorithm gained the 2nd best score on the hidden test set.
Our code is available at https://github.com/martinkropf/ecg-classification
Paper is available at https://iopscience.iop.org/article/10.1088/1361-6579/aae13e


Is there a model-agnostic way to determine feature importance?

Ways to "determine feature importance" are normally called feature selection algorithms.

There are 3 types of feature selection algorithms:

Filter approaches: they choose variables without using a model at all, just looking at the feature values. One example is scikit-learn's variance threshold selector.
Wrapper approaches: they use whatever prediction algorithm to score different subsets of features and choose the best subset based on that. These use a model but are model agnostic, as they don't care about which model you use. One example is recursive feature elimination.
Embedded approaches: in these approaches, the variable selection is part of a model, hence the feature selection and the model are coupled together. This is the case of the feature_importances_ in random forest algorithm.

From the question, I understand that both filter and wrapper approaches are suitable for the OP needs. A classic article that covers both very well is this one by Kovavi and John.

Here you can see an overview of scikit-learn feature selection capabilities, which includes examples of the three aforementioned types of variable selection algorithms.


Duplicated features for gradient descent

In 'Efficient Backprop' by Lecun and others (http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf), they explain why correlated variables are bad (§ 4.3 normalizing the inputs).

Duplicated data is a special case of linear dependence, which is a special case of correlation. Say you have duplicated variables $X1 = X2$, so the network output is constant over the weight lines $w_2 = c - w_1$, where c is a constant. It means that the gradient of the error is 0 along those lines : moving along those lines has no effect on the learning. In that sense the solution won't be unique.

It's bad because you could be solving a problem of lesser dimension. Removing one of the input will leave you with a network of lesser dimension.

Other than the dimension problem, for simple learner I don't think it will affect the learning process that much. For more complex learning processes (learning rate depending on time / on variables), it might get more complex.


Find multiple categories of promises in texts

That would be a sequence labeling task, the most common type is Named Entity Recognition, you'll find many examples about it but you can train a custom system with your data. The traditional method is Conditional Random Fields, there are a good few libraries available.

Side note: usually a single CRF model is used to do both detecting and labeling at once (your steps 1 and 2).


Since your model performances worse on held-out data (validation or test), you have a high variance model.

The most common methods to lower variance are:

Add more data to the training dataset.
Increase regularization. Since you are using LinearSVC, that means increasing C.
Switching to a bagged or boosted algorithms.


How can I group texts with similar content together?

Well, after further googling I found the solution: MinHash or SimHash will do the job and I also found a tool implementing MinHash written in Scala on GitHub right at this link