Time Series prediction using LSTMs: Importance of making time series stationary In this link on Stationarity and differencing, it has been mentioned that models like ARIMA require a stationarized time series for forecasting as it's statistical properties like mean, variance, autocorrelation etc are constant over time. Since RNNs have a better capacity to learn non-linear relationships (as per given here: The Promise of Recurrent Neural Networks for Time Series Forecasting) and perform better than traditional time series models when the data is large, it is essential to understand how stationarized data would affect its results. The questions I need to know the answer of are as follows:

In case of traditional time series forecasting models, stationarity in time series data makes it easier to predict, why and how?
While building a time series prediction model using LSTMs, is it important to make the time series data stationary? If so, then why?

In general time series are not really different from other machine learning problems - you want your test set to 'look like' your training set, because you want the model you learned on your training set to still be appropriate for your test set.  That's the important underlying concept regarding stationarity.  Time series have the additional complexity that there may be long term structure in your data that your model may not be sophisticated enough to learn.  For example, when using an autoregressive lag of N, we can't learn dependencies over intervals longer than N. Hence, when using simple models like ARIMA, we want data to also be locally stationary.

As you said, stationary just means the model's statistics don't change over time ('locally' stationary).  ARIMA models are essentially regression models where you use the past N values as input to linear regression to prediction the N+1st value. (At least, that's what the AR part does).  When you learn the model you're learning the regression coefficients.  If you have a time series where you learn the relationship between the past N points and the next point, and then you apply that to a different set of N points to predict the next value, you are implicitly assuming that the same relationship holds between the N predictor points and the following N+1st point you're trying to predict.  That's stationarity.  If you separated your training set into two intervals and trained on them separately, and got two very different models - what would you conclude from that? Do you think you would feel confident applying those models to predict new data? Which one would you use? These issues arise if the data is 'non-stationary'.
My take on RNNs is this - you are still learning a pattern from one segment of a time series, and you still want to apply it to another part of the time series to get predictions.  The model learns a simplified representation of the time series - and if that representation applies on the training set but not in the test set, it won't perform well.  However, unlike ARIMA, RNNs are capable of learning nonlinearities, and specialized nodes like LSTM nodes are even better at this.  In particular, LSTMs and GRUs are very good at learning long-term dependencies.  See for example this blog post.  Effectively this means that what is meant by  'stationarity' is less brittle with RNNs, so it's somewhat less of a concern. To be able to learn long term dependencies, however, you need LOTS of data to train on.

Ultimately the proof is in the pudding. That is, do model validation like you would with any other machine learning project.  If your model predicts well for hold-out data, you can feel somewhat confident in using it.  But like any other ML project - if your test data is ever significantly different than your training data, your model will not perform well.


Do you have any real example of Data Science reports? I recently found this use cases on Kaggle for Data Science and Data Analytics.

Data Science Use Cases

However, I am curious to find examples and case studies of real reports from other professionals in some of those use cases on the link. Including hypothesis, testing, reports, conclusions (and maybe also the datasets they have used)

Do you have any kind of those reports to share?

I am aware of the difficulty to share this kind of details and this is why it is not a Google-search-and-find answer.

I think you can better go through various university thesis reports and data science related journal papers to get more details on "Including hypothesis, testing, reports, conclusions" of the above mentioned Data science related problems.

Check these links from Stanford university :

http://cs229.stanford.edu/projects2014.html

http://cs229.stanford.edu/projects2013.html

http://cs229.stanford.edu/projects2012.html


How to transform time series data to apply supervised learning algorithms to it? Apologies in advance for what may be a very basic question.

I have a dataset consisting of marketing calls to different clients, which include the timestamp for the call.
My goal is to train a model to predict if whether a customer will answer a call or ignore it based on the call time, as well as other features like caller id, etc.

The issue is that the outcome of the call is clearly time dependent, that is, the order of the data points per client matters for the prediction of the dependent variable at a given time.

My question is, how can I transform my features, so that I can use standard classification algorithms like Logistic Regression/Random Forest to classify a new data point? Are these algorithms effective for these scenarios? If so, how  should I proceed to take the previous data points into consideration?

I have read that time series data can be converted to a supervised learning problem, by including lead and lag columns of the dependent variable. But since my test data will not have those columns, I am confused to how they might help me.

Thank you very much.

You need to transform your timestamp column into several numerical/categorical columns representing that specific timestamp. After converting that timestamp column into features, you can simply throw supervised algorithms on the problem.

You could leverage for example the fast.ai function add_datepart that you can find here:


Is there any way to read Xlsx file in pyspark?Also want to read strings of column from each columnName pd is a panda module is one way of reading excel but its not available in my cluster. I want to read excel without pd module. Code1 and Code2 are two implementations i want in pyspark.

Code 1: Reading Excel

pdf = pd.read_excel(Name.xlsx)
sparkDF = sqlContext.createDataFrame(pdf)
df = sparkDF.rdd.map(list)
type(df)

Want to implement without pandas module

Code 2: gets list of strings from column colname in dataframe df

stringsList = []
columnList = list(df[colname])
for i in range(len(columnList)):
    if type(columnList[i]) != float:
        text = columnList[i]
        stringsList.append(text.lower())
    else:
        stringsList.append(u'')
return stringsList

I want to implement this in pyspark.

Is pandas itself available on the cluster?
If so, you may try to go with the in-built read_excel().

You may also try the HadoopOffice library, it contains a Spark DataSource, also available as Spark Package, you can easily test it out without any installation:

$SPARK_HOME/bin/pyspark --packages com.github.zuinnote:spark-hadoopoffice-ds_2.11:1.0.4

Some people also recommend the Spark Excel dependency.


Are validation sets necessary for Random Forest Classifier? Is it necessary to have train, test and validation sets when using random forest classifier?

I understand it is important with Neural Networks but I am not understanding the importance of it with RF. I understand the idea of having a third unseen set of data to test on is important to know the model isn't overfitting, esp with Neural networks, but with RF it seems like you could almost not even have test or validation data (I know in practise this isn't true) but in theory since each tree of the forest   uses a random sample (with replacement) of the training dataset.

At the moment I am missing out on approx 250 samples by keeping them unseen from the train and test set and I know the model would improve with the extra data, so is it possible to have only train and test and not designate a seperate validation set, whilst still having a reliable model?

You can use Out-of-bag error as you validation error, if you are short of data.

As you may know, Random Forest fits multiple decision trees, and for each tree it only fits on a subset of data. So data that hasn't been used for fitting a given tree is called Out of Bag data, and it could be used as your validation set 1

Sklearn in Python has a hyperparameter of Out-of-bag error


Optimizing vector values for maximum correlation I'm new to ML, linear algebra, statistics, etc. so bear with me on the terminology...

I’m looking to find a vector that produces the maximum correlation for the relationship between 1) all relationships among dimensions of the vector (determined by subtraction) and 2) some output value produced by said relationships.  I'm specifically using this to create a sports ranking system that takes a number of matches and the resulting scores and attempts to assign a value to the teams that can be used to predict future scores.  In other words, the difference between any two team's ratings should be predictive of the score differential for the next match between the two.

So for example, if I have 3 teams, A, B, and C, each start with unknown ratings:

$$
\begin{array}{c|c}
  A&?\\
  B&?\\
  C&?\\
\end{array}
$$

If each team played each other team once, the left table would be used to calculate their rating differences (column team's rating minus row team's rating).  The right table would be the difference in scores in the respective matchups.

$$
\begin{equation}
\begin{array}{c|c}
  &A&B&C \\
  \hline
  A&*& B - A& C - A\\
  B&A - B& *& C - B\\
  C&A - C& B - C& *\\
\end{array}
\Rightarrow
\begin{array}{c|c}
  &A&B&C \\
  \hline
  A&0&3&6\\
  B&-3&0&3\\
  C&-6&-3&0\\
\end{array}
\end{equation}
$$

Here is a possible solution that, for this example, would result in a perfect correlation between team rating differentials and score differentials.

$$
\begin{array}{c|c}
  A&1\\
  B&2\\
  C&3\\
\end{array}
$$

This would be the regression line where x2 is the column team's rating and x1 is the row team's rating.

$$y = 3 * (x_2 - x_1)$$

It’s worth noting that what matters is the relationship between the various values (not their nominal values) since this would be another possible solution:

$$
\begin{array}{c|c}
  A&2\\
  B&4\\
  C&6\\
\end{array}
$$

Which would result in a linear equation that looks like this, which would also have a correlation of 1:

$$y = {3\over 2} * (x_2 - x_1)$$

What I want to do is find a method to determine values for A, B, and C that maximizes the correlation between the pairwise differences and resulting output values.  The one additional catch for the teams example is that not every team will play every other team so any resulting matrices will be asymmetrical (assuming that matters).

Are there any existing techniques to address this problem?

This is one approach you can follow:

Setup a linear regression system, with each match as a row, and each feature corresponding to a team.
The unknown feature coefficient for each team is the team 'strength' that we try to determine.
The feature values will be one of 0, 1, or -1, depending on whether the team did not play, was the column team, or the row team respectively in that match.
The regression target will be the score differential (column team score - row team score) in that match.

Eg: For the result matrix above, the system would be:

$$
(-1) * x_1 + (1) * x_2 + (0) * x_3 = 3\\
(-1) * x_1 + (0) * x_2 + (1) * x_3 = 6\\
(0) * x_1 + (0-1) * x_2 + (1) * x_3 = 3
$$

One solution to the above system is: $x_1=-6; x_2=-3; x_3=0$
Multiple solutions are possible, which can be viewed as translations of each other (adding a constant to all team strengths).
If there are 'n' teams, then there are only 'n-1' linearly independent columns in the regression. (In R, one of the coefficients comes out as NA. This can be treated as 0, or dropped from the regression, effectively making it 0).


Having trouble installing and loading tidyverse- No DIB package after a fresh (Control panel, Windows 7) uninstall, then re-install of Rstudio, I have tried to install and load tidyverse.  I get the following message, seemingly because the package DBI is missing:

  install.packages("tidyverse")
  Installing package into ‘C:/Users/jshea/Documents/R/win-library/3.3’
  (as ‘lib’ is unspecified)
  trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/tidyverse_1.1.1.zip'
  Content type 'application/zip' length 41994 bytes (41 KB)
  downloaded 41 KB
  package ‘tidyverse’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\jshea\AppData\Local\Temp\1\RtmpmoPd5V\downloaded_packages

  library(tidyverse)
  Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
    there is no package called ‘DBI’
  Error: package or namespace load failed for ‘tidyverse’

Earlier this morning, when I did this the first time, the system seemed to download all the packages except there was some kind of error associated with DBI- I think a database package.  Even uninstalling and deleting obvious remnants of Rstudio does not seem to give me a clean base to fix this.  What can I do?

It sounds like you do not have a dependency installed which tidyverse needs to run correctly.

Usually dependencies install with the package, but at times you need to coerce them to do so, so I usually install with the coercion for all packages.

install.packages("tidyverse", dependencies=TRUE)

In this case, if that was the only error, or you installed in this manner and did not get DBI, then I would just install it directly:

install.packages("DBI", dependencies=TRUE)

Once you have done that, if you are still having problems, explicitly load DBI:

require(DBI) or library(DBI)

and you should be good to go.

If you have a problem still, comment and I will try to help you chase it down.


How do we determine what is learnt by a ML model? I am trying to investigate/justify the output of a random forest regression model for a financial problem, where justification of the output is also important. In that context, for the random forest regression model that I built, I'm using the tree interpreter for justification of the output using the contributions. But is there any other way I can do this better? Is this the best approach?

The idea is to be able to explain the output of the random forest regression model to a layman supported with technical points.

Shap values is your friend.

model = RandomForestRegressor(max_depth=6, random_state=0, n_estimators=10)
model.fit(X_train, Y_train)
import shap
shap_values = shap.TreeExplainer(model).shap_values(X_train)
shap.summary_plot(shap_values, X_train, plot_type="bar")

There are several other techniques with their own drawbacks. Some are:

Permutation Importance
Partial Dependence plots
LIME
Global Surrogate Models
etc.. You can add to this list.


Using Trainable=True in Keras Embedding obtained better performance It is suggested by the author of Keras [1] to use Trainable=False when using the embedding layer in Keras to prevent the weights from being updated during training. But in my experience, I always got better performance (lower error in regression) when setting Trainable=True in text processing.

My question, can I claim my result or I must use Trainable=False?
Do I make a cheat when using Trainable=True?

According to my understanding, it just a choice to use (or not to use) previous information to update the weight, so it is allowed to use Trainable=True.

[1] https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html

It depends upon where do you want to submit your results that you claim, and what is the submission criteria.

First, it is unclear if "lower error in regression" is training or validation/test error. I am sure that letting Embedding layer to be trainable will adapt it to fit the training set better, but it might cause overfitting and cause higher error on validation/test set.

If for your set of tasks were you use Embedding layer you consistently get lower test error, and the requirement does not specifically say so to use trainable=False for Embedding layer - why bother? It is not cheating.


Causation vs correlation problem in case of fixing other parameters and changing only one I have a game that players play. D7 retention is defined as percentage of players who installed my game and after exactly 7 days later they played my game.

Now I am doing analytics and it shows that only 10% of players play my game on day 7. I change the difficulty of the game significantly and collect analytical data. Data shows that now D7 is 15%.

Can I conclude that D7 change is caused by difficulty change or this is one of the examples that correlation is not causation? And how I can identify if I can think of causation or not.

Physicists, psychologists in experiments have done a lot such things. Fixing all parameters changing one and observing the behavior change and drawing conclusion. What is the right way to approach this?

The concept of causality is a little tricky. You can (usually) not test for that. You need to come up with a theory-based idea of how things work, such as if the price for a good goes up, demand goes down. And in empirical applications you need to think about, what other things influence demand, so that you don't omit important aspects of the data generating process.

When I read your description, it seems that you might have a good chance to isolate the causal effect of the change in difficulty. This seems to be a classical example for "difference-in-difference" (DiD). The only problem may be that you changed the diffculty for all players, so you need to see how to handle this.

Another thing that comes to my mind is, that you essentially look at survival rates (after X days), this would be a task for "survival analysis.

As a (more or less) naive approach, I would say that your approach goes in the right direction. However, make sure that you control for anything you can, like player characteristics (because it is possible that now different people play your game compared to the past).

So when you have

$$y = \beta X + \gamma d + u$$,

where $y$ is D7, $X$ is anything you know about players, and $d$ is difficulty (0 diffcult, 1 less difficult), you should get a more or less good idea, what changing $d=0$ to $d=1$ does to y. When you use OLS (linear regression), the coefficient $\gamma$ should directly give you the change. But remember, this is a more or less naive approach (which should give "okay" results).


Exploratory Data Analysis I am working on this dataset.
Dataset has missing values. What would be the best method to impute the missing values. Also values are missing from target feature as well. So far I have dropped those observations from the dataset.

There are multiple instances of the same ID for which some variables will not change i.e. age, weight, height etc., but some values are missing. I am trying to impute the missing values by other given values for the same ID. How can I go about it in Python? Thanks in advance.

To deal with missing data you can use one of the following three options:

If there are not many instances with missing values, you can just
delete the ones with missing values.
If you have many features and it is affordable to lose some information, delete the entire feature with missing values.
The best method is to fill some value (mean, median) in place of missing value. You can calculate the mean of the rest of the training examples for that feature and fill all the missing values with the mean. This works out pretty well as the mean value stays in the distribution of your data.

Note: When you replace the missing values with the mean, calculate the mean only using training set. Also, store that value and use it to change the missing values in the test set also.

In python you can use the Imputer() class to fill up the missing values as follows:

from sklearn.preprocessing import Imputer
impute = Imputer(strategy="median")
impute.fit(df)

Also you can check the calculated median values for each column by using:

imputer.statistics_


Is the _error_ in the context of ML always just the difference of predictions and targets? Simple definitional question: In the context of machine learning, is the error of a model always the difference of predictions $f(x) = \hat{y}$ and targets $y$? Or are there also other definitions of error?

I looked into other posts on this, but they are not sufficiently clear. See my comment to the answer in this post:

What's the difference between Error, Risk and Loss?

The error can have different forms depending on the application. For example for a simple regression we often use the sum of squared deviations between the actual output $y_n$ for the input $x_n$ and the predicted output $\hat{y}(x_n)$ for the input $x_n$. The total loss $J_\text{Gauss}$ is then given as the sum over all squared errors (also known as Gaussian Loss)for each observation.

$$J_\text{Gauss}= \sum_{n=1}^N\left[y_n-\hat{y}(x_n)\right]^2$$

If we use absolute values instead of squares we obtain the Laplacian loss function $J_\text{Laplace}$, which is given by

$$J_\text{Laplace}=\sum_{n=1}^N\left|y_n-\hat{y}(x_n)\right|$$

If we rather try to compare two probability distributions $p(x)$ and $q(x)$ we use a unsymmetric distance meassure called Kullback-Leibler divergence

$$
{\displaystyle D_{\text{KL}}(P\parallel Q)=\int _{-\infty }^{\infty }p(x)\ln {\frac {p(x)}{q(x)}}\,dx}.
$$

For binary classification we can use the hinge-loss

$$J_\text{hinge}=\sum_{n=1}^N\max \{0, 1- t_n \hat{y}(x_n)\},$$

in which $t_n=+1$ if observation $x_n$ is from the postive class and $t_n=-1$ from the negative class.

For support vector regression the $\varepsilon$-insensitive loss $J_\varepsilon$ is used. It is defined by the following equation.

$$J_\varepsilon=\max\{0,|y_n-\hat{y}(x_n)|-\varepsilon\}$$

This loss acts like a threshold. It will only count something as an error if the error is larger then $\varepsilon$.

As you can see there are some meassures (see this Wikipedia article for loss functions used for classification) of error for comparing the predicted output and the observed output.


Why Huber loss has its form? Huber loss formula is

$\hspace{3.0cm} L_\delta(a) = \begin{cases} \frac{1}{2} a^2 && |a| \leq \delta \\ \delta (|a| - \frac{1}{2} \delta) && |a| > \delta\end{cases}$ where $a = y - f(x)$

As I read on Wikipedia, the motivation of Huber loss is to reduce the effects of outliers by exploiting the median-unbiased property of absolute loss function $L(a) = |a|$ while keeping the mean-unbiased property of squared loss function $L(a) = a^2$ within the typical region (i.e. region without outliers)

I don't know why don't Huber loss has the form:

$\hspace{3.0cm} L_\delta(a) = \begin{cases} \frac{1}{2} a^2 && |a| \leq \delta \\ c |a| && |a| > \delta\end{cases}$ where $c$ is some constant which controls the error magnitude of outliers

or similar forms ?

Let's check for continuity for the new proposed function. For convenience, let me call it $L_{\delta, c}$.

$$\lim_{a \to \delta^+}L_{\delta, c}(a)=c\delta$$ and $$\lim_{a \to \delta^-}L_{\delta, c}(a)=\frac{\delta^2}2$$

If we want the function to be continuous, we would want $c$ to take value $\frac{\delta}2$.

Now, let's check for diffentiability of $L_{\delta, \frac{\delta}2}$.

$$\lim_{h \to 0^+} \frac{L_{\delta, \frac{\delta}2}(\delta+h)-L_{\delta, \frac{\delta}2}(\delta)}{h}=\lim_{h \to 0^+} \frac{\frac{\delta(\delta+h)}2-\frac{\delta^2}2}{h}=\frac{\delta}2$$

$$\lim_{h \to 0^-} \frac{L_{\delta, \frac{\delta}2}(\delta+h)-L_{\delta, \frac{\delta}2}(\delta)}{h}=\lim_{h \to 0^-} \frac{\frac{(\delta+h)^2}2-\frac{\delta^2}2}{h}=\delta$$

which shows that we can't have $L_{\delta,c}$ that is differentiable.

Now, let's examine the differentiable of the original huber loss at $\delta$.

$$\lim_{a \to \delta^+}L_\delta (a)=\delta(\delta-\frac{\delta}2)=\lim_{a \to \delta^-}L_{\delta}(a)=L_{\delta}(\delta)$$

Also,

$$\lim_{h \to 0^+} \frac{L_{\delta}(\delta+h)-L_{\delta}(\delta)}{h}=\lim_{h \to 0^+} \frac{\delta(\frac\delta2+h)-\frac{\delta^2}2}{h}=\delta=\lim_{h \to 0^-} \frac{L_{\delta}(\delta+h)-L_{\delta}(\delta)}{h}.$$

The huber loss function is constructed such that it is differentiable.


Scikit learn kmeans with custom definition of inertia? I've coded a small clustering algorithm for time signals using kmeans, which works ok (gives acceptable results).

However, kmeans uses the sum of squared differences. I would like to be able to input instead my own measure of difference, but there doesn't seem to be a way provided by the library to do that.

What would be the easiest way to achieve this? Any other python library which may provide me some way to input instead my own function to define the distance? Or I guess I could instead re-implement the algorithm myself, but I'd rather keep the sci-kit one (since they provide functionalities I want to use such as parallel processing).

K-means cannot optimize arbitrary measures.

The mean optimizes squared errors. It does not optimize, e.g., Euclidean distances, Manhattan distances, etc. It won't crash, but the solution will not be optimal (not even locally) because the centers are not well placed.

So it makes little sense to add support for another "inertia" inside k-means (nor other distances), as it can't optimize that.

If you want to optimize other distances, there is for example the PAM algorithm, and k-medians (for Manhattan).

There is nothing wrong with computing such a quality afterwards though, but then it belongs into the evaluation package, not in the KMeans class.


How to combine PCA and MCA on mixed data? Suppose I have mixed data and (python) code which is capable of doing PCA (principal component analysis) on continuous predictors and MCA (multiple correspondence analysis) on nominal predictors. Is it possible to combine results from PCA and MCA into one?

You may want to use Factor analysis of mixed data.

It allows you to do dimension reduction on a complete data set.

A R implementation could be found in the FactoMineR package. But this function struggle when you have a high number of data/columns.

I am not aware of the existence of the equivalent in python.


How backpropagation through gradient descent represents the error after each forward pass In Neural NEtwork Multilayer Perceptron, I understand that the main difference between Stochastic Gradient Descent (SGD) vs Gradient Descent (GD) lies in the way of how many samples are chosen while training. That is, SGD iteratively chooses one sample to perform forward pass followed by backpropagation to adjust the weights, as oppose to GD where the backpropagation starts only after the entire samples have been calculated in the forward pass).

My question is: When the Gradient Descent (or even mini-batch Gradient Descent) is the chosen approach, how do we represent the error from a single forward pass? Assuming that my network has only a single output neuron, is the error represented by averaging all the individual errors from each sample or by summing all of them? To me this sounds like an implementation-dependent manner but I want to know if there is such a conventional way of that.

To get total error before back propagating - it is common to take an average of all the forward-pass errors. This is what's done in RNN such as LSTM.

In the case of linear regression and logistic regression, The traditional Mean Squared Error Function can produce such a value.

In essence, this value is represented by an average of errors: $Q(w) = 1/n{\sum_{i=1}^n Q_i(w)}$

Also, as a reminder, speaking of an actual backpropagation - from wikipedia:

When used to minimize the above function, a standard (or "batch") gradient descent method would perform the following iterations:

$$w:=w - {\eta}\nabla Q(w) = w - \eta{\sum_{i=1}^n}\nabla Q_i(w)/n$$

notice the $/n$ When used with the ${\sum_{i=1}^n}$ it results in the average of all gradients

:= means 'becomes qual to'

$\eta$ is the learning rate


Giving a score (from 1 - 5) using weights I have a dataset with 8 features (columns). Each row is a customer, for which I want to give a score from 1-5 using the 8 features for each customer. The range of values are as follows:

Feature 1: 0-19

Feature 2: 0 - 4651107

Feature 3: 0 - 3525

Feature 4: binary (0 or 1)

Feature 5: binary (0 or 1)

Feature 6: binary (0 or 1)

Feature 7: binary (0 or 1)

Feature 8: 0 - 25857

I tried giving each feature a weight (from 0 to 1). So, the most important feature will have a weight of 0.9, then 0.85, then 0.8, etc... Then the score is given by:

  Score = $\lambda_1x_1$+$\lambda_2x_2$+...+$\lambda_8x_8$ where $\lambda$ are the weights for each feature $x$

I need to somehow scale the final scores from 1-5, which I failed on doing. I'd appreciate an opinion on either of the below:

1) If you think my method is flawed due to the ranges of each features, or something else, please suggest a way from scratch to get the scores :)

2) If you think my method of linear combinations works, then please suggest how to scale the scores from 1-5 :)

3) Anything else! Maybe KNN or something? I don't really know haha

Thank you all very much.

If I understand the question correctly, this 0-5 weighting is quite arbitrary and is based on your criteria, right? e.g. how loyal some customer is. In this case, a practical way of doing it could be the following:

Transform values of all features to be on the range (0,5). In order to achieve this normalize them to take values into (0, 1) and then multiply with 5 (see here for normalization with pandas)
Apply weights on the features: each weight should be in the range (0,1).
For each customer, multiply weights with transformed feature values (in a similar way as you showed) and calculate the average. This should be in (0,5).

In case that weights represent something that you can get past information on E.g. weights might mean how likely it is that the certain customer would be interested in some product and you can have access on historical information about whether customers with those characteristics (features) have bought it in the past, you can then train a probabilistic classification model and then rescale this probability into (0, 5) in order to turn it into weights. It this context, you might find useful this and this discussions.


Image recognition of selfie images I developed an Android app that lets anyone upload pictures of encyclopedic things (bridges, museums, dishes, landscapes, paintings, etc) to Wikimedia Commons.

Unfortunately, 5% of the users find it funny to upload their own selfie. So I want to programmatically check whether the picture is a selfie or not, and if it probably is, warn them that selfies are off-topic.

As a data set, I have:

1000 pictures that I consider as undesirable selfies. It is in part subjective, but usually such pictures show one or two human faces taken from an arm's distance and random backgrounds.
1000 pictures that are not selfies (bridges, museums, dishes, etc, anything really). Tricky: this also includes pictures of famous people, usually they are easy to distinguish from a selfie because the persons are at a further distance. If you see an extended arm then you can be sure it is a selfie.

All pictures are taken with smartphones (hundreds of different models), they are JPG files of 2MB to 5MB in various sizes and ratios, in portrait or landscape mode.

I must use only open source, and the resulting detection code must run in less than a second on low-end Android phones.

What approach and steps does this task call for?

Implementation of Image Recognition techniques
There are lots of open source libraries available for the image recognition and classification.
You can make use of the "TensorFlow" library for image recognition and can be integrated with your android application.


How to measaure the similarity between two series? I'm confused about how to measure the similarity between two time series with the same length. For example, both time series are 2 hours in length and every 5 minutes a point. I really want to know which Distance Algorithm should I use. I have tried the Euclidean Distance but it didn't work well on this type of data. Should I find some point-to-point distance algorithms?

A classical approach for time series similarity computation is Dynamic Time Warping (DTW).

From your description, it may suit your use case:

  In general, DTW is a method that calculates an optimal match between two given sequences (e.g. time series) with certain restriction and rules:

  Every index from the first sequence must be matched with one or more indices from the other sequence, and vice versa
  The first index from the first sequence must be matched with the first index from the other sequence (but it does not have to be its only match)
  The last index from the first sequence must be matched with the last index from the other sequence (but it does not have to be its only match)
  The mapping of the indices from the first sequence to indices from the other sequence must be monotonically increasing, and vice versa, i.e. if $j>i$ are indices from the first sequence, then there must not be two indices $l>k$ in the other sequence, such that index $i$ is matched with index $l$ and index $j$ is matched with index $k$, and vice versa

Some alternatives to DTW can be found in this literature review on time series dissimilarity measures.


TF-IDF vs TF for classification Let's suppose that I have a dataset of 1000 documents.

Each document is a restaurant review (so relatively short text) and it has labels {Negative, Indifferent, Positive}.

Let's suppose that the dataset has 600 positive reviews, 200 indifferent reviews and 200 negative reviews.

I want to train a classifier to classify a review as Negative or Indifferent or Positive based on the text of the review.

Without using any word embeddings for now the best way to go is to use a TF-IDF model.

However, as I am thinking this more carefully I am not entirely surely if this is the best way to go in comparison with a simply TF model.

Specifically, a TF-IDF model will take the inverse document frequency of terms irrespectively to any labels/categories.

So in the example above if many of the positive reviews have the word 'positive' in them then this word will automatically have modified (and in general lower) TF-IDF scores simply because the majority of the documents in the dataset are positive (600 documents).

On the other hand, with a simply TF then the word 'positive' would have a very high value and it would be evident that it is directly related to positive reviews.

Why the TF-IDF is necessarily the best way to go in cases like these?

So in the example above if many of the positive reviews have the word 'positive' in them then this word will automatically have modified (and in general lower) TF-IDF scores simply because the majority of the documents in the dataset are positive (600 documents).

You're thinking of the word positive as example because you are a human and you know what to select. The words which have the highest frequency in English are stop words: the, a, is... These are useless and would add a lot of noise in your model. You can remove them using a predefined list of stop words, but what about words which are not stop words but are frequent enough, for instance food, place, chef... This is where IDF helps.


Differential Evolution optimal tolerance parameter I am trying to optimize the parameters of a global optimization system for my set of data, because I will have a bunch of similar data to process so I need to fine tune the global optimizator so that it can find the minimum of a function in a reasonable time.

The parameter optimization takes a very long time, and it only has to be done once after which I will just use the best combination and just run those on the other similar datasets, hopefully finding the best value there too.

The global optimizator that I use is called differential evolution and I use the python/numpy/scipy package implementation of it. Here is the wikipedia definition and the relevant papers in the references.

The problem is that it's extremely slow to sample enough combinations of the parameters to find any kind of trend which would suggest me and kind of pattern that I should follow. My problem is especially with the tol parameter which is defined as:

  np.std(pop) <= atol + tol * np.abs(np.mean(population_energies))

Which exits the optimizer if the standard deviation of the population goes below the average of the population energy.

The tricky part is that if the tol=0.01 as per default then the optimizer exits quickly and gives you a low accuracy value, so I run it multiple times, if I set it to a very low number then it doesn't exit at the above criteria but it exits at the maxiter value the maximum number of iterations the function is allowed to do. So either I run it with a big tolerance many times or with a small tolerance few times, the question is which exit clause is better?

What I am measuring is which tol value can give me the best value within a given amount of time, currently 2 hour per set, and I group them as {1e-01,1e-02,...,1e-10} batches, testing which batch will give me the best value in the given time.

I am testing it now as we speak however the low tolerance values are very hard to sample because it takes ~30min for one to be done, so I would need a sample size of 200 from each which would take just too much to compute. My data is 15 dimensional and I only allow 90000 evaluations in total per run (15*popsize*maxiter), which with a low tolerance rate like 1e-10 gets hit very often, which makes drawing samples very slow.

Is there any other way to find the best tol parameter, I would guess a higher one is better because exiting with a condition is more efficient than just brute forcing through an arbitrary set of cycles, but only if the shape of my data is favorable enough, and I have no way of knowing.

Is there any literature on what the rule of thumb best parameters are for the differential evolution global optimizer, that on average is optimal for all functions?

I suspect the optimal range to be around [1e-4,1e-0] but I have no way of proving it, but if it's true then optimizing the other parameters would be a piece of cake as calculations within this tolerance range are very quick.

After 4 days of hard continuous computing the results suggest that indeed the tol=0.01 parameter seems to be the best one. This of course depends on the topology of the data and other factors but if the literature suggests that plus my own findings as well this might hint that it might be a universal value applicable for perhaps all shapes of data.

There is a very low correlation between the tolerance parameter and the output result with a sample size of >1000. The average value barely changes as we increase the tol parameter from 1e-10 to 1e-0 (0.9 being the max value I tried). However the computing time drastically lowers itself from around 7 minutes to 5 minutes per run. Since there is no change to the output value but only in the duration of the calculation it is rational to use 0.01 as a tolerance parameter. However above 0.01 the values actually start to increase, which means that 0.01 is as good as it gets.

As for premature terminations of the optimizer, this should not be a problem since this can still be calibrated with the recombination parameter (CR= crossover probability), by setting it to a low value for a delayed convergence and a high one for an earlier one. This in it's turn also has to be optimized.

One thing is worth noting is that using a Latin Hypercube as init values is not good. I have found much higher results with random sampling, much better coverage, and if there is a problem with clustering of the values, then just increase the number of runs. There is no way the differential evolution optimizer will find the best value in 1 run, so it has to be run multiple times either way.

Unfortunately I can't provide the data, but if anyone else has the time to verify these findings on their own data that would be good too.


Neural Network parse string data? So, I'm just starting to learn how a neural network can operate to recognize patterns and categorize inputs, and I've seen how an artificial neural network can parse image data and categorize the images (demo with convnetjs), and the key there is to downsample the image and each pixel stimulates one input neuron into the network.

However, I'm trying to wrap my head around if this is possible to be done with string inputs? The use-case I've got is a "recommendation engine" for movies a user has watched. Movies have lots of string data (title, plot, tags), and I could imagine "downsampling" the text down to a few key words that describe that movie, but even if I parse out the top five words that describe this movie, I think I'd need input neurons for every english word in order to compare a set of movies? I could limit the input neurons just to the words used in the set, but then could it grow/learn by adding new movies (user watches a new movie, with new words)? Most of the libraries I've seen don't allow adding new neurons after the system has been trained?

Is there a standard way to map string/word/character data to inputs into a neural network? Or is a neural network really not the right tool for the job of parsing string data like this (what's a better tool for pattern-matching in string data)?

Using a neural network for prediction on natural language data can be a tricky task, but there are tried and true methods for making it possible.

In the Natural Language Processing (NLP) field, text is often represented using the bag of words model.  In other words, you have a vector of length n, where n is the number of words in your vocabulary, and each word corresponds to an element in the vector.  In order to convert text to numeric data, you simply count the number of occurrences of each word and place that value at the index of the vector that corresponds to the word. Wikipedia does an excellent job of describing this conversion process.  Because the length of the vector is fixed, its difficult to deal with new words that don't map to an index, but there are ways to help mitigate this problem (lookup feature hashing).

This method of representation has many disadvantages -- it does not preserve the relationship between adjacent words, and results in very sparse vectors.  Looking at n-grams helps to fix the problem of preserving word relationships, but for now let's focus on the second problem, sparsity.

It's difficult to deal directly with these sparse vectors (many linear algebra libraries do a poor job of handling sparse inputs), so often the next step is dimensionality reduction. For that we can refer to the field of topic modeling:  Techniques like Latent Dirichlet Allocation (LDA) and Latent Semantic Analysis (LSA) allow the compression of these sparse vectors into dense vectors by representing a document as a combination of topics.  You can fix the number of topics used, and in doing so fix the size of the output vector producted by LDA or LSA. This dimensionality reduction process drastically reduces the size of the input vector while attempting to lose a minimal amount of information.

Finally, after all of these conversions, you can feed the outputs of the topic modeling process into the inputs of your neural network.


Forecasting vs non-forecasting predition for time series anomaly detection I have got the objective of implementing a uni/multivariate online anomaly detection system.

After multiple days of research, I could collect many ways to achieve this (Eg. moving average solutions such as ARIMA, Space state solutions as Kalman filters, Holt-Winters double/triple exponential smoothing, CUSUM, one-class SVM, deep learning sliding-windows autoencoding approaches, deep learning using autoregressive neural networks, etc).

In general, anomaly detection on time series works with a threshold on the deviation originated from the difference between a predicted point or group of points of the original timeseries and the predicted one.

Attending to this prediction, this can happen in:

a forecasting way (such as ARIMA would do, or you could achieve this result also by using a LSTM deep learning model),
or in a non-forecasting way (eg. denoising with an autoencoder would do, or analyzing fragments STL+ESD used by Twitter).

Which are the (dis)advantages of each one, attending to the objective I mentioned?

I had the same question and ultimately tried an LSTM forecaster and LSTM autoencoder. For me, the choice of forecaster vs autoencoder had effects on the following two points:

Seasonal Pattern:
if you have a seasonal pattern (e.g. weekdays vs weekend) in your data, and the timeframe you are looking at has the pattern -- but is shifted by e.g. a day, you are likely to find that outlier with the Forecaster, but not as likely with Autoencoder.
This Disadvantage of the Autoencoder can be circumvented by also feeding in the weekday and time for each datapoint.

Needed Datapoints:
With the Autoencoder, you don't need datapoints before the timeframe you want to examine. You can just take the series you are interested in and give it to your model.


How to capture the detail of an attribute in a sentence? The problem is best explained using an example, so please consider the sentence below:
Made of airy cotton with a touch of stretch, the Pinafore Dress features a modern square neckline.
Here, cotton (FABRIC) and square neckline (NECKLINE) are two important attributes in the sentence. What I need to do is to capture the word airy which is a detail of the fabric. FABRIC and NECKLINE can be successfully captured using NER, but NER is not working well when it comes to capturing the detail terms like airy.
Can someone point out how I could solve this?

You can try using dependency parsing for that, e.g., as implemented in Spacy or any other NLP toolkit.
The details should be adjective modifiers (dependency label amod) of what you call attribute that you detected using a NER-like approach.

Alternatively, they can form compound nouns as "airy cotton" in your example. In this case, it is a word that depends on the entity with the dependency label compound. But I guess this will be rarely the case because such compounds will be more often recognized as one entity.

Spacy has an online demo for dependency parsing.


In a SVD with user/video bias, why is the UV contribute so small? I'm testing a SVD-based collaborative filter on my data set, in which the label, $r_{ij}$, is a real value from 0 to 1.

Like the many papers suggested, to have a better performance, instead of using $ \hat{R} = U \cdot V^T $ directly, I use $\hat{R} = \mu + B_u + B_v + U \cdot V^T $, where $\mu$ is the average rating, $B_u$ is the bias of user, and $B_v$ is the bias of item.

Thus, this model corresponds to a loss function:
$\min_{B_u, B_v, U, V}  = ||I\circ(R-\mu-Bu-B_v-U\cdot V^T)||_F^2 + \lambda (||B_u||_F^2 + ||B_v||_F^2 + ||U||_F^2 + ||V||_F^2)$

where I is the masking matrix in which $I_{ij} = 1$ if $R_{ij}$ is known, and $||\cdot||_F$ is the frobenius norm.

Then, I solve this by gradient descent, it seems to work fine, and the test RMSE is 0.25.

However, when I investigate the contribution of each part in predict function $\hat{R} = \mu + B_u + B_v + U \cdot V^T $, I notice that, $\mu$ is about 0.5, $b_u$ and $b_i$ are about $\pm0.3$, but the part of $ U \cdot V^T $ is quite small, normally about $\pm 0.01$.

Why does this part contribute so small? Since this part is the actual part where the collaborative filter works, I expect it to contribute more in prediction.

I think that depends on the pattern of your data, its skewness and sparsity.
Because it would press these two bias term, which is designed to detect the difference with global mean.

For example, you can plot mean and std of the user's bias: ($R_u$ - global_mean) then make a histogram of std. Another way, just make up a rating matrix $R'$ according to the $\mu + B_u + B_v$ you've got. And run the solver again and see what happens. These parts play its own role, even your change on global_mean would make a difference to the final RMSE, I think the role of that latent factors are born to capture the REST non-linear pattern in user & item's interaction.

So, as for your question:

  Why does this part contributes so small?

Again, you can simply using $R = UV^T$, in this time, I think it contributes quite a lot, right? The contribution of each part is hard to say.


Need of Weighted Mean Squared Error We have MSE and RMSE as evaluation metrics for regression problems. I have for some problems people use Weighted Mean Squared Error (WMSE) as the evaluation metrix.

Below is the WMSE formula:

Can anyone explain me the real need of WMSE and when not to use MSE.

Thanks in advance.

Weighting MSE is a way to give more importance to some prediction errors than to others in the overall score. This is useful if you are using MSE as a performance metric for your model, especially during the model training (loss function) or validation (hyper-parameter setting).

In the case you cite as example, more importance is given to cases with more clicks. If you use this WMSE as performance metric for validation, your model will tend to be better on cases with high number of clicks, than if you had used MSE.

Just a note on your example: the used squared error ($(predictedClicks - observedClicks)^2$) is an absolute squared error (I would have expected a relative error), and therefore already increases with the number of clicks. So in this case, weighting that way reinforces the performance on cases with high number of clicks.


Is there any alternative to L-BFGS-B algorithm for hyperparameter optimization in Scikit learn? The Gaussian process regression can be computed in scikit learn using an object of class GaussianProcessRegressor as:

 gp= GaussianProcessRegressor(alpha=1e-10, copy_X_train=True,
    kernel=1**2 + Matern(length_scale=2, nu=1.5) + WhiteKernel(noise_level=1),
    n_restarts_optimizer=0, normalize_y=False,
    optimizer='fmin_l_bfgs_b', random_state=None)

Here, we see that the L-BFGS-B algorithm has been used to optimize the hyperparameters. Is there any alternative (for example trust-region-reflective algorithm) to this algorithm available in sklearn?

EDIT: It provides some Constrained multivariate methods for optimization. Is there such functions available for other methods like trust-region-reflective algorithm and swarm intelligence (eg. Firefly algorithm)?

Scikit-learn package has a limited selection of optimizers. The scipy package has many more optimitizers, including trust-region-reflective algorithm. You would have to use another third party package for Firefly algorithm.


How to normalize a position in 3d game? I'm pretty new to anything and everything related to this kind of stuff, I was wondering how would I normalize the coordinates of a entity in my game for a nerual network? Would it just be the same normalizng everything else? I don't assume it'd be the same just because how big these numbers are, you'd have to have a BUNCH of date (I think?)

Edit: also I'd like to know if this would be the proper way of giving your neural network entitie's position in a 3d game?

here's an example of the coordinates, 235.56, -2332.93, -8.57

Assuming each coordinate axis has a fixed max and min value, and $|min_{x}| = max_{x}$, $|min_{y}| = max_{y}$, $|min_{z}| = max_{z}$, the standard normalisation procedure would be:

$$ (x,y,z) \mapsto (\frac{x}{max_{x}},\frac{y}{max_{y}},\frac{z}{max_{z}})$$

I.e. mapping the values to within the range $(-1,-1,-1)$ to $(1,1,1)$.

E.g. if your corrdinate system was fixed between $-100$ and $100$ for each axis, the coordinate $(10,-52,-8)$ would normalise to:

$$ (10,-52,-8) \mapsto (\frac{10}{100},\frac{-52}{100},\frac{-8}{100}) = (0.1, -0.52, -0.08)$$


Which model could I use for time-series regression? I have a time-series (sensor data) with 2 variables x and y.
                         x     y
2020-05-20 05:15:00    1192  355675
2020-05-20 05:30:00    1171  357600
2020-05-20 05:45:00    1260  392680

Rather than traditional time-series prediction with autoregression, my goal is to estimate y given x and the timestamp.
I've gotten acceptable results using non-temporal models (regression, ensembles, etc), but this way I'm throwing away the time information. The other model I've considered using is Vector Autoregression - given that Granger Causality exists between the two variables - but my understanding is that this requires autoregression input from both variables.
What would be the best choice of model for the relationship between these variables over time? My goal is to be able to use only sensor x, and estimate probably values of y.

You could start using a dynamic regression model, they are used when besides the times series to be forecasted, you have another series where you could use it.
Essentially, this is a usual regression model, also called Autoregressive Distributed Lags (ADL), where you include not only the dependent variable $ y_{t} $ but also explanatory variable $ x_{t} $. Following equation exemplifies that:
$$ y_{t} = c + a_{1} y_{t-1} + b_{0} x_{t} + b_{1} x_{t-1} + \epsilon_{t} $$
Now notice that equation above uses $ x_{t} $, meaning that at time you have variable $ x $ so you could use it to forecast $ y_{t} $. This is not always possible, so take care of data leakage.
You can read more about it in Forecasting: Principles and Practice - Chapter 10 Dynamic regression models
Having said all that, you can also improve your ML approach by computing temporal features. Rolling Averages up to lag t, Rolling sum, max, min, and all lags variables could be example of feature engineering you could apply and still use a machine learning approach. Also, if you use python, there is a fresh package called sktime where you can use Reduced Regression.

Example from their notebook (link above):

from sktime.forecasting.compose import ReducedRegressionForecaster

forecaster = ReducedRegressionForecaster(regressor=regressor, window_length=12, strategy="recursive")

forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
plot_ys(y_train, y_test, y_pred, labels=["y_train", "y_test", "y_pred"]);
smape_loss(y_test, y_pred)


Alternative to VGAM for Zero Truncated Negativ Binomial GLM in R Is there an alternative to the vgam Package to do a zero truncated negativ Binomial GLM in R?

I think the answer is "no". countreg does zero-inflated and zero-truncated count distributions, but only Poisson and NB. actuar gives pdf, cdf, et cetera, but doesn't do regression.


Multi-task learning for Multi-label classification? I have a multi-label classification problem wherein each example can belong to one of the pre-defined classes (or can belong to none of them).

I was wondering if I can somehow apply multi-task learning (MTL) to this problem. Essentially, treat each class as a separate class and use a NN based model with common feature extractor layer, and on top of it class specific layer.

My doubt is that generally there is a correlation between labels in a multi-label setting. In the MTL model, after feature extraction each class is handled separately without sharing information. Does a MTL kind of model for multi-label makes sense, given this information?

MTL is supposed to enable your shared layer to generalise better.For eg. in a text classification problem your encoder would be trained on separate tasks:
one could be multi class classification(original task) itself and let's take  identifying the sentiment of the sentence(again a classification task; but different than the original task) as the other task.

For each of these tasks encoder will be same but heads(decoders) will be different.

Decoder/head of one task won't be affected by the other task.

MTL always makes sense. Your encoder/shared layers will be able to generalise better putting less load on your decoders which in turn will increase the performance.


Time series with additional information Given a time series with job-submission counts, how can I predict which certain features about the jobs?

I need to predict how many jobs and which jobs arrived in some system. Using pandas.groupby, I've sliced the data into 15 minutes intervals.

I can predict how many jobs arrived but also need to predict which type of job will arrive and some other features.

I'm assuming the displayed time series shows number of jobs submitted per 15 minute interval.

Categorical features

Divide the time series per category. If the jobs can be divided into type1, type2, type3 then make a time series for each type and predict each series individually. So type1-time series has number of type1-jobs per 15 minute interval.

Continuous features

For continues features e.g time-to-do-job you can divide the jobs into categories of time00,time10, time20, time30 for jobs that take 0-9 minutes, 10-19 minutes, 20-29 minutes etc respectively. As before generate a time-series per division.

Depending on how much data you have and how it is distributed you can make more groups or space them differently.


Should I try to predict the probability of being 1 or zero based on recall score? I built a random forest model to classify if a men's NCAA basketball game would go over or under the total. The model was given training data that specified over as 1 and under as 0. I then used a classification report to calculate the precision, recall, f1-score, and support. These were my results:

for a model like this when my goal is to accurately predict whether the test data will have an outcome of 1 or 0 should I use recall in my decision for choosing whether to try to predict if the outcome will be a 0 or 1?
Also I had my model using predict_proba and using the second value from the array return which would give me the probability of the test data outcome being a 1, but from this classification report it looks like I should be trying to predict the probability of the outcome being 0 as it is more descriptive when it comes to recall.
# Example:
# less accurate way

ynew = model.predict(Xnew)

results = ynew[0][1] # predict probability of Xnew being 1

# Example 2:
# more accurate way

ynew = model.predict(Xnew)

results = ynew[0][0] # predict probability of Xnew being 0

Is my assumption correct?

I have some bad news: your model is doing nothing useful.

From the confusion matrix you can see that the model predicts class 0 for 68% of the instances and class 1 for 32%: (1446+1355)/4093=0.68 and (676+616)/4093=0.32.
You can also see that when the true class is 0 the model predicts 68% of the instance as class 0 and 32% as class 1: 1446/2122=68% and 676/2122=32%. Almost same result when the true class is 1: 1355/1971=69% and 616/1971=31%.

This means that for any instance (no matter the true class) the model randomly predicts the label: class 0 with 68% chance, class 1 with 32% chance. You can also see this problem from the fact the sum of the top-left to bottom right diagonal (correctly classified cases) is 2062, almost the same as the bottom-left to top-right diagonal (errors) which is 2031. A good model would have a much larger difference between correct cases and incorrect cases.
This is why the accuracy is only 50.4%, barely more than a random baseline with two classes, and this is why the recall for each class corresponds exactly to the arbitrary probability that the model assigns to them.

should I use recall in my decision for choosing whether to try to predict if the outcome will be a 0 or 1?

This doesn't make sense: the recall is an evaluation score, it can only be calculated after the predictions have been made.

Also I had my model using predict_proba and using the second value from the array return which would give me the probability of the test data outcome being a 1, but from this classification report it looks like I should be trying to predict the probability of the outcome being 0 as it is more descriptive when it comes to recall.

There are at least two misunderstandings here:

By itself, the fact that the model predicts class 0 more often than class 1 is neither good or bad. It would be completely wrong to choose to evaluate the performance using only the recall on the most frequent class: it's true that it's a higher value, but it's not at all a good performance indicator. For example, if one uses a model which always predicts class 0 (100% of the time) then by definition this classifier will have recall 100% for class 0, even though this model is pointless.
In binary classification, the probability of predicting the two classes are mathematically bound together: $p(C=0)+p(C=1)=1$. So choosing to predict the outcome 0 instead of 1 doesn't change anything about what the model does, it would be exactly the same model with the same performance.

What I suggest you should try is to work on your features: normally the features should be indicators that the model uses to predict the class, but unfortunately your current features are terrible at their job. Assuming that the features "make sense" for the task, it should be possible to "present them" to the model so that it can actually use them efficiently. This is called feature engineering: it's often the hard part of the job but also the most likely to really make things work :)


How much features are needed for Reinforcement learning? I am trying to learn and use reinforcement learning. Now I have only 6 numeric features in my dataset. Can I still use RL? in other words can be using RL for a such a number of features sensible?

It really depends on how much data (samples) you have instead of how many features each samples has. And more importantly it depends on how you plan to structure the problem into an Environment with States and Actions.


averaging multiple scores on small chunks of data or raw score on single collated data I am using IBM Watson tool to determine tones (https://tone-analyzer-demo.mybluemix.net/) and personality scores (https://personality-insights-livedemo.mybluemix.net/) on different files containing natural language. What would be the recommended strategy and rationale for choosing between:

Getting scores from different files and averaging/manipulating them.
Getting one raw score on aggregated data and using it.

I think this is 'mood' vs. 'character'. And as such, it depends on the question you ask. E.g. did I make my customer upset? Or is my customer loyal?

Though mood and character correlate, as someone with a certain character will be more likely to be in some mood, there is also variation.

I think you will measure 'mood' with the 'tone' analyzer, and 'character' with the 'personality' analyzer.

For the 'personality' you will need all the data you can get to get the 'average', for the 'mood', a single data point can be sufficient.


No registered env with id: BanditTenArmedGaussian-v0 for the package gym_bandits of OpenAI I've made the following instruction :

git clone https://github.com/JKCooper2/gym-bandits.git
cd gym-bandits
pip install -e .

After installing :

import gym_bandits
import gym

Now we finally :

env = gym.make("BanditTenArmedGaussian-v0")

Even tough the first three steps was successfull, the last one doesn't work. Here is my error :

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
c:\windows\system32\gym\gym\envs\registration.py in spec(self, path)
    120         try:
--> 121             return self.env_specs[id]
    122         except KeyError:

KeyError: 'BanditTenArmedGaussian-v0'

During handling of the above exception, another exception occurred:

UnregisteredEnv                           Traceback (most recent call last)
<ipython-input-13-645d2b1dfa73> in <module>
      4 import math
      5 import random
----> 6 env = gym.make("BanditTenArmedGaussian-v0")

c:\windows\system32\gym\gym\envs\registration.py in make(id, **kwargs)
    143
    144 def make(id, **kwargs):
--> 145     return registry.make(id, **kwargs)
    146
    147 def spec(id):

c:\windows\system32\gym\gym\envs\registration.py in make(self, path, **kwargs)
     87         else:
     88             logger.info('Making new env: %s', path)
---> 89         spec = self.spec(path)
     90         env = spec.make(**kwargs)
     91         # We used to have people override _reset/_step rather than

c:\windows\system32\gym\gym\envs\registration.py in spec(self, path)
    129                 raise error.DeprecatedEnv('Env {} not found (valid versions include {})'.format(id, matching_envs))
    130             else:
--> 131                 raise error.UnregisteredEnv('No registered env with id: {}'.format(id))
    132
    133     def register(self, id, **kwargs):

UnregisteredEnv: No registered env with id: BanditTenArmedGaussian-v0

Does anyone know how to fix it ?

Thanks and regards.

The standard installation of OpenAI environments does not support Windows. See the GitHub issue here.

To get that code to run, you'll have to switch another operating system (i.e., Linux or Mac) or use a non-standard installation.


Beautifulsoup iterating through scraped data I have this html code that is repeating multiple times:
     <div class="Company_line-logo image-loader-target" data-image-loader-height="47" data-image-loader-height-mobile="47" data-image-loader-src="/var/fiftyPartners/storage/images/startups/woleet/3261-1-fre-FR/Woleet_company_line_logo.png" data-image-loader-src-mobile="/var/fiftyPartners/storage/images/startups/woleet/3261-1-fre-FR/Woleet_company_line_logo_mobile.png" data-image-loader-width="189" data-image-loader-width-mobile="189" style='background-image:url("http://en.50partners.fr/var/fiftyPartners/storage/images/startups/woleet/3261-1-fre-FR/Woleet_company_line_logo.png");'>

Woleet

By using:
    for blocks in soup:
        block = soup.find('a', class_='Company_line logo-contains-name').find('h5').get_text()

I can get what I want that is "Woleet" in between the h5 tags.
I tried to iterate this to get all of them:
    block = soup.find('a', class_='Company_line logo-contains-name')
    for name in block:
        names = block.find_all('h5')

it returns only 1
h5 class="Company_line-title">Woleet</h5
whereas I should get 50 names like "Woleet".
How can I iterate this and get the text from all h5 tags?
Thanks!

I could iterate and get the text from all the h5 tags with this code:
    main_block = soup.find(class_='Node_full startups content_class_folder')
    block = [h5.get_text() for h5 in main_block.find_all('h5', class_='Company_line-title')]

I'm wondering if there is another way to extract the text from the h5 tags.


R: Calculations based on frequencies / grouped / aggregate data I am trying to do simple calculations in R when no raw data but grouped data with frequencies is available only. This is the case when I have a large amount of records in a database, say a large SQL table, and then for given reasons GROUP BY and COUNT to aggregate instead of downloading the original table for analysis in R. As I understand, one could say in R that I'm talking about data in a table format.

To give a simple example:

> original=c(1,2,2,3)
> aggregate=table(original)
original
1 2 3
1 2 1

In the case I describe, only data structured like aggregate would be available as the result of my database query, usually in a data.frame similar to this:

|value|frequency|
|1    |1        |
|2    |2        |
|3    |1        |

What are standard approaches in R to work with this kind of data, particulary when it's the aim to run analysis on value? E.g., I may want to

Calculate Mean, Median, Quantiles
Plot a boxplot which should give the same results as when I would run boxplot(original)

I feel I'm just missing a basic concept of aggregated data processing in R, cannot imagine it's not something being build in a native way in R.

The only idea I could find googling this was basically an approach to let R create original based on the frequency information.

I think working with "collapsed" or "summarized" data as opposed to the data itself directly inside the SQL database is counter-intuitive. Also, trying to download the original table for analysis in R is a very bad route if the data is too big to fit in the memory, and given that you already have a tool like SQL, which is designed for that specific purpose. You can calculate the mean, median, quantiles, etc. in SQL; and then pull that into R (even into Excel) and draw box plots.

Even though overkill, here is what you can do:

Calculate the mean, median, quantiles from the frequency chart by coding basic math in R. For example mean of a series is the sum over frequency(i)*value(i)/sum(frequency(i)), where i=1,...,n, and n is the number of distinct values in the frequency chart. Median is the value which is in the mid point of series 1,...,sum(frequency(i)).
You can recover the original series from summary statistics, but memory is again of concern. Let's say your original series was this:

  mySeries <- c(5,9,4,4,2,5,8,9,9,5,5)

But obviously it sits in the database, and this is what you get from SQL as you described:

> fromSQL <- data.frame(table(mySeries))
> fromSQL
  mySeries Freq
1        2    1
2        4    2
3        5    4
4        8    1
5        9    3

You can just use the rep function to recover the series like so:

> recoveredSeries <- as.integer(as.character(rep(fromSQL$mySeries, fromSQL$Freq)))
> recoveredSeries
 [1] 2 4 4 5 5 5 5 8 9 9 9

Now define a function that returns the summary of this series and gives you what you want:

> multiSummary <- function(x) {
+   c(mean = mean(x), median = median(x))
+ }
> multiSummary(recoveredSeries)
    mean   median
5.909091 5.000000

As I said, in my opinion, all of this is overkill. Different tools are tailored for different needs, and trying to do something with a tool that is not really the best for your need will give you less-than-optimal results and cost you a lot of time. My recommendation would be performing SQL operations, or going for MapReduce framework for this kind of purposes, depending on the size of your data.


How to group by multiple columns in dataframe using R and do aggregate function I have a dataframe with columns as defined below. I have provided one set of example, similar to this I have many countries with loan amount and gender variables

      country          loan_amount          gender
1      Austia              175                F
2      Austia              100                F
3      Austia              825                M
4      Austia              175                F
5      Austia             1025                M
6      Austia              225                F

Here I need to group by countries and then for each country, I need to calculate loan percentage by gender in new columns, so that new columns will have male percentage of total loan amount for that country and female percentage of total loan amount for that country. I need to do two group_by function, first to group all countries together and after that group genders to calculate loan percent.

    Total loan amount = 2525
     female_prcent = 175+100+175+225/2525 = 26.73
     male_percent = 825+1025/2525 = 73.26

The output should be as below:

      country            female_percent   male_percent
1      Austia              26.73            73.26

I am trying to do this in R. I tried the below function, but my R session is not producing any result and it is terminating.

  df %>%
  group_by(country, gender) %>%
  summarise_each(funs(sum))

Could someone help me in achieving this output? I think this can be achieved using dplyr function, but I am struck inbetween.

library(dplyr)
library(tidyr)
df %>% group_by(country, gender) %>%
       summarise(total_loan_amount =sum(loan_amount)) %>%
       spread(gender, total_loan_amount) %>%
       ungroup() %>%
       transmute(country = country,  female_percent = F / (F+M), male_percent = M /(F+M))

results in

 Source: local data frame [1 x 3]

      country female_percent male_percent
       (fctr)          (dbl)        (dbl)
    1  Austia      0.2673267    0.7326733


Data visualization for pattern analysis (language-independent, but R preferred) I want to plot the bytes from a disk image in order to understand a pattern in them. This is mainly an academic task, since I'm almost sure this pattern was created by a disk testing program, but I'd like to reverse-engineer it anyway.

I already know that the pattern is aligned, with a periodicity of 256 characters.

I can envision two ways of visualizing this information: either a 16x16 plane viewed through time (3 dimensions), where each pixel's color is the ASCII code for the character, or a 256 pixel line for each period (2 dimensions).

This is a snapshot of the pattern (you can see more than one), seen through xxd (32x16):

Either way, I am trying to find a way of visualizing this information. This probably isn't hard for anyone into signal analysis, but I can't seem to find a way using open-source software.

I'd like to avoid Matlab or Mathematica and I'd prefer an answer in R, since I have been learning it recently, but nonetheless, any language is welcome.

Update, 2014-07-25: given Emre's answer below, this is what the pattern looks like, given the first 30MB of the pattern, aligned at 512 instead of 256 (this alignment looks better):

Any further ideas are welcome!

I would use a visual analysis. Since you know there is a repetition every 256 bytes, create an image 256 pixels wide by however many deep, and encode the data using brightness. In (i)python it would look like this:

import os, numpy, matplotlib.pyplot as plt

%matplotlib inline

def read_in_chunks(infile, chunk_size=256):
    while True:
        chunk = infile.read(chunk_size)
        if chunk:
            yield chunk
        else:
            # The chunk was empty, which means we're at the end
            # of the file
            return

fname = 'enter something here'
srcfile = open(fname, 'rb')
height = 1 + os.path.getsize(fname)/256
data = numpy.zeros((height, 256), dtype=numpy.uint8)

for i, line in enumerate(read_in_chunks(srcfile)):
    vals = list(map(int, line))
    data[i,:len(vals)] = vals

plt.imshow(data, aspect=1e-2);

This is what a PDF looks like:

A 256 byte periodic pattern would have manifested itself as vertical lines. Except for the header and tail it looks pretty noisy.


Are we allowed to transform the continuous target variable by creating a log transformation in order to have a normal distribution? The following code gives the target variable Item_Outlet_Sales before transformation and Item_Outlet_Sales_log which is transformed
#treat extreme values in Item_Outlet_Sales
train['Item_Outlet_Sales_log'] = np.log(train.Item_Outlet_Sales)
test['Item_Outlet_Sales_log'] = np.log(test.Item_Outlet_Sales)

plt.figure(1)
plt.subplot(121)
sns.distplot(train.Item_Outlet_Sales)
sns.distplot(test.Item_Outlet_Sales);
plt.subplot(122)
sns.distplot(train.Item_Outlet_Sales_log)
sns.distplot(test.Item_Outlet_Sales_log);

Then use the new target variable (Outlet_Item_Sales):
#creating dummies for the training dataset
X = train.drop('Item_Outlet_Sales', 1) #drop the log target column
y = train.Item_Outlet_Sales_log

X = pd.get_dummies(X)
train = pd.get_dummies(temp_train)

If your objective is to convert non-normal data into something that looks more normal / gaussian - try the Box-Cox Transform here.
It is a family of transforms that looks at your data - and provides the best possible transformation.


How to train a spacy model for text classification? Can i know the way or steps to train a spacy model for text classification. (binary classification in my case)

Please help me with the process and way to approach.

You have several good tutorials on the web :

https://www.kaggle.com/poonaml/text-classification-using-spacy

Basically, you have to :

Import the data in python, here POSITIVE is the variable to predict and 0 and 1 are the 2 encoded classes.

TRAIN_DATA = [(Text1, {'cats': {'POSITIVE': 1}}),
(Text2, {'cats': {'POSITIVE': 0}})]

Initialize a textcat pipe in a spacy pipeline object (nlp), and add the label variable in it.

nlp = spacy.load('en_core_web_sm')
if 'textcat' not in nlp.pipe_names:
  textcat = nlp.create_pipe("textcat")
  nlp.add_pipe(textcat, last=True)
else:
  textcat = nlp.get_pipe("textcat")

textcat.add_label('POSITIVE')

Iterate the training examples to optimize the model

other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'textcat']

n_iter = 1

# Only train the textcat pipe
with nlp.disable_pipes(*other_pipes):
    optimizer = nlp.begin_training()
    print("Training model...")
    for i in range(n_iter):
        losses = {}
        batches = minibatch(train_data, size=compounding(4,32,1.001))
        for batch in batches:
            texts, annotations = zip(*batch)
            nlp.update(texts, annotations, sgd=optimizer,
                      drop=0.2, losses=losses)


Can Boosting and Bagging be applied to heterogeneous algorithms? Stacking can be achieved with heterogeneous algorithms such as RF, SVM and KNN. However, can such heterogeneously be achieved in Bagging or Boosting? For example, in Boosting, instead of using RF in all the iterations, could we use different algorithms?

The short answer is yes. Both boosting and bagging meta-algorithms do not assume specific weak learners, thus any learner can do, no matter if uses same algorithm or different one.
The way the meta-algorithms are defined, they use the weak learners as black-box models, without reference to their implementation or algorithmic principle, nor similarity.
For Boosting:

In machine learning, boosting is an ensemble meta-algorithm for
primarily reducing bias, and also variance[1] in supervised learning,
and a family of machine learning algorithms that convert weak learners
to strong ones.[2] Boosting is based on the question posed by Kearns
and Valiant (1988, 1989):[3][4] "Can a set of weak learners create a
single strong learner?"
A weak learner is defined to be a classifier that is only slightly
correlated with the true classification (it can label examples better
than random guessing). In contrast, a strong learner is a classifier
that is arbitrarily well-correlated with the true classification.

For Bagging:

Although it is usually applied to decision tree methods, it can be
used with any type of method. Bagging is a special case of the model
averaging approach.

For Bagging the required condition to improve performance is that the weak learners should be instable (thus perturbed versions of the learners affect outcomes), but other than that, learners are black boxes, as mentioned above.
For further reference:

Boosting, wikipedia
Bagging, wikipedia
The Strength of Weak Learnability
Bagging Predictors


Comparing 2 data frames, if value is present replace with 1 or else 0 nrow(df1$v1) = 63849
    nrow(df2$v2) = 3244
ifelse(df1$v2 == df$v1, 1, 0)

I know this is an easy question but I tried different procedures but none of them are useful,

for(i in 1:nrow(df2)){
for(j in 1:nrow(df1)){
    if(df2$v1[i] == df1$v2[j]){
      df1$v2<- 1
    }
    df1$v2 <- 0
  }
}

This does the job but it take quiet sometime to get the job done.

Other Methods:
Method 1:

df1$v2 <- ifelse(df2$v1 %in% df1$v1, 1,0 )
Error in `$<-.data.frame`(`*tmp*`, v2 , value = c(1, 1, 1, 1, 1, 1,  :

replacement has 3244 rows, data has 63849

Method 2:

df1$v2 <- ifelse(do.call(paste0, df2$v1) %in%
do.call(paste0, df1$v1), 1,0 )
summary(df1$v2)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
      0       0       0       0       0       0

Do suggest me if you have any better solution.

Here is a way using dplyr:

require(dplyr)
X = data_frame(A=c(1,2,3,4))
Y = data_frame(B=c(5,4,6,7))

X <- X %>%
  mutate(A = A %in% Y$B,
         A = A*1.0) #converts boolean to numeric

or using base R:

X$A <- 1.0*(X$A %in% Y$B)


Tune hyperparameters for cost-sensitive classification I have an unbalanced data set with about 8% of negative examples. The goal is to minimize false negatives given a cost matrix. It seems like SVM (with radial kernel) and random forest work best. How should I tune hyperparameters in this setting?

My suggestion: separate data into train/validation set, use probabilistic output together with cost matrix to assign predicted classes, tune hyperparameters to maximize accuracy.

How can I increase performance? Currently I use random forest with nodesize=1 and mtry=5 which gives about 97% accuracy.

Tuning the hyperparameters of a model is still more of an art than a science.

Both R (caRet) and Python (sklearn) have frameworks for performing grid searches of hyperparameters using cross validation. You can define custom cost functions in these frameworks, which will allow you to directly assess your cross-validation results using your cost matrix. See the scikit-learn documentation for Python, or the caRet documentation for R. You will get much better results using your cost matrix directly to evaluate your models, instead of using F-1, precision, recall, accuracy, or any other metric.

Other Guidance

It is important to understand what the different hyperparameters actually mean in the context of each learning algorithm to help guide the parameter search, as computational limitations usually strictly limit the number of different models you can build total, limiting the size of the grid you can realistically search.

With such a low percentage of negative examples in your set, you will want to ensure that you use stratified sampling for your cross-validation. This will ensure that each fold has the same percentage of negative examples as the base set. Otherwise, your cross-validation results will likely be biased by the non-uniform negative class distribution.


Is there a definitive and more conclusive way of interpreting the R^2 score from a linear regression model in terms of prediction accuracy? I'm trying to find a definitive way to conclude the R^2 score from a prediction accuracy point of view rather than variance. How should I do it?

Conceptually, most blogs / articles explain R^2 as:

The variation in the data points that can be explained by the model.
A measure of how closely fitted the data points are to the regression line.

I find these answers to be unsatisfactory.

If the R^2 score is 0.7, is it right for me to say that 70% of the data has been predicted accurately by the model? Since most explanations are tied to the explanation of the variance I'm thinking that looking at it from an 'accuracy' stand point of view is wrong. It implies that if I predicted 10 random points on the x-axis I would accurately guess 7 of its actual values.

According to scikit-learn's documentation page at the 'score' section:

The coefficient R^2 is defined as (1 - u/v), where u is the residual sum of squares ((y_true - y_pred) ** 2).sum() and v is the total sum of squares ((y_true - y_true.mean()) ** 2).sum()

Apart from scikit-learn's way of calculation, another way of calculation is simply the squared of R (coefficient of correlation).

Which way is right? Are both the same? Or are they related in some way?

Assuming the right way of calculation is based off scikit-learn's documentation where the R^2 score is the variance from the predicted mean divided by the variance from the actual mean, is it even possible that the variance from the predicted mean is smaller than the variance from the actual mean?

How can the variance from the prediction model which is built upon another dataset (let's say dataset A) be smaller than the actual variance of dataset B?

Deeply appreciate any thoughts, comments or clarification.

Quoting Dr. Bruce Ratner:

"R-sq is a first-blush indicator of a good model. R-sq is often misused as the measure to assess which model produces better predictions...The root mean squared error (RMSE) is the measure for determining the better model. The smaller the RMSE value, the better the model is (the predictions are more precise)."

I sourced this from Dr. Ratner's LinkedIn, so I can't link to it. His website is: dmstat1.com

Be careful with R-sq. The more variables you use the higher your R-sq value will be no matter how good or bad your model is. If you are using more than a few predictor variables you should also look at Adjusted-R-sq.

If you would rather get a measure of accuracy you should look at RMSE. If you are comparing two models also look at Akaike Information Criterion (AIC) and Bayesian Information Criterion (BIC).

To answer your question, if the R-sq is 0.7, it is incorrect to say that 70% of the data has been predicted accurately by the model. R-sq is a measure of how much variance is accounted for within the predictor variables you are using. R-sq of 0.7 means that your predictor variables can explain 70% of the variance in the response variable.

For example pretend that the price of a cup of coffee is influenced ONLY by the price of water and the price of coffee beans. If I only know the price of water and I predict the price of coffee using only the price of water my R-sq is 0.5, because I know only 1/2 of the things that influence the price of a cup of coffee.

In reality, it is virtually impossible to know ALL predictors that influence an outcome (R-sq = 1) because we either don't know what variables influence an outcome, or the variables that influence an outcome aren't contained in the dataset.


What is the main difference between Hadoop and Spark? I recently read the following about Hadoop vs. Spark:
Insist upon in-memory columnar data querying. This was the killer-feature that let Apache Spark run in seconds the queries that would take Hadoop hours or days. Memory is much faster than disk access, and any modern data platform should be optimized to take advantage of that speed. Also, columnar data storage greatly reduces the amount of memory spent on empty or redundant data.
Can someone explain: 1) what Apache Hadoop and Spark are, 2) how they differ, and 3) how this relates to memory vs. disk access.

Hadoop is a framework for the distributed storage and processing of big data on the Hadoop File System (HDFS) where data is stored in a cluster of "nodes" and can be set up to be fault tolerant. Since data is stored accross multiple nodes it can be processed in parallel, and Hadoop uses the MapReduce algorithm for doing so. This is basically achieved by each node in the cluster fetching the data it needs from disk, performing the neecessary computations which are then aggregated and returned.
Spark is a distributed processing framework for big data, but does not provide storage. Consequently it needs to work on top of distributed storage, which could be Hadoop. Spark is designed as an in-memory engine, and is therefore much faster than MapReduce on Hadoop. It is also fault tolerant. Spark includes a version of SQL which allows for much better querying of the underlying data compared to Hadoop/MapReduce. Since in practice, Spark is often still limited by memory availability when the underlying data is too large, so there is still some disk-bound operations, but at least in theory it is capable of 100% in-memory real time analytics. This is not possible with Hadoop.
Both Hadoop and Spark can work with columnar data, although Spark is columnar by default, and in Hadoop, the underlying data is often row-oriented, such as csv files (but can support columnar data such as parquet and ORC). Data stored in columnar format is much faster to query.
Hadoop is designed for large scale batch processing. Spark is also designed for batch processing but can also handle streaming and other data flows. Both are scalable technologies, but Hadoop scales nearly linearly, whereas with Spark, although it will generally be faster than Hadoop for similar sized data, there are limitations based on the memory available in the cluster, above which performance will deteriorate much faster than with Hadoop. That is, with Hadoop speed will decrease approximately linearly as the data size increases. With Spark, once the memory in the cluster is exceeded, relative performance will decline faster than with Hadoop.
Due to the memory needed in a Spark cluster, it will generally cost more to operate than Hadoop in terms of uptime. However for smaller datasets the much faster in-memory processing may completely offset the higher cost of compute time.
Some of the above are purposely general statements, there could be edge cases and other situations where they don't apply.


How to Scaling Out Artifical Neural Networks? I have started to study ANNs with Tensorflow and Keras. Now I want to find a solution to use ANNs over Hadoop. I have learnt that Spark 2.0 does have a Multilayer Perceptron Classifier, but as far as I can see it is quite "primitive" in comparison with TF and Keras (there are only feedforward types and hidden layer/output layer activation functions are hard-wired), there is no wide variety of optimizers, cost functions, architecture types etc.

Is there any competitive alternative for large scale neural networks? Of course, I could use Amazon with very powerful, GPU-driven machines but these are not parallel frameworks either...

You might take a look over TensorFrames, a Databricks library which allow running TensorFlow code on top of Apache Spark :

https://github.com/databricks/tensorframes


Order images in the correct order This is a captcha where you have to select the side that makes more sense.
I am trying to use machine learning trying to break it.
My approach is to use Google's vision AI to extract keywords from those images and then use a markov chain and machine learning to predict the correct order.

Is there a better approach in your opinion? What are the chances to get it working?

Hmm the first few ideas of other things you could look for to make your model a little more sharp...

Examine id's of the images/metadata to see if a statistically significant pattern emerges for how to arrange the images. (i1.png,i2.png,i3.png,etc),(Largest image is always on top, etc)
They may purposely scramble the images in a way were they never let a picture be purposely on the right location which can be used to your advantage. You only have to decide in between three locations the correct location for any image.
You may want to see if you can incorporate word2vec and/or LSTM for getting image to->words to->story syntax/context correct.
If they allow two chances before reseting then your model will be more accurate if you can incorporate that. If it outputs a probability each of the images are at a location then you need to create two guesses not one.

Chances are low but I would be interested to see your results I think if they didn't make a terrible captcha you will have about 16% < x < 30 accuracy if they have systemic flaws could be a lot better.


Reinforcement Learning : Why acting greedily with the optimal value function gives you the optimal policy? The course of David Silver about Reinforcement Learning explains how you get the optimal policy from the optimal value function.

It seems to be very simple, you just have to act greedily, by maximizing at each step the value function.

In the case of a small grid world, once you have applied the Policy Evaluation algorithm, you get for example the following matrix for the value function :

You start from the up-left corner and the unique actions are the four classic displacements.

If we respect a greedy evolution, by maximizing at each step the value function, we gonna go right and then down, right, up ... never reaching the state with the best value (20).

So the question is simple : why don't we have to guess what will be the future rewards to get the optimal policy from the optimal value function ? In other words, why is one-step look ahead enough to get the optimal policy ?

A similar problem already appear with the rewards, and we solved this issue by weightening a sum of future expectations... Why don't we need to do the same ?

Could you help me understand what's wrong with my interpretation ?

Thanks a lot for your help !

You gave an exemple of a value function, but not an optimal value function, that would verify :
$$ V^{*}(s) =max_\pi(V^{\pi}(s)) $$
Such a value function would allow to use one-step look ahead approach to get the optimal policy. Because the optimal value function would be defined somehow iteratively to take neighbor value into account.
In other words, the problem with your value function is that it is not optimal, notably  because of the significant step from 0 to 20. If it was optimal you would have a value nearer to 20 near it and the look-ahead would follow this path.


Statistical methods for Sequence learning I am bit more traditional and I am looking for a statistical method that can help me also do inference or predictions.

I have some table that contains some transitions, where each row of the table correspond to a specific user. If you are into python this is it:

smallData=pd.DataFrame(np.array([[1,2,3,4,5],[4,5,6,7,8],[1,2,3,3,3],[1,2,2,3,3],[1,2,3,5,3]]),columns=range(0,5))
smallData
Out[266]:
   0  1  2  3  4
0  1  2  3  4  5
1  4  5  6  7  8
2  1  2  3  3  3
3  1  2  2  3  3
4  1  2  3  5  3

Now first row means that the following transitions happen
1-2-3-4-5

this kind of transitions can be of course be visualized as a tree or diagram. So far I have not found any method or library in python or R that can calculate the statistics of transitions. For example for the two transitions [1,2] three transition can happen

Transition 3 by 50%,
Transition 4 by 25% and
Transition 5 by 25%.

These were calculated from the table above.

Even harder will be to predict two steps forward.

I have seen the topic of sequence prediction to be discussed a lot in the machine learning community by using LSTM. I still wonder if in the statistics community are methods to handle such problems and if yes if there are in python, R libraries that can be used for such problem.

it sounds like a stochastic process problem. Have you looked into estimating transition matrices for markov chains?


Learning parameters when loss is a piecewise function I have a network to generate a single number $T$. I know in advance: a property of the loss function is that, when $T \in [a_1, a_2]$, the loss has the same value $L_1$; when $T \in [a_2, a_3]$, the loss has another value $L_2$; etc. The loss function resembles a piecewise function.
A concrete, simplified example of this problem is perhaps something like object classification. I have a set of objects, and their distances to a category $C$ that I want to classify these objects into. The distances are $[d_1, d_2, \dots, d_K]$. Assume without loss of generality that $d_1 \leq d_2 \leq \dots \leq d_K$. I want to learn a threshold $T$ for these objects that says, if the distance is near enough, then they belong to the category $C$; otherwise, they are not members of the category. For example, if $d_3 \leq T \leq d_4$, then objects $1, 2$ and $3$ (with distances $d_1, d_2, d_3$) belong to $C$.
What learning techniques I may use to learn weights of the network? Any help will be greatly appreciated.
I will next combine the above network with other differentiable learning components, so ideally it would be good if the approach is compatible with gradient descent.

I would consider Cubic splines to approximate your piecewise function.
https://en.wikiversity.org/wiki/Cubic_Spline_Interpolation#:~:text=Cubic%20spline%20interpolation%20is%20a,Lagrange%20polynomial%20and%20Newton%20polynomial.
You can regularize your spline by adding continuity constraint on the first and the second derivatives at the nodes so the function would be smooth and would be a good approximation to your piecewise loss.


How to check if sentiment analysis is required? I have a CSV file having a bunch of sentences related to science. Before I do sentiment analysis on the sentences I want to programatically decide whether sentiment analysis is required on the sentence or not.

Basically some of the sentences are opinions of a particular topic in which case doing sentiment analysis makes sense. However some of the sentences are just definitions and sentiment analysis is not required on such sentences. So is there any way to detect the presence of a sentiment in a sentence?

(Note that the length of a sentence varies from 9 to 30 words.)

TextBlob, a Python package, does this (and much more). It uses a pre-trained model, thus requires no training. Given a sentence, TextBlob will return the polarity from -1 to 1. -1 is negative, 1 is positive, 0 is neutral. It will also return subjectivity from 0 (very objective) to 1 (very subjective).


Reason: Average word vector embedding encodes word content and word order effectively I was going through a research paper: FINE-GRAINED ANALYSIS OF SENTENCE
EMBEDDINGS USING AUXILIARY PREDICTION TASKS

The key take away was Comparison of Encoder decoder and average word sentence embedding validated for accuracy for sentence embedding on 3 basic language characteristics- sentence length, word content, and word order.

I found it surprising, that an averaged word embedding for a sentence is better at predicting presence of a word in a sentence than an Encoder Decoder. Also, how is it that, increasing embedding size deteriorates it's performance.

Same question goes for word ordering, how is average word embedding able to do that? The experiments are able to explain, what would happen if prediction is based on permutation of words, but the explanation doesn't feel intuitive to me. How is simple avg word embedding able to contain information like word order when taking it's average kinda nullifies the order info

When reading the paper there actually is some points towards the background of these phenomena:

About predicting a word meaning there is only the surprise present, but about predicting the order there was a whole section about the statistical influence of natural language underlying mechanics to the performance of the CBOW. It appears, that order of sentences or even word pairs was easy for CBOW whereas a random permutation with no natural order dropped also the performance.

If we go very basics, CBOW (Continuous Basket Of Words) mission is to:

  Predicting a word given its context. [1]

Thus, with even an average (or only because of it) we can identify which word is which, although in a little bit weird way around. Just because that is how it is behaving. Secondly, basket is for training the neural network, not the full algorithm. What is the model obtained would be different of the way it gets its data.

What makes me guessing is the drop on ability to identify from larger range of words. As we all are surprised how averages make the trick so, well, maybe the true nature is revealed with larger dimensions.

[1] https://www.quora.com/What-are-the-continuous-bag-of-words-and-skip-gram-architectures