What are the disadvantages of using deep neural networks compared to a linear model? We have heard a lot about the advantages that artificial neural networks have over other models but what are the disadvantages of them in comparison to the simplest case of a linear model?

First of all, we should say that a single affine layer of a neural network without any non-linearities/activations is practically the same as a linear model. Here we are referring to deep neural networks that have multiple layers and activation functions (non-linearities as relu, elu, tanh, sigmoid etc.)

Second of all nonlinearities and multiple layers introduce a nonconvex and usually rather complex error space which means that we have many local minimums that the training of the deep neural network can converge to. This means that a lot of hyperparameters have to be tuned in order to get to a place in the error space where the error is small enough so that the model will be useful.

A lot of hyper parameters which could start from 10 and reach up to 40 or 50 are dealt with bayesian optimization using Gaussian processes to optimize them which still does not guarantee good performance.

Their training is very slow and adding the tuning of the hyperparameters into that makes it even slower where in comparison the linear model would be much faster to be trained. This introduces a serious cost-benefit tradeoff.

A trained linear model has weights which are interpretable and give useful information to the data scientist onto how various features play a role for the task at hand.


Is probabilistic machine learning just the mathematical background of machine learning? I wanted to begin with machine learning, I went through the contents of the course on ML by Andrew Ng and found that though the course was based on mathematics, but wasn't too much on the probability or statistics.
But in many university curriculua the book: "Probabilistic Machine Learning: An introduction" by K. Murphy is being used, and it seemed to be full of probability, statistics etc.
I wanted to know if the ML - which is being used in the industry, like the neural networks or other ML techniques - has any statistical or probabilistic foundation which is often not bothered or is it a new emerging field with mathematical basis which is not from probability or statistics?
I really want to know if the ML in academia is really the foundation part of the ML used in the industry or is it the ML in its old form?
And finally, are the contents of the book: "Probabilistic Machine Learning An Introduction", relevant in the ML being used today?
I am aware that these are quite broad and open ended questions from someone entering into the field, I would be happy to informative answers or links?

Knowledge in Probability and Statistics is absolutely important. It might feel you can do quite a bit without that and can get things work using the high level ML libraries that we have today. However if you are a serious practitioner you will figure out that beyond a point your knowledge on how these techniques work and are tuned is very hollow and that will automatically lead you into the more mathematically inclined books that give you the foundation knowledge.


Can I continue a machine learning process after shutdown? I use Mask RCNN for object detection and instance segmentation.
I am new to neural networks, this is my first project.

I use jupyter notebook. Can I continue learning after shutdown?

I would like start learning from 6 epoch. How can I do it?
One epoch ~ 1h. I don't want to waste time, and continue this process. Is it possible?

In order to continue the learning process, you will need to use checkpoints.

# source: https://stackoverflow.com/questions/36356004/continue-training-from-a-specific-epoch
weight_save_callback = ModelCheckpoint('/path/to/weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', verbose=0, save_best_only=False, mode='auto')
model.fit(X_train,y_train,batch_size=batch_size,nb_epoch=nb_epoch,callbacks=[weight_save_callback])

# load model
model = Sequential()
model.add(...)
model.load('path/to/weights.hf5')

# use model.fit to continue training.


How to report results of RL with high variance? I run Q-learning and SARSA algortihms on the same problem but the results fluctuate heavily and when I draw them, there is no smooth graph. How should I repost the results? I run algorithms for 500 runs and 40000 episodes; however, it seems that 500 runs are not sufficient, and because of timing, it is difficult to increase the number of runs.
This graph shows reward_q which is a vector of 40000 and each element number shows the summation of rewards in each episods for 500 runs, and I draw the graph based on number of episodes vs reward_q/n_runs.
Note: The title of the x-axis is not correct.

One way is to calculate a moving average and moving standard deviation.
My preferable method is smoothing (like tensorboard's smoothing function). Here is a very nice script in python. Just tune the weight parameter to get the results you want. For example:


What is the mean of inconsistent in machine learning and why 1NN is well known to be inconsistent I really need help to understand the meaning of consistency in machine learning, why it's important, and why 1NN  is considered to be inconsistent.

Thanks

Concistency of any algorithm in machine learning or statistics rather means that assuming you train on an infinite amount of data that your algorithm will converge to the true value of your estimate. Meaning that if you feed infinite datasets into the algorithm your error will converge to 0

Now regarding 1NN:
1NN has an important property: When feeding infinite training examples the error rate of the 1NN converges to a limit of twice the Bayes error. Now if you take the definition above and match the 1NN's property you'll see the reason why it's inconsistent.

See his paper for a proof of the property: http://cseweb.ucsd.edu/~elkan/151/nearestn.pdf


Python uses conda-base tensorflow-gpu instead of conda environment installation I want to create a virtual environment using anaconda for python 3 in which I can use a specific version of tensorflow-gpu.

My base conda python is 3.6.8 and the base conda tensorflow-gpu version is 1.10.1.
I want to create an environment with python 3.6.9 and tensorflow-gpu 1.11.0.
I am running Ubuntu 18.04.

To do this, I have been running the following code in my bash:

$ conda create --name test_env python=3.6.9
$ conda activate test_env

However, when I open a python console and check the tensorflow version it is instantly 1.10.1 which is the tensorflow-gpu version of my conda base environment.

Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.10.1'

The problem seems to only occur with the tensorflow package. Other modules like keras give the expected ModuleNotFoundError error when I try to import them after i created the test_env environment.

Even if I install a specific tensorflow version with the following command, the tensorflow version remains to be 1.10.1.

$ conda install tensorflow-gpu==1.11.0

When i use conda list it (only) lists the target tensorflow-gpu version:
tensorflow-gpu            1.11.0               h0d30ee6_0

Do you have any ideas on how to use tensorflow-gpu version 1.11.0 in a new  virtual environment?

You can create the conda environment and install all required packages at the same time:

$ conda create --name test_env python=3.6 tensorflow==1.11


NGBoost and overfit - which model is used? While training an NGBoost model I got:
[iter 0] loss=-2.2911 val_loss=-2.3309 scale=2.0000 norm=1.0976

[iter 100] loss=-3.3288 val_loss=-2.8532 scale=2.0000 norm=0.7841

[iter 200] loss=-4.0889 val_loss=-1.5779 scale=2.0000 norm=0.7544

[iter 300] loss=-4.8400 val_loss=8.8107 scale=2.0000 norm=0.6710

[iter 400] loss=-5.4463 val_loss=51.7171 scale=2.0000 norm=0.5999

It looks like overfit occurred between iterations 100 and 200.
Is the best (val_loss wise) model saved, or did I get the last one reported (with a massive overfit, -5.4463 in train loss vs 51.7171 in validation loss)?
If I really do get the overfitted model, how can I introduce early stopping (or model saving) based on the validation score?

No, I'm afraid you won't get the best model if you don't ask for it specifically.
But don't despair - just set the fit parameter of early_stopping_rounds to a number - and it will stop after this number of rounds in which the validation loss got worse.
The number of rounds that would work best for you should be set experimentally (i.e., just fiddle around fit till you get best results, but don't forget hyperparameter overfit...)
PS.
The remarks in the code claim that this number of rounds is consecutive, but looking a the code it does not look like the count is reset each time validation loss improves (the counter val_loss_list is not reset).
You could change this manually be editing ngboost.py.


R has {drake} which makes it easy to make reproducible data pipelines. Does Python have a similar package? See R's {drake}. It allows you to define a reproducible pipeline

plan <- drake_plan(
  raw_data = readxl::read_excel(file_in("raw_data.xlsx")),
  data = raw_data %>%
    mutate(Species = forcats::fct_inorder(Species)),
  hist = create_plot(data),
  fit = lm(Sepal.Width ~ Petal.Width + Species, data),
  report = rmarkdown::render(
    knitr_in("report.Rmd"),
    output_file = file_out("report.html"),
    quiet = TRUE
  )
)

# call the pipeline
make(plan)

The great thing about drake is you that you can reload any of raw_data, data, hist, fit, report at any point. And if you change part of the code and make(plan) and {drake} will figure out which has change and just run that.

Sklearn has pipeline. If you have fit and transform attributes iteratively, you can make them pipeline by Pipeline class in sklearn.pipeline.

Read the docs:

https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html

Additionally you can save and load a pipeline object by joblib.dump and joblib.load.


Why can't I choose my hyper-parameter in the training set? Say I've divided the data into 3 parts: training, validation and test. I know for example, that in Neural Networks, the number of hidden layers is a hyper parameter. Why can't I train numerous NN architectures in the training set and then test their accuracy in the test set; thereby allowing me to choose one final model? What is the purpose of the validation set in this instance?

I wouldn't say you can not tune the hyper-parameters in the trainig dataset, but the purpose of doing so is different than in the validation set.

In general, what it is intended with a ML algorithm is how to optimally classify or perform regression given some training data. Once the model is trained, it will be used with new data to obtain predictions, thus:

We need some training data. With this dataset, the model will look for the optimal weights and biases that minimizes the selected loss function.
We need some upper bound to our performance metric: for example, we can decide that our model has to perform like a human, which has showed an average metric of 95% for some specific task. The upper bound can be the best score in a certan benchmark, etc.
We start training the model on the training data and we evaluate its metric, lets take the accuracy. If the accuracy is too low, we can tune the hyper-paramters until the accuracy increases in the training data (no evaluation dataset is used here). The difference between our model's accuracy and the upper bound is taken as the bias of the model (this bias has not to be confused with the biases of the neurons). So, the accuracy on the training data gives us the bias of our model.
Once we decided our bias is reasonable (optimally 0) arises the question of how the model will perform when it is fed with data that has not be used for training (the real application). The difference between the accuracy on this unseen data and the accuracy on the training data is the variance of the model. This unseen data is the validation set and give us an idea of how the model generalizes to new data. If the variance is high, then the model poorly generalizes to new data. Then, we perform hyper-paramater tunning and evaluating the accuracy on the validation data until the variance is low enough, trying to not worsening the bias (variance-bias trade-off).

Example: desired accuracy = 95%. Training accuracy = 93%. Validation accuracy = 82% -> Bias = 2%, Variance = 11%

Summarizing:

Tunning in the training data = decreasing bias
Tunning in the validation data = decreasing variance

What's more unclear is the role of the test dataset. I usually use it when I decided that the model will be no longer changed and I need the final metric that describes the model. The test set can also be used to have an idea about how the model performs with data which have not be intended to work with. In general, the test set gives the power of the model for the inference task.


Seq2Seq Model training: Encoder vs. Decoder Can someone point me to an article which explains how the model training is done in Seq2Seq? I know "Teacher Forcing" is used but what I found so far hasn't been detailed enough. What I am most confused about is where the training happens? The back-propagation goes back to the encoder?

Any insight here would be really appreciated. Thanks!

You can check medium page, https://link.medium.com/iolQKIWawX.
You can also get more detailed pages on medium itself, shared link includes the references of those.


Keras VAE example loss function The code here:

https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py

Specifically line 53:

xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)

Why is the cross entropy multiplied by original_dim? Also, does this function calculate cross entropy only across the batch dimension (I noticed there is no axis input)? It's hard to tell from the documentation...

Keras metrics.binary_crossentropy computes the cross entropy averaged across all inputs (pseudocode):

original_dim = 3
x = [1,1,0]
x_decoded = [0.2393,0.7484,-1.1399]

average_BCE = binary_crossentropy(x, x_decoded)
print(average_BCE)
>>>0.1186

For this part of autoencoder loss we need the sum, not the average over all squared differences between input and output pixels, which is equivalent to  average_crossentropy_of_pixels * num_pixels (original_dim)

print(original_dim * average_BCE)
>>>0.3559

Another way of writing this part would (which I think is more illustrative of what's happening, but probably less performant in Keras land):

xent_loss = K.sum(K.square(x - sigmoid(x_decoded)))
print(xent_loss)
>>>0.3559

Regarding the second part, since the first operation you are really doing is subtraction, it implied the tensors for input and output are the same size. This can be found if you check the implementation code - which is better than the docs in this case - go to line 3056:
https://github.com/keras-team/keras/blob/master/keras/backend/tensorflow_backend.py


Generating normally distributed data frame with 3 columns I am trying to generate a normally distributed data frame in Python with 3 columns. Each column requires a separate input of mean, standard deviation, lower and upper values. I am trying to generate sales data of Unique Coke products, Unique Products and Total Revenue which are normally distributed. Also, how can I find the mean of the entire normally distributed data frame?

A possibility in the following code snippet.

import pandas as pd
import numpy as np
df = pd.DataFrame()
mean = [0,1,2]
scale = [1,2,3]
max_val = [2,3,4]
min_val = [-2,-1,0]
for n in range(3):
    df[n] =  np.random.normal(loc=mean[n],scale=scale[n], size=100)
    df.loc[df[n] < min_val[n],n]= min_val[n]
    df.loc[df[n] > max_val[n],n]= max_val[n]
print('The Dataframes mean is: ',df.mean().mean())


what is darknet and why is it needed for YOLO object detection? what is darknet and why is it needed for YOLO object detection ? I read that its a neural network written in C , but why is it needed for YOLO object detection  when we  have lot of machine learning framework,api like tensorflow,keras,pytorch .

Im trying to train yolo from git code and i could see they are using tensorflow/keras as well but not sure why darkenet is used initially for traning yolo  .

darknet/yolo algorithm

https://pjreddie.com/darknet/ is their website...

I cite :

"Darknet: Open Source Neural Networks in C

Darknet is an open source neural network framework written in C and CUDA. It is fast, easy to install, and supports CPU and GPU computation."

As to why they used that, well it's open source and in C, which are good points and seems to be performant (see the graphs in your link and associated paper). But the main point seems to be about history. The darknet project seems to have started in 2014. Were tensorflow / keras available and perfromant at this time in the first place ? Even if they were why should they use keras / tensorflow ?


Which feature selection I should trust? I use Python and Weka to run feature selection on my dataset (91 predictor variables). I can see a huge difference (feature ranking) from different algorithms. And these results are still quite different from that derived from random forest or gradient boosting fitting. So how can I treat this gap or which algorithm I should trust? Is there any performance evaluation method or rule of thumb?

# Univariate Selection
import pandas
import numpy
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

# feature extraction
test = SelectKBest(score_func=chi2, k=4)
fit = test.fit(X, y)
# summarize scores
numpy.set_printoptions(precision=3)
print(fit.scores_)

# Feature Extraction with RFE
from sklearn.feature_selection import RFE

# feature extraction
model = LogisticRegression()
rfe = RFE(model, 15)
fit = rfe.fit(X, y)
print("Num Features: %d" % fit.n_features_)
print("Selected Features: %s" % fit.support_)
print("Feature Ranking: %s" % fit.ranking_)

# VarianceThreshold
from sklearn.feature_selection import VarianceThreshold
sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
sel.fit_transform(X)

idxs = sel.get_support(indices=True)
np.array(X)[:, idxs]

It is generally not best practice to trust ML techniques to do the work for you. A data scientist knowns on what principles the methods that he or she applies work, and then stills tries multiple in the same setting, to see which methods best apply to the domain at hand. How well certain measures work can be a clue to the structure of your data.

Right off the bat it is important to understand that Weka offers 3 kinds of feature selection methods: Filters, a Wrapper and PCA. Filters apply criteria (mostly based on a measure of how it divides a set) for feature suitedness (variations on how well they divide the search space) , wrappers use a learning algorithm for their evaluation and PCA doesn't so much select features as that it recombines features to maximize variance in the dimensions.

That being said: I suggest you start looking at InformationGainEval a method that uses information gain to select attributes. It is quite popular and easy to understand. You can find a good explantation here:
https://stackoverflow.com/questions/33982943/how-the-selection-happens-in-infogainattributeeval-in-weka-feature-selection. Than work your way through the others (the documentation is accessible from the GUI).


Should we do Feature selection in parallel with feature engineering? I'm working with LightGBM on a large data set about 3M row and about 8 columns. When i started feature generation after every new feature i was measuring the RMSE of the model and if it was the same or slightly worse i was removing it so is that procedure right ? or i should do all the feature generation and then apply feature selection on them and why ?

You can add features as you create them and check if the scoring improves (and by how much). Always measure the score on test data so you can detect overfitting.

When you are done with feature engineering you can perform feature selection to reduce the number of features or understand their importances in the model.


How are the gradients of a Neural Network calculated just by matrix multiplication? I would have expected some kind of derivative solving equation to be at work in order to back propagate the loss to each neuron. I hope my question is not too confused to answer.
In the network below, why does h_relu.T @ grad_y_pred (y hat - y) produce the gradient for w2? I understand sometimes the derivative is just something you multiply something by. Like the derivative of y = h_relu * w2 being h_relu, which doesn't make much sense to me either. I understand why the derivative of x^2 is 2x and that's about as far as my understanding of derivatives goes.

import numpy as np

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random input and output data
x = np.random.randn(N, D_in)
y = np.random.randn(N, D_out)

# Randomly initialize weights
w1 = np.random.randn(D_in, H)
w2 = np.random.randn(H, D_out)

learning_rate = 1e-6
for t in range(500):
    # Forward pass: compute predicted y
    h = x.dot(w1)
    h_relu = np.maximum(h, 0)
    y_pred = h_relu.dot(w2)

    # Compute and print loss
    loss = np.square(y_pred - y).sum()
    print(t, loss)

    # Backprop to compute gradients of w1 and w2 with respect to loss
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w2 = h_relu.T.dot(grad_y_pred)
    grad_h_relu = grad_y_pred.dot(w2.T)
    grad_h = grad_h_relu.copy()
    grad_h[h < 0] = 0
    grad_w1 = x.T.dot(grad_h)

    # Update weights
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2

Like the derivative of y = h_relu * w2 being h_relu, which doesn't make much sense to me

These are partial derivative and depends on w.r.t variable. In the end, we have to complete the chain of partial derivative from output to the respective weights.

Your Network is -
$$x \xrightarrow[\text{}]{\text{}} w_1 \xrightarrow[\text{}]{\text{  h  }} ReLU \xrightarrow[\text{}]{\text{  h_relu  }} w_2 \xrightarrow[\text{}]{\text{ }} y\_pred$$
By definition ReLU is - $f(x)=\max(0, x).$
Derivative of ReLU - $\frac{d}{dx}\text{ReLU}(x)$
=$1 \forall x > 0$
=$0 \forall x < 0$

In backpropagation -
$\frac{\partial y\_pred}{\partial w_2} $ = $h\_relu$  ;since, y_pred = $h\_relu*w_2$
$\frac{\partial w_2}{\partial h} $ = $1$  ;derivative of ReLU=1,ignoring negative part for simplicity
$\frac{\partial h}{\partial w_1} $ = $x$ ;since, h = $x*w_1$
$\frac{\partial y\_pred}{\partial h\_relu} $ = $w_2$ ;since, y_pred = $h\_relu*w_2$

So, Loss gradient at different weights will be -
 $L$ at the $y\_pred$
 $L*h\_relu$ at $w_2$
 $L*w_2$ at $h\_relu$
 $L*h\_relu*1*x$ at $w_1$


Python package to assess text coherence I am looking for a python package that calculates how well one sentence of a natural text follows the next. One could simply count how many identical words are in the next sentence but the better method would be to compare word similarities using something like word vectors (=semantically similar words instead of exact matches or synonyms).

Coherent:

Tom loves reading books.
He prefers reading books at library.
So he always goes to library.

Non-coherent:

Tom loves reading books.
He missed his lunch today.
So he always goes to library.

I guess there must be several well written packages for such an automatic assessment (or similar methods) but I just can't find it. Any ideas?

This task looks similar to what is called text segmentation, in particular topic segmentation. I don't know any python package to do it but apparently Google gives a good few results for "semantic text segmentation python" (I'm not sure that this is the best phrase, you might want to try variations).

Note: this is still an active NLP research topic as far as I know. I don't know how fast reliable python packages are written and maybe there are some for this, but I wouldn't be too surprised if there were only research prototypes available at this stage.


Machine learning worker performance features for optimum allocation of tasks to workers Suppose we have an English to French translation task in a company, and there are 100s of workers who are proficient in doing this task, but each worker has its own unique attributes which enables them to do certain translations in a better way than the others (e.g., a translator who is a doctor would do better translations of medical documents). Consider 1000s of documents for translation received per day, and each document takes one day to translate. So, there is a two-fold problem:

Allocating the tasks to available translators using a queuing model or any other efficient allocation mechanism.
Learning the worker model based on the past performance of each (e.g., correctness scores, static parameters like skill set, qualifications, experience, etc.) using a machine learning system.

Is there a setup/system/model which solves both of the above problems in one cohesive system? From my reading, this problem calls for the application of the assignment problem, user modelling, queuing, and machine learning optimization. I am searching for existing models or frameworks which integrate all of this, can anyone give me some direction for this task of optimum allocation?

Your problem is going a lot of directions.

If you have known worker attributes, like doctor, engineer, or accountant, you could have mirror document classification, and use machine learning to classify.

If you are going to measure performance, you will need to get a sample from each user. For example, take the top $\frac{1}{4}$ performance and have a target vector for the user.  Map a document to a user's strength.  If that user is over-queued, pick the next highest $\frac{1}{4}$ and so on.  You could use cosine similarity to give you a score. This can get tricky, as you may want to normalize on productivity.  You may get a document that a high productivity user is better at, but a low productivity user is OK at. By way of analogy, an american football player may be the best receiver and half back, but you might have a good backup receiver and no good backup halfback, so you let your backup play receiver.

Static parameters would be different, and I am not sure you should use them if you have a performance measure. My guess would be to queue before you have performance for a worker.  But even at 4 documents, you can select a top $\frac{1}{4}$.

You could also create clusters and measure a user's performance versus the cluster. This is the easiest approach to queuing, as you just take the next document out of the cluster that is a user's best cluster.  You only need a few cross-cluster workers to level queues. The downside is, if you have 20 clusters, it would take a while to get user performance versus cluster, so you would need to use static attributes until you get a valid sampling.


After anaconda installation spyder not working I am using ubuntu 16.04.
after anacoda installation sypder not open.
Anaconda version: 4.4.6

Anaconda environment

my_root                  /home/shiva/.conda/envs/my_root
py27                     /home/shiva/.conda/envs/py27
py35                     /home/shiva/.conda/envs/py35
base                  *  /home/shiva/anaconda
myenv                    /home/shiva/anaconda/envs/myenv

I get the following error

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/spyderlib/spyder.py", line 2331, in main
    mainwindow = run_spyder(app, options, args)
  File "/usr/lib/python3/dist-packages/spyderlib/spyder.py", line 2215, in run_spyder
    main.setup()
  File "/usr/lib/python3/dist-packages/spyderlib/spyder.py", line 752, in setup
    from spyderlib.plugins.inspector import ObjectInspector
  File "/usr/lib/python3/dist-packages/spyderlib/plugins/inspector.py", line 41, in <module>
    from spyderlib.widgets.ipython import IPythonControlWidget
  File "/usr/lib/python3/dist-packages/spyderlib/widgets/ipython.py", line 28, in <module>
    pygments_patch.apply()
  File "/usr/lib/python3/dist-packages/spyderlib/pygments_patch.py", line 22, in apply
    from pygments.lexer import _TokenType, Text, Error
  File "/usr/lib/python3/dist-packages/pygments/lexer.py", line 19, in <module>
    from pygments.filters import get_filter_by_name
  File "/usr/lib/python3/dist-packages/pygments/filters/__init__.py", line 20, in <module>
    from pygments.plugin import find_plugin_filters
  File "/usr/lib/python3/dist-packages/pygments/plugin.py", line 39, in <module>
    import pkg_resources
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2927, in <module>
    @_call_aside
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2913, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2952, in _initialize_master_working_set
    add_activation_listener(lambda dist: dist.activate())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 956, in subscribe
    callback(dist)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2952, in <lambda>
    add_activation_listener(lambda dist: dist.activate())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2515, in activate
    declare_namespace(pkg)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2097, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2047, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2066, in _rebuild_mod_path
    orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'

how can I solve this problem?

According to this bug report, you can solve this with:

pip install --upgrade pip setuptools


Running a python script with heavy memory requirements I need to run a python script where I am loading several(~15) large modules including keras and load several (approx 20) LSTM models and large amount of data for computation. These script has several methods which are needed to build an environment class for a reinforcement learning problem. The process seems to be working very slowly on my 16GB RAM i7 CPU(haven't tried out multiprocessing module yet but might not work as most of the task is not parallelism friendly.) Can you provide me some alternative possibly going for a higher end hardware setup ideal for such heavy computations which can be done on a local machine. Since it's a problem I am facing in ML related work which other researchers over here might have faced, that's why I am adding the question in this forum. Please suggest other forums if possible where I can get a better suggestion

Python is by default single-threaded. Read about the Global Interpreter Lock (GIL). Although some modules with utilise multi-threading, asyncronous processing and multicore solutions, these will not take effect when you are simply loading modules and data.

Unless you are using some distributed system, or perhaps something like Tensorflow-serving, you will not be able to train/exectute your 20 LSTM models in parallel in a single Python script, so perhaps you could just load one at a time. Alternatively, just make 20 copies of the script - one for each model - then launch all 20 scripts in different terminals (or via a multi-threaded or multicore script).

Asking on StackOverflow would reach an audience that is more adept in the sorts of problems.


Can the F1 score be equal to zero? As it is mentioned in the F1 score Wikipedia, 'F1 score reaches its best value at 1 (perfect precision and recall) and worst at 0'.

What is the worst condition that was mentioned?

Even if we consider the case of: either precision or recall is 0.
The whole F1-score value becomes undefined. Because when either precision or recall is to be 0, true postives should be 0. When the true positives value becomes 0, both the precision and recall become 0.

F1 will never be zero, but very near to zero for a bad classifier. If TP or TN is zero then there isn't any need to check F1.


What is the hypothesis space used by this AND gate Perceptron? Per this post

  The hypothesis space used by a machine learning system is the set of
  all hypotheses that might possibly be returned by it.

Per this post, the Perceptron algorithm makes prediction

$$
\begin{equation}
  \hat y =
    \begin{cases}
      1 & wx+b >= 0\\
      0 & wx+b<0
    \end{cases}
\end{equation}
$$

  we can conclude that the model to achieve an AND gate, using the
  Perceptron algorithm is

$x_1 + x_2 – 1.5$

In this case, what is the hypothesis space used by this AND gate Perceptron?

As far as I understand:

A hypothesis is a model which is capable of predicting outputs from inputs, hence the $x_1 + x_2 - 1.5$ is a hypothesis but not the only one. The whole models that have the same capability are regarded as hypothesis space.

We know that in AND gate:

     x1       x2     output
|---------|--------|--------|
|     0   |    0   |    0   |
|     0   |    1   |    0   |
|     1   |    0   |    0   |
|     1   |    1   |    1   |
|---------------------------|

and we have $w .\cdot x + b$, based on which, either $0$ or $1$ turns out as output.
$$w \cdot x + b$$
$$w_1 \cdot x_1 + w_2 \cdot x_2 + b$$

trying all the inputs in this expression:

$w_1 \cdot 0 + w_2 \cdot 0 + b <= 0$ (because the output should be 0)  $b < 0$

$w_1 \cdot 0 + w_2 \cdot 1 + b <= 0$ ---> $ w_2 + b <= 0$  So $w_1 < |b|$

$w_1 \cdot 1 + w_2 \cdot 0 + b <= 0$ ---> $ w_1 + b <= 0$  So $w_2 < |b|$

$w_1 \cdot 1 + w_2 \cdot 1 + b > 0$ ---> $ w_1 + w_2 + b > 0$

Firstly, we initialize the weights and bias parameters and then if needed, change them.

Here, since $b < 0$ we set it as $-1$

Since $w_1 < |b|$,  $w_2 < |b|$ and weights are not negative, we set them as 1.
So we would have:

$w_1 \cdot 0 + w_2 \cdot 0 + b = -1 < 0$ is right, returns 0 because is negative.

$w_1 \cdot 0 + w_2 \cdot 1 + b = w_2 + b = 1 - 1 = 0$  wrong, it returns 1 while it should return 0

$w_1 \cdot 1 + w_2 \cdot 0 + b <= 0$ ---> $ w_1 + b <= 0$  So $w_2 < |b|$ wrong, it returns 1 while it should return 0

$w_1 \cdot 1 + w_2 \cdot 1 + b > 0$ ---> $ w_1 + w_2 + b > 0$

So we set b for a smaller value like -1.5 (Note), then all the expressions would work appropriately. Hence $x_1 + x_2 - 1.5$ is a hypothesis for this problem.

Note: we know that in using perceptron algorithm, when reaching at any point that is not following the current model, weights and bias are updated as follows:

w = w + yx
b = b + y

Here, in the source that you referred to, maybe for simplicity they haven't done so and have just found a sample of a plausible model (hypothesis)

The other hypothesizes should follow the previously mentioned rules, thereby, $x_1 + x_2 - 2$ can also be another hypothesis for this problem, etc.


Detect a pattern in time series data I have the following time serie data and I need to detect the following pattern:
- a 1st spike above a given value
- a 2nd spike close to the given value

What defines "a spike" is that its value is x% above the surrounding spikes.

I would like to use python (numpy and pandas modules) to detect these.

As a background, the goal is to trigger an action when the second spike is detected (outlined in red in the attached image).

scipy.signal's finds_peaks should have most of what you need.

With the height argument you can set a minimum threshold to get the first peak.

With the prominence argument you should be able to approximate the second peak. This requires peak_prominences.

  The prominence of a peak measures how much a peak stands out from the
  surrounding baseline of the signal and is defined as the vertical
  distance between the peak and its lowest contour line.


MultiVariate Regression with LSTM I am interested to use multivariate regression with LSTM (Long Short Term Memory).
As an example, we can take the stock price prediction problem, where the price at time t is based on multiple factors (open price, closed price, etc.). Using this information we need to predict the price for t+1.
Another example can be the house price prediction (which depends on factors like, age of house, area, no. of beds, no. of baths, etc). Using this information, we can predict the price of a given house.

I have found many examples where they just used a single variable regression, but I am interested to use multiple features.

Has anyone tried this and can anyone point out the right direction for this?

I tried the following but have no idea if this is the efficient way.

# Importing the training set
training_set = pd.read_csv('Google_Stock_Price_Train.csv').iloc[:,1:3].values#has fields: Date,Open,High,Low,Close,Volume
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler()
training_set_scaled = sc.fit_transform(training_set)
X_train = training_set_scaled[0:1257]
y_train = training_set_scaled[1:1258]
# Reshaping
X_train = np.reshape(X_train, (1257, 1, 2))
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 2))
from keras.models import Sequential
from keras.layers import Dense, LSTM
regressor = Sequential()
regressor.add(LSTM(units = 4, activation = 'sigmoid', input_shape = (None, 2)))
regressor.add(Dense(units = 2))
regressor.compile(optimizer = 'rmsprop', loss = 'mean_squared_error')
regressor.fit(X_train, y_train, batch_size = 32, epochs = 100)

Thanks.

Here's a great example for Multivariate Regression using LSTM:

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

Most Examples you will find will be for time-series prediction using LSTM, like the one you listed for Stock Prices.


Training, cross validation and testing accuracy (RMSE and R2) differs when using different shuffles and splits I have a very small data set of 60 observations. My training, cross-validation and testing accuracy (RMSE and R-squared) differ in a considerable amount when using different random states while performing shuffling and then splitting. The training, testing and cross validation accuracy changes each time a different random state is used. How can I solve such an issue and how to really assess the performance of the model?

In terms of evaluation, the best you can do with a very small amount of data is repeating $k$-fold cross-validation many times (i.e. very large $k$), and consider the whole distribution of scores as the performance (in particular take into account the variance across folds).

It's going to be difficult anyway to obtain a reliable measure of performance with such a small dataset. Two options come to mind:

obtain more instances, possibly by using some interpolation method to generate artificial data (but it's not as good as real data).
make the model less complex by reducing the number of features, as this is likely to reduce the variance in the performance.


Decision Tree : PlayTennis Data Set I am practicing to use sklearn for decision tree, and I am using the play tennis data set:

play_ is the target column.

as per my pen and paper calculation of entropy and Information Gain, the root node should be outlook_ column because it has the highest entropy.

But somehow, my current decision tree has humidity as the root node, and look likes this:

My current code in python:

from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
from sklearn.preprocessing import LabelEncoder

import pandas as pd
import numpy as np

df = pd.read_csv('playTennis.csv')

lb = LabelEncoder()
df['outlook_'] = lb.fit_transform(df['outlook'])
df['temp_'] = lb.fit_transform(df['temp'] )
df['humidity_'] = lb.fit_transform(df['humidity'] )
df['windy_'] = lb.fit_transform(df['windy'] )
df['play_'] = lb.fit_transform(df['play'] )
X = df.iloc[:,5:9]
Y = df.iloc[:,9]

X_train, X_test , y_train,y_test = train_test_split(X, Y, test_size = 0.3, random_state = 100)

clf_entropy = DecisionTreeClassifier(criterion='entropy')
clf_entropy.fit(X_train.astype(int),y_train.astype(int))
y_pred_en = clf_entropy.predict(X_test)

print("Accuracy is :{0}".format(accuracy_score(y_test.astype(int),y_pred_en) * 100))

If you would skip this line:

X_train, X_test , y_train,y_test = train_test_split(X, Y, test_size = 0.3, random_state = 100)

and change the method parameters to train your model like this:

clf_entropy.fit(X, Y)

it should work as expected.


metric learning and information retrieval I am interested in parsing semi-structured text.
Assume that I have a text with labels of the kind: year_field, year_value, identity_field, identity_value, ..., address_field, address_value, and so on.

These fields and their associated values can be everywhere in the text, but usually they are near to each other, and more generally the text in organized in a (very) rough matrix, but rather often the value is just after the associated field with eventually some non-interesting information in between.

The number of different format can be up to several dozens, and is not that rigid (do not count on spacing, moreover some information can be added and removed).

I am looking toward machine learning techniques to extract all those (field,value) of interest.

I think metric learning and/or conditional random fields (CRF) could be of a great help, but I have not practical experience with them.

Does anyone have already encounter a similar problem?

Any suggestion or literature on this topic?

Conditional Random Fields (CRFs) can be used for segmenting/labeling sequential problems. Try CRF++: Yet Another CRF toolkit,  a simple, customizable, and open source implementation of Conditional Random Fields (CRFs)

You can label and create a tagged training corpus and use CRF++

You also need to create a feature template

Refer : http://taku910.github.io/crfpp/ for more details.

Check the example from data for CoNLL shared task (PoS tagging).


Real purpose of pooling Recently I had a doubt as to what is the real purpose of pooling layers in neural networks is? The most common answer is

To select the most important feature
To increase the receptive field of the network

I feel that these are not real reasons for using a pooling layer because

There is no real need to select important features because the fully connected layer at the very end could be used to identify the most important features
The receptive field could be increased by increasing the kernel size in the successive layers.

So the only real reason for using pooling is to reduce to the size of the feature representation thus leading to smaller memory and computational footprint as the networks deeper.

Do you agree with the analogy? Do you feel there is any other reason as well?

So the only real reason for using pooling is to reduce to the size of the feature representation thus leading to smaller memory and computational footprint as the networks deeper. yes, cost efficiency but also:

Generalisation: We get rid off small--unimportant details when we combine several values into one representative one. Hence what you really get is reduced chance of overfitting


Understanding Reinforcement Learning with Neural Net (Q-learning) I am trying to understand reinforcement learning and markov decision processes (MDP) in the case where a neural net is being used as the function approximator.

I'm having difficulty with the relationship between the MDP where the environment is explored in a probabilistic manner, how this maps back to learning parameters and how the final solution/policies are found.

Am I correct to assume that in the case of Q-learning, the neural-network essentially acts as a function approximator for q-value itself so many steps in the future? How does this map to updating parameters via backpropagation or other methods?

Also, once the network has learned how to predict the future reward, how does this fit in with the system in terms of actually making decisions? I am assuming that the final system would not probabilistically make state transitions.

Thanks

In Q-Learning, on every step you will use observations and rewards to update your Q-value function:

$$
Q_{t+1}(s_t,a_t) = Q_t(s_t,a_t) +  \alpha [R_{t+1}+ \gamma  \underset{a'}{\max} Q_t(s_{t+1},a') - Q_t(s_t, a_t)]
$$

You are correct in saying that the neural network is just a function approximation for the q-value function.

In general, the approximation part is just a standard supervised learning problem. Your network uses (s,a) as input and the output is the q-value. As q-values are adjusted, you need to train these new samples to the network. Still, you will find some issues as you as using correlated samples and SGD will suffer.

If you are looking at the DQN paper, things are slightly different. In that case, what they are doing is putting samples in a vector (experience replay). To teach the network, they sample tuples from the vector, bootstrap using this information to obtain a new q-value that is taught to the network. When I say teaching, I mean adjusting the network parameters using stochastic gradient descent or your favourite optimisation approach. By not teaching the samples in the order that are being collected by the policy the decorrelate them and that helps in the training.

Lastly, in order to make a decision on state $ s $, you choose the action that provides the highest q-value:

$$
a^*(s)= \underset{a}{argmax} \space Q(s,a)
$$

If your Q-value function has been learnt completely and the environment is stationary, it is fine to be greedy at this point. However, while learning, you are expected to explore. There are several approaches being $\varepsilon$-greedy one of the easiest and most common ways.


Semi-supervised anomaly detection I am currently exploring anomaly detection methods for my work and, basically I have gone through Local Oulier Factor and Isolation Forests, both unsupervised methods.

Now, the thing is, there might be a chance that I do not want a point that is far away to considered as an outlier, and so I would need some sort of supervised or semi supervised method for the outlier detection.

So what I am thinking is:

1.Label a bunch of points as outlier using LOF/IF.

2.Train a classifier on top of the labels, and then make manual adjustements if needed.

Is this what is considered a semi-supervised method? Does anybody have any experience with this sort of problem that could say if I am missing something here?

Also, because I am labeling outliers the dataset will be very unbalanced. My idea is to use bagging for this. Let's say my dataset is 1% outliers, I would train 100 equally proportional models (the outliers parts remains the same on each model, but the normal points change until I go over the entirety of the dataset) and then the final prediction is a vote of all the models. Is this stupid or a good idea?

If you use the anomaly detector to label the data directly, there is no way the  supervised step that follows can be better than that. One can of course go in an "adjust" the labels afterwards, but there is risk of being biased by the pre-existing labels if a human sees it up front.

Instead of sampling data to label randomly, you could sample weighted based on the anomaly score. This has two effects 1) reduces class imbalance 2) focuses labeling effort on likely anomalies

This assumes a well-tuned anomaly detector though. Which there is no good way to do without a validation set. That dataset should preferably be sampled randomly, to avoid bias.


How many features should I take out of 515 features? In continuation to this question, I have a conceptual question. If I am not using the 'nfeatures' then I am getting 7 features from a feature set of 515 features. If I use the 'nnfeatures' tag (this tag specifies the minimum number of features I want it to display) with a value of 10, then I am getting those 7 faetures along with 3 new one. Now I am unable to decide how many features should I take. Is it okay to take 7 features out of 515 features? Will any ML algorithm be able to capture the characteristics of the data with just these 7 features?

Another question is, a criterion value is given when the sequentialfs algorithm is applied in MATLAB. What is this criterion function? From this page I came to know that a set of features are chosen whose mean criterion value is the lowest. So does it mean that only those features are chosen with the minimum criterion value? Also, if this is the case, then how does the algorithm decide how many features it should select?

Disclaimer:  I do not know enough about MATLAB to answer the full question but I'll have a go at answering the rest
1 - How many features should you take?
If this is a predictive problem you're trying to solve, how about you try and assess the accuracy of your predictions by using features sets of 7 and 10 respectively? You might find that there is very little difference in predictive power so taking 7 might be OK.
NB: I am quite surprised that your feature selection algo selected such a small proportion of your available features - you're right to want to dig a little deeper on what's happening under the hood..
2 - Will any ML algorithm be able to capture the characteristics of the data with just these 7 features?
If your feature selection algo is working fine then there's all the likelihood that a ML would be able to characterise the underlying structure of your dataset relatively easily.
There's a number of problems out there that have a large amount of explanatory variables but are best characterised by a select few (for instance GDP is usually relatively well explained by what it was last quarter, with all other related variables having small impacts on prediction accuracy).
3 - MATLAB QUESTIONS: TBC
Sorry I can't answer all your questions - maybe someone will come by with MATLAB knowledge and we can roll our answers together. Otherwise you might be better splitting this into two questions to get more detailed answers on the MATLAB algo.


KNIME model suggestion I have the following model:

a table with events, each row is event, each event has a timestamp.

I want to try and learn how many event happen per day of week per hour.

I tried to work with the naive bayes learner (with binning), but got bad
results, I think because of the binning, the numbers does have meaning.

I'm assuming there is a relation between day and hour to the amount of events, some simple formula/tree.

What learner should i use that can work with numbers and not nominal values?

thanks,

You can check this whitepapers for time series prediction with KNIME.
https://www.knime.org/files/knime_bigdata_energy_timeseries_whitepaper.pdf
https://www.knime.org/files/Anomaly_Detection_Time_Series_final.pdf

I am not sure though if you are trying to predict how many events will happen say in the next hour or just count how many events have happened per hour in the past. In this last case, just use a GroupBy node.

Let me know if this helps

-- Rosaria


Does orange transfrom categorial variables into dummy variables when using hierarchical clustering? I am using Orange to cluster a large amount of data consisting of three attributes. Each attribute only contains categorical unordered variables. My question is does Orange transform categorical variables into dummy variables when using Distances and Hierarchical clustering widgets on such a data set?

The documentation states this is not the case. Categorical values are compared as either same or different.


How to determine key frames in a video for video classification? How to detect changes between when the changes between 2 frames of videos that are significant enough to be counted for video classification? Thinking of the problem as analogous to edge detection in an image where the change between 2 adjacent pixel is threshold-ed to detect an edge.

Most video formats should already mark such frames marked as "Key Frames" (Frames 1 in this example).

It is possible to get identifiers of such frames (or split video around such frame). For example, mkvmerge tool.

https://mkvtoolnix.download/doc/mkvmerge.html

http://nickyguides.digital-digest.com/keyframes.htm


Which packages or functions can I use in R to plot 3D data like this? There're many data points, each of which is associated with two coordinates and a numeral value, or three coordinates. And I wish it is coloured.
I checked packages "scatterplot3d" and "plot3D" but I couldn't find one like the example I give. It is like it has a fitting surface.

My data is basically like the following. In this way I think this kind of plot is gonna be perfectly suitble for this data:

    ki,kt,Top10AverageF1Score
    360,41,0.09371256716549396
    324,41,0.09539634212851525
    360,123,0.09473510831594467
    36,164,0.09773486852645874
    ...

But I also may have one more additional variable, which makes it like:

    NeighborhoodSize,ki,kt,Top10AverageF1Score
    10,360,41,0.09371256716549396
    15,324,41,0.09539634212851525
    15,360,123,0.09473510831594467
    20,36,164,0.09773486852645874
    ...

Do you also have any good idea for visualizing the second case? What kind of plot and which packages and functions, etc.

You could use the wireframe function from the lattice package:

library("lattice")
wireframe(volcano[1:30, 1:30], shade=TRUE, zlab="")


Applying random forest model to a dataframe with multiple types of data I am trying to solve the following classification problem: one has a large (~2-10 GB) .csv file containing a data frame of various types. In particular, there are columns containing numeric floats as well as date/times, text strings and discrete parameters from a small set. There is also a binary parameter classifying each entry which one wants to predict in the future using the machine learning approach.

Now, what would be the most appropriate tool for this job? After some research I settled on scikit-learn as the main option, but if there is other software radically better suited for the task, I'd be happy to switch.

Secondly, is the random forest approach the best way to go here? I realise there are probably many factors influencing the answer to this question, but from playing around with a few tutorials and looking at the scikit-learn flowchart I am currently leaning towards random forests.

Assuming one uses random forests, how exactly one should prepare these data in order for the model to make sense of it? From what I understand, scikit-learn takes only numerical floats as inputs for the model. While I could discretise date/time and other discrete fields with something like OneHotEncoder, it is not immediately clear what to do with text fields since the processing, I suppose, is very different. (I have looked at the bag-of-words approach, but being someone new to the field I fell it a bit hard to grasp right away.) Is there a meaningful way of transforming text fields for use in the model?

Finally, what is the best way of dealing with large datasets for this task? While for the start their size could be limited to something fitting inside runtime memory, ideally there should be a way to extend the training set to be very large.

Apologies for the vagueness of some questions. Since I am quite new to data science in general, I would appreciate if you could include actual working examples in your answers. Thanks!

One thing to remember when we use Random Forest is when you use a categorical feature for training it shouldn't have more than 53 categories. Sometimes RandomForest takes numerical data as categorical. To overcome that make sure to convert all categorical as factors using this command.

df['col_name'] = df['col_name'].astype('category')

Lastly, make sure that all the levels in a categorical feature are presnent in both train and test data or else the model cannot hadle such situations.

I think you might get the model running if you can use any server(as the model can use the servers processing capacity to its fullest) and deploy on that and let it run before you leave for the day, before deploying make sure that you have tested the code on sample data. So, that it won't abend with some error.

Do let me know if you need any additional information.


How to train a neural network model in python or any language that can train itself from a excel file and validate itself also from a excel file? I have a large amount of data. I need to make a neural network that trains itself  from an excel data sheet and validates its output from another excel data set.
There will be an output coloumn in the validate excel file. The model needs to give the output near about the givern row.

You are mentioning two different problems:

data gathering: here you want to get your data from an excel file for both the training and the validation
modeling your data: here you want to do it with a neural network.

For the data gathering, I suggest you to use pandas, a python library which can extract data from excel files with pandas.read_excel as well as many other source (csv files, parquet files). Then once you have the data in python you can  try to understand them with a neural network. The book I used to learn it was handson machine learning by Aurelien Gueron (https://github.com/ageron/handson-ml). Hope you will find this useful.

Before going to neural network you should also have a look at scikit learn a python library which is used for machine learning when you do not have enough data to train a neural network, if you are getting you data from excel files I guess you have less than 100 thousand data rows which might be a bit too few to train a neural net.


Why RMSProp converges faster than Momentum? Why is RMSProp in many cases converging faster than Momentum?

Momentum:

$$v_{dW} := \beta v_{dw} +(1-\beta)dW$$
$$W := W-\alpha v_{dw}$$

RMSProp:

$$ S_{dw} := B \cdot S_{dw} + (1-B)\cdot (dW)^2$$
  $$W := W- \alpha \frac{dW}{\sqrt{S_{dw}}}$$

Where $\alpha$ is the learning rate (0.01 etc), $\beta$ is the momentum term (0.9 etc), similar to B

From my point of view, both momentum and RMSProp have "tendency to keep moving". Well, I can see how RMSprop will naturally accelerate on flat surfaces due to

$$\frac{1}{\sqrt{S_{dw}}}$$

when $S_{dw}$ is small, but is there another benefit that RMSprop provides?

The basic intuition is that you should not have the same learning rate for different dimensions. For instance, you can have a high slope in one direction but not for another. Consequently, you should not have the same speed for the two directions. Momentum adds acceleration. Suppose gradient is your instant velocity and the average is your average velocity. Momentum is actually viscosity or somehow friction. Suppose that you are near your optimal points, your gradients become zero and you have low average which means your speed changes slowly. They have both alpha term but what is going to be used is the running average, just a kind of average which is simple to be calculated. Take a look at here and here for making an analogy.


What happens if at leaf node both classes have same number of samples? I analyzed a small dataset which had three features, so I kept max_depth of decision tree to be 3, in doing so I found it something intresting, there was a leaf node which had number of samples of both classes to be equal and decision tree choose one class, now I am intrested to know how class is decided in such scenario, is it random or some other criteria, I have attached image to explain my scenario

This is an implementation detail, and I wouldn't necessarily rely on this behavior, but presently in sklearn, it will choose the "first" class.
The predict method calls for the probability prediction, then takes the argmax, which in case of ties takes the first one:
https://github.com/scikit-learn/scikit-learn/blob/fd237278e/sklearn/tree/_classes.py#L403
https://numpy.org/doc/stable/reference/generated/numpy.argmax.html


How to mathematically explain the translational and rotational invariance of PCA There is a homework question for a course I am self studying (not a student) that is:

let our $n \times d$-dimensional data vectors be denoted by $x_1,\ldots,x_n$  and let $R$ be a $d \times d$ rotation matrix. For simplicity, you may assume that the $x_t$'s have been centered at $0$. Let $$x_t' = Rx_t + v$$
where $v$ is some fixed translation. Forming a second dataset.
Now, for any $K$ we pick, let us use PCA on each of the two data sets to obtain $K$-dimensional projections $y_1,\ldots ,y_n$ and $y_1',\ldots,y_n'$, respectively.

Write down a relationship between the two PCA projection matrices $W$ and $W'$ in
terms of the rotation matrix $R$ and the translation vector $v$. Explain mathematically how you arrived at this answer.

My answer is basically that while for the untransformed dataset we have that $W$ is the top $k$ eigenvectors of $S[\mu] (S[\mu]$ is the covariance matrix of the untransformed dataset centered at $\mu (\mu$ is $0$ for untransformed dataset)) that $W'$ will be the top $k$ eigenvectors of the matrix $(R^T) \times S[R^T(\mu-v)] R$. ($R^T$ is $R$ transposed). I did this by applying the transformation in the definition of the covariance matrix $S'$ to find a relation between $S'$ and $S$.

The goal of the problem is to show the rotational and translational invariance of PCA. Can anyone give an explanation for this?

Let $e$ be the all-one column vector of length $n$.

Let $X\in \mathbb{R}^{n\times d}$ be the original matrix.

and $X'$ be the transformed matrix, that is we have $X'=XR^T+ev^T$.

Let's first compute the transformed mean assuming the mean of $X$ is $0$.

$$\frac{e^TX'}{n}=\frac{e^TXR'}{n}+\frac{e^Tev^T}{n}=v^T$$

To compute the singular vectors of the original matrix, theoretically, we can compute the eigenvalues of $$X^TX=UDU^T$$

And we can extract $W$ from $U$.

To compute the PCA for the transformed data

We have $$(X'-ev^T)^T(X'-ev^T)=(XR^T)^T(XR^T)=R^TX^TXR^T=RUDU^TR^T$$

Now, we can extract $W'$ from $RU$.

That is $$W'=RW.$$


Why is DDPG a Policy Gradient Method? Why is DDPG a Policy Gradient Method even though it's actor does not output probability?

This is explained in the original article where deterministic policy gradient theorem was first proposed, in section 3.3:

The deterministic policy gradient theorem does not at first glance look like the stochastic version (Equation 2). However, we now show that, for a wide class of stochastic policies, including many bump functions, the deterministic policy gradient is indeed a special (limiting) case of the stochastic policy gradient. We parametrise stochastic policies $\Pi_{\mu_\Theta,\sigma}$ by a deterministic policy $\mu_\Theta : S \rightarrow A$ and a variance parameter $\sigma$, such that for $\sigma = 0$ the stochastic policy is equivalent to the deterministic policy, $\Pi_{\mu_\Theta,0} \equiv \mu_\Theta$.
Then we show that as $\sigma \rightarrow 0$ the stochastic policy gradient converges to the deterministic gradient (see Appendix
C for proof and technical conditions).

Therefore, the Deterministic Policy Gradient method is equivalent to a normal (stochastic) Policy Gradient Method in the limit.


Feature Selection with one-hot-encoded categorical data I have a dataset with 400+ columns. Almost 90% of these are categorical data with One-Hot-Encoding (OHE). I'm using the dataset for a classification problem.

My professors asked me to perform feature selection using sequential forward selection (mlxtend).

Is there really a point of doing this since it is also very time consuming? Is it logical to remove categorical data? If so, what would be the k_features number for sfs that I should use? Or is the method(sfs) even suited for this?

Sequential forward selection appears to be a greedy search algorithm if I am not mistaken? It appears you initially fit all possible one variable models, and then choose the one variable model that is highest performing. You then attempt to add a second variable to the highest performing one variable model from the prior step, by fitting all possible two variable models. You choose the highest performing two variable model, and if it is superior to the best one variable model, you move to the two variable model. The process continues, trying all three variable models, then four (if the best three variable model is superior to the best two)... until you choose the best k features or until the model stops improving.

The best k seems quite arbitrary and if there is a way to let the algorithm continue until there is no improvement then that is what I would do. However, here are some initial impressions of this approach that I have:

I suppose that a problem with this is that you have a large chance of not finding the most optimal model (local solution). A candidate variable may work particularly well with other variables that have yet to be included in future steps, or you may not even get the chance to use potentially useful predictors if the algorithm terminates beforehand.

However, the largest problem with this method as I see it is just how expensive it is. You state that you have 400+ columns. Say you have 415 variables. Then, in the first pass you will have to fit 415 models, then on the second pass, 414, etc...and this is not even including possible hyperparameter tuning and cross validation. This is a huge amount of models, and to be honest this is often the problem with wrapper based methods of feature selection in particular. The majority of them end up fitting a large amount of models for potentially marginal, if any, gain in model performance. The problem is made worse when you have hyperparameters that need to be tuned, and so I find the tradeoff between performance gains and the computational time spent using these methods to not be worthwhile, unless you want the absolute best model.

In your case, Peter's advice in the comments is the route I would take purely because it is way faster and often good enough. Almost every modern ML method offers regularization in some way that will explicity feature select for you aka embedded feature selection (by not using unhelpful predictors at all) or by strongly limiting their influence via. shrinkage. Ridge/LASSO/Elastic Net mentioned by Peter is a terrific suggestion. Other methods such as those based on trees also have embedded feature selection and may work well in your case, considering the dimension of your dataset.


precision recall curve is like stairs I am training an ensemble model using a 400 data set sample this led to a precision recall curve that looks like stairs ? what would be the reason beside the low number of samples ?

A precision-recall curve (as well as a ROC curve) is made from the different thresholds which can be used to separate predictions between positive and negative instances. These thresholds naturally depend on the possible values predicted by the classifier.
For example, if the classifier predicts only 3 different values $a<b<c$, then the threshold $t$ can only be in these positions:

$t<a$
$a<t<b$
$b<t<c$
$c<t$

Each of these positions will correspond to one pair of values for precision and recall, so exactly one point in the curve. Therefore the shape of the curve (how smooth it is) depends on how many different values the classifier predicts.
Thus a stairs-shaped curve shows that the classifier predicts only a few different values. There can be various reasons for that, but since you mention ensemble learning I guess that the final predicted value is based on the proportion of individual learners predicting the positive class, right? If yes that would explain it: if there are $N$ individual learners then the final predicted value can only be $0,1/N,2/N,...,N$. This means a maximum of $N+1$ different values and therefore a maximum of $N+1$ points on the curve.


Building a content-based music recommendation system I am trying to build a recommendation system in Python that recommends songs based on a playlist.

What I have is two datasets:
1. One dataset consists of 350 songs from my playlist and 13 acoustic features for each one like timber, energy, key, tempo, etc. that I extracted using the Spotify API
2. The other dataset consists of 340 000 songs and their acoustic features (got it from here: https://components.one/datasets/billboard-200/)
I've gotten both datasets in the same format and ready to be worked with.

Now what I am trying to do in Python is use my first dataset to get 30-40 songs with similar acoustic features from the second dataset but I have no idea how to approach this.
Should I use some machine-learning models or do something entirely else?
I thought about comparing the songs from my first dataset to the songs on my second dataset and pulling the ones with a similarity score of let's say over 70% or something like that but I feel like there is probably a much better way of doing this.

If the features are identical, good start would be to use n-neighbors approach.

It would be something like that

from sklearn.neighbors import NearestNeighbors
all_songs_features = [[0, 0, 2], [1, 0, 0], [0, 0, 1], [100, 100, 100], [0, 0, 1.5]]
neigh = NearestNeighbors()
neigh.fit(all_songs_features)

my_song_features = [[0, 0, 1.3], [1.1, 0, 0]]
print(neigh.kneighbors(my_song_features, 2, return_distance=False))

These code returns the indexes of similar songs from the bigger dataset.

Note, if the features are with different range (i.e. one with values 0-1 and another 10000-1000000), then you need to scale them, for example with StandardScaler


why R-square always keep increasing I have read in multiple articles that R-square always increases with the number of features, even though a feature may not be of any significance.
The formula for R-square is
$$1 - \frac{\sum(y-\hat{y})^2}{\sum(y-\bar{y}^2)}$$
If the denominator is constant that means R-square is dependent upon only numerator, so basically on $\hat{y}$.
$$\hat{y} = a +b_1x_1 +b_2x_2 \ldots$$
Now if I have a new feature which is really not important shouldn't the beta coefficient of that feature be zero? And if it is really zero how will it really impact R-square?

Linear regression has no way of knowing if features have significance or not. It will find the βs that produce the smallest squared error. That will usually not be zero even if the data is just noise. More features, regardless of significance, give more ways of describing the target variable and get a lower error.
Here is an example where you can see that this holds true even if all features have no significance at all:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import numpy as np
import matplotlib.pyplot as plt

# generate data that is just noise
X = np.random.randn(100, 100)
y = np.random.randn(100, 1)

r2_scores = []

# fit 1-100 features on noise and calc r2
for i in range(1, X.shape[1]+1):
    x = X[:,-i:]
    lr = LinearRegression()
    lr.fit(x.reshape(100, -1), y)
    coef_sum = lr.coef_.sum()
    r2 = r2_score(y, lr.predict(x.reshape(100, -1)))
    coefs.append(lr.coef_)
    r2_scores.append(r2)

plt.plot(r2_scores)
plt.xlabel('Number of features')
plt.ylabel('r2 score')

Which will give you something like:


What exactly is bootstrapping in reinforcement learning? Apparently, in reinforcement learning, temporal-difference (TD) method is a bootstrapping method. On the other hand, Monte Carlo methods are not bootstrapping methods.

What exactly is bootstrapping in RL? What is a bootstrapping method in RL?

Bootstrapping in RL can be read as "using one or more estimated values in the update step for the same kind of estimated value".

In most TD update rules, you will see something like this SARSA(0) update:

$$Q(s,a) \leftarrow Q(s,a) + \alpha(R_{t+1} + \gamma Q(s',a') - Q(s,a))$$

The value $R_{t+1} + \gamma Q(s',a')$ is an estimate for the true value of $Q(s,a)$, and also called the TD target. It is a bootstrap method because we are in part using a Q value to update another Q value. There is a small amount of real observed data in the form of $R_{t+1}$, the immediate reward for the step, and also in the state transition $s \rightarrow s'$.

Contrast with Monte Carlo where the equivalent update rule might be:

$$Q(s,a) \leftarrow Q(s,a) + \alpha(G_{t} - Q(s,a))$$

Where $G_{t}$ was the total discounted reward  at time $t$, assuming in this update, that it started in state $s$, taking action $a$, then followed the current policy until the end of the episode. Technically, $G_t = \sum_{k=0}^{T-t-1} \gamma^k R_{t+k+1}$ where $T$ is the time step for the terminal reward and state. Notably, this target value does not use any existing estimates (from other Q values) at all, it only uses a set of observations (i.e., rewards) from the environment. As such, it is guaranteed to be unbiased estimate of the true value of $Q(s,a)$, as it is technically a sample of $Q(s,a)$.

The main disadvantage of bootstrapping is that it is biased towards whatever your starting values of $Q(s',a')$ (or $V(s')$) are. Those are are most likely wrong, and the update system can be unstable as a whole because of too much self-reference and not enough real data - this is a problem with off-policy learning (e.g. Q-learning) using neural networks.

Without bootstrapping, using longer trajectories, there is often high variance instead, which, in practice, means you need more samples before the estimates converge. So, despite the problems with bootstrapping, if it can be made to work, it may learn significantly faster, and is often preferred over Monte Carlo approaches.

You can compromise between Monte Carlo sample based methods and single-step TD methods that bootstrap by using a mix of results from different length trajectories. This is called TD($\lambda$) learning, and there are a variety of specific methods such as SARSA($\lambda$) or Q($\lambda$).


How to find farthest data points from a predefined cluster in a data set with Python? I have a data set where certain rows are labeled as one class (and interpreted as distinct cluster #1 as such), but other points are either unlabeled or ambiguous. Hence I want to figure out which unlabeled data points lie farthest from cluster #1 by sorting them by their respective distance from cluster #1 (more precisely, from the closest point of cluster #1 to the respective unlabeled points).
My first idea would to create a similarity matrix between and calculate the closest distances per unlabeled points from this, but somehow this seems a but clumsy, is there a more elegant/effective way?
(I used to use sklearn for similar tasks, but as far as I know, unsupervised clustering algos don't explicitly provide this kind of specific information.)

You want to know the nearest neighbor of you unlabeled data in you labeled cluster.
Using sklearn, you can fit a NearestNeighbors() class with a giving metric, algorithm (Ball-tree, KD-tree,...) and all other parameters (see here).
Then get the labeled nearest neighbor from your unlabeled datapoint and its distance by using kneighbors() method.
Here is a sample code:
import numpy as np
from sklearn.neighbors import NearestNeighbors

# Fake data
labeled_samples = [[0, 1.2], [0, 1.3], [0, 1.4]]
unlabeled_samples = [[0, 1.7], [0.5, 0.5], [1, 1]]

# Create your class with your labeled cluster
neigh = NearestNeighbors(n_neighbors=1)
neigh.fit(labeled_samples)

# get the distance/index to the nearest neighbor of you unlabeled data
distances, indexes = neigh.kneighbors(unlabeled_samples, 1, return_distance=True)

Then you just have to sort the result.
Note: using this approach is more optimized than computing all distances from all labeled datapoints and then sort them. See this note for more info.


Is there an easy to use .net library for neural networks? I know 'easy to use' is going to be subjective, so let me qualify the question a little. Is there a library or working scrap of code that I can essentially copy into my project, change the number of neurons in each layer, the number of layers, and the source of the inputs and then click run. I've written at least 15 of these myself, not a single one has worked properly - I need to see something working in a language I understand so I can get to grips with why I'm apparently a moron!

(I can post some of my own code if anyone is interested, but it's lengthy and not too related to the question)

I have no personal experience with it, but I came across Accord, which has limited support for neural networks. Honestly, .net does not have a machine learning community, so you might be better off using something else if you do not want to DIY.


What is the quantity sold for a specific fruit & country combination? What is the algorithm that generates these potential quantities that meet the given criteria?

Essentially - there are number of quantities for a fruit and country combination. E.g:

    Country+Fruit   Potential Quantity
1   India+Apple     25
2   India+Apple     27
3   India+Banana    35
4   India+Banana    37
5   France+Apple    130
6   France+Apple    132
7   France+Banana   11
8   France+Banana   13
9   France+Banana   15
10  France+Cherry   88

For complete dataset click here.

Each Country has to be as close as possible to the following values for all fruits sold:

   Country                 Total Fruits Sold
1. Total India Fruits        1403
2. Total China Fruits        1370
3. Total England Fruits      1115
4. Total France Fruits       1169
5. Total Germany Fruits      1470

And total fruit quantity across all countries has to be as close as possible to the following values:

   Fruits           Total Fruits Sold Across All Countries:
1. Total Apples        508
2. Total Bananas       253
3. Total Cherries      982
4. Total Guavas        389
5. Total Kiwis         681
6. Total Oranges       489
7. Total Mangos        608
8. Total Strawberries   1060

An example combination that comes close to meeting the above criteria (having 5 countries and 8 fruits) is:

    Country Fruits      Matched Combination
1.  India Mango         120
2.  India Apple         40
3.  Germany Apple       20
4.  Germany Mango       80
.
.
40. France Mango        186

What is the algorithm that generates these potential quantities that meet this criterion?

Problem 1: do we have to do a brute force method of generating all possible combinations or is there a more efficient way?

Problem 2: how do we define "closeness" - the exact match is the best - what if there is no exact match - then what is the next best option?

Minimize

If I understand your question correctly, you want to solve an optimization problem. Using two countries and two fruits as an example, the problem can be written as a linear program:

Minimize

$$\Delta = \delta_{Apple} + \delta_{Mango} + \delta_{India} + \delta_{China}$$

s.t.

$$X_{India, Apple} + X_{China, Apple} = 508 + \delta_{Apple}$$
$$X_{India, Mango} + X_{China, Mango} = 608 + \delta_{Mango}$$
$$X_{India, Apple} + X_{India, Mango} = 1403 + \delta_{India}$$
$$X_{China, Apple} + X_{China, Mango} = 1370 + \delta_{China}$$

A simple optimization problem like this can be solved using many software packages, including Excel. Note that it is likely that the solution is not unique.


training gradient boosting algorithm in python testing in Golang What are the best strategy to train and save a gradient boosting algorithm, e.g. LightGBM or XGboost or Catboost in Python but load the model in GoLang and make prediction with Golang ?

There's actually a few libraries that handle the inference part well.  https://github.com/dmitryikh/leaves is probably the most common one and seems to fit your need.


Using embedding layer output as input to .fit() call in Keras I want to build a classifier in Keras that predicts the next item bought by a customer (i.e. multiclass classification). One of the features I intend to input to the model will be the last item bought by a particular customer. My problem is that the list of possible items is extremely large, several tens of thousands. With that in mind, I'm going to feed the ItemID feature into an Embedding Layer in Keras, and concatenate that with other features before running it through the model.

My question is, can I use the Keras shared layer functionality to also embed the labels for the training data (since they are all from the same vocabulary as that embedded ItemID input), such that instead of having a softmax output of n_classes I have an output of n_dimensions, where n_dimensions is however many dims I decided to set that Embedding layer too?

I got it. You define a new model, which has an input, the shared embedding layer and a flattened output. Pass the output of .predict() from that model to y parameter of your main model's .fit() call, in like fashion:

NUMERIC_FEATURES = [
    # Define the subset of features that need passing to the numeric input layer
]

vocab_size = 10000 # number of items
n_dimensions = 32 # dimensions to embed down to

# Shared layer for Item ID embeddings.
itemEmbedding = Embedding(vocab_size, n_dimensions, name='Item-Embedding')

# "Main" model definition, which has ItemID feature plus a bunch of
# numeric features to input
ii = Input(shape=(1,), name='Item-Input')
ie = itemEmbedding(ii)
if = Flatten()(ie)

ni = Input(shape=(7,), name='Numeric-Inputs')

c = Concatenate()([ni, if])

d1 = Dense(512, activation='relu')(c)
d2 = Dense(256, activation='relu')(d1)
d3 = Dense(128, activation='relu')(d2)
o = Dense(n_dimensions, activation='relu')(d3)

model = Model(inputs=[ni, ii], output=o)
# Don't take my word for the loss here, this is a toy example of code that works but is not
# intended to be completely "correct"
model.compile(optimizer='adam', loss='mse')

# "Labels" model definition, which embeds the labels using the shared
# embedding layer
li = Input(shape=(1,), name='Label-Input')
le = itemEmbedding(li)
lf = Flatten(le)

labelModel = Model(inputs=li, output=lf)

# Train model, using output of labelModel.predict() as the y parameter
model.fit([df[NUMERIC_FEATURES], df.ItemID], labelModel.predict(df.ItemID))

That's how it's done, whether it's useful, I have not yet evaluated.


Confidence value for face recognition In the context of face recognition I have the following histogram:

blue bins count the comparison distances for "self matches" (comparing two images of the same person). Orange bins count the distances for cross matches (different persons).

The distance is the value returned by the neural networks as the result of a comparison between two faces, how much two "faces vectors" (embeddings) differ.

I'm looking for a function that, given a distance, can tell how likely it is for two pictures to be from the same person.

This function should be look like this red line (with a different y-scale):

So with distance 0.5 it is extremely likely, with distance 1.4 it is close to a 50% chance.

Is there such a function? How is it called?

It is similar to this question but also very different. In my case a very small z-value (distance) still means high confidence even if it is many standard deviations away from the mean. The same is true for a value like 1.0.

This question is very similar too but I'd like to extract the distance to probability mapping function from actual measurements, on a reference dataset, and not from the distance by itself. So this should not depend on the loss or distance definition but on the data distribution alone.

If I had to calculate such a function, I would:

Calculate the probability (not Z-score) for $x$ in a two-tailed test (https://en.wikipedia.org/wiki/One-_and_two-tailed_tests) for the probability that $x$ belongs to the distribution for both distributions. Then you have $p_s(x)$ and $p_c(x)$ for self-match/cross-match. Don't use probability $0.1\%$ to reject, but to compare.

Then calculate $$\frac{p_s(x)}{p_s(x) + p_c(x)}$$

Which tends to $1$ if $p_s(x)$ is the larger term, is $0.5$ if they are the same, and tends to $0$ if $p_c(x)$ is the larger term.

At the far tails, $p_s(x)$ and $p_c(x)$ both tend to zero, but the order of magnitude will select the good probability.


Connection between cross entropy and likelihood for multi-class soft label classification The connection between cross entropy and log likelihood is widely expressed for the case when sample multi-class labels are one hot binary vectors (basically the same). Cross entropy is defined when labels are not one-hot but any valid probability distribution.

What is the relation between the general cross entropy and log-likelihood?

One-hot case

Both log likelihood and cross entropy come down to:

$-\sum\limits_{i}\sum\limits_k1\{{y_i^{(k)}=1\}}{\log{\hat{y}_i^{(k)}}}(\theta)$

When:

$i$ - the sample index
$k$ - the class index
$\theta$ - model parameters
$y$ - samples labels (one-hot vector)
$\hat{y}$ - predictions (probability distribution like vector)
$1\{\}$ - indicator function

General case ($y_i$ is probability distribution)

Cross entropy can be reduced to:
$-\sum\limits_{i}y_i\log\hat{y}_i(\theta)$

How does this relate to log-likelihood?

If I understand it correctly, you are asking how log likelihood in a multi-class classification problem relates to the cross entropy loss. So here is my try:

Assuming we have a multi class classification problem ($C$ different classes) where we estimated the conditional probabilities for each class given the data $x$ (e.g., using a neural network) and where the classes are one-hot encoded:
$$
\hat{p}_i^{(k)} \in [0, 1] \quad \text{denotes the estimated probability of the } i\text{th sample for the } k\text{th class}\\
p_i^{(k)}\in \{0, 1\}\quad \text{denotes the true probability of the } i\text{th sample for the } k\text{th class}\\
y_i^{(k)} = p_i^{(k)} = 1_{y_{i}^{(k)}==1} \quad \text{hard labels, i.e.,} \begin{cases} 1 & \text{if }i \text{th sample belongs to } k\text{th class},\\ 0 &\text{else.}\end{cases}
$$
Note that $\hat{p}_i^{(k)} = \hat{p}_i^{(k)} (\textbf{x}_i, \theta)$ is actually a function that depends on parameters $\theta$ (e.g., network weights) and the input data $\textbf{x}_i$. In the end, we want to optimize the parameters $\theta$. For the sake of simplicity, we just write $\hat{p}_i^{(k)}$ (silently knowing that these estimated probabilities come from some kind of function approximator).

The idea of maximum likelihood is to maximize the likelihood of our estimated conditional probabilities given the data, i.e.,
$$
\large{
\max P(Y|X) = \max \left( \prod_{i=1}^N P(y_i| x_i) \right) =\max \left( \prod_{i=1}^N \prod_{k=1}^C  \hspace{0.2cm} \left(\hat{p}_i^{(k)} \right)^{1_{y_i^{(k)} == 1}} \right)}
$$
Let's take a moment to digest this formula. It's actually pretty easy what we are doing here, we want that the estimated conditional probabilities $\hat{p}_i^{(k)} \in [0, 1]$ correspond to the true labels $p_i^{(k)}\in \{0, 1\}$. Note that the true labels are hard labels, e.g., $\textbf{p}_0 = \begin{bmatrix}0 &1&0 \end{bmatrix}^{\text{T}}$, but the predictions are (probably) soft labels, e.g., $\hat{\textbf{p}}_0 = \begin{bmatrix}0.2 &0.7&0.1 \end{bmatrix}^{\text{T}}$. We optimize our likelihood by maximizing the predicted probability of the true label, i.e., where the label $y_i$ corresponds to the class $k$, this is denoted by the indicator function $1_{y_i^{(k)} == 1}$

In the extreme case where our conditional probabilities always predict the true class with probability 1, this yields the maximum possible value for our likelihood:
$$
\max P(Y|X) = 1
$$

By definition of the hard labels, we can rewrite the formula of the likelihood into
$$
P(Y|X) = \prod_{i=1}^N \prod _{k=1}^C {\left(\hat{p}_i^{(k)}\right)}^{p_i^{(k)}}
$$

Now we take the negative log-likelihood (hence the maximization problem becomes a minimization problem):
$$
\max P(Y|X)  \equiv \min \left(- \log P(Y|X)\right) = \min \left(- \sum_{i=1}^N \sum_{k=1}^C p_i^{(k)} \cdot \log \left(\hat{p}_i^{(k)}\right) \right)
$$
(This formula is the same as in your question).

Let's suppose, there is only one datapoint $N=1$, then we get
$$
- \log P(Y|X) = - \sum_{k=1}^C p^{(k)} \cdot \log \left(\hat{p}^{(k)}\right) = H(p, \hat{p})
$$
This is known as the cross-entropy loss, i.e., minimization of the cross-entropy loss corresponds to maximum likelihood when hard labels are provided.


Error in Convolutional Neural network for input shape I have 1000, 28*28 resolution images. I converted those 1000 images into numpy array and formed a new array with size of (1000,28,28). So, while
 creating convolution layer using Keras, input shape(X value) is specified as (1000,28,28) and output shape(Y value) as (1000,10). Because I have 1000 examples as inputs and 10 categories of output.

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=(1000,28,28)))
.
.
.
model.fit(train_x,train_y,batch_size=32,epochs=10,verbose=1)

So, while using fit function, it shows ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1000, 28, 28) as an error. How can I provide proper input and output dimension for CNN.

Code:

 model = Sequential()
 model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=(4132,28,28)))
 model.add(MaxPooling2D((2, 2)))
 model.add(Dropout(0.25))

 model.add(Conv2D(64, (3, 3), activation='relu'))
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))

 model.add(Conv2D(128, (3, 3), activation='relu'))
 model.add(Dropout(0.4))

 model.add(Flatten())
 model.add(Dense(128, activation='relu'))
 model.add(Dropout(0.3))
 model.add(Dense(10, activation='softmax'))

 model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
 model.summary()

 train_x  = numpy.array([train_x])

 model.fit(train_x,train_y,batch_size=32,epochs=10,verbose=1)

The convolution operation used in CNNs are batch operations. The customary inputs for these networks, at least for Keras and Tensorflow, are channels last and channels first. The first dimension specifies the number of data in your batch, the second and third can be height and width if you choose channels-last and the last one will be the number of channels of your input, for instance the number of channels for grey-scaled images is one whilst for coloured images is three. If you use channels first, the first dimension would be the number of data in your batch, the second would be the number of channels and finally, the third and fourth dimensions would specify the number of height and width respectively.

For your case, it seems you are using MNIST data-set. What you need to do is just reshaping your input images to one of the standard mentioned manners. You can resize your input to (1000,28,28, 1).


What is cohen kappa metric, implementation in Python? Can somebody explain indetail explanation on Quadratic Kappa Metric/cohen kappa metric with implementation in Python

Quadratic Kappa Metric is the same as cohen kappa metric in Sci-kit learn @ sklearn.metrics.cohen_kappa_score when weights are set to 'Quadratic'.

quadratic weighted kappa, which measures the agreement between two ratings. This metric typically varies from 0 (random agreement between raters) to 1 (complete agreement between raters). In the event that there is less agreement between the raters than expected by chance, the metric may go below 0. The quadratic weighted kappa is calculated between the scores which are expected/known and the predicted scores.

Results have 5 possible ratings, 0,1,2,3,4. The quadratic weighted kappa is calculated as follows. First, an N x N histogram matrix O is constructed, such that Oi,j corresponds to the number of adoption records that have a rating of i (actual) and received a predicted rating j. An N-by-N matrix of weights, w, is calculated based on the difference between actual and predicted rating scores.

An N-by-N histogram matrix of expected ratings, E, is calculated, assuming that there is no correlation between rating scores. This is calculated as the outer product between the actual rating's histogram vector of ratings and the predicted rating's histogram vector of ratings, normalized such that E and O have the same sum.

From these three matrices, the quadratic weighted kappa is calculated.

Code implementation in Python

Breaking down the formula into parts

5 step breakdown for Weighted Kappa Metric

First, create a multi-class confusion matrix O between predicted and actual ratings.
Second, construct a weight matrix w which calculates the weight between the actual and predicted ratings.
Third, calculate value_counts() for each rating in preds and actuals.
Fourth, calculate E, which is the outer product of two value_count vectors
Fifth, normalize the E and O matrix

Calculate weighted kappa as per formula

Each Step Explained

Step-1: Under Step-1, we shall be calculating a confusion_matrix between the Predicted and Actual values. Here is a great resource to know more about confusion_matrix.

Step-2: Under Step-2, under step-2 each element is weighted. Predictions that are further away from actuals are marked harshly than predictions that are closer to actuals. We will have a less score if our prediction is 5 and actual is 3 as compared to a prediction of 4 in the same case.

Step-3: We create two vectors, one for preds and one for actuals, which tells us how many values of each rating exist in both vectors.

Step-4: E is the Expected Matrix which is the outer product of the two vectors calculated in step-3.

Step-5: Normalise both matrices to have the same sum. Since it is easiest to get the sum to be '1', we will simply divide each matrix by its sum to normalize the data.

Step-6: Calculated numerator and denominator of Weighted Kappa and return the Weighted Kappa metric as 1-(num/den)

More Info


How does Gradient Descent and Backpropagation work together? Please forgive me as I am new to this. I have attached a diagram trying to model my understanding of neural network and Back-propagation? From videos on Coursera and resources online I formed the following understanding of how neural network works:

Input is given, which gets weight assigned to it using a probability distribution.
The activation functions use the weights to provide the predicted value.
The cost or loss functions calculate the error of the prediction between the actual class and the predicted value.
The optimization functions such as gradient descent use the results of the cost function to minimize the error.

If the above is correct then I am struggling to understand the connection between Gradient Descent and Backpropagation?

Here is an image of my understanding so far:

First, remember that the derivative of a function gives the direction in which the function increases, and its negative, the direction in which the function decreases.

Training a model is just minimising the loss function, and to minimise you want to move in the negative direction of the derivative. Back-propagation is the process of calculating the derivatives and gradient descent is the process of descending through the gradient, i.e. adjusting the parameters of the model to go down through the loss function.

Back-propagation is called like this because to calculate the derivative you use the chain rule from the last layer (which is the one directly connected to the loss function, as it is the one that provides the prediction) to the first layer, which is the one that takes the input data. You are "moving from back to front".

In gradient descent one is trying to reach the minimum of the loss function with respect to the parameters using the derivatives calculated in the back-propagation. The easiest way would be to adjust the parameters by substracting its corresponding derivative multiplied by a learning rate, which regulates how much you want to move in the gradient direction. But there are some more advanced algorithms like for example ADAM.


Can i expect good results having low correlation attributes? This was a question i saw in an interview for a data scientist position:
"Here is the following correlation heatmap that i got from my attributes. Regarding the correlation of each feature with the dependent variable (target/class), it is noticeable that correlations are not very expressive.

Yet, i would like to know if can i expect good results from a classification model using this dataset. Also, what further investigation can i do (if i shouldn't look after correlation only)?"

The correlation does not effect your model using decision trees in a classification problem.
In the theory of decision tree models, you don`t need correlation or check of multicollinearity. Because the split in decision trees is made of entropy/information gain.
The correlation does only check linear dependencies. The same is, when the dataset is highly correlated. You will get very good results with decision trees, because there you don´t need to delete correlated features or do dimension reduction (if you don´t have to).
It can be, that you don´t get very good results, when you use linear structured models like multiclass neural network, or multiclass logistic regression. There you will see that dimension reduction etc. can have a high influence on the accuracy in these models.
I had a similar question but with highly correlated features:
decision -tree regression to avoid multicollinearity for regression model?
In your case I would say, if we use decision trees, it is not noticeable. However we should check this with the permutation importance of the features and check the polynomial dependencies. Of course you should ask the interviewer more question about his questions and the target of his question, to get more background information. This is very important in interviews.