Pruning and parameter reduction for decision trees I am trying to perform a classification using a decision tree classifier. I was wondering whether using a Feature reduction method is relevant for decision trees since they automatically use pruning?

My idea would be to perform a loop from 5 to 15 parameter reduction and then compare the classification accuracy of each decision tree, and then conclude the optimal number of parameters for my classification.

Thank you.

Pruning and feature reduction are different things.

Pruning: It basically compares the purity of the two leafs separately, and, together. If the leafs together are purer, than the two leafs are pruned. Thus, the decision over the parameter(s) at the node is wiped off.

Let's say you have N different parameters. You tree might be tall enough such that pruning has been used over all the parameters at different nodes. And in the same time, all these parameters might have been used in other nodes. If not, the decision tree will take the decision itself not to use this parameter - doesn't prevent from overfitting though.

Dimensionality Reduction:
It you reduce the number of parameters, then these parameters would never appear in your tree, at any node. Whereas they might have been relevant at some point.

They are not uncompatible, and performing a dimensionality reduction may increase the accuracy of your task for a further classifier (as decision trees).

However, decision trees are also used for dimensionality reduction: After being trained, one might scan the features importance within the decision tree, i.e. how much each feature is used to create a split a different nodes. based on this new knowledge, you can used only the most important features to train another classifier.


Which is better: multi-output model or separate models for similar tasks? I am working on two problems:

classification of images into high-level classes (e.g. shoe, dress, jacket etc.)
classification of the attributes of the same images on a lower level (e.g. shoe style, color of the dress etc.), assuming that the high level class is known

Currently, I have designed an architecture for the 2nd problem as a multi-class multi-output network with ResNet50 as the backbone. Now I am dealing with the 1st problem and I have two paths to follow:

consider both problems as different tasks and train separate models for them
share the backbone between 1st and 2nd problems and train only one multi-output model

Which option is better? Which one should work better? Are there any good practices for combining similar tasks?

If both the tasks consolidated classes set is small,using the multi-output network will be useful. But if your consolidated classes set is very large then I would suggest you go with different networks.
Personally, I would prefer to train individual models as it is hard to optimize multi-output networks.


Q learning - how to use experience replay, when playing against other agent? I am currently trying to create a tic tac toe q learning neural network to introduce me to reinforcement learning, however it didn't work so I decided to try a simpler project requiring a network to train against static data rather than another neural network.
This lead to me following the guidelines from this website - http://outlace.com/rlpart3.html

however after programming this, the simple version works half the time, this is the version without experience replay. on some runs of the program the game will be played correctly, others it just moves back and forth wehn doing test runs.
When trying to implement experience replay to complete the harder version. the program will just constantly get itself into a loop of going back and forth when testing

i have a limit of 100 batches in which a batch is what the neural network is trained on. I am wondering whether this is an appropriate amount, or if there could be any common problems with implementing experience replay that i may have made.

My current perspective of what experience replay is:
1. run the program
2. after each turn, the data you used to train the network on, gets saved into a batch
3. when you have reaches x(100) amount of batches, pick one out and train on it.
4. overwrite the oldest batch with the new batches that come in.

If anyone could let me know where I have gone wrong, or if there is any feedback about the experience replay or the quality of the question please let me know and i would be very grateful.

EDIT: Another question I have in terms of training a neural network against a neural network, is that do you train it against a completely separate network that trains itself,m or do you train it against a previous version of itself. And when training it against the other neural network, do you turn the epsilon greedy down to make the opposing neural network not use any random moves.

I am pretty sure you have to:

wait for enough data in the memory (100 entries, etc), then take out a minibatch (for example, 10 random elements), run backprop 10 times - once for each selected element.

You then take an average of resulted gradients and finally, correct your network with that averaged gradient.

After,

put the elements back in the memory (where you've taken them from)

  Samples that have high priority are likely to be used in training many times. Reducing the weights on these oft-seen samples basically tells the network, "train on these samples, but without much emphasis; they'll be seen again soon."

or throw it out, substituting it with new memories instead.

Then keep playing the game, adding say, another 30 examples before doing another weights-correction session using a batch of 10 elements

I refer to the "Session" meaning a sequence of backprops, where the result is the average gradient used to finally correct the network.

  EDIT: Another question I have in terms of training a neural network against a neural network, is that do you train it against a completely separate network that trains itself, or do you train it against a previous version of itself. And when training it against the other neural network, do you turn the epsilon greedy down to make the opposing neural network not use any random moves.

Consider using just one network.  Let's say our memory bank contains several elements:

...
{...}
{stateFrom, takenActionIndex, takenActionQval, immediateReward, stateNext }  <-- a single element
{...}
{...}
{...}
...

When using each element in your memory during the Correction session (one element after the other), you need to:

pick an element. As seen, it contains stateFrom, taken action (action X), qvalue of action X, reward you received, state that it led to.
Run forward prop as if you were in "next state" (mentioned in that element).  Get its best [action Y, qvalue]. In fact, your action Y doesn't have to be the action with highest q-value. Instead, it could be the "epsilon-greedy decided action" from the "next state" - you would then have SARSA instead of Q-learning
obtain delta by which the "qvalue of action X" differs from the immediate reward + "qvalue of action Y".
get deltas of several other elements that you've chosen from the bank
Using each delta, compute a gradient tensor for your network's weights. Average those gradients into a single tensor. Punish your network's weights with this tensor.

I've intentionally circumvented 1 thing - you don't actually store takenActionQval, because they might become obsolete by the time you fetch its element from MemoryBank. You have to re-compute these scores during backprop.

Therefore, you are training against the version of your network from the similar but previous correction session.

Notice, you don't store ["next state", action Y] because by the time you select it to train (maybe you didn't select it for several minibatches), the network might have a different q-value for that action Y.

You could also copy your network to the second network (target network), but only, say, every 200 timesteps. You would still proceed to punish your network for any differences with the target network in the meantime, after every 30 timesteps.

Notice, the intuition of why this works is because: the Q-values are sort-of "flowing" from finish back to the beginning, by a little with every new journey. And you are always updating the current state towards the successor state. And train the successor state against its own (even further) successor.


Do Recurrent Neural Networks assume stationarity or just a general kind of sequential dependence? Just when I thought I had convinced myself that RNNs make no other assumption about a sequence other than that there are dependencies between the inputs and that (in the case of monodirectional RNNs) the past affects the present, Goodfellow, Bengio and Courville (2016) hit me with this:
"The parameter sharing used in recurrent networks relies on the assumption that the same parameters can be used for different time-steps. Equivalently, the assumption is that the conditional probability distribution over the variables at time t + 1 give the variables at time t is stationary, meaning that the relationship between the previous time step and the next time step does not depend on t."
Could someone elaborate on what this means with regards to the stationarity assumption for the time series?
(I have the feeling that there is a notion of local or "input-conditioned" stationarity here but that is just my intuition.)
Thanks!

The distribution is stationary, but it doesn't mean the time series should be stationary.
Let's imagine the recurrent network, that returns the n+1 if it was n on previous step. Distribution is not changing with time, but the time-series always goes up.


Quasi-categorical variables - any ideas? Let's say I'm trying to predict a person's electricity consumption, using the time of day as a predictor (hours 00-23), and further assume I have a hefty but finite amount of historical measurements.

Now, I'm trying to set up a linear model akin to

$power.used = \alpha* hr.of.day + \beta * temperature$

Problem: using the $hr.of.day$ as a numerical value is a very bad idea for many reasons, the fact that 23 and 0 are actually quite close values is one problem that can be solved with a simple transformation [1]. The fact that electrical consumption is often bi-modal is another problem which isn't solved by a simple transformation.

A possible solution that works rather well is to treat the time of day as a categorical variable. That does the trick, but it suffers from a significant drawback in that there's no information sharing between neighbouring hours.

So what I'm asking is this: does anyone know of a "soft" version of categorical values? I'm suggesting something quite loosely defined: Ideally I would have some parameter alpha that reduces the regression to numerical regression where $\alpha = 1$ and reduces to categorical regression where $\alpha = 0$, and behaves "in between" if it's some other number.

Right now the only answer I can think of is to alter the weights in the regression in such a way that they tend towards zero the further away the quasi-categorical value is from the desired value. Surely there are other approaches?

[1]
introduce the hour variable as two new variables: $cos(time.of.day/24)$ and $sin(time.of.day/24)$

I would suggest you to use the idea of so-called 'fuzzy clustering', where you put each of your hours of the day value into several clusters at the same time. Details in paper: http://home.deib.polimi.it/matteucc/Clustering/tutorial_html/cmeans.html

The idea is trivial:

You decide how many clusters you want to have. For example, 4 (so you divide your day hours into 4 cathegories). Instead of computing just 1 number (which defines cluster membership) for each of your day hours you compute 4 numbers which represent the degree of membership to each of 4 clusters. So for example if you 4 clusters will contain periods 12 AM-6 AM, 6 AM- 12 PM, 12 PM - 6 PM and 6 PM - 12 AM then you would replace for example 4 AM hour in original data with vector of 4 numbers, first one is the biggest, second is smaller, third one is the smallest one etc.

Then you could use these 4 numbers in your model to fit a regression line.

Of course, if you want you could use 24 clusters and in such case each your day of hour would have a high 'relation' with nearby hours and almost 0 with the distant hours.


Trained BERT models perform unpredictably on test set We are training a BERT model (using the Huggingface library) for a sequence labeling task with six labels: five labels indicate that a token belongs to a class that is interesting to us, and one label indicates that the token does not belong to any class.
Generally speaking, this works well: loss decreases with each epoch, and we get good enough results. However, if we compute precision, recall and f-score after each epoch on a test set, we see that they oscillate quite a bit. We train for 1,000 epochs. After 100 epochs performance seems to have plateaued. During the last 900 epochs, precision jumps constantly to seemingly random values between 0.677 and 0.709; recall between 0.729 and 0.798. The model does not seem to stabilize.
To mitigate the problem, we already tried the following:

We increase the size of our test data set.
We experimented with different learning rates and batch sizes.
We used different transformer models from the Huggingface library, e.g. RoBERTa, GPT-2 etc.
Nothing of this has helped.

Does anyone have any recommendations on what we could do here? How can we pick the “best model”? Currently, we pick the one that performs best on the test set, but we are unsure about this approach.

BERT-style finetuning is known for its instability. Some aspects to take into account when having this kind of issues are:

The number of epochs typically used to finetune BERT models is normally around 3.
The main source of instability is that the authors of the original BERT article suggested using the Adam optimizer but disabling the bias compensation (such a variant became known as "BertAdam").
Currently, practitioners have shifted from Adam to AdamW as optimizer.
It is typical to do multiple "restarts", that is, train the model multiple times and choose the best performing one on the validation data.
Model checkpoints are normally saved after each epoch. The model we chose is the checkpoint with best validation loss among all epoch of every restart we tried.

There are two main articles that study BERT-like finetuning instabilities that may be of use to you. They describe in detail most of the aspects I mentioned before:

On the Stability of Fine-tuning BERT: Misconceptions, Explanations, and Strong Baselines
Revisiting Few-sample BERT Fine-tuning


How to evaluate clusters base on an attribute of the dataset? I have a data set of persons with attribute job that have 300 different nominal value.
attribute job have a lot of noise. I decide to cluster my data base on other attribute (other feature of person) that related to their job. I hope I reach to clusters with high correlation to job.

Now, I want to know, how much I were successful. In my opinion, more correlation between job and the clusters means more success. I found chi-squared as a method to check correlation between two nominal attributes.
But the result is very low p-value for all clustering.

Is there any method to evaluate a clustering with a predefined class?

Use external cluster evaluation.

In particular, use ARI and NMI.


I liked the way you put across your question!

I think we cannot cannot say in specific will work well with data, it is most likely trial & error method, If ARIMA is not performing well and assuming that there is no trend in data then you can use AR, Exponential Smoothening. These are basic techniques but as you know in many scenarios basic models can explain better than complex models.

These both works well in such scenarios. Give a try.

It would be nice if you can share some graphs which can explain us a bit more about the how the data is, noise etc.
I mean if you have anything from your exploratory analysis.

Hope my answer is helpful!


Drug Making Using Genetic Algorithms I want to create a drug using N different chemicals for fighting a bacterial infection those N chemicals are contained inside the drug in different quantities my work environment is a simulated one and I want to create the drug using genetic algorithms.
How can I use genetic algorithm for this problem. What's representation for chromosomes can I use and what kind evaluation function should I use?
What could go wrong using genetic algorithms for these kinds of problems?

A simple representation of the problem in terms of a genetic algorithm could be something like this:

A gene represents a chemical and the values it takes represent the proportion of the chemical in the drug
An individual is a combination of N genes/chemicals, each with their particular proportion
A cross-over is the combination of the genes of two individuals A and B, either by picking randomly the value of either A or B for each gene or by some other kind of aggregation. A mutation is the random modification of a gene to a different proportion.

The standard genetic algorithm works like this:

Randomly pick a set of say 100 individuals (first generation)
Calculate the "performance" of every individual, i.e. evaluate how good this particular combination of chemicals proportions is.
Select say the top 10 individuals according to their performance, then produce the next generation of 100 individuals by cross-over among these top 10. Optionally add some random mutations to the new individuals' genes.
Iterate again from step 2. Keep iterating unless some stop condition is satisfied, for example the average performance over the last 5 generations doesn't increase anymore.

As far as I understand the task, the main problem is the evaluation in step 2: if there is no automatic way to evaluate the performance of a combination of chemicals, it's impossible to use the genetic algorithm. In theory the evaluation doesn't need to be automatic, one could imagine doing a manual experiment for every individual, but that means performing thousands of experiments, a lot of them probably useless.


Are deep - learning toolkits targeted for certain areas or all-purpose tool kits? Are any of the open deep learning toolkits targeted to certain areas, or a all toolkits all purpose toolkits, meaning it is a blackbox for deep learning.

My question comes in regards to Microsoft's CNTK which seem to contain examples of speech and text classification, where others usually just have MNIST or CIFAR...

Yes, different toolkits are suited for different purposes given they contain different algorithms. This is due to the lack of a general AI (an AI that is intelligent in all respects). Modern AI has different algorithms which are better suited for different tasks. For example a CNN framework is much better for images than a NN would be, and much better than encoders. However, a LSTM framework would be better suited to capture temporal dependencies in the data.

However, if two frameworks are using the same algorithms they should both be relatively the same. Differences will arise due to the underlining codes being different as can be seen between Keras and TensorFlow. Although, this should be limited.

Worry more about the algorithm you are choosing for your task than the framework. Coding up most machine learning algorithms is not that hard if you want to customize it.


Why my results have time delay when I use LSTM? I am trying to fit and test LSTM on a numeric series(like stock prices). But it seems that I always get a lag in predicted graph(Blue) with respect to real graph(red). Does anyone know why this happened? (I searched and realized this is a problem for others too).

I use Keras.sequential.LSTM library.

I've had similar results when working with time series data. My conclusion was that the model does not learn any real pattern, except that the next value is pretty close to the previous one (or a smoothing of previous ones) + some noise.

This can be easily verified, if you feed your model's predictions recursively into the model to further predict the future (multi-step ahead forecasting based on the model's predictions). In the above mentioned case, your predictions will explode exponentially pretty quickly.


Split large documents into many smaller ones? I want to build a basic language detector for English, French and German.

I went to wikipedia and I downloaded the page of 'Technology' in all these languages.

In all these cases, we are talking for about 10000 words.

So basically I have 3 documents of 10000 words each for each of the 3 languages above.

My question is the following:

Should I split these documents in smaller documents e.g. of 100 words and create in this way more labeled observations in my dataset or should I leave them like this for training my classifier (e.g. with a TF-IDF model)?

I think it should be easy to detect a language based on a standard vocabulary (bag of words).

However, I would split the articles into small pieces (maybe not 100 but a bit more words, eg. 200-500), so you can train and test your model without problems.


Input Ouput dimensions python keras CODE

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('D:\\test.csv')
X = dataset.iloc[:, 0:2].values
y = dataset.iloc[:, 2].values
print (X)

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_0 = LabelEncoder()
X[:, 0] = labelencoder_X_0.fit_transform(X[:, 0])
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
print (X)
onehotencoder = OneHotEncoder(categorical_features = [0,1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 0:]
X = X[:, 1:]
print (X)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print (X_test)

import keras
from keras.models import Sequential
from keras.layers import Dense
import keras.utils

classifier = Sequential()
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 9))
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)

_, accuracy = classifier.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

#predictions
dataset = pd.read_csv('D:\\test1.csv')
a = dataset.iloc[:, 0:2].values
labelencoder_a_0 = LabelEncoder()
a[:, 0] = labelencoder_a_0.fit_transform(a[:, 0])
labelencoder_a_1 = LabelEncoder()
a[:, 1] = labelencoder_a_1.fit_transform(a[:, 1])
onehotencoder = OneHotEncoder(categorical_features = [0,1])
a = onehotencoder.fit_transform(a).toarray()
a = a[:, 0:]
a = a[:, 1:]

predictions = classifier.predict_classes(a)
# summarize the first 2 cases
for i in range(2):
    print('%s => %d' % (a[i].tolist(), predictions[i]))

data after label encoders looks like

[[0 0]
 [0 1]
 [0 2]
 [0 3]
 [0 4]
 [0 5]
 [0 6]
 [0 7]
 [0 8]]

after one-hot looks like

[[1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1.]]

after [X_test = sc.transform(X_test)] looks like

[[ 0.          1.         -0.40824829 -0.40824829 -0.40824829 -0.40824829
  -0.40824829 -0.40824829 -0.40824829]
 [ 1.          0.         -0.40824829 -0.40824829 -0.40824829 -0.40824829
  -0.40824829 -0.40824829 -0.40824829]]

and X_train looks like

[[-0.40824829 -0.40824829 -0.40824829 -0.40824829  0.          2.44948974
   0.         -0.40824829 -0.40824829]
 [-0.40824829 -0.40824829 -0.40824829 -0.40824829  0.         -0.40824829
   0.         -0.40824829  2.44948974]
 [-0.40824829 -0.40824829 -0.40824829 -0.40824829  0.         -0.40824829
   0.          2.44948974 -0.40824829]
 [ 2.44948974 -0.40824829 -0.40824829 -0.40824829  0.         -0.40824829
   0.         -0.40824829 -0.40824829]
 [-0.40824829  2.44948974 -0.40824829 -0.40824829  0.         -0.40824829
   0.         -0.40824829 -0.40824829]
 [-0.40824829 -0.40824829 -0.40824829  2.44948974  0.         -0.40824829
   0.         -0.40824829 -0.40824829]
 [-0.40824829 -0.40824829  2.44948974 -0.40824829  0.         -0.40824829
   0.         -0.40824829 -0.40824829]]

It is trained successfully. But when i run prediction code
it gives error

Error when checking input: expected dense_205_input to have shape (9,) but got array with shape (6,)

variable (a) after one_hot looks like

[[1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]

I can't understand what is the problem with my prediction sample.

Your problem originates in the fact that your testing set consists of examples of just 6 possible classes, while your model is trained with a training set of 9 different labels.

When converting the test set to one-hot-encoding, you need to add the number of possible labels as the 'n_values' parameter. Otherwise by default it relies on the number of classes in the test set (which is 6 in you case).


Where to start on neural networks First of all I know the question may be not suitable for the website but I'd really appreciate it if you just gave me some pointers.

I'm a 16 years old programmer, I've had experience with many different programming languages, a while ago I started a course at Coursera, titled introduction to machine learning and since that moment i got very motivated to learn about AI, I started reading about neural networks and I made a working perceptron using Java and it was really fun but when i started to do something a little more challenging (building a digit recognition software), I found out that I have to learn a lot of math, I love math but the schools here don't teach us much, now I happen to know someone who is a math teacher do you think learning math (specifically calculus) is necessary for me to learn AI or should I wait until I learn those stuff at school?

Also what other things would be helpful in the path of me learning AI and machine learning? do other techniques (like SVM) also require strong math?

Sorry if my question is long, I'd really appreciate if you could share with me any experience you have had with learning AI.

No, you should go ahead and learn the maths on your own. You will "only" need to learn calculus, statistics, and linear algebra (like the rest of machine learning). The theory of neural networks is pretty primitive at this point -- it more of an art than a science -- so I think you can understand it if you try. Ipso facto, there are a lot of tricks that you need practical experience to learn. There are lot of complicated extensions, but you can worry about them once you get that far.

Once you can understand the Coursera classes on ML and neural networks (Hinton's), I suggest getting some practice. You might like this introduction.


If a dataset is imbalanced in real life, should I train on my machine learning model on an imbalanced dataset I have a dataset where around 20% of the data is the positive class and 80% of the data is the negative class. When I undersample and train my classifier on a balanced dataset and test on a balanced dataset, the results are pretty ok. However, if I train on the balanced dataset and test on an imbalanced dataset that replicates the real world (80-20 split) the metrics are not great. Should I train the model on the original imbalanced dataset if I want it to perform well on real world test data that is also imbalanced.

When I undersample and train my classifier on a balanced dataset and test on a balanced dataset, the results are pretty ok

It's not surprising that the results are good since the job is easier in this case. It's actually a mistake to test on the artificially balanced dataset, since it's not a fair evaluation of how the system will perform with real data.

  Should I train the model on the original imbalanced dataset if I want it to perform well on real world test data that is also imbalanced.

Both training on the original dataset or the balanced dataset are valid methods, choosing between the two options is a matter of design and performance. It's often a good idea to try both and then pick the one which performs better than the other on the real imbalanced dataset.


How to gauge the Complexity of Pre trained Neural Networks? What does one mean when they are talking about the simplicity of the networks? Does it mean that the shallower the networks the simpler they are, or does it mean that lesser the number of trainable parameters, simpler the models?

It can mean both, depending on the context. In order to improve prediction, a Network might need either more parameters or more depth, but these features improve models in different ways. For example, it's true that a Network with too few parameters won't be able to learn much; there is a minimal amount of parameters required simply to transform and represent signals in a sufficiently sofisticated way. On the other side, depth is fundamental in order to make your model generate more complex abstractions that will make it superior to other ML algorithms.

In other words: the number of parameters overall tells you how many things your network can learn. Its depth tells you how complex and sofisticated these things can be.

Deep Neural Network complexity is a multifaceted thing, it can't be represented on a single dimension.


How exp(-z) is working in a sigmoid function in neural networks while z is a matrix? function g = sigmoid(z)

%SIGMOID Compute sigmoid function
%J = SIGMOID(z) computes the sigmoid of z.

g = 1.0 ./ (1.0 + exp(-z));

end

I'm going through the Andrew Ng Coursera course. I doubt that how exp(-z) is computed directly while z is a matrix?

In many languages and libraries, operations that apply to a scalar can be applied to vectors, matrices and tensors. They're just applied element-wise, and the result is another vector, matrix, etc with each value transformed by that function.


How to use the same minmaxscaler used on the training data with new data? Im using the keras LSTM model to make prediction, and the code above is to scale the data:
inputs are shaped like  (n, 11, 1) and the label is 1D
DailyDemand.py

#scaling data
scaler_x = preprocessing.MinMaxScaler(feature_range =(-1, 1))
x = np.array(x).reshape ((len(x),11 ))
x = scaler_x.fit_transform(x)
scaler_y = preprocessing.MinMaxScaler(feature_range =(-1, 1))
y = np.array(y).reshape ((len(y), 1))
y = scaler_y.fit_transform(y)

# Split train and test data
x_train=x[0: train_end ,]
x_test=x[train_end +1: ,]
y_train=y[0: train_end]
y_test=y[train_end +1:]
x_train=x_train.reshape(x_train.shape +(1,))
x_test=x_test.reshape(x_test.shape + (1,))
# Train and save the Model named fit1 in a json and h5 files
[....]
# serialize model to JSON
model_json = fit1.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
fit1.save_weights("model.h5")
print(">>>> Model saved to model.h5 in the disk")

and Now im trying to predict a new values of a new data with this trained model. so i loaded the model from the files:

predict.py

from DailyDemand import scaler_y
from DailyDemand import scaler_x
[...]
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

########################################
# make prediction with the loaded model

FeaturesTest = [267,61200,695,677,70600,116700,130200,768,659,741,419300]
xaa = np.array(FeaturesTest).reshape ((1,11 )).astype(float)
print(xaa)
xaa = scaler_x.fit_transform(xaa)
xaa = xaa.reshape(xaa.shape +(1,))
print("print FeaturesTest scalled: ")
print(xaa) # incorrect scalled value, always returns -1 ones

xaa = [[[-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]
  [-1.]]]

tomorrowDemand = loaded_model.predict(xaa)
print("tomorrowDemand scalled: ", tomorrowDemand)
prediction = scaler_y.inverse_transform(np.array(tomorrowDemand).reshape ((len(tomorrowDemand), 1))).astype(int)
print ("the real demand is 95900 and the prediction is: ", prediction)

The problem is how i can use the same scaler used in the training on the new data?
i want to know if i made a mistake in this code to use the same scaller on the new data?

You are refitting scaler_x on your test set, which you don't want.  Change this line:

xaa = scaler_x.fit_transform(xaa)

to

xaa = scaler_x.transform(xaa)

You are getting [-1, -1, ..., -1] because with one sample, each feature is equal to the minimum.


how to get probability in classification problem I am learning ML with this tutorial. https://pythonprogramming.net/using-trained-model-deep-learning-python-tensorflow-keras/
When I try to use the model to predict, I only get 0 or 1, but I thought that since the last layer is
model.add(Dense(1))
model.add(Activation('sigmoid'))

the final output would be in the range [0,1] and not the extreme value 0 or 1. How can I get the exact result?

Thanks a lot for your help. I tried model.summary() and looked at the outputs of intermediate layers. My mistake was not normalizing the test input.


How to normalize noisy data Suppose I have 1-D data which has some outliers, I want to normalize the data to be in the range [0,1].
I tried calculating the maximum value and the minimum value as follows:
q1,q2,q3 = quartiles of the data
max = q3 + (q3-q1)*1.5
min = q1 - (q3-q1)*1.5

I used the above approach because I have read that data above maximum or data less than the minimum (as calculated above is noise).
My question is: whatever I am doing, is it correct or is there any other way to achieve good results?
Thank you for helping.

The question,

whatever I'm doing is it correct or ...

will occur over and over again, until you have a firm understanding of statistics. See if you are looking for short-term learning, then you might pick up some fragments of information here and there, but, for long term learning, I'll suggest that you read a good text on introductory statistics. Try, this book, or this one on Applied Predictive Modeling
Now to answer the question, Yes what you are doing is correct. I've explained it below;
Outliers are observations that fall below the lower limit or above the upper limit. The five number summary of a continuous variable (or numerical variable) is Min, Q1, Q2, Q3, Max. Where the lower and upper limits are defined as, Lower Limit = Q1 - 1.5 x IQR, Upper Limit = Q3 + 1.5 x IQR and IQR is the Inter Quartile Range, which is the difference between the first and third quartile.
Q1 is the first quartile, Q2 is the second quartile or the median and Q3 is the third quartile.
Finally, please always plot your data.


learning curve Sklearn I was trying Random Forest Algorithm on Boston dataset to predict the house prices medv with the help of sklearn's RandomForestRegressor.

Just to evaluate how good is the model performing I tried sklearn's learning curve with below code

 train_sizes = [1, 25, 50, 100, 200, 390] # 390 is 80% of shape(X)

    from sklearn.model_selection import learning_curve
    def learning_curves(estimator, X, y, train_sizes, cv):
        train_sizes, train_scores, validation_scores = learning_curve(
                                                     estimator, X, y, train_sizes = train_sizes,
                                                     cv = cv, scoring = 'neg_mean_squared_error')
        #print('Training scores:\n\n', train_scores)
        #print('\n', '-' * 70) # separator to make the output easy to read
        #print('\nValidation scores:\n\n', validation_scores)
        train_scores_mean = -train_scores.mean(axis = 1)
        print(train_scores_mean)
        validation_scores_mean = -validation_scores.mean(axis = 1)
        print(validation_scores_mean)

        plt.plot(train_sizes, train_scores_mean, label = 'Training error')
        plt.plot(train_sizes, validation_scores_mean, label = 'Validation error')

        plt.ylabel('MSE', fontsize = 14)
        plt.xlabel('Training set size', fontsize = 14)
        title = 'Learning curves for a ' + str(estimator).split('(')[0] + ' model'
        plt.title(title, fontsize = 18, y = 1.03)
        plt.legend()
        plt.ylim(0,40)

If you notice I have passed X, y and not X_train, y_train to learning_curve.

I am not understanding should I pass X, y or only the training subset X_train, y_train to learning_curve.

Update 1

Dimensions of my Train/Test split (75%:Train and 25%:Test)
        X.shape: (489, 11)
        X_train.shape: (366, 11)
        X_test.shape: (123, 11)
I had few additional questions regarding the working of learning_curve

Does the size of test data set varies according to the size of train dataset as mentioned in list train_sizes or it is always fixed (which would be 25% in my case according to train/test split which is 123 samples) for example

When train dataset size = 1 the will the test data size be 488 or will it be 123(the size of X_test)
When train dataset size = 25 the will the test data size be 464 or will it be 123(the size of X_test)
When train dataset size = 50 the will the test data size be 439 or will it be 123(the size of X_test)

Update 2

In the blog the dataset has 9568 observations and the blogger passes entire dataset X to learning_curve.

train_sizes = [1, 100, 500, 2000, 5000, 7654]

In first iteration when train_size is 1 then the test_size should be 9567 but why he say that

  But when tested on the validation set (which has 1914 instances), the MSE rockets up to roughly 423.4.

Shouldnt the test_size be 9567 instead of 1914 for first iteration

In second iteration when the train_size is 100 then the test_size should be 9468

What I meant to say is the test_size will be variable according to train_size correct me if I am wrong

[After Update1/2]

A learning curve shows how error changes as the training set size increases. One basically change the size of training data points and measure a desired score and compare it against a fixed test set to see how it generalizes. For you the utmost important part to note is the fixed test set. If you were to change the test set, that you could theoretically, how could you evaluate the performance of the same model by changing the size of the training size (because you are changing both datasets at the same time)?!

That is why you pass the the whole X,y in The Sklearn's learning curve, and it returns the tuple containing train_sizes, train_scores, test_scores. As far as I understood, it does a nested validation cross-validation. It basically takes the whole X,y, and split into train/test  (keeping the test data strictly independent and fix in size), depending on how you pass the cv parameter, and keep increasing the training size and record the performance scores for plotting the learning curve. This ensures a proper measure of the optimal model's performance.

Concerning your Update 1: Having just discussed the details, then your Update 1 example scenario would be wrong. We train a model (no matter how), and would like to evaluate its performance on a hold-out dataset. If you change your hold-out dataset, you wouldn't know whether changing the training size led to the change in test score or the variability of the hold-out dataset itself!

Concerning your Update 2: This is exactly what we have been discussing so far. No matter what train_size is, the test_size would remain 1914, as long as the same cv method is used.

Example: To further clarify let's imagine we have the following scenario:

train_sizes = [1, 50, 100, 200, 400, 600]
# X.shape: (1000, 5)
# len(train_sizes)=6

If we were to use the following settings in learning_curve class in sklearn:

cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)
learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)

The learning_curve returns the train_sizes, train_scores, test_scores  for six points as we have 6 train_sizes. And for these points the train_sizes and test_size would look like this:

Point 1: train_size: 1,   test_size:200
Point 2: train_size: 50,  test_size:200
Point 3: train_size: 100, test_size:200
Point 4: train_size: 200, test_size:200
Point 5: train_size: 400, test_size:200
Point 6: train_size: 600, test_size:200

I think train_sizes are clear (taken from the list we provided). The test_size remains fixed, and it is decided because we chose test_size=0.2 in the cv (recall that we have had 1000 data points, so 20% of it would be -> 1000 * 0.2 = 200), and it has nothing to do with our train_sizes!! This is the hold-out set that we keep fixed during this experiment. Hope this last example clarifies it altogether. ;-)

I would still keep that blogpost here as reference for others.


How to extract most occuring words based on month & what tool to use? Hi guys I'm very new to data science,
I have intermediate background on programming and have used Pentaho Data Integration tool once for DB migration & data cleansing.

Let's say I have this kind of data:

item_details, timestamp
Wooden chairs, 01-07-2017
Plastic chairs, 02-07-2017
Stainless table, 11-07-2017
Decorated window, 12-07-2017

and so on

I want to know based on monthly time frame  what are the top trending items in that month.
Let's say in January the top 3 item is:
1. Table
2. Chairs
3. Window
In February :
1. Door
2. Chair
3. Cupboard
.. And so on

How can I achive this and using what kind of tool? (preferably free or open source tools, can be GUI based or script library, having a visualization or  dashboard is a plus)

Thanks for the help. Sorry for noob questions

If you are new to data science and data munging, this could be kind of a tricky task, but a good one to get your feet wet.  Many programming languages have the capability to do this (R, Python, Matlab, etc.).  I use R primarily, so I'll give you a brief heuristic for how I'd approach the task in R.  Perhaps looking into these steps will get you started.

Install R
Install some packages that will help you along ('tm' for text mining,'dplyr' for cleaning/organizing your data, and perhaps also 'lubridate' for working with dates/times)
Read in your data from your source, be it a text file, spreadsheet, or some database (if a database, you'll have to conquer connecting R to said database too)
You want to do a word frequency analysis for each month. How you accomplish this will depend on how the data is organized, which I do not know, but it would involve first rounding all your dates to month (using lubridate's 'floor_date()' function is one way), then parsing the text for each month into a corpus that can be analyzed (using package tm).
Finally, for each month I would make a table counting words, sorting by frequency.  That would give you, for each month, the top 'trending' words. To discount words like 'the' and 'a', I might also use some of the tools in the 'tm' package to clean things up.
Note that in #5 I said 'words', not 'terms'.  If you want to account for terms consisting of > 1 words, you'll have to 'tokenize' them, but that's beyond the scope of this very brief intro.

As with many data science tasks, there are many ways to attack this; the above is but one of many possibilities.

Hope that helps.


Shouldn't training and validation loss be approximate before the first epoch is finished? I'm having this burning question in my head, and I couldn't find the answer anywhere. During training, at least in Keras, the training loss is computed on the current batch, so the weights can be updated. So, at least at the first epoch, every batch loss is computed before the model actually learns from that particular epoch. Given this, shouldn't the validation loss for the first epoch be somewhat near the training loss for the first epoch, since both are computed on examples not seen by the gradient descent algorithm? For every model I've built so far, the training loss is always lower than the validtion loss. I expected the training loss to be somewhat near the validation loss on the first run through the dataset, and then, as the model learns from the training dataset (but not from the validation dataset), those errors would start to create a gap between them. Am I missing something trivial here?

Is your batch size the length of the full dataset? If you have $N$ samples, and feed in mini-batches of size $k<N$, then batch losses are computed and the model's weights are updated with every $k$ samples. By the end of the first epoch, there may already have been significantly many updates (learning) from the training set. I believe Keras aggregates these batch losses to compute the epoch loss.

If $k=N$ however, it is possible that the distribution of your training and validation sets are different.


How to split a large set of coordinates into smaller sets using something like nearby selection? I have a dataset filled with 3817 coordinates (latitude, longitude). What I want to do is create groups of ~500 coordinates so that smaller group can be used to solve Vehicle Routing Problem. The reason I want to do this is because the original dataset is large the solving the VRP takes too long.

I've tried splitting the map into grids and simply grouping the coordinates based on which grid they are in. But this is not the most efficient method. I've read one of OptaPlanner's blog posts where they write about Nearby Selection and would like to do this with my dataset.

Any ideas on how to accomplish this?

Have a look into KD-Trees. They work by partitioning your space into discrete blocks, via a (wait for it...) tree. Here is the wikipedia example for a 3d space:

Each of the cuboids is represented by a leaf or node in the tree. It works via binary splits (splitting the space into two via a criterion that you could choose yourself). Here is a short intro to the algorithm.

There is an implementation in Scikit-Learn. You build a tree and then you can query the data for a given point and radius, returning all points within that radius/distance.

This has the benefit that you could also test your algorithms over a group of subsets by querying a selection of coordinates - and you only need to construct the tree once.

Small demo

I will generate some random points random points on a 2d integer grid, so values between 0 and 10. Some points on the grid could therefore be empty.

I put them into a Pandas Dataframe just to make things a little easier to manage and plot:

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: from sklearn.neighbors import KDTree         # could use cKDTree

In [4]: import matplotlib.pyplot as plt

In [5]: data = {"x": np.random.randint(0, 11, 200), "y": np.random.randint(0, 10, 200)}    # 200 random coords

In [6]: df = pd.DataFrame(data)

In [7]: df.head()
Out[7]:
   x  y
0  1  7
1  9  8
2  6  1
3  3  2
4  4  3

Now I create the KDTree and query the coordinate (5, 5) in the centre - I want all points back that lie within a radius distance of 3:

In [8]: tree = KDTree(df.values)

In [9]: ix = tree.query_radius([(5, 5)], r=3)[0]

Now I use the returned indices (ix) to filter out those points I want - and just plot them here in red, along with the original complete dataset in blue:

In [10]: ax = df.plot.scatter("x", "y", c="b", alpha=0.5);   # blue base points

In [11]: df.iloc[ix].plot.scatter("x", "y", c="r", ax=ax);   # query results

In [12]: plt.scatter(5, 5, c="g");    # query point

In [11]: plt.show()

The queried point is green (5, 5)
Original points are blue
Query results are red

Faster please!

There is also the cKDTree class, which works very similarly, but is implemented in C and so should be faster in many cases. Have a look here at some differences. the cKDTree is the best choice if you want to do simple querying like in my example!


Combinatoric system which using in bookies "S3" system I am interested what type of combinatorics is using for following bookmakers system called "S3":

We have  N={1..8} events
We build express pairs C=8 like

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

Each event repeats 3-times: 1 =>  (1, 2, 4), (1, 3, 6), (1, 6, 8)..

It's not permutation, combination... Please advice what is it?

I want as example generate in similar way 4 from 10 if this possible. After I'd like to predict the optimal way for building the `pairs'

This can be interpreted as an unusual error correcting code with a non-binary signal.

Essentially, you want the tuples to be very different, i.e. there are no two tuples that agree in two positions. That is also a simple strategy for producing such codes (the more interesting question is what (x,y) combination is best.)

Think of it this way: assuming you only know part of the tuple. Say the first is unknown: (?, 2, 4) then you want to be able still conclude the correct triple.

https://en.wikipedia.org/wiki/Forward_error_correction


Extract all the data of a particular month from dataset of different years In a dataset containing temperatures of different years and I want to extract data of particular month from all different years in single liner code what is the syntax? To extract all the data from a single month the syntax is :df['Temperature'].loc['2010-08'] It is always year followed by month.What if I want to extract 2010 & 2011 years temperatures in August in single line.

df.loc[df['Temperature'].isin(['2010-08', '2011-08'])]

isin() will help in setting filter for multiple values.

The other way is to use regex to extract data of particular month from different years:

import re
df['Temperature'].filter(lambda x: re.match('-08$', x))

It will check if the value ends with -08 (in this case, if the month is August) and select only such records.

Hope it helps!


How to compare performance between SVM and Keras models I applied both SVM and CNN (using Keras) on a dataset. Now, I want to compare the performance of both models.

Keras model.evaluate function predicts the output for the given input and then computes the metrics function (A metric is a function that is used to judge the performance of a model) specified in the model.compile and based on y_true and y_pred and returns the computed metric value as the output. It is obvious that SVM's accuracy calculation is different than Keras' model.evaluate.

For my dataset, Keras model (accuracy calculated based on model.evaluate) gives better accuracy than the SVM. However, a comparison between y_true and y_predict (get the y_predict by model.predict) gives a similar accuracy (this accuracy is a little lower than the accuracy return from Keras evaluate) to the SVM.

I want to know, in this scenario, how can I compare the result between both SVM and Keras model? May I conclude that Keras' model is better as its model.evaluate is giving better accuracy than SVM. What is the standard approach in this scenario to compare performance between the two models?

If you are training classifiers, you can check: plain accuracy percentage, F1 score and other metrics (sensitivity, specificity, etc.), you can visualise true vs predicted values with confusion matrices. There's plenty of options.

Please remember that the quality of a model can be assessed only on test data, i.e. data that you model didn't see in training phase.


Gaussian distribution in python without using libraries I am implementing Gaussian distribution of a variable, but it gives multiple bell shapes. It should be a single bell shape. Below is my code and plot.

def gaussian_transform(arr):

    u=arr.mean()

    sig=arr.std()

    fd=[]

    a=0
    pi=math.pi
    for i in arr:

        a=((i-u)**2)/(2*(sig**2))
        e=math.exp(-a)
        fd.append((1/(sig*math.sqrt(2*pi))) * e)

    return fd

y=gaussian_transform(df.Age)

plt.plot(df.Age,y)

plt.ylabel('some numbers')
plt.show()

`

You need to sort arr. For example you sort df.Age then apply the function and after plotting you will get a beautiful chart.

For example, I used your function and a range from 0 to 99 that is already sorted:

import numpy as np
import math
from matplotlib import pyplot as plt

arr = np.arange(100)
y=gaussian_transform(arr)
plt.plot(arr,y)

and got the following plot:

To make the plot smooth you need to add more points to the chart. In the following code I used vector functions of numpy to make the computation faster and write less code. I also used the linspace function to fill in the space between max and min of the data with more points for smooth charts.

import numpy as np
import math
from matplotlib import pyplot as plt

def gaussian_plot(arr , n=1000):
    u = arr.mean()
    sig = arr.std()
    x = np.linspace(arr.min(), arr.max(), n)
    e = np.exp(-a)
    y = 1/(sig*math.sqrt(2*math.pi)) * e
    return x, y

arr = np.arange(100)
x, y=gaussian_plot(arr, 10000)
plt.plot(x,y)

And the result:


Any guidance for new beginners interested in data science I am a student with a master degree in biostatistics. I am interested in data science. I know SAS and R. No experience with python. May I ask for your experts' advice on how to teach myself data science from zero, please?

Any advice is much appreciated.

In order to learn data science you should know about main tools and ideas in the data scientist's toolbox.
Two steps which most newbee's follow are:

The first is a conceptual introduction to the ideas behind turning data into actionable knowledge which involves following steps:

    Getting and Cleaning Data,Data Analysis,Reproducible Research

    Statistical Inference, using Regression Models and Machine Learning

The second is a practical introduction to the tools that will be used in the program like version control, markdown, git, GitHub, R, and RStudio.

I would advice you to complete a general course on data science from any website or tutorial you find easy for example I personally like this course: https://www.coursera.org/specializations/jhu-data-science

And then start building some projects of your own for example in my free time I build a lot of small data science projects playing with any available data like US census, petroleum data etc. For example see following link for one of my many data science projects:
https://arjun-chaudhary.shinyapps.io/Data_Analytics/


How to handle NaNs for ratio feature for binary classifier? I'm creating a churn model and would like to create a ratio (# customers / total transaction) for each merchant. About 70% of the data are NaNs (zero/zero).

I was wondering what I should impute for the 70% of the NaNs. I have other features and I don't like to delete the 70% of the data.

But if I impute 0s, the distribution would probably become different from ground truth since lower number means large transaction volume with a few customer. If I impute mean though, it'll also be different since the element (zero / zero) has no actions fundamentally.

I was about to impute -1 to distinguish the NaNs with non-NaNs. Would that make sense for a feature of binary classifier?

Thanks!

The usual solution is a mix of a few methods :

remove these records, but this should be avoided if possible
encode it based on general business knowledge, you could pick a
distinct number ( extremely large or small ) and then train the
model on it.
finally, I like usually like to plot one-way charts and
impute with values with the dependent (say churn rate) close enough
to the observed dependent rate over the NaN values for that
variable.
Edit - more recently, I have been using the package MICE and think it  could be used for imputing when you cannot do point 2 and there are far too many variables to do point 3 individually.


How can I perform categorical encoding when the dataset is too large for memory? I generally do preprocessing before fitting estimators using Scikit-Learn. My latest project is using significantly more data than I have used in the past, and whilst I know I can use online learning with either Keras using .fit_generator() or sklearn using .partial_fit(), I'm kinda at a loss as to how to do the categorical encoding steps in such a scenario. Obviously OrdinalEncoder requires a knowledge of every possible value in the feature to fit itself fully...but I can't give it that because I can't load the data into memory.

I haven't tried, but it might be possible to load a single categorical feature at a time and train an encoder on that data, before deleting it from memory and loading the next feature to train another encoder. That seems quite clunky though given there's not actually any reason something like categorical encoder couldn't partially learn categories in an iterative fashion, but I can't find anything in sklearn that will accommodate that.

Is there no "online learning" equivalent for preprocessing data?

You can read the file line by line (or block by block), assuming it is in a format where that can be done, and keep track of the unique values of the category.

Can handle multiple fields in 1 pass of the data instead of the 1 field like the below code.

Of course the easiest way is to get more memory or move to a cluster. The line-by-line solution will not be the fastest.

In pseudocode

unique_values_in_field_1 = set()

file = open(input)
while not EOF
    line = readline(file)
    # can also read a block of lines and adjust the code accordingly
    field_1 = line.field_1
    # make a function to handle multiple fields during the same pass of reading the file
    if field_1 not in unique_values_in_field_1:
       unique_values_in_field_1.add(field_1)

close(file)

# now have all of the unique values in this field. Do whatever is needed to transform them.


Input shape for simpler time series in LSTM+CNN Following is code from https://machinelearningmastery.com/how-to-develop-rnn-models-for-human-activity-recognition-time-series-classification/ which uses LSTM and CNN with TimeDistributed for human activity timeseries:

def evaluate_model(trainX, trainy, testX, testy):
    # define model
    verbose, epochs, batch_size = 0, 25, 64
    n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
    # reshape data into time steps of sub-sequences
    n_steps, n_length = 4, 32
    trainX = trainX.reshape((trainX.shape[0], n_steps, n_length, n_features))
    testX = testX.reshape((testX.shape[0], n_steps, n_length, n_features))
    # define model
    model = Sequential()
    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'), input_shape=(None,n_length,n_features)))
    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(Dropout(0.5)))
    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
    model.add(TimeDistributed(Flatten()))
    model.add(LSTM(100))
    model.add(Dropout(0.5))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(n_outputs, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    # fit network
    model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
    # evaluate model
    _, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
    return accuracy

However, I want to modify above code for a simple time series data (1000 rows and 100 columns), where each row is a time series of 100 values (shown as columns). The output is a class and there are 7 types of classes (hence n_output=7). It is a "sequence classification" problem.

What change should I make in above function for this? Do I need n_steps and n_length (since my data is simpler than human activity recognition data)? For simple CNN1D, I have to reshape as follows:

trainX = trainX.reshape(nrows, ncols, 1)
# then use (ncols, 1) as input shape

How should I reshape here?

I tried by removing n_steps and n_length and putting input_shape as (100, 1) (after reshaping data to (1000,100,1)) but it did not work.

For LSTM in tensorflow the tensor has three inputs. So, let's assume we have:
[samples, time steps, features]. This means that you have n number of samples, and each sample is divided in m time steps. All of the samples have the same number and the same features.

So in your case n_steps, n_length = 4, 32 means that n_steps are going to be taken in the data with 32 samples, meaning each 4samples are fed into the LSTM at one single point of time. Or 8 different subsets are going to be fed into the LSTM. LSTM usually takes subsets(not single row-samples!)

According to your reshape  (1000,100,1) you say that you have 1000 samples(imagine them as subsets of the dataset) and each of these 1000 subsets you divide into 100 time steps. This means that in each of the 1000 subsets, you must have at least 100 samples or a number divisible by 100, otherwise, it won't work. The third parameter "1" is referring to the number of features in your dataset/subset. When you work with accelerometer data I don't believe that you work with only one feature. My guessing is that there you need to have at least 3 features(x,y, and z) or maybe more.

Please check their documentation site: https://www.tensorflow.org/tutorials/sequences/recurrent


Backprop Through Max-Pooling Layers? This is a small conceptual question that's been nagging me for a while: How can we back-propagate through a max-pooling layer in a neural network?

I came across max-pooling layers while going through this tutorial for Torch 7's nn library. The library abstracts the gradient calculation and forward passes for each layer of a deep network. I don't understand how the gradient calculation is done for a max-pooling layer.

I know that if you have an input ${z_i}^l$ going into neuron $i$ of layer $l$, then ${\delta_i}^l$ (defined as ${\delta_i}^l = \frac{\partial E}{\partial {z_i}^l}$) is given by:
$$
{\delta_i}^l = \theta^{'}({z_i}^l) \sum_{j} {\delta_j}^{l+1} w_{i,j}^{l,l+1}
$$

So, a max-pooling layer would receive the ${\delta_j}^{l+1}$'s of the next layer as usual; but since the activation function for the max-pooling neurons takes in a vector of values (over which it maxes) as input, ${\delta_i}^{l}$ isn't a single number anymore, but a vector ($\theta^{'}({z_j}^l)$ would have to be replaced by $\nabla \theta(\left\{{z_j}^l\right\})$). Furthermore, $\theta$, being the max function, isn't differentiable with respect to it's inputs.

So....how should it work out exactly?

There is no gradient with respect to non maximum values, since changing them slightly does not affect the output. Further the max is locally linear with slope 1, with respect to the input that actually achieves the max. Thus, the gradient from the next layer is passed back to only that neuron which achieved the max. All other neurons get zero gradient.

So in your example, $\delta_i^l$ would be a vector of all zeros, except that the $i^*$th location will get a values $\left\{\delta_j^{l+1}\right\}$ where $i^* = argmax_{i} (z_i^l)$


How to display graph with MathJax in jupyter notebook? I am trying to create a jupyter-notebook with a graphical depiction of a neural network.  For starters, I am trying to display a simple network that evaluates a boolean NOT operator.

I am using the %%dot cell magic that is documented at https://metakernel.readthedocs.io/en/latest/source/README.html?v=20190725125340#id13 and I have a code cell in my notebook with the following contents:

%%dot
digraph NOT {
  rankdir = LR
  node [shape="circle"]
  x0 [label="+1"]
  x1 [label="$x_1$"]
  h [shape="plain"]
  y1 [color="coral" label=""]
  x0 -> y1 [label="10"]
  x1 -> y1 [label="-20"]
  y1 -> h [label="g()"]
}

This is rendered into an SVG that is displayed as the output of the code cell.
However, for the label on the x1 node I've used $x_1$, which I'd like to render as $x_1$ (presumably by MathJax, as it does in the Markdown cells).  Unfortunately, I just get a blank node with no label, like this:

Any idea what I can do to fix this?

I ended up creating a new magic, based on the %dot magic, called %dotMJ.  It works by looking for <text> objects in the SVG (corresponding to labels in GV), checking if they are LaTeX code (start/end with $), and if so converting them to a <div> and wrapping with a <foreignObject>.  This allows MathJax to "see" the LaTeX code and convert it appropriately for display.

Source is available at: https://gitlab.com/jello/ML-Notebooks/blob/master/dotMJ_magic.py


Impact of unlabelled documents for label prediction via SVM I have a corpus of text documents, some of which are labelled by analysts with label L. I am using this data to train an SVM for predicting if a new document should have label L.

So far it's straight-forward, but there is an issue: The analysts have not evaluated all documents in the training set, so there are in fact three classes of documents:

Documents labeled L
Documents the analysts have looked at, and chosen not label L (so you could say they're labelled not-L)
Documents the analysts have not looked at.

Unfortunately, at training time, I have no way to separate documents in 2. and 3, or not-L and unlabelled documents. I believe this is a problem, because a not-L label gives information to the SVM, but an unlabelled document is more "neutral".

How can I estimate the impact of this issue on predicting if a new document should have label L?

It's a slight variation on your problem as described, but if your goal is to build a good model for predicting documents labeled $L$, I would initially formulate this as a recommender system problem, until you've reached a desirable point in the learning curve of your system. I did exactly this in a publication a few years back. In my approach, all labeled documents are used to train the SVM, while all those unlabeled are used for classifying. I use the signed margin distance of each classified document for ranking, and take the top-$n$ as the next $n$ documents the reviewers should assign a label $\in(L, L_{not})$. A side effect of this process is that you get to iteratively evaluate the performance of your classifier as you add new labeled documents to the model, which should get you what you need. If this approach sounds useful to you, I recommend looking over my paper, as I describe some performance metrics useful for this, in addition to outlining the specific procedure to use that will ensure you're not biasing your model.


How to save/load a Model (Pickle) with a specific path/directory I seems like a very basic think but I couldnt find an answer to it.

I want to save my model to a specific directory using pickle.
The two algorithms below work fine for saving it in the same directory as the code itself but I want to save all my models in a dedicated folder.
I tried to just change the "filename" to "filepath" and well, make it a path but the world isnt that easy it seems.

Example Path: C:\Learning\Python\Data Science\02_TrainedModels

.

# save the model to disk
filename = 'Father_Son_Height_Model.pckl'
pickle.dump(lm, open(filename, 'wb'))

filename = 'Father_Son_Height_Model.pckl'
loaded_model =    pickle.load(open(filename, 'rb'))

With This Code:

# save the model to disk
filepath = r'H:\99_Lernen\Python\Data Science\02_Trained Models\Father_Son_Height_Model.pckl'
pickle.dump(lm, open(filepath, 'wb'))

I get this Error:
FileNotFoundError: [Errno 2] No such file or directory: 'H:\99_Lernen\Python\Data Science\02_Trained Models\Father_Son_Height_Model.pckl'

In this line of code:

pickle.dump(lm, open(filepath, 'wb'))

The "\" escapes the following sign when parsed, so the path cannot be read.

Use "/" instead and it should work. Also this question was probably more of a SO question ;)


Handling large imbalanced data set I have an imbalanced data set consisting of some 10's of millions text strings, each with thousands of features created by uni- and bigrams, and additionally I have also the string length and entropy of string as features.

It is a multiclass data set (40-50 classes), but it is imbalanced. Some classes can be 1000x smaller compared to the largest class. I have restricted the data to 1 million strings per class as maximum, otherwise the imbalance could be even larger.

Because of this I want to use over-sampling to improve the data for the underrepresented classes. I have looked into ADASYN and SMOTE from the python imblearn package. But when I run it the process eats up all my RAM in the swap memory, and soon after the process gets killed. I assume because the memory is not enough.

My question is now how to best proceed. Obviously my data is too large to be over-sampled as it is. I have thought of two options, but I cannot make out which is the most "correct".

I sent in only one underrepresented class and the largest class, and repeated this for each underrepresented class. I am not sure if this could mean that classes might start to overlap though.
I instead under-sample the data, maybe down to 100k samples per class. This might reduce the data enough such that I can run oversampling on the less represented classes (with 1k-10k samples).

Any other options that are more appropriate that I have missed?

There are multiple options, depending on your problem and the algorithms you want to use. The most promising (or closest to your original plan) is to use a generator to prepare batches of training data. This is only useful for models that allow for partial fits, like neural networks. Your generator can just stratify examples by for example generating a batch that includes exactly one of each target. One epoch would be when you served all the samples from the biggest class.

Downsampling is not a bad idea but it depends on the difficulty of your task, because you do end up throwing away information. You could look at some curves depending on the amount of samples for your model, if it looks relatively capped this wouldn't be a big issue.

A lot of models allow for weighting classes in your loss function. If we have 10,000 of class A and 1,000 of class B, we could weight class B 10x, which means mistakes that way count much harder and it will focus relatively more on samples from class B. You could try this but I could see this going wrong with extreme imbalances.

You can even combine these methods, downsample your biggest classes, upsample your smaller classes and use weights to balance them perfectly.

EDIT: Example of the batch options:

We have 4x A, 2x B and 1x C, so our set is:

A1 A2 A3 A4 B1 B2 C1

Regular upsampling would go to:

A1 A2 A3 A4 B1 B2 B1 B2 C1 C1 C1 C1

But this will not fit in our memory in a big data setting. What we do instead is only store our original data in memory (could even be on disk) and keep track where we are for each class (so they are seperated on target).

A: A1 A2 A3 A4
B: B1 B2
C: C1

Our first batch takes one of each class:

A1 B1 C1

Now our C class is empty, which means we reinitialize it, shuffle them (in this case it's only one example).

A: A2 A3 A4
B: B2
C: C1

Next batch:

A2 B2 C1

B and C are empty, reinitialize them and shuffle:

A: A3 A4
B: B2 B1
C: C1

Next batch is:

A3 B2 C1

And our last one of the epoch would be A4 B1 C1

As you can see, we have the same distribution as the full memory option, but we never keep more in memory than our original ones, and the model always gets balanced, stratified batches.


Analysis of data E-learning website Have a dataset of a E-learning company  which have information related to the students demographics, package, payment info, services used, class or grade (KG to 12), login details.
 If you want i can share the sample data set.
Could anyone help me out in understanding what business value can be derived from this data. I am really confused. I did some analysis was able to find out the max service consumption. Any help would be appreciated

Read news articles on schools for inspiration.

Student achievement compared by demographic
Student advancement (or lack therefore) across grade
Class popularity by enrollment
Average cost per student


Previous work Replication and Research ethics Ask Question I am very much concerned about biding by research ethics in my work, especially issues to do with plagiarism. I come across a recent research paper in my field of study that applies state-of-the-art tools (deep learning architectures) in their work using a publically available dataset.

I am impressed by their work and feel I should apply the same methodology they used but using my dataset (private).

Would this be considered a plagiarised version of their work?

I wouldn't think so. If they're publishing their methodology, they want other people to see how well it works and apply it to their work. You'll probably want to explain why you think this method works best for your dataset and compare the performance results to other methods commonly used.


Dealing with issues in "test" predictons for single "items" (null values, standardization in place, etc) I know this is kind of a broad question but I have tried to scour both this forum and the internet in general to no avail for this particular situation. So imagine I have a model trained for which, though the data initially might not be complete and clean, I took steps to make the data complaint with the model requirements (no outliers where appropriate, de-skewed if necessary, normalized if necessary, null values imputed appropriately). This is done in a cross validation framework. All this stuff works and is absolutely fine when tuning the model but I run into problems when I try to make a single prediction out of it (meaning I have a single "test" record - think web service with some fields that can be null). In fact, null values generally need a dataset to refer to for filling, as well as for the normalization/outlier procedures.

Initially I thought about linking such "test" record to a portion of the "train" dataset so that I would not run into this problem (such problems would be resolved) but at that point other issues would arise: how would I choose such dataset? if I used the most recent data, would I bias it somehow? and using the whole dataset is impractical as well as potentially unfeasible when dealing with "big" data.

do you happen to know whether there are some best practices on the topic or could you refer me the themes/keywords that deal with these issues?

p.s.: for the relevance of the problem, the null values most likely will remain there (I have no way of forcing them beforehand in the web application in order to have a smoother user experience)

You need to save the instructions for performing these preprocessing steps, not necessarily the dataset that you extracted them from.

See
Obtaining consistent one-hot encoding of train / production data
Binary Classification - One Hot Encoding preventing me using Test Set
one-hot-encoding categorical data gives error

In particular, sklearn preprocessors can be pickled, then used with their transform method in production, if you can use sklearn in deployment.  PMML also cam translate most transformers.  Or you can write your own simple transformer.

As to using newer data to rework the transformers, that's getting closer to retraining; in most settings, I would keep it in the same place as refitting the model: either both offline or both online.


Computer for DS curriculum I actually need to decide on which computer I will be taking for a Data Sciences curriculum, including machine learning and further hands on Hadoop. Some computer vision applications are also planned.
I already have a Tesla K20 5GB, I think it's enough for beginning, they suggest me the following choices :
Choice 1 : Core i5-4570n 16GB RAM, 240GB SSD+3TB HDD
Choice 1 : Ryzen 5 2600, 16GB RAM, 480GB SSD+3TB HDD

What would you recommend me ? Some changes can be done over configurations if necessary.

Just to add - I've found a second high quality monitor to be invaluable in improving productivity when working with datasets.


Prediction Intervals Using XGBoost I want to obtain the prediction intervals of my xgboost model which I am using to solve a regression problem. I am using the python code shared on this blog, and not really understanding how the quantile parameters affect the model (I am using the suggested parameter values on the blog).
When I apply this code to my data, I obtain nonsense results, such as negative predictions for my target values while my target values are always over 10K.
I don't understand how should this code vary according to my data and would really appreciate any help.

Differences in my data to the data that is used on the blog are:

My distribution is Poisson like.
I have over 100 features.

Note: I tried tuning the delta, threshold and var parameters, but they don't seem to have a controllable effect on the results and predictions remains nonsense.

To produce confidence intervals for xgboost model you should train several models (you can use bagging for this). Each model will produce a response for test sample - all responses will form a distribution from which you can easily compute confidence intervals using basic statistics. You should produce response distribution for each test sample.

this answer is provided here:
https://stackoverflow.com/questions/37418938/how-to-obtain-a-confidence-interval-or-a-measure-of-prediction-dispersion-when-u


Convert Neural network to Keras Classifier I am training a Neural Network for Multi-Class classification. After succesfully training it and validating the model through cross validation, I would like to use this network inside a voting Classifier. In order to perform cross validation on my trained network I convert it to a Keras Classifier and then calculate its validation score. However when I parse the same exact "Keras Classifier" in the Voting Classifier method, I get the following error:
ValueError: The estimator KerasClassifier should be a classifier

The code can be seen below:
import random
random.seed(42)

from keras.layers import Dense
from keras.models import Sequential
from keras.wrappers.scikit_learn import KerasClassifier
import tensorflow as tf
from sklearn.ensemble import VotingClassifier

def NeuralNetwork():
    model = Sequential()
    # define first hidden layer and visible layer
    model.add(Dense(600, input_dim=k, activation='relu'))
    model.add(Dense(20, activation='relu'))
    # define output layer
    model.add(Dense(3, activation='softmax'))
    # define loss and optimizer
    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
    return model

NN = KerasClassifier(build_fn=NeuralNetwork , epochs=100, batch_size=100, verbose=0)
cross_val_score(NN, X_new_train,y_train_no_id, cv=3)

votingC = VotingClassifier(estimators=[ ('LR1', LR1),('LR2', LR2), ('XGB',XGB),('NN',NN)], voting='hard', n_jobs=4)
#LR1 and LR2 are some other Logistic Regression estimators defined in another section
votingC = votingC.fit(X_new_train, y_train_no_id)
votingC.predict(X_new_test)

There are a couple of Github issues on this - see here and here. In short, while scikit-learn native models include an _estimator_type attribute:
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf._estimator_type
# 'classifier'

this is not the case with a KerasClassifier; using your own NN gives
NN = KerasClassifier(build_fn=NeuralNetwork , epochs=100, batch_size=100, verbose=0)
NN._estimator_type

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-4-a2fd193c154f> in <module>()
----> 1 NN._estimator_type

AttributeError: 'KerasClassifier' object has no attribute '_estimator_type'

As reported in a comment here, setting the attribute manually seems to work:
NN._estimator_type = "classifier"

so add this line immediately after your NN definition.


Remove Local Outliers from Dataframe using pandas Could someone please suggest how to remove local outliers from the dataframe? I have the code to detect the local outliers, but I need help removing them(setting these values to zero) in the dataframe. Any advice would be highly appreciated.

The code to detect the local outliers is below:

def printOutliers(series, window, scale= 1.96, print_outliers=False):

rolling_mean = series.rolling(window=window).mean()

#Print indices of outliers
if print_outliers:
    mae = mean_absolute_error(series[window:], rolling_mean[window:])#mean absolute error is a measure of difference between two continuous variables.
    deviation = 3*np.std(series[window:] - rolling_mean[window:])
    lower_bound = rolling_mean - (mae + scale * deviation)
    upper_bound = rolling_mean + (mae + scale * deviation)
    outliers_lower = series[series<lower_bound]
    outliers_upper = series[series>upper_bound]
    print("values beyond lower bound are: " +  "\n"  + str(outliers_lower))
    print("values beyond lower bound are: " + "\n" + str(outliers_upper))

printOutliers(df['Column1'].dropna(how='any'), 10, print_outliers=True)

You could use this approach here:

#------------------------------------------------------------------------------
# accept a dataframe, remove outliers, return cleaned data in a new dataframe
# see http://www.itl.nist.gov/div898/handbook/prc/section1/prc16.htm
#------------------------------------------------------------------------------
def remove_outlier(df_in, col_name):
    q1 = df_in[col_name].quantile(0.25)
    q3 = df_in[col_name].quantile(0.75)
    iqr = q3-q1 #Interquartile range
    fence_low  = q1-1.5*iqr
    fence_high = q3+1.5*iqr
    df_out = df_in.loc[(df_in[col_name] > fence_low) & (df_in[col_name] < fence_high)]
    return df_out


How to model & predict user activity/presence time in a website I need to make a prediction model based on some historical data from a website's user login system. Suppose my dataset has some features like user login time and logout time for each day for a specific user. Login and logout times can be multiple in a day for a specific user. Suppose, If the user login 5 times in the website in a day, there will be five entry points as rows to the dataset for that user, logout also works like this. Now from the login and logout time, I need to find out the active time that user was logged in to the website as well as predicting the inactive time in which user is not available/present in the website. How can I do this? Which algorithm should I use and which prediction model (Linear regression/Logistic Regression/Time series) need to choose in this case? It will be very much helpful if you can suggest me for this specially to implement in R. Thanks.

Edit:

Actually I need to find out/predict a time in which the user is active in a website during the day. I have a dataset with 3 columns listed as "user_id", "login_time" and "logout_time". Now I am trying to make another column "active_time" in which I'm trying to compute the user's active time in the website by subtracting the login time from logout time and it can be multiple as user can access website multiple times in a day. Now I need to predict the time in which the user is active in the website where active time is the target variable and login, logout time as predictor. I also trying to make a linear regression model for this prediction. But I don't know whether my process is correct or not for this problem. Can anyone please let me know which type of model I need to build for this prediction? Is it will be Linea regression, logistic regression or time series ?

I suggest approaches based on neural networks for time to event data. Depending on your data I think you can also use

https://www.slideshare.net/mobile/datasciencelondon/survival-analysis-of-web-users

https://arxiv.org/abs/1807.04098

https://arxiv.org/abs/1801.05512

https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/


What is Teacher Helping technique? I read this paper, but I am having trouble understanding what Teacher Helping technique (page 3) is in context of RNN.

Can someone explain to me what it is? Please assume I don't have much experience in statistics. I'll be glad if this explanation would be as simple as possible (I'll be even more grateful for some example).

In normal RNNs, you can train using either back-propagation through time (BPTT) or teacher forcing. With BPTT the network receives as input for each time step the output of the previous time step. In teacher forcing, the network receives the gold data tokens directly; this induces exposure bias on the trained network.

With textual GANs, you normally don't feed the gold data to the generator, because the generator is generating the sequence so there is no gold data to use.

In the article you refer, they are taking subsequences from real data, feeding them to the generator and having it only generate the final token. Then the discriminator receives the concatenation of the real sequence prefix and the generated final token. This is what they call "teacher helping".


What is the best approach to perform information extraction from tourist reviews using NLP, DL? I am interested in performing some information extraction from tourist reviews about different places.
I have data of 50 different places and around 300-400 reviews about each of them and I would like to get information like: weather, experience(happy/not happy), costly/cheap, places around, best time to visit, etc.
Reviews are like:

Mahabaleshwar is a hill station situated nearby Pune. You can reach
there by road via Pune. Air connectivity is available up to Pune and
from there you can travel by road. Mahabaleshwar is having so many
mountain range. Weather is remaining very beautiful throughout the
year.
There are so many points to be visited in Mahabaleshwar like Parsi
point, Elphinstone point, Wilson Point, Arthur seat point. There is
Vennna Lake where you can have boating and horse riding. Strawberry is
available at each and every place in Mahabaleshwar which is its main
attraction. There are so many hotels available but they are somewhat
costly. By the way it is the nice place to visit.

Mahabaleshwar is a best place for tourists. It is located in Maharashtra. I also visit Mahabaleshwar when I'm free time. It is
famous for their's natural beauty. Here is a one river ans when I
visit to Mahabaleshwar then I definitely go to ride on small boat, it
Is very joyful.

Need some suggestions on what algorithm can be used and about data processing approach. Links or tutorials are would be helpful too.
Thanks!

I'm currently reading Jurafsky and Martin's Speech and Language Processing on NLP. I suggest you to take a look specifically at Chapter 17: Information Extraction. All models for this task are devoted to the extraction of lexico-syntactic patterns. They describe supervised, semi-supervised and unsupervised approaches.


Speed Date or Dating Conversation Dataset / Corpora I'm working on an LSTM model that uses dating conversation dataset. Any suggestion as to where I can get such data? Any ideas will be much appreciated.

I have a feeling that you are never going to come across this, most of this data is closely guarded. It's no secret that dating sites like OK Cupid use data science on their app, so I think that most of these sites aren't quick to make their data public.


Learning with Positive labels only I have ~7 million rows of customer data (~500 sparse attributes)
A million out of them have opted in to a new service.
How do I use this signal to predict which of the remaining customers are likely to adopt the service? And how do I measure the effectiveness?
Problems face so far -

Unable to treat this as a supervised problem due to lack of definitely negative variable
Unable to apply label propagation because there is only one class

Apart from treating this as an anomaly detection problem (oneclasssvm etc.), I also tried using nearest neighbors based approach.
Looking for other ways to solve the problem if there are some go-to techniques that I am missing.
I know there is an answer here but it only talks about oneclasssvm that I have already tried. Also trying to find ways to measure model effectiveness along with any novel ways to solve.

The topic you are interest in is called "PU learning" or "positive and unlabeled learning".
You can start by having a look into  survey literature.


How to keep a subsetted value for calculating mean I am currently learning R and I have to solve an Issue were I have to extract values from a data set which are from a specific month and from this values I should calculate the mean Temp.

I did it like that:

data[data$X..Month.. == 6,]

mean(data$X..Temp.., na.rm=TRUE)

It gave me the mean value but without taking my first statement into consideration.
What do I need to do that both statements are taken into consideration?

You can answer this using tapply, which will give you the mean for all your subsets, e.g.

dummy data

df = data.frame(mon=rep(month.abb, times=10)

calculate mean for all months

tapply(df$temp, df$mon, mean, na.rm=T)


What are the practical applications of inverse reinforcement learning? Inverse reinforcement learning is about using expert trajectories to learn a reward function. Now the most successful method is Maximum Entropy Inverse Reinforcement Learning. But in that, you need a model-based reinforcement learning. But most of the practical world problems are model-free which is hard to calculate the state transition probabilities. So how can we use this inverse reinforcement learning in real-world problems?

I think that the field has moved on from that paper. There is a trend to use the data from the expert to either "precondition" the agent or extracting a policy directly from the data. You can search for imitation learning or behavioral cloning. Some of these algorithms: Generative Adversarial Learning, DAGGER and Deep Q-learning from Demonstrations . AlphaGo used Supervised Learning as well to get to a good policy before getting trained in a RL setting. So instead of trying to recover a complicated reward function from the data you can use the above methods to get a good policy or initialize the parameters of your agent to more promising directions. Hope this helps!


Using TensorFlow with Intel GPU Is there any way now to use TensorFlow with Intel GPUs? If yes, please point me in the right direction.
If not, please let me know which framework, if any, (Keras, Theano, etc) can I use for my Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor Integrated Graphics Controller.

At this moment, the answer is no. Tensorflow uses CUDA which means only NVIDIA GPUs are supported.

For OpenCL support, you can track the progress here.

BTW, Intel/AMD CPUs are supported.


Unsupervised Anomaly Detection in Images I would like to detect defects/anomalies in images. Due to the lack of images with anomalies, I try to solve the problem in an unsupervised manner. Until now, I trained a variational autoencoder together with an generative adversarial network with “good” images. Now, I have an encoder/decoder network which is able to produce images without anomalies. Here are two examples:

original image | cropped image & input of the encoder | output of the decoder

In those images, if the object is rotated (not vertical), then it is an anomaly (like the second image). To measure the difference between the input/output of the encoder/decoder network, I tried the structural similarity metric SSIM. However, the result is not satisfying enough as many images without an anomaly also have a low similarity value. Also the pixel-wise MAE or MSE metrics are not good as they do not look specifically on the structure and are very sensitive to the brightness. Here is the result with the SSIM metric (green are the good examples, red are the anomalies):

What I want to avoid is to add specific rules for the detection, in this example to detect vertical lines because there are many other objects with different anomalies which I would like to detect.

Do you have a suggestion for another image similarity metric? Am I on the right path or are there other/better methods to do unsupervised anomaly detection?

PS: There are only a few images with anomalies (<10 images), but many without an anomaly..

There seems to be a clear line between samples with anomalies and those without. Label them and run a classifier? e.g SVM. One shouldn't use more complex models than needed.

Also, be a bit careful with trying to detect all kind of anomalies. It's not always realistic and might lead you away from more straightforward, and perhaps better solutions.

You could also try something as simple as a histogram from one part of the image. Looks like it should be able to find the anomalies.


Imputation of missing values and dealing with categorical values I have a dataset (10 million rows, 55 columns) with many missing values. I need to predict those values somehow using other non-missing values, i.e. replace them with something that is not NaN. Mean and median are not the solution here.

I tried to research other methods for that but none of them works since I have many categorical variables. I also tried to use one hot encoding to convert categorical variables to integers but I am not sure if that is a solution in my case since from only 1 categorical column I would get 600 new columns. If I do the same with other categorical columns, I would get many millions of new columns. One of the categorical columns is URL string and it is different for every row, so I have 10 million different URLs for example.

The other categorical column is a description and it is also different for every row. I could probably remove the URL column, but I can't remove description, title, location and others for example. I tried PCA, but it also doesn't work with categorical data. I have missing data for both categorical and integers/floats values. Would get_dummies method be a good approach to deal with this? For missing values imputation I tried KNN and maximum likelihood but I am getting errors due to categorical variables. Missing data is completely randomly missing.

Do you have any suggestions how to approach this problem and also which packages should I use?

I think like @El Burro suggested, you I believe you should focus on feature transformation mainly. Use different techniques for different features. For straightforward features, such as occupation or gender for example, use one-hot encoding, while for others  you can use some kind of hierarchical mapping-clustering (e.g. map values to groups defined by you, for example if those urls linked to products, make 30 different groups with similar types of products and map the urls to these groups. Then you can use again one-hot encoding for these mapped features).

For the textual features you mentioned, I'm pretty confident you could drop some of them. If you really want to exploit some kind of information generated from text though, for starters you could either do some tf-idf feature extraction, so as to generate features for each text-snippet or consider topic modeling (take a look at gensim for python if interested) and represent each text as a mixture of topics.


Multilabel Classification With Ranking I have a dataset as below:

     Key Attr1 Attr2 Attr3 Attr4 Attr5 Attr6
     kd1 l1    l2     l3    l4   l5     l6
     kd1 l1    l7     l8    l9   l5     l10
     kd1 l11   l12    l13   l14  l5     l10
     kd1 ..................................
      .
      .
      .
     kd2 ..................................
     kd2 ..................................
      .
      .
     kd3 ..................................
      .
      .
      .

For each instance, I have multiple combinations of target outputs(Attr1-Attr6).
Whenever I use multilabel libraries, I get a single combination of outputs.

I want a ranked list(top 3) of target label classifications for each key given as input.

For example:

predict('kd1') should return the following:

res = [ [l1,l7,l8,l9,l10], [l1,l2,l3,l4,l5,l6], [l11,l12,l13,l14,l5,l10] ]

Here res[0] is the best combination, res[1]  is the second best combination and so on.

How do I go about that?

the task seems very hard to understand what you want to achieve as you say that there several equally good representation for each data key... In other words, you say following?

kd1 = l1 + l2 + l3 + l4 + l5 + l6
kd1 = l1 + l7 + l8 + l9   l5 + l10
kd1 = l11 + l12 + l13 + l14 + l5 + l10
kd1 = ...


Tensorflow hub module taking long time for embedding single sentence I am using universal sentence encoder from tensorflow hub to encode sentence into embedding.

import tensorflow_hub as hub
import tensorflow as tf

module_url = "https://tfhub.dev/google/universal-sentence-encoder/1?tf-hub-format=compressed"

def get_emb(text):
    embed = hub.Module(module_url)
    similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
    similarity_message_encodings = embed(similarity_input_placeholder)
    with tf.Session() as session:
            session.run(tf.global_variables_initializer())
            session.run(tf.tables_initializer())
            message_embeddings_ = session.run(similarity_message_encodings, feed_dict={similarity_input_placeholder: text})
    tf.reset_default_graph()
    return message_embeddings_

Now the problem is each time I call the function the time is taking is about 10 seconds which is very high. Is there a way to save the model or something and do it instantly. This will be used in restful service where the response should be near instant

I would recommend using the model like this:
import tensorflow_hub as hub
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
model = hub.load(module_url)
print ("module %s loaded" % module_url)
def embed(input):
    return model(input)

And then:
embed(["This is a nice string!"])

Which would return:
<tf.Tensor: shape=(1, 512), dtype=float32, numpy= array([[ 8.84598400e-03, ... , 1.89793427e-02]], dtype=float32)>

That is the 512 dimension vector, but you can choose the one you need between all the encoders here: https://tfhub.dev/
Hope it helps.