How to approach different image resolutions in deep learning for regression problem?

In a fully convolutional neural network, i.e., a neural network containing only convolutional and pooling layers, you can get away with input images of different sizes. but that would only work if you didn't need the output of your network to be of a fixed size. for example for segmentation tasks, because the output is the same size as the input, you can actually design a fully convolutional network where inputs are images of different resolution. This is basically one of the ideas behind FCN.
Aside from that in classification or regression, because your output is a certain fixed size, you have to fix each layers size and this also means that the input must have a certain size. Plus in classification or regression tasks, fully connected layers are used as the last layers of the network, thus because of this reason too, you have to have fixed size inputs.
Long story short, in your case, there is nothing you can do architecture-wise to get away with this. So you have to handle it in your sample data.
What is usually done, is that you transform your dataset and zero-pad all the images to a fixed resolution and then use it.
Don't scale your images to a certain resolution as convolutional layers are not scale invariant and this will mess up your test phase.


Skewed Data doesnt follow normal distribution

To address skewed data you can do data transformation like logarithmic transformation, squared transformation etc.

Alternately you could try non-parametric machine learning algorithms that does not have any assumption of normality of x variables.

Hope this will help....


Data visualization for pattern analysis (language-independent, but R preferred)

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

import os, numpy, matplotlib.pyplot as plt

%matplotlib inline

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

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

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

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

This is what a PDF looks like:

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


Binary Classification - One Hot Encoding preventing me using Test Set

You can use handle_unknown parameter of sklearn while encoding training data.

sklearn.preprocessing.OneHotEncoder(handle_unknown='ignore')

When this parameter is set to ‘ignore’ and an unknown category is encountered during transform, the resulting one-hot encoded columns for this feature will be all zeros.

Note : I assumed you are using scikit-learn.

Source : sklearn.preprocessing


Here's an approach which we can try and probably work for less complex problems.

A large corpus of data: We will require a very large corpus of textual data. It should contain as much as words possible. Natural language phrases and collocations are also required.

Now, we encode all the sentences in the corpus using Word2Vec.

After that, we will train a text generation model which given a word predicts the next word.

If we take in consideration the above example, the word "card" is colloquial with the word "credit". This relation will be learnt my the model.

  The model will behave like, Which is the most frequent word after "credit"? It isn't "cart".


How to create multi-hot encoding from a list column in dataframe?

You can try using sklearn's MultiLabelBinarizer (https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MultiLabelBinarizer.html):
mlb = MultiLabelBinarizer()
mlb.fit(d['IDs'])

new_col_names = ["ID_%s" % c for c in mlb.classes_]

# Create new DataFrame with transformed/one-hot encoded IDs
ids = pd.DataFrame(mlb.fit_transform(d['IDs']), columns=new_col_names)

# Concat with original `Label` column
pd.concat( [d[['Label']], ids], axis=1 )


Monte Carlo Dropout as Uncertainty predection

Monte Carlo Dropouts (MCDO) is used during the prediction / inference phase to provide an estimate of uncertainty for the model's predictions.
Regular dropout during the training phase is a regularization technique.


Nearest Neighbors on mixed data types in high dimensions

Conceptually, there's no reason why this 'could' not work, but practically speaking, there are probably better approaches than using KNN.

Suitability of KNN

One immediate problem with KNN is that each new instance must be compared to each existing reference instance before a class can be determined. As the number of reference instances increases, the time to classify a new instance increases.  With highly dimensional data, you're going to need a lot of reference instances.

Dimensionality of the Data

If you are set on using KNN, you will want to do everything you can to reduce the number of dimensions, particularly on the categorical columns.  In your example, you may be better off reducing the animals down to some larger groups, for example: [Pet, Domesticated, Wild] or biological family.  If not, once you encode the categorical values, you will maybe have thousands (to millions) of animal columns alone.  If you don't have many examples of each, you're going to match on everything but the animal because
$$Distance\ between:\ dog\ and\ cat = 1$$
$$Distance\ between:\ dog\ and\ giraffe = 1$$
So, this will mean other attributes in the data will likely be more similar, yet, they may be uninformative to the classification process.  For example, number of legs.

On the positive side though, KNN may deal with the introduction of new attributes a little better than others because the classification is based on the currently available data, so adding a new column is not as big a deal as say adding a new column to a neural network, which will need to be retrained to be able to accept a new data set.

Conclusion

While there are other pros and cons to KNN and ways to optimize it for performance; based on your description of the data, and perhaps making a few assumptions, KNN does not sound like a good fit for your classification problem.

Most models are going to perform better if you can reduce the number of dimensions, but if the number and nature of attributes is relatively static, I would start with something like a Decision Tree.  The benefit of starting with a decision tree is that you can get a feel for the importance of the columns to the class assigned.


The a second bullet is the value in feature hashing.  Hashing and one hot encoding to sparse data saves space.  Depending on the hash algo you can have varying degrees of collisions which acts as a kind of dimensionality reduction.

Also, in the specific case of Kaggle feature hashing and one hot encoding help with feature expansion/engineering by taking all possible tuples (usually just second order but sometimes third) of features that are then hashed with collisions that explicitly create interactions that are often predictive whereas the individual features are not.

In most cases this technique combined with feature selection and elastic net regularization in LR acts very similar to a one hidden layer NN so it performs quite well in competitions.


You need to know what the outcome should be of a given test on a dataset before you try to test a new method on them. Ask yourself, 'What do I expect from this?'

Linear SVM finds a plane to cut through the data to best represent the difference between two sets.

If you have a look at what you are separating (Iris_setosa from Iris_virginica and iris_versicolor), you'll find that the clumps themselves are perfectly separated. You can draw a line easily on each graph you care to use, and that is what I have done in the picture below. If the clumps are perfectly separated, then the SVM will return a perfectly separated result.

By Nicoguaro - Own work, CC BY 4.0, https://commons.wikimedia.org/w/index.php?curid=46257808

Test the SVM on separating virginica and versicolor to see how it does in a more difficult context. Or alternatively, just generate a dataset of your own from randomly placed gaussian points.


Which algorithm or tool to use to classify as good or bad?

Based on the comments I'll try to answer. I guess you don't have the corresponding labeles. What you can do as a solution is that you can use k-means algorithm as the easiest start point to and specify the hyper parameter, k, to two. Then you can find two clusters and you yourself can evaluate the results. As another approach, you can increase the size of k and again evaluate your answers. You can also use Gaussian Mixture Models for finding better non-convex clusters which have better results. The point is that you have to evaluate the results as an expert and label them manually. This task accelerates the labeling process. After that you can employ a simple MLP for finding a descriminative model.


How to evaluate distance in k-means clusters?

There are several important points to keep in mind in considering your questions:

You should always normalize or standardize your data before applying k-means clustering. This is true of most other clustering algorithms also. If you are clustering in more than one dimension, the distance metric is meaningless unless each dimension has the same weight, so normalization is essential.  Imagine clustering people by body weight and income. Without normalization the results will depend on whether you think in pounds and dollars, kilograms and pesos, or moles and euros. The lack of normalization introduces non-determinism.
Strictly speaking, the stability of the k-means algorithm has been shown for Euclidean distance metrics and there is no assurance of convergence with other distance metrics.  More practically, most sensible metrics attain convergence and its not much of an issue, but its worth putting that warning out there.
k-means isn't a clustering algorithm that readily lends itself to statistical analysis within the cluster. Every point in the space is a member of one of the k clusters regardless of how much of an outlier the point is. There are other clustering methods that are more adept at finding and ignoring outliers.  DBSCAN is one such algorithm that is very good and finding clusters and ignoring noise.

Now, answering your questions:

Is it relevant to center / scale euclidean distance on each cluster ? (and then consider outliers as the ones with the highest scaled distance)

Yes, you can certainly do this. Combining k-means with outlier detection is certainly possible but is probably not the most elegant or efficient algorithm.  It kind of sounds like poor-mans's DBSCAN.  Euclidean distance works fine, but just do a second set of normalizations using the centroids and the standard deviation of the cluster.

Are there other kind of distance to consider?

There are lots of other metrics that are useful for many different reasons.  As stated, the k-means convergence proofs hold only for Euclidean distance. For outlier detection Euclidean seems best, but there may be cases where Cosine Similarity metrics could be useful. People may suggest L1 (Manhattan) distance metrics, but I find this is only useful when there is significant linear dependence in your data, which can be remedied with dimensionality reduction.

Short answer:

Give it a try as Euclidean should work fine, but also take a look at clustering via DBSCAN, which has outlier detection built into it.

Hope this helps!


ManufMktConc <- read.csv("Downloads/Manufacturing.csv",
                         stringsAsFactors = FALSE)

ManufMktConc %<>%
  tbl_df %>%
  slice(-1) %>%
  filter(CONCENFI.display.label!="All companies") %>%
  select(-GEO.id,-GEO.id2,-GEO.display.label,-COMPANY,-RCPTOT,-YEAR.id)

##  use melt and cast from reshape2
require(reshape2)

# melt makes the data set tall and thin using id variables and measure variables
#
ManufMktConc_molten <- as.data.frame(ManufMktConc) %>%
  melt(id.vars=1:4, measure.vars=5:6) %>%
  filter(value!="X")
ManufMktConc_molten[1:5,]

    NAICS.id       NAICS.display.label CONCENFI.id CONCENFI.display.label  variable value
1      311        Food manufacturing         856    4 largest companies CCORCPPCT  16.3
2      311        Food manufacturing         857    8 largest companies CCORCPPCT  24.2
3      311        Food manufacturing         858   20 largest companies CCORCPPCT  38.4
4      311        Food manufacturing         859   50 largest companies CCORCPPCT  50.9
5     3111 Animal food manufacturing         856    4 largest companies CCORCPPCT  30.2

# make a new column with the eventual column header. (Note this is different that your example)
ManufMktConc_molten$label <- paste0(ManufMktConc_molten$variable,
                                    trimws(substr(ManufMktConc_molten$CONCENFI.display.label,1,2)))

# cast it into multiple columns (something like a pivot in Excel).
ManufMktConc_result  <- ManufMktConc_molten %>%
  cast(NAICS.id + NAICS.display.label ~ label) %>%
  select(1,2,4,6,3,5,7)     ## reorder columns

ManufMktConc_result[1:5,]

      NAICS.id             NAICS.display.label CCORCPPCT4 CCORCPPCT8 CCORCPPCT20 CCORCPPCT50 VSHERFI50
    1      311              Food manufacturing       16.3       24.2        38.4        50.9     110.7
    2     3111       Animal food manufacturing       30.2       40.7        57.8        71.5     368.6
    3    31111       Animal food manufacturing       30.2       40.7        57.8        71.5     368.6
    4   311111  Dog and cat food manufacturing       67.8       80.6        89.6        96.5    2019.4
    5   311119 Other animal food manufacturing       24.3       36.2        51.5        68.2     228.3


Matching new set of data with pre-defined sets

You don't need Machine Learning for doing this.

You can subtract the vector describing the user to the vector of the task, and calculate the magnitude of those vectors.

The vector with the smallest magnitude will come from the most matching task.


Why should I care about seasonal data when I forecast?

"Because its there".

The data has a seasonal pattern. So you model it. The data has a trend. So you model it. Maybe the data is correlated with the number of sunspots. So you model that. Eventually you hope to get nothing left to model than uncorrelated random noise.

But I think you've screwed up your STL computation here. Your residuals are clearly not serially uncorrelated. I rather suspect you've not told the function that your "seasonality" is a 24-hour cycle rather than an annual one. But hey you haven't given us any code or data so we don't really have a clue what you've done, do we? What do you think "seasonality" even means here? Do you have any idea?

Your data seems the have three peaks every 24 hours. Really? Is this 'gas'='gasoline'='petrol' or gas in some heating/electric generating system? Either way if you know a priori there's an 8 hour cycle, or an 8 hour cycle on top of a 24 hour cycle on top of what looks like a very high frequency one or two hour cycle you put that in your model.

Actually you don't even say what your x-axis is so maybe its days and then I'd fit a daily cycle, a weekly cycle, and then an annual cycle. But given how it all changes at time=85 or so I'd not expect a model to do well on both sides of that.

With statistics (which is what this is, sorry to disappoint you but you're not a data scientist yet) you don't just robotically go "And.. Now.. I.. Fit.. An... S TL model....". You look at your data, try and get some understanding, then propose a model, fit it, test it, and use the parameters it make inferences about the data. Fitting cyclic seasonal patterns is part of that.


How to model to predict hotel booking abnomality?

Look at the distribution of the features you want to consider for anomalies (e.g., user attributes) conditioned on the date, so you do not trigger a warning for normal seasonality. An anomaly then is when the current conditional distribution is significantly different from the historical average. For more information look into contextual anomaly detection. Welcome to the site and good luck!


Extracting and tagging/labeling text from pdfs

Since the main goal is to extract headers(which has different characteristics from other text). It would be best to convert pdf to xml as you will get features like style,hpos,vpos for each text in the file.
To work with xml you can refer ElemenTree.


In the context of natural language processing, can anyone give a concrete example of True Positive, True Negative, False Positive, False Negative?

Imagine a hot news classifier.

True Positive (TP):
Reality: a piece of hot news.
classifier predicts: hot.

True Negative (TN):
Reality: not a piece of hot news.
classifier predicts: not hot.

False Positive (FP):
Reality: not a piece of hot news.
classifier predicts: hot.

False Negative (FN):
Reality: a piece of hot news.
classifier predicts: not hot.


Decision boundary for KMeans clustering algorithm

Can you post a picture of the plot here and maybe some reproducible code? My understanding with the info you've given is that the contour plot is not actually plotting a decision boundary. In fact, I think its not really helpful to think about k-means as producing a decision boundary. At the end of iteration you have K centroids and 'classify' points based on which centroid it is closest to.


Beautifulsoup find_all returns TypeError: list indices must be integers or slices, not str

Use list comprehensions:
comp_name = [link['href'] for link in para.find_all('a')]


There are two things to consider:

Sampling bias
Metric

The sampling bias problem is that your test set is likely not the complete set of things you're interested in. So, no, you can't simply check MSE_1 < MSE_2 and conclude it is always the case when it's "just" for your dataset the case. This is what significance tests are for. (Although this kind of reasoning is super common in machine learning and I did it myself already )

Then the question if the metric is the correct one for your application. Typical choices are: MSE, mean absolute error, custom cost functions


What's the advantage of multi-gpu training in real?

I see two main advantages of using multi-GPU instead of one as they distribute certain resources:

using large DNN models - some recent models occupy vast space in memory so they simply cannot fit regular GPU and using multiple GPU allow to distribute some parts of the model to different GPU instances.
speed-up DNN training is also a very positive effect of using multiple GPU but only if you have a high-speed connection among GPUs as NVIDIA came with their NVLink


How to deal with different audio formats for audio classification?

If the goal of the project is to classify the groups, then just use the file format information. It is common in machine learning to use meta-data information as a feature.
If you want to contrive the problem by not using metadata, then convert them to format that audio quality matters less such as spectrogram or Fourier transform.


my xgboost model accuracy decreases after grid search with

I think there are several points to take into account:

First, it is possible that, in this case, the default XGBoost hyperparameters are a better combination that the ones your are passing through your params__grid combinations, you could check for it
Although it does not explain your case, keep in mind that the best_score given by the GridSearchCV object is the Mean cross-validated score of the best_estimator (source), it is, the mean test score across the k trainings over the k-folds, so it can maybe give you a more reliable score value (including also the standard deviation of the k scores)

Consider also that you do not need to retrain your model with the best params fron the grid search CV, since you can access the best model via search.best_model

Lastly, I would also recommend other hyperparameter tuning methods like the bayesian tuning, below a sample code gist using hyperopt (info about bayesian optimization here):

from hyperopt import hp
from sklearn.model_selection import StratifiedKFold

from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
from sklearn.model_selection import cross_val_score

def return_model_scores(params, dataX, dataY, n_folds=5):
    import numpy as np

    defined_model, edp_cost_scores, histories =
    evaluate_model_cost(dataX = dataX, dataY = dataY)

return np.array(edp_cost_scores).mean()

param_space = {'max_depth' : hp.choice('max_depth', range(5, 30, 1)),
  'learning_rate' : hp.quniform('learning_rate', 0.01, 0.5, 0.01),
  'n_estimators' : hp.choice('n_estimators', range(20, 205, 5)),
  'gamma' : hp.quniform('gamma', 0, 0.50, 0.01),
  'min_child_weight' : hp.quniform('min_child_weight', 1, 10, 1),
  'subsample' : hp.quniform('subsample', 0.1, 1, 0.01),
  'colsample_bytree' : hp.quniform('colsample_bytree', 0.1, 1.0, 0.01),
  'scale_pos_weight': hp.uniform('scale_pos_weight', 0.2, 0.8)}

# Some variable
dataX = X_train
dataY = y_train
assert len(X_train)==len(y_train)
n_folds=5

global best # global variable defined for convenience of this use case
best = 0
i = 0
def f(params):
    cost = return_model_scores(params, dataX, dataY, n_folds=5)
    if i == 0:
        best = cost

    if cost < best:
        best = cost
    print('new best:', best, params)

    return {'loss': cost, 'status': STATUS_OK}

trials = Trials()
best = fmin(f, param_space, algo=tpe.suggest, max_evals=10, trials=trials)

print ('best:')
print (best)

where evaluate_model_cost is the function you build for getting the cost values after evaluation:

def evaluate_model_cost(dataX, dataY):
    from tqdm import tqdm
    from sklearn.model_selection import KFold

    scores, histories = list(), list()
    # prepare cross validation
    kfold = KFold(10, shuffle=True, random_state=1)
    # enumerate splits
    k = 0
    for train_ix, test_ix in tqdm(kfold.split(dataX)):
        print('kfold {}'.format(k))

        # select rows for train and test
        trainX, trainY, testX, testY = dataX.iloc[train_ix],
          dataY.iloc[train_ix], dataX.iloc[test_ix], dataY.iloc[test_ix]

        # fit model
        history = defined_model.fit(trainX, trainY,
                                eval_metric= 'auc',
                                eval_set=[(testX, testY)])
    # evaluate model
    y_preds = defined_model.predict(testX[features_to_train_on])

    y_true_values = testY
    true_predicted_tuples = pd.DataFrame({'y_true': y_true_values, 'y_predicted': y_preds, 'days_till_slag': days_until_slag})
    true_predicted_tuples = true_predicted_tuples.reset_index(drop=True)
    #true positives, false positives and false negatives number
    tp_savings = 0

    first_tp_detected = False

    while first_tp_detected==False:
        for index in true_predicted_tuples.index:
            # TRUE POSITIVE condition:
            if ((true_predicted_tuples.iloc[index]['y_true']==1)&(true_predicted_tuples.iloc[index]['y_predicted']==1)):
                tp_savings += return_true_positive_savings(true_predicted_tuples.iloc[index]['days_till_slag'])
            break
        first_tp_detected = True

    fp_number = len(true_predicted_tuples[(true_predicted_tuples.y_true==0)&(true_predicted_tuples.y_predicted==1)])
    fn_number = len(true_predicted_tuples[(true_predicted_tuples.y_true==1)&(true_predicted_tuples.y_predicted==0)])

    final_cost = ((costs_dict['fp_cost'])*fp_number) +
        ((costs_dict['fn_cost'])*fn_number) - tp_savings

    score = final_cost_custom_function

    print('score en evaluate_model_with_slag_days', score)

    # append scores
    scores.append(score)
    histories.append(history)

    k = k + 1

return defined_model, scores, histories

where final_cost_custom_function is your custom function of costs to minimize.
This might be a bit complicated case, if you want a very simple example of bayesian optimization, start with:

import pickle
import time
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials

def objective(x):
    return {
     'loss': x ** 2,
     'status': STATUS_OK,
     # -- store other results like this
     'eval_time': time.time(),
     'other_stuff': {'type': None, 'value': [0, 1, 2]},
     # -- attachments are handled differently
     'attachments':
        {'time_module': pickle.dumps(time.time)}
     }
trials = Trials()
best = fmin(objective,
 space=hp.uniform('x', -3, 3),
 algo=tpe.suggest,
 max_evals=100,
 trials=trials)

print('with 100 trials: ', best)

trials = Trials()
best = fmin(objective, space=hp.uniform('x', -3, 3), algo=tpe.suggest,
 max_evals=1000, trials=trials)

print('with 1000 trials: ', best)


Generating text using NLP based on parameters

A direct way would be to encode any binary input features as embedded vectors and add them together as the initial hidden state for the LSTM, and then you train it as a normal language model.

The "little manual introduction" could be supplied to the language model (together with the initial hidden state created from the binary features) at inference time and then use it autoregressively to generate the description.

Given that you are going to deal with proper nouns (i.e. the name of the game), you should use a vocabulary that does not lead to out-of-vocabulary words. I would suggest using a subword-level vocabulary (e.g. byte-pair encoding).

For the implementation, you could do it in Pytorch, using as a starting point their language model example.


An error with respect to filter weights in CNN during the backpropagation

One possible option is padding the lower dimensional object to match the size of the higher dimensional object. Padding is most commonly done so the input and output volume will always have the same spatial dimensions. The additional cells could be padded with any value. It is most common to use zero or the mean of the other cells, depending on the filter operation.


KerasRegressor serialize/save a model as a .h5df

Citing Keras' official page:

  It is not recommended to use pickle or cPickle to save a Keras model.

  You can use model.save(filepath) to save a Keras model into a single
  HDF5 file which will contain:

  the architecture of the model, allowing to re-create the model
  the weights of the model
  the training configuration (loss, optimizer)
  the state of the optimizer, allowing to resume training exactly where you left off.


You dont specify explicitly how far in the future you want to make your predictions, but rather you do that implicitly when you make sure your model is robust and does not over-fit.

What does that mean?

Make sure that distribution of labels with your available independent varaibles has similiar distributions of that what you expect in future. You cant expect your model to learn patterns that were not there in the first place. So variables that show same information for distinct median gross rent values 3 years in the future are what you want to include.


Using dhmm_em to form the hmm of mfccs' from song clips

Edit: It turns out that the above hmm can only be applied to discrete
  values which is possibly why I was getting errors. It seems I have to
  use another function for 'gaussian output' instead, mhmm_em. I'll post
  another question to figure out its working.

In order to clarify this a bit.
The functions you were trying to use are indeed defined for discrete data represented by probability mass functions.

When working with continuous data and modeling them with an HMM, one needs to decide what probability distribution will best represent the data. When the data has an unbounded support, the Gaussian is often a good first choice, and most machine leaning toolboxes have built-in functions for the training of these HMMs. However, an HMM can be trained with other probability distributions such as the Dirichlet distribution (if your data is proportional), or the Student-t, Weibull distributions,...

Keep in mind that the distribution you choose is a modeling hypothesis that you do with respect to the data you have. If the hypothesis does not make sense, the results might not be accurate. Unfortunately, the use of these other distributions mostly rely on you to either, derive and code the needed equations (however, in some cases the equations are available in some scientific papers).


neural network function approximation with constraints

One option is to synthetically generate that data and provide it as training data to the model. Deep Neural Networks are quick at learning patterns. If the data is always monotonic in the first coordinate, the model will learn that.
Another option is using something like TensorFlow Lattice to add "common sense" constraints.


Reshape features in rows intro columns python?

Reflecting the DataFrame over its main diagonal by writing rows as columns and vice-versa is known as "Transpose". if your pandas dataframe is named "df" then

df_transposed = df.T

Check out this
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html


Working with few instances of specific target feature over large dataset

When you lack data you can use some theoretical knowledge on what should happen before the failure. For example for bearings a vibration in the frequency of the bearing should rise exponentially. The same goes for temperature, but there are too many variables that influence temperatures and it is not that easy to separate it. There are temperature sensors in some bearings. That should be more precise. Microphones can hear the problems like humans do. Ultrasound tools allow humans to hear the problems earlier.

You can read about some of these patterns in An introduction to predictive maintenance by R. Keith Mobley


Understanding general approach to updating optimization function parameters

I reached the same conclusion for a general case below. However, in practice, at least in the case of neural networks, weights are initialized randomly; initializing to the same value is avoided.

A general problematic case

Suppose data is $x=(x_0, x_1)$, where $x_0$ is the vector of features and $x_1$ is the outcome. Let the loss function be:

$$J_{\theta}(x) = (x_1 - f_{\theta}(x_0))^2$$

Only one data point $x$ is considered for simplicity (for a set of points, conclusion would be the same since the argument applies to each term in the summation).

Each parameter $\hat{x}_i$ could be a vector or a single parameter. Gradient of loss would be:

$$\nabla_{\hat{x}_i}J_{\theta}(x) = 2\nabla_{\hat{x}_i}f_{\theta}(x_0)(x_1 - f_{\theta}(x_0))$$

According to this gradient, if two parameters $i$ and $j$ have symmetric roles in $f$, i.e. $$\nabla_{\hat{x}_i}f_{\theta} = \nabla_{\hat{x}_j}f_{\theta},$$ their  corresponding loss gradient will also be the same since the components $\nabla f_{\theta}(x_0)$, $x_1$, and $f_{\theta}(x_0)$ are all the same. A concrete example would be a neural network with equal weights and mean squared loss function. All weights between two specific layers would have the same role in the network, thus they will remain equal after each update. However, in practice, weights of neural networks are initialized randomly which breaks this role symmetry between the weights.

A specific counterexample

For example, consider 2D data $x=(x_0, x_1)$ and two 1D parameters $\theta=[\hat{x}_0, \hat{x}_1]$, and let the loss function be:
$$J_{\theta}(x)= \hat{x}_0 x_0 + \hat{x}_1 x_1,$$
for a batch of one point $x$ (this is for simplicity to avoid a summation over points in the batch).

The gradient w.r.t. parameters is:
$$\frac{\partial J_{\theta}(x)}{\partial \hat{x}_0} = x_0,\mbox{and }\frac{\partial J_{\theta}(x)}{\partial \hat{x}_1} = x_1,$$

This illustrates the dependency of gradient on data $x=(x_0, x_1)$. Now, if both parameters are zero, i.e. $\hat{x}_0=\hat{x}_1=0$, the gradient is still different and non zero. More specifically, suppose learning rate is $\lambda$, the next values for parameters would be:
$$\hat{x}'_0 = \hat{x}_0 - \lambda\frac{\partial J_{\theta}(x)}{\partial \hat{x}_0} = 0 - \lambda x_0 \neq 0 - \lambda x_1 = \hat{x}_1 -\lambda\frac{\partial J_{\theta}(x)}{\partial \hat{x}_1} = \hat{x}'_1$$

But, what if $x_0=x_1$?
In this case, parameters always remain the same if we always use specific data point $x$ to update the parameters. However, this case is pathological (unlikely). Because, to let this equality keep going, any other data point $y$ that we peak must satisfy $y_0=y_1$ too. So in this example, the problem is unlikely to happen.


Learning the task execution steps for a target goal

Input - A Target Goal (e.g. Go To Cinema Hall)
Output - A sequence of steps (e.g. Go To Bus Stop -> Onboard A Bus -> Drop at Location X -> Walk 100 meters.

I assume that there's no other information available to the model, such as geographical information about the possible locations (i.e. the training data is made only of examples objective + sequence). As far as I can tell this implies that there's no way to use similarity between two objectives, since there's no way to know if similar places are in the same area.
Under this definition, the objective is a single categorical variable. So there's just nothing to learn except mapping an objective with its corresponding sequence, no need for ML at all: just store the list which maps any objective in the training data to its corresponding sequence in a map (dictionary).


Python: Unable to install pandas_profiling

Try:

pip install pandas-profiling

or:

conda install -c anaconda pandas-profiling


Residual blocks contain weights as you can see in this overview of some different variants:

Source: this blog post

Since weights are learned parameters the neural net can learn to use or not use non-skip/non-identity paths, i.e. by optimizing with gradient descent the network can learn to skip these blocks (or not).

To phrase it differently: the networks "knows" by following the opposite direction of the gradient.


Identifying templates with parameters in text fragments

You might consider using word2vec to identify phrases in the corpus.  The presence of a phrase (instead of single tokens) is likely to indicate a 'template.'

From here, the tokens most similar to your template phrase are likely to be the values for your parameters.


To Tokenise, clean up symbols (i.e. Normalise), etc. just use one of the widely used NLP libraries, they should be able to do most of the work for you.
Examples include:

NTLK
Spacy
SparkNLP
.. and many more. Perhaps look up some articles comparing their strengths and weaknesses on Google to decide what's best with your project.

As for the detecting English words, that might be slightly trickier, but you can find answers to this already from a bit of Googling. E.g. https://intellipaat.com/community/5638/removing-non-english-words-from-text-using-python
Might also be worth posting some code, output examples and what you're intending to do down the line (e.g. training a neural network?) so that other's can provide further help.
All the best,
Kelvin


how can i implement a loss function with percentile cutting?

What you want to do is weird, and i'm not sure i understand it correctly, but i assume you could use the answer to this post.


After you pass your input through your first convolutional layer it's size decreases from 32x32 to 6x6. This is then further decreased by the max pooling layer after that, which decreases the size from 6x6 to 3x3. It is then not possible to try and apply a second convolution with a kernal size of 11. You should therefore decrease your kernel size in the second layer (and probably also in the first convolutional layer).


From 98 boxes to 3 boxes, it involve many other things as well.

  x*y*2 = 98, where 2 are the anchor boxes i.e. each grid will predict two bounding box.
  Non Max Suppression: As correctly said by you, discard those boxes which have lesser probability. You can set some threshold value.
  IOU (Intersection over Union): Step used to identify and discard the overlapping boxes.

Once you done all these activities you will get 3 final boxes.

  More about the complete process:

  Discard bounding box which has low confidence score. Say less than 0.6.

  Now pic the grid having highest confidence score. As mentioned below:
  Here 0.9 confidence score has been picked up.
  Now identify all those girds which has IoU score greater than some threshold, say 0.5. Here highlighted in dark blue and discard such
  grids.

  Still left some grids which predicts the object say car, but IoU is low that is not yet discarded. Repeat such process from step 2 again,
  until we left no such grid.

Note: Suggest you to read more about last two process as it involve the core concept of identifying the bounding boxes.


Validity of PU learning while using character-level encoding using CNNs for classifying text data

Well, I went over the code a couple more times, and I figured it out. There is a glaring error in the following line:

output_layer = layers.Dense(1, activation="softmax")(flattened_sentence)

Softmax needs a one-hot vector for output. I'd used softmax because I wanted the probabilities for the two to be separate, but forgotten at some point when I was writing the generator for my input. Converting it to sigmoid leads to the classifier getting to about 90% accuracy on the test set.

The important lesson there is that softmax requires both classes to be present in the output for binary classification, while sigmoid requires only one class, which varies from 1 to 0.


Extending a trained neural network for a larger input

Its okay as long as the nerwork you are planning to create has the same number of layers and units i.e the dimensions of your network must be compatible with the weights that you are borrowing from the trained model. Also it would be better if you follow the second blog post of suriyadeepan practical seq2seq where he trains a conversation model on twitter chat. The code is much simpler and easier to understand, also it is on a smaller dataset, also he mentioned that the bot trained on cornell movie dialog corpus wasnt performing so well. Mainly to use the pre-trained weights all you have to do is load the model, create placeholders for the weights, assign thr weights from the loaded model to the placeholders and run a forward pass. This blog and this question might help you with this task


How to approach clustering of time-series with a single variable

Welcome to the community Blitva!

There are things to clear at the beginning:

You may cluster authors based on their time-series but it does not necessarily answer the question you asked. How prolific they were in a certain time-period might need another approach to the problem. In other words, you may cluster the authors but you don't necessarily know what was the reason behind that unless you explicitly define Similarity based on which clusters are formed.
Feature Extraction is sometimes needed when you want a specific question to be answered. See my example bellow to understand it more.
What does being prolific in a certain time period mean? Imagine a person who published 1 2 1 papers in 3 consecutive years and another one who published 10 20 10 papers in those days. Are they similar? If being prolific is calculated between them they are certainly not similar. But if it is calculated according to each person, then they have completely similar patterns of being prolific! Which one are you looking for?

Now back to the question. My answer is as follows:

If you are looking for similar patterns i.e. rise and fall in the number of publications, go for DTW for calculating the similarities. I am sure there are R implementations of DTW but I am a Python guy ;) After calculating pairwise DTW similarities, you may apply your Hierarchical Clustering to it (or maybe some other algorithms like Spectral Clustering).

Good Luck!


Handling a feature with multiple categorical values for the same instance value

One possible approach is to perform an encoding, where each level of the feature2 corresponds to a new feature (column).
This way you may describe the 1:N relation between the feature 1 and 2

Here a small example in R

> table1  <- data.frame(id = c(1,2),  feature1 = c("xyz","abc"), predict = c(T,T))
> table2  <- data.frame(id = c(1,1,1,2), feature2 = c("class1", "class2", "class3", "class2"))
>
> ## encoding
> table(table2)
   feature2
id  class1 class2 class3
  1      1      1      1
  2      0      1      0

The new object contains the (now unique) id and setting of the feature2.
You need only to merge (join) the result to the table1 (basically same task a DB join - which variance: inner, outer or full depends on your requirements).


Statistical test to infer if brand preference depends on income

You are going to need to know the sample size as well and multiply it back out from the percentages into frequency counts.

After that, my suggestion is you do a Chi Square Test for independence, since you have two categorical variables.

Either that or you can do an ANOVA (you technically have 3 variables: something for the percentage like a yes/no, brand, and income) , but you'll have to structure you data differently to do that and you will need the sample detail. You'll also need to convert your categorical variables into dummy variables.


Got it. Thanks to everyone who helped. The issue had nothing to do with kmeans.

I did not realize that when you do this:

dataframe[col] = Series, the series gets merged based on the index, as opposed to a simple appending of the column.

My dataframe had been filtered before I generated the lists of features, so the index was not 0,1,2 but rather 0,2,5, etc. I needed to perform reset_index() on the original dataframe before assigning a new column in the dataframe to the labels from the algorithm.


Priviet, feeper!

I created some simple program to extract data from documents.
Works pretty well.

https://gist.github.com/fuwiak/780cb4abbe01aa5d1438269dfa0a3cfc

Best


I don't know why the values are missing since when it's appropriate there's a 0 value in it.

The first step is to check with some SME (Subject Matter Expert) or the Data Custodian. I can't tell you how many times I've built a model/started analysis just to figure out that the data was wrong. Try to figure out the reason behind the Nulls/0.

Besides that there are many ways to handle missing data a few are below:

Remove records with this missing value in your column. If this is an important column to your model it may be best to get rid of that record depending on the shape (rows x cols/features) of your dataset. Don't throw off the results of your model because there's some data that may throw it off (even if you use some of the methods below)
Mean/Median/Mode Impute - A common method of handling missing data is to fill the missing values with the column's mean or median (rarely do you use the Mode).
Fill the values that creates a normal distribution - it depends on your data, but filling the values so you get normally distributed column data can be beneficial
Try all these methods and more - When you start modeling you'll learn to "throw stuff at the wall" and see what sticks. Look at your model results, talk with SMEs, and think about what makes sense. Some ways of handling missing data will work better with different models/datasets. Experiment and have fun!


Why MLP only learns bias for unbalanced binary classification?

Though you know that dataset is naturally balanced but your model will still consider it as unbalanced dataset. If you take a example financial fraud detection, number of fraudulent transactions are always less than the ligit translations, to handle such such cases data scientists will have to balance the classes for better results.
You will have to balance your dataset to get the better results from model.


Which regularization in convolution layers (conv2D)

Nowadays, people don't tend to add much regularisation like $L_2$ to convolutional networks. Usually, regularisation is achieved through the use of techniques like dropout and batch normalisation (although exactly how this regularisation actually happens is not well understood).

Having said that, it may be different for your particular problem. I recommend that after experimenting with dropout and batch norm, you should experiment with $L_2$ and see how the predictive performance is affected on a validation set.


k-means is very sensitive to noise

because it is designed as a least-squares approach. Noise deviations, when squared, become even larger.

Twitter is mostly noise

Twitter is full of spam and nonsense tweets. These will be entirely unlike any other and thus have the largest deviations.

Chances are you get one "cluster" that contains almost everything, and the other k-1 clusters consist of a few tweets with their duplicates. Clusters are not topics. They are more likely to be duplicates than topics.

An appropriate clustering algorithm for tweets should probably discard 90% of tweets and produce thousands of clusters. But it will rarely be better than finding all tweets in common - most tweets only have 2-3 usable words.


How to extract the sample split (values) of decision tree leaves ( terminal nodes) applying h2o library

So far I'm not seeing a way to extract training information from the model.  The H2OTree.predictions can/should give you proportion information, but won't give you leaf sample sizes.  For that, you should be able to use predict_leaf_node_assignment, passing your training set in (to wastefully get passed through the model, *shrug*).

predict_leaf_node_assignment should return a dataframe with the leaf assignment for each of your training points.  (The R version appears to support returning either the path or the node id, but the python one doesn't seem to have it.)  You could take this, join to the original frame, and use group and aggregation functions to produce the desired [n1,n2].*

Regarding the output of predictions, see  https://stackoverflow.com/questions/44735518/how-to-reproduce-the-h2o-gbm-class-probability-calculation .  In particular, the default learning rate in H2O's GBM is 0.1, which helps explain your muted results.

Finally, for a little more fun with the the model's tree objects, see https://www.pavel.cool/machine%20learning/h2o-3/h2o-3-tree-api/
and
https://novyden.blogspot.com/2018/12/finally-you-can-plot-h2o-decision-trees.html

*EDIT: For doing the grouping and aggregation:
(I'm more used to pandas than H2O frames, so I'll convert first.  And given that H2O thinks your FALSE class is the main class, maybe those are strings not boolean?)

predicted_leaves_frame = treemodel.predict_leaf_node_assignment(data).as_data_frame()
df = data.as_data_frame()
df['binary_dep_var'] = df['dep_var'].apply(lambda x: 1 if x=='TRUE' else 0)
df['T1'] = predicted_leaves_frame['T1.C1']
print(df.groupby('T1')['binary_dep_var'].agg(['sum','count','mean'])

This should give for each leaf the number of TRUE samples and the total number of samples and the ratio.  If you really need the number of FALSE samples, you could define your own aggregation function or just post-process this new dataframe.


How to measure variable contribution to an observation in a non-linear model?

Try LIME. In other words, compute local representation of your global highly nonlinear model for a given observation. This local proxy should be simple enough to be explainable as well. Decision trees or simple linear regressions are good choices.


Importance Sampling in Off-policy n-step Sarsa

It seems like someone else asked themselves the same question on Cross Validated. It makes sense for the ratio to be multiplied to G only, since it's only G that uses the behavior policy to sample the next n rewards.


k-means: Only one-dimensional cluster predictions in two-dimensional space

KMeans does correctly do what it is supposed to do.

Just plot your data correctly, with the same scale on both axes...

Y deviations do not matter, they are tiny compared to the X axis. Deviations there are 100x larger, so squared deviations even 10000x. Since KMeans minimized squared errors, only x matters

When plotted correctly, your data more looks like this:


Classification: how to handle reviews/long english words in feature set with all other numerical features

One of the ways to address your use case could be to create 2 separate models, one model using your text data features and another one using your numerical features and combine their results using ensembling.

The other way could be to create numeric features out of your text features (e.g. tf-idf, word2vec) and combine them with your numeric features and feed them to your model.

Sample code-

from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
import pandas as pd

words = ['i hv paid','i dont like','its good','yum yum']
tf = TfidfVectorizer()
vector = tf.fit_transform(words)
df = pd.DataFrame([10,20,30,40])
df2 = pd.DataFrame(vector.toarray())
pd.concat([df,df2], axis=1)

Sample output-


How to understand Bionomial Theroem and the Recursion Rule?

Firstly, this is probably best to ask on the math stackexchange, but I'll have a crack at it anyway.

Let's start by establishing something you already know: $(a+b)^n$ will expands to a sequence of terms with general form $c_ia^xb^y$ such that $n=x+y$. We also know that the expansion process hits every positive integer combination of $x$ and $y$ that sum to $n$ (of course assuming $n$ is a positive integer).
I'd also like to mention that you can notate the expansion entirely with $choose$ coefficients: the first and last terms have coefficients given by $n\choose{0}$ and $n\choose{n}$. Below, we'll see why this is important.

Let's do an example where $n=4$.
Let's start by solving for the first term where $x=n$ and $y=0$. We can ask ourselves: Given the first $(a+b)$ term, how many paths can we take, by multiplying out the rest of the terms $(a+b)(a+b)(a+b)(a+b)$, to reach a term with $x=n$ and $y=0$?

For the first term, it is of course one path: you need to multiply the first $a$ with the second and third and fourth: $aaaa$. Given only one path, the first coefficient is $c_1=1$.

Now let's solve for the second term where $x=n-1$ and $y=1$. We can ask ourselves the previous question with the modified values of $x$ and $y$. We find, stepping from one $(a+b)$ to the next, that starting with $a$, there are three ways to get to $a^3b$ and starting with $b$, there is only one way to get to $a^3b$. Namely, starting with $a$, we could take the following paths: $abaa$,$aaba$, or $aaab$. And starting with $b$, we can only take one path: $baaa$. So $c_2=4$.

Solving for the third term where $x=n-2$ and $y=2$, we can find three paths starting from $a$ and three paths from $b$. Namely: $aabb$,$abab$, and $abba$ for starting with $a$ and $bbaa$,$baba$, and $baab$ for starting with $b$. So in total, $c_3=6$.

We can see a pattern of solving for $c_i$ whereby we ask ourselves How many ways can we construct a sequence of $a$'s and $b$'s such that the number of $a$'s is $x$ and the number of $b$'s is $y$?
This question is precisely solve via the choose operator!
The first coefficient, $c_1$, can be solved by asking, How many ways can we construct $4$ elements consisting of $a$'s and $b$'s such that the number of $a$'s is $4$? This pretty much analogous to the choose operator's definition: ${4\choose4}=1$. Similarly, $c_2$ asks how many ways we can construct the sequence such that there are $3$ $a$'s and $1$ $b$, which is, by definition, ${4\choose3}=4$.

I hope this answers your question about the binomial expansion theorem. Unfortunately, I do not understand what you are trying to ask in your second question, but again, it probably should be asked on the math stack exchange instead. Good luck!


Automatic annotation of medical text data

You could use linear regression on the genome sequence to predict the occurrence of words in the description. More specifically:

Use dummy variables to encode the genome sequence.
Use stemming to make different conjugations of the same word the same.
Use a bag-of-words representation to represent the words.
Use a scaling of the word counts $w_i$ like $\log(w_i+1)$ or the more advanced TF-IDF.
Since you have a lot of independent variables (maybe more than the number of records?) you should use some regularization of the model. Lasso would be a good choice if you want a sparse model, use ridge regression if you want to put a zero prior on the coefficients.

This method you can use to predict which words are typical for a gene sequence and, hence, characterize the sequence.

You could use the intermediate result of the linear model to see what tissue is important for the prediction. The important ones are the dummy variables that are "on" and have high coefficients. Because you have multiple outputs you could simplify this by only using the coefficients of the top-n likely words.


It's buggy but this is what I'm trying to use:

https://github.com/nocluebutalotofit/autokeras/blob/2efe5358f89c279ee4a7d615bb2bdca3e0ced3cb/docs/templates/tutorial/structured_data_regression.md


A question on data distribution probability

You stated that D are i.i.d. so they are independent to each other.

The crucial question is whether D are all independent of x.

p(D|x) = p(D)

only if D are all independent of x


Machine learning anomaly detection for continues data

You can save sklearn models with pickle. This blog post shows how.


How can I find to starting point of skewed data in python?

Try looking at ways to find outliers, such as Tukey's fences or the modified Thompson's tau.


Which decision tree model is used in "standard" random forest?

My experience is with sklearn and python.

A Random Forest is a bagging of decision trees, the sklearn package uses a Decision Tree Classifier which is a CART.

You can see in this post that I made a bit ago of how to build a Random Forest tree the exact same as a decision tree.


Segmentation Network produces noisy output

I'm going to try and attempt to answer my question, but won't accept it as an answer, simply because I'm sure there is more than one reasons as to why this is happening.
I've solved the issue by increasing the areas by adding more "features", e.g., I've made sure there is more text, table boxes and other visual features for the convolutions to "pick up". To a certain degree this seems to have helped a lot.
What also helped, is using a more modern model, I've tried Unet with a resnet34 encoder and I also tried a DeepLabV3 which outperformed all others. So I suspect that the "noise" (for lack of a better word) is a by-product of the network being uncertain where exactly the boundaries of the segmentation are, due to absence of features. I suspect that the more modern models are better suited to deal with that problem.
EDIT
So far what seems to have worked for me is DeepLabV3 with Resnet encoders, but another thing that really seems to make a difference is how "large" the segmented area is. Obviously in some domains this is not possible to adjust (e.g., robotics, autonomous vehicles and suchs) but in document or text processing it most likely is. What I've noticed is that after the downsizing to 224x224 smaller and thinner areas become very hard if not impossible to learn, whereas larger and thicker areas are easier. I suspect that averaging CELoss might be a culprit here, and that there may be a loss mechanism which emphasises errors in smaller regions/areas where errors still exist. The hypothesis is that the smaller regions will tolerate errors due to the averaging operation whereas larger ones won't tolerate the errors as much.


This is not a complete answer to your question, but I can explain at least part of the problem. Since you do not provide your data, I cannot reproduce your results. However, it is easy to demonstrate the same problem with other data.  I will use the well-known iris data set that is provided with R and Weka and is easy to access.

The same problem is apparent with the iris data.

library(CORElearn)
library(RWeka)
library(FSelector)
IG.CORElearn <- attrEval(Species ~ ., data=iris,  estimator = "InfGain")
IG.RWeka     <- InfoGainAttributeEval(Species ~ ., data=iris,)
IG.FSelector <- information.gain(Species ~ ., data=iris,)

IG.CORElearn
Sepal.Length  Sepal.Width Petal.Length  Petal.Width
   0.5572327    0.2831260    0.9182958    0.9182958

IG.RWeka
Sepal.Length  Sepal.Width Petal.Length  Petal.Width
   0.6982615    0.3855963    1.4180030    1.3784027

IG.FSelector
             attr_importance
Sepal.Length       0.4521286
Sepal.Width        0.2672750
Petal.Length       0.9402853
Petal.Width        0.9554360

As with your example,  all three packages give completely different results.

One Issue: Units (Base of Logarithm)

If you look at the documentation for information.gain in FSelector,  you will see this parameter description:

  unit
  Unit for computing entropy (passed to entropy). Default is "log".

Following that trail,  we look at the description of the entropy function and see:

  unit
  the unit in which entropy is measured. The default is "nats"
  (natural units). For computing entropy in "bits" set unit="log2".

If we override the default and calculate IG using unit="log2" we get

IG.FSelector2 <- information.gain(Species ~ ., data=iris, unit="log2")
IG.FSelector2
             attr_importance
Sepal.Length       0.6522837
Sepal.Width        0.3855963
Petal.Length       1.3565450
Petal.Width        1.3784027

Notice that now the values for Information Gain agree with RWeka for Sepal.Width and Petal.Width. Part of the difference was simply using a different base for the logarithm. RWeka uses base 2 (entropy measured in bits). By default, FSelector uses base e,  but allows you to change the base and get some of the same results. It appears from the documentation that neither RWeka nor CORElearn let you select the base for the logarithm.

But it is almost stranger that once we get RWeka and FSelector in the same units, they agree on two variables, but not the other two. There is still something else going on.


How do I read a dat file, for which I don't know its structure?

You can try:
np.fromfile(yourfilepath, dtype="byte")

to read content from .dat and then load as a Numpy array.


Lasso Regression for Feature Importance saying almost every feature is unimportant?

Change (search over) the penalty parameter of lasso.  FinalRevenue = RevenueSoFar is a good baseline "model," but hopefully your other features can improve on that.  You might also consider just modeling the target FinalRevenue - RevenueSoFar.


Using Spark for finding similar users to a user?

PMC from Mahout here- we're in the middle of a site re-org at the moment, and things are... well they're a mess.

Here's a link to something I think is more useful. A tutorial on Co-Occurance in Spark.

http://mahout.apache.org/docs/latest/tutorials/cco-lastfm/

Re "A Spark Library" well, mahout IS the spark library.

To use Mahout (Scala only, sorry if you're a Python-phile, however the syntax, especially for Mahout is very pleasant), you either need to download mahout and run ./mahout spark-shell from the bin/ directory. Or if you like GUIs notebooks and Apache Zeppelin, check out this tutorial for setting up Mahout+Spark on Zeppelin

http://mahout.apache.org/docs/latest/tutorials/misc/mahout-in-zeppelin/

(If you are compiling a Jar, you just add Mahout as a dependency.)


I suspect you are facing a dying ReLU problem.  Check the gradients for each layer and see if they are starting to become 0.  Due to the way backpropagation works and a simple application of the chain rule, once a gradient is 0, it ceases to contribute to the model.  Therefore, if you're model is stuck then it's likely that a significant number of your neurons are now dead.

Try using a LeakyRelu or Maxout and see if that helps.


I'm not sure that anything is wrong here. With a binary target, chance is 50%, and 75% is half way to perfect accuracy of 100%.  Kappa, which ranges from 0 at chance to 1 at perfect separation, is 0.4, which is again a little less than half way to perfect prediction.  So it seems to be corroborating the performance of your model.

Kappa is defined as:
(observed accuracy - expected accuracy)/(1 - expected accuracy)

Since your classes appear balanced, you have (0.75 - 0.5) / (1 - 0.5) = 0.5


If you have a really small dataset like a few 100 samples it's okay to do preporcessing via hand. But since you have a thousand samples it's better to automate the process. You can use the na_values attribute in pandas to fill in the "???" or "??" value with nan. Then for each column replace the Nan values using some statistical measure (in your case the most occuring word..etc). But if youre data-wrangling is unworkable via an automation process or takes too much time, then you should proceed manually. If it's about meeting a deadline a deadline the manual way works best for really messy data.


ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

First of all, to make logical test in Python, you should not use & for a single values equalities (see this) and you should not use question marks around boolean values False and True.

Now, concerning you specific error : When writing product_details['Status'] and product_details['DeletedStatus'] you get each time a Series, which you cannot test for a logical and between them. If you have unique item codes, you can use:

if product_details.iloc[0]['Status'] == 2 and product_details.iloc[0]['DeletedStatus'] == False:
    activ_product.append(itemcode)

It will simply select the first row of product_details and subset the desired column so that the result is a single value and you can compare it.


use .apply() function to change values to a column of the dataframe

use lambda function like as follows

dataFrame['opcodes'] = dataFrame['instructions'].apply(lambda x:[i.split()[0] for i in x])


As Emre already mentioned, you may use the groupby function. After that, you should apply reset_index to move the MultiIndex to the columns:
import pandas as pd

df = pd.DataFrame( [ ['Hospital 1', 'District 19', 5],
                     ['Hospital 1', 'District 19', 10],
                     ['Hospital 1', 'District 19', 6],
                     ['Hospital 2', 'District 10', 50],
                     ['Hospital 2', 'District 10', 51]], columns = ['Hospital_ID', 'District_ID', 'Employee'] )

df = df.groupby( ['Hospital_ID', 'District_ID'] ).mean()

which gives you:
  Hospital_ID  District_ID  Employee
0  Hospital 1  District 19       7.0
1  Hospital 2  District 10      50.5


using neural networks to predict set of charactertics

Yes, you can generate multiple features as output of network.

In this example, network will have X outputs. 1 output will have most probable gender , another will have most probable age and so on.

Input_1= Input(shape=(n_features, ))

x = Dense(100, activation='relu')(Input_1)
x = Dense(100, activation='relu')(x)
x = Dense(100, activation='relu')(x)

out1 = Dense(1,  activation='linear')(x)
out2 = Dense(1,  activation='linear')(x)
out3 = Dense(1,  activation='linear')(x)
..
..
outX = Dense(1,  activation='linear')(x)

model = Model(inputs=Input_1, outputs=[out1,out2,out3,...,outX])
model.compile(optimizer = "rmsprop", loss = 'mse')

Complete example of this :

https://www.pyimagesearch.com/2018/06/04/keras-multiple-outputs-and-multiple-losses/


As you can see in lines 286-296 in newmodels.py the model can use two different loss functions for the four different outputs.
loss = {'pred1':lossfxn,
        'pred2':lossfxn,
        'pred3':lossfxn,
        'final': losses.tversky_loss}

loss_weights = {'pred1':1,
                'pred2':1,
                'pred3':1,
                'final':1}
model.compile(optimizer=opt, loss=loss, loss_weights=loss_weights,
              metrics=[losses.dsc])

The first three outputs of the model, out6, out7, and out8 uses the loss function, lossfxn, which is given as the third argument to the attn_reg function. In isic_train.py, this happens to be the Focal Tversky Loss. For the final output of the UNet (out9) the Focal Tversky Loss is always used. The total loss for the model is then the weighted sum of the different losses for the four model outputs, which is simply equal to the sum given that all weights are set to 1 in loss_weights.


When training NN, how do data loaders work on large datasets?

For large data sets the limitations of available memory prevents you from loading all the data simultaneously. What is typically done is to provide your data to the network in batches. Batches are simply groupings of your data. The batch size maximum value is limited by available memory. If you use Keras the ImageDataGenerator.flow from directory provides a convenient way to present your data to the network in batches. Documentation is here. There is also a benefit to presenting your data in batches. For example if your data consists of 50,000 samples and it fits into memory you can provide the 50,000 samples to the network and after the data is forward passed do 1 step of back propagation to adjust the weights. A better solution is to partition your data into batches, say 10 batches of 5,000 samples. In this case if the network is enabled to train on batches you can get 10 steps of iteration of the weights versus the single iteration. This enables the network to converge faster at a lower computational cost.


Translating a business problem into a machine learning solution: job-adds website

With this kind of general problem there are many possible approaches, and I can't list them all.  In general, you want to go with the simplest model that creates value over a more naive approach.  So we can look at the different approaches, ranked by complexity, and see what you need for each.  This is the order I would try for this analysis:

(1) The simplest approach is just contact a flat 20%, drawn randomly.  This is very easy to do, but hopefully it can be improved upon.

(2) Next, you can try an approach based on the basic statistics of the problem at hand.  For example, you can calculate for those employers which previously placed ads, how many applications* did they get.  And then start with the ones that got the most, and work your way down.

(3) Now that you have baselines sets from doing a blind and then a basic descriptive analytics approach, you can try something a bit more complicated.  Assuming you have the text from the past ads, you can mine that text for features which might be descriptive of what they are looking for.  If the employers placed multiple ads, you can combine those and look for features in the aggregate.  Once you do the same with the resume data, you can create a recommendation engine to match potential employers with employees.  You can then use that data to determine which possible employer is most likely to have potential matches in your client pool.  As a side effect, this engine can also guide your clients to which jobs might be the best matches.  There are many variations which you can use here.  For example, for the recommendation engine, you may want to weight your client sample towards clients which have logged in the most recently, or most frequently, so or you can just filter out the "stale" clients who haven't really been looking recently.

*An alarm bell went off when you said you know who applied.  Are you sure?  Or do you just have clickthrough statistics to know whether they started an application?  How do you know that they applied?  The first rule of real world data analytics is that you need to be skeptical of your data and especially of what you think your data represents.


Getting 0 accuracy and NaN mae for all epochs training my NN

Maybe I'm not interpreting this correctly (I don't use Keras), but why does your softmax layer only have a single unit? The softmax function normalizes a vector to represent a probability distribution: $ f : ℝ^n \rightarrow (0,1)^n  $ such that $\sum f(\vec x)=1 $. I would have assumed the softmax layer to use the same number of units as the previous, but again, maybe I'm not understanding Keras.
Usually, gradient explosions cause nan. If you are unfamiliar with it, you could read about the related vanishing gradient problem here. However, in your case, I think the final layer is more suspicious.


word2vec -storing a word and its vector yet be able to efficiently run k-nearest neighbour

A most popular way of obtaining the approximate nearest neighbors is the locality sensitive hash. And here are some practical results. Then once you have the neighboring keys, it's straightforward to use a key-value store to retrieve the corresponding words.


Will the job of Data Science is going to be at risk?

I highly doubt that the job itself will going to be at risk. It is rather the other way around: Data science and machine learning will replace a lot of other jobs. In the end there, at least, always needs to be someone providing the data to the machine.
It seems like your question rather should be "will there be enough data science positions in the future to have good chances of getting a job". While I can not give you a concrete answer to this question, I can tell you that, I think, most other jobs have a way higher risk. Data science or, maybe, even more machine learning, seems like a good and future-proof choice for me.


How to visualize two small samples with different sizes?

By downloading the Free Wolfram Engine for Developers you may use Wolfram Language.  If you are using Python then use the Wolfram Client Library for Python to access the engine in Python.

You may use BoxWhiskerChart with Labeled to show the distribution of responses and label how may responses in each set.

I'll generate some responses for each of the two groups.

SeedRandom[123]
survey = MapThread[RandomVariate, {UniformDistribution /@ {{3, 5}, {2.5, 4}}, {6, 9}}]

{{3.91144, 4.95565, 4.88643, 4.92443, 3.6047, 3.93342},
 {2.59246, 3.07847, 3.14476, 3.66812, 2.57289, 3.4424, 2.91698, 2.63533, 3.81488}}

Then plot with a number of observations label located at the "UpperFence" of each set.

BoxWhiskerChart[
 Labeled[#, "Obs: " <> ToString@Length@#, "UpperFence"] & /@ survey,
 ChartLabels -> {"Smoke free", "Smoking"},
 ChartStyle -> {Orange, Purple},
 GridLines -> {None, Automatic}]

Hope this helps.


How can I perform stratified sampling for multi-label multi-class classification?

The error here seems to be because you want train and test data (so two data sets), meaning that each class must be present in each of the data sets. This would mean that each class must have at least two samples. It is a design choice of whoever implemented train_test_split. I guess it might not technically be stratified otherwise.

You can see where it is implemented in the SciKit Learn source code, within class StratifiedShuffleSplit:

classes, y_indices = np.unique(y, return_inverse=True)
n_classes = classes.shape[0]

class_counts = np.bincount(y_indices)

if np.min(class_counts) < 2:
    raise ValueError("The least populated class in y has only 1"
                     " member, which is too few. The minimum"
                     " number of groups for any class cannot"
                     " be less than 2.")

np.unique finds indices of the each the classes in y. Because the option return_inverse=True is passed, it returns an array of indices that will allow full reconstruction of the input array, y. This means, to get the total number of classes that are present, you need to use np.bincount; creating class_counts.

The final check is whether or not class_counts is less than the number of data sets you want to create. If it is, then you cannot create a properly stratified split of your data - so you get an error.

As to how you might create your own version: one way I implemented stratified sampling was to use histograms, more specifically NumPy's histogram function. It worked well for continuous labels (i.e. not discrete classes) - and I was not looking at a multi-label problem, so you might have to adjust my suggestion to allow it to accomodate your needs.

The main idea is to split the labels into bins of a histogram and then randomly sample from those bins, with the option to allow for duplicates. That is really the part that will solve your specific problem of < 2 labels in a class. I realise this doesn't specifically answer your problem, but perhaps it will give you some new ideas.

If duplicates don't make sense or are strictly not allowed in your experiment, then you could think about merging the smaller classes toether in some way, so they will have > 2 labels per class. This might be more useful than deleting them, but whether or not it is feasible  will depend on your data.


How to normalize data without knowing the min and max values?

The minimum and maximum values are just known limits that are parts of the formula that reshapes the distribution of the data, so if a value is bigger than the previously known value the resulting feature scaling (Normalization) will be still appropriate.

An alternative is z-scores if you don't feel like using minimum and maximum values.

x'= (x-x̄) / σ
Where x is the original feature vector, x̄  is the average of the vector x  is the mean of that feature vector and σ is its standard deviation.


Real-time or offline Data Augmentation for segmenting microscopy images?

I think you have way too less of data assuming that you would have to separate it out further to training, test and validation sets. Your model will not generalize well. Consider procuring some more data (around 1500-2000 samples atleast)


Why CNN input images are often square shaped?

I'm not sure, (also studied the CS231n class), but I'm guessing it's something to do with:

matrix operations,
convention, and/or
control size of image out.

Not a lot of evidence points towards (1), cause we have insane libraries to do this (ATLAS, MKL, or OpenBLAS). (2) makes more sense practically. (3), because you mentioned zero padding (hyperparameter), the CS231n course says this is done to control the size of output from the current CNN layer and control the spatial size so that input (h x w) = output (h x w). Also, I guess it also reduces errors in matrix operations when you have the same image size (h = w) instead of (h > w or h < w).

Most likely, (3) is the predominant reason. This is just my thoughts. Someone please confirm.


Fine-tuning a model from an existing checkpoint with TensorFlow-Slim

You can get all information you need from the source code.

train_dir: Directory where checkpoints and event logs are written to. (default: /tmp/tfmodel/)
dataset_dir: The directory where the dataset files are stored.
dataset_name: The name of the dataset to load. (default: imagenet)
dataset_split_name: The name of the train/test split. (default: train)
model_name: The name of the architecture to train. (default: inception_v3)
checkpoint_path: The path to a checkpoint from which to fine-tune.
checkpoint_exclude_scopes: Comma-separated list of scopes of variables to exclude when restoring from a checkpoint.
trainable_scopes: Comma-separated list of scopes to filter the set of variables to train. By default, None would train all the variables.


Deconvolution layer is a very unfortunate name and should rather be called a transposed convolutional layer.

Visually, for a transposed convolution with stride one and no padding, we just pad the original input (blue entries) with zeroes (white entries) (Figure 1).

In case of stride two and padding, the transposed convolution would look like this (Figure 2):

All credits for the great visualisations go to

Vincent Dumoulin, Francesco Visin - A guide to convolution arithmetic for deep learning

You can find more visualisations of convolutional arithmetics here.


How to apply data binning on reviews data?

You can bin on Len of the data. you can also bin on Keywords you are interested in. You can use Countvectorizer with YellowBricks library to arrive at Frequency Plot. You can tokenize into N grams and find the frequencies. Depends on your problem statement that you are trying to solve. You can do Sentiment Analysis on each of the reviews and bin them into positive negative and neutral. There are many ways you can think off.

I hope this helps.


Stacked barchart, bottom parameter triggers Error: Shape mismatch: objects cannot be broadcast to a single shape

if you change your code to the following:

import numpy as np
import matplotlib.pyplot as plt

columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j']
pos = np.arange(9)
var_one = np.array([40348, 53544, 144895, 34778, 14322, 53546, 33623, 76290, 53546])
var_two = np.array([15790, 20409, 87224, 22085, 6940, 27099, 17575, 41862, 27099])
var_three = np.array([692, 3254, 6645, 1237, 469, 872, 569, 3172, 872])

plt.bar(pos, np.add(np.add(var_three, var_two), var_one), color='yellow', edgecolor='yellow')
plt.bar(pos, np.add(var_two, var_one), color='purple', edgecolor='purple')
plt.bar(pos, var_one, color='green', edgecolor='green')
plt.xticks(pos, columns)
plt.show()

The result will be like this:


I found a solution that we should use the softmax activation function in the last layer. previously I used the sigmoid activation function


It sounds like you're describing optimal interpolation (AKA Gauss-Markov analysis, objective analysis, probably others too). This is a solid intro, and this is a powerpoint on the subject. It's a hard process to summarize quickly, but roughly speaking, you're on the right track.

Optimal interpolation is fairly common in meteorology and other environmental sciences, but I'm not convinced it's really the best tool for the job. Any statisticians want to take a crack at it?


Scikit Learn's RandomForestRegressor is not giving results on large data set

Some of the settings are likely too aggressive for your machine:

As others have pointed out, n_jobs might help by taking advantage of openMP threading that is likely available on a multicore processor.

But lets also look at some other parameters from the documentation:

RandomForestRegressor(n_estimators=10, criterion='mse', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, bootstrap=True, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False)

Why don't you set n_estimators to 10 and then slowly ramp it up.
max_depth should probably be set both so you trees aren't too computationally intensive and so you don't overfit your model (are you cross validating?)
You have set max_features to auto which specifies that it use n_features, but you have not set n_features.  You could consider turning this down to some percentage of your full feature set.

In general you need to cross validate your model and tinker with the parameters in order to find something that runs on your box in finite time using the available memory and performs adequately.  You could also consider tuning and cross validating a simpler decision tree and then using that insight to tweak the parameters of the random forest.

Sample the data:

Big datasets often require lots of computing power.  I sometimes drastically reduce the size of my training data while I am zeroing in on the best model and then time the scaling as I increase the dataset.  This will give you a better idea of how much time you should expect for the full problem.

Other models:

Finally, maybe you have a problem for which 'bootstrap aggregation' is a bad fit. Maybe another type of aggregation would work better like 'boosting' in which case a 'random jungle' or 'ada-boost' algo might work better.  Or perhaps you might want to move to a computationally less intense method that doesn't require aggregation like a 'support vector regressor'.  Scikit-learn has a great benefit in that it is very simple to swap out different models and try different pipelines.

Cloud Computing:

I'm not sure what box or laptop you are running this on, but it might just be easier to spin up an AWS (Google/MS/IBM) instance and run this on a well-powered box in the cloud.  The AWS spot market is crazy affordable over the weekends or in the middle of the night, so you can work on some very powerful 8 core machines for $.50 per hour, so a 24 hour job is only $12.

Hope this helps!


Use of negative correlation coefficient in pearson correlation algorithm for recommender systems

One thing you can do is to separate the contributions of:

a) who have a positive correlation with you
b) who have a negative correlation with you

Then you can:

Predict the rating using only users in a) $\to$ those will have positive correlation, this positive weights. Call this $\hat{r}_a$
Predict the rating using only users in b) $\to$ in this case, consider the weights as positive (even in they are negative correlation). Call this $\hat{r}_b$
The final predicted rating is $\hat{r} = \frac{\hat{r}_a - \hat{r}_b}{2}$


How to represent a set of sets as a vector

You are describing one-hot encoding. There is a slot for each element. If the element is present, the slot has a one, and if the element if not present, the slot has a zero.

Typically, people will encode orthogonal features in different dimensions. In your case, cities would be one dimension and transportation type would be another dimension. A given data point would be one-hot encoded in a matrix (a 2D collection of vectors). If you want to add people, you would another dimension. That would create a 3D tensor with each person in a row.

Another way to compress your data is to encode not the cities (nodes) but the path between the cities (edges). From that encoding, you can create a Laplacian matrix that sets up spectral clustering. Since you have multiple transportation methods for the routes, you can create clusters with multi-dimensional spectral clustering.


How to learn from multiple data sources with different input variables but the same underlying pattern?

What you are referring to is multi-view learning. Multi-view learning basically tells us how multiple data sources or multiple feature subsets can be combined to create a more robust learning curve for the algorithm.

In recent years, starting from 2013 a lot of research has been carried in this rapidly growing field. A good introduction to the topic can be found in the link below. It contains a more theoretical and mathematical approach to understanding the method.

http://research.ics.aalto.fi/airc/reports/R1011/msml.pdf


If a time series has random time events, how to detect patterns?

As a first step, to segregate the messages that appear to be a bot, you could first try binning by message size. For example, if messages sent by bots are likely to be around 128 bytes to 140 bytes, assign these to a unique bin.

Next, create a time series based on this bin. Try to decompose the time series using an additive or multiplicative method such as Holt Winters. A strong seasonal component would help you identify regular and repetitive messages which are being generated automatically.


Difference between RMSProp and Momentum?

Optimizers evolved with small Fix/Improvement on the previous one. So, if you will read in sequence, you will have a better understanding. In this context, RMSProp was a fix on Adagrad and it was an improvement on Momentum.
Let's see this Loss surface which is like a Valley (Imagine a River)
$\hspace{2cm}$
$\hspace{5cm}$Image source - http://d2l.ai/
Momentum -
Let's start from the red-circled point. We have a very large Gradient in X2 direction and very little in X1 and global minima is towards X1.
In Momentum, we accumulated the resultant Gradient which will obviously point more towards X2.
As a result, we will move very fast towards the other side of the river and very little towards X1. As we cross the river and start moving up, counter Gradient of X2 will start minimizing the Aggregate. Remember, it's leaky aggregation i.e. recent ones have more say. At a point, it will stop and reverse.
In the whole process, we had a little movement in X1 and a lot of oscillations in X2
 This was one of the points of the Author
What AdaGrad did -
 - Manage Gradient for each coordinate separately
 - Added a scaling factor in the denominator which will act as a brake. This scaling is based on the square of past Gradients.
Now X2 will have a large brake, so it will not move so fast with momentum to cross the river. Since X1 is having a very small Gradient, its scaling will be positive(if < 1) Or almost constant(if ~ 1). So, movement in X1 will be same Or even faster.
That's why the author said: "RMSProp impedes our search in direction of oscillations"
Problem with Adagrad was that it aggregates all the past Gradients for the scaling factor which causes the brake to become larger for any case after a good number of iterations even if it has not reached Global optimum.
 Let's say, if Gradient is small 0.5 then also it will start dividing by 2.5 after 10 iterations. if it is large e.g. 10, then it will start dividing by 1000 after 10 iterations. Even this large gradient will become small in the subsequent iteration.
 What RMSProp changed - It made the aggregation leaky i.e. recent one will be considered more (Just like Momentum does for Gradient). With this change, the aggregation will be almost constant or at least not die as fast as in Adagrad.
$\hspace{2cm}$
$\hspace{5cm}$Image source - http://d2l.ai/


Understanding convolutional pooling sizes (deep learning)

In the given example from the e-book, the number $4$ comes from $(12-5+1) \over 2$, where $12$ is the input image size $(12*12)$ of the second constitutional layer; 5 is the filter size (5*5) used in that layer; and $2$ is the poolsize.
This is similar to how you get the number $12$ from the first constitutional layer: $12= {(28-5+1) \over 2}$. It's well explained in your linked chapter.
Regarding your "For instance" code, your 6th line is not correct:

ConvPoolLayer(image_shape=(mini_batch_size, 20, 12, 1),

The number 12 should be replaced by $(81-5+1)\over2$ which unfortunately is not an integer. You may want to change the filter_shape in the first layer to (6,1) to make it work. In that case, your 6th line should be:

ConvPoolLayer(image_shape=(mini_batch_size, 20, 38, 1),

and your 11th line should be:

n_in=40171, n_out=1000, activation_fn=ReLU, p_dropout=0.5),


What's a difference between the neoperceptron and CNN?

According to the research paper, neoperceptrons are a class of CNN that are not sensitive to rotations.

One of the issues with traditional kernels (that was the case before CNN and it is still true with them) is that the rotation of the input image would lead to different results, because the neurons in the dense layer would have different levels of activations.

With these new neurons, you don't get an issue with orientation. So in theory, if you have a gradient in your image, no matter what the orientation is, you would get the same value.

For a traditional CNN, you would get maximum activation with the original orientation, inverse with a 180° rotated image, and no activation with 90° or 270°.


Find matches between columns and manipulate data based on match

One possible way to solve this would be to create a dictionary of which components should be changed and then use replace. So in the example above, we want to create a dictionary {'B': 'A'} since all B should be replaced by A in the new Product column.

This can be done as follows:

components = df['Component'].unique()
d = df[df['Sub-component'].isin(components)].set_index('Sub-component')['Component'].to_dict()

and then we can create the new column:

df['Product'] = df['Component'].replace(d)

Resulting dataframe:

    Component   Sub-component   Product
0   A           a               A
1   A           B               A
2   A           c               A
3   B           d               A
4   B           e               A
5   B           n               A
6   B           a               A
7   B           v               A
8   B           c               A
9   B           i               A
10  C           g               C
11  C           s               C
12  C           g               C
13  C           a               C
14  C           c               C
15  C           i               C
16  C           q               C
17  C           g               C
18  C           v               C
19  C           b               C
20  C           l               C


Well since the points would be shown when the target is zero and the predicted value is $1$, then all the plots would be located in the same place and would overlap. So I think you can give a random value for these points and then plot them.

If the white space is the density of $(0,0)$ values and depicts the ratio of $(0,0)$ points, then you should change the size of the figure or the points. Do you want to do so?

import numpy as np
import matplotlib.pyplot as plt
predicted = list([random.choice([0,1]) for i in range(100)])
target = np.zeros(100)

#adding a random value to the predicted and random values
randomed_predicted = [random.random() for i in range(predicted.count(1))]
randomed_target = [random.random() for i in range(len(randomed_predicted))]
plt.figure(figsize=(5,5))
plt.gca().axes.get_yaxis().set_visible(False) #hiding the yaxis labels
plt.gca().axes.get_xaxis().set_visible(False) #hiding the xaxis labels
plt.scatter(randomed_target, randomed_predicted, c="black")


My network optimises for mean_squared_error, but the predictions are useless

What you are doing right now is regression on the review score, therefore accuracy is not a suitable metric to assess performance. Your model is optimizing the mean squared error, thus it makes sense that it is predicting the (almost) average review score.

Since you are interested in the accuracy on the rating's prediction, you should set your output layer as a softmax layer with 10 possible outputs, as in:

model.add(keras.layers.Dense(10), activation='softmax')

Then you can optimize the categorical_crossentropy of your model, and report the accuracy. In this way, you'll do classification of the ratings.

model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])


Exploration in Q learning: Epsilon greedy vs Exploration function

Any exploration function that ensures the behaviour policy covers all possible actions will work in theory with Q learning. By covers I mean that there is a non-zero probability of selecting each action in each state. This is required so that all estimates will converge on true action values given enough time.
As a result, there are many ways to construct behaviour policies. It is possible in Q learning to use equiprobable random action choice - ignoring current Q values - as a behaviour policy, or even with some caveats learn from observations of an agent that uses an unknown policy.
There are practical concerns:

If the behaviour policy is radically different from optimal policy, then learning may be slow as most information collected is not relevant or very high variance once adjusted to learning the value of the target policy.

When using function approximation - e.g. in DQN with a neural network - the distribution of samples of state, action pairs seen has an impact on the approximation. It is desirable to have similar population of input data to that which the target policy would generate*.

In some situations, a consistent policy over multiple time steps gives better exploration characteristics. Examples of this occur when controlling agents navigating physical spaces that may have to deviate quite far from current best guess at optimal in order to discover new high value rewards.

These concerns drive designs of different exploration techniques.
The epsilon-greedy approach is very popular. It is simple, has a single parameter which can be tuned for better learning characteristics for any environment, and in practice often does well.
The exploration function you give attempts to address the last bullet point. It adds complexity, but may be useful in a non-stationary environment since it encourages occasional exploration paths far away from the current optimal one.

Now my question is, are these 2 above strategies just 2 different ways of Q learning?

Yes.

Or can the exploration function be used along with the epsilon greedy Q learning algorithm as a form of some optimization?

Yes it should be possible to combine the two approaches. It would add complexity, but it might offer some benefit in terms of learning speed, stability or ability to cope with non-stationary environments.
Provided the behaviour policy covers all possible actions over the long term, then your choice of how exploration for off-policy reinforcement learning works is one of the hyperparameters for a learning agent. Which means that if you have an idea for a different approach (such as combining two that you have read about), then you need to try it to see if it helps for your specific problem.

* But not necessarily identical, because the approximate Q function would then lose learning inputs that differentiate between optimal and non-optimal behaviour. This can be a tricky balance to get right, and is an issue with catastrophic forgetting in reinforcement learning agents.


There are several rundowns between the two services like this one.
Key for me:

Only Kaggle supports R, only Colab supports SWIFT
Colab is a Google product and is therefore optimized for Tensorflow over Pytorch
Colab is a bit faster and has more execution time (9h vs 12h)
Yes Colab has Drive integration but with a horrid interface, forcing you to sign on every notebook restart

Kaggle has a better UI and is simpler to use but Colab is faster and offers more time.


What is the more natural parsing, the one that leads to the preferred reading of the sentence

Can anyone explain to me, what is more natural in English and why ?

This is a classic example of PP-attachment ambiguity (PP = prepositional phrase). For a full overview of the problem and some traditional approaches, check out this paper. Here I'll cover the basics.
The quick explanation is that the first analysis corresponds to the interpretation
Twain ( bought (a book (for Howells)) )

whereas the second analysis means
Twain ( bought (a book) (for Howells) )

The second one is probably the most natural interpretation: somebody (Twain) buys something (a book) for somebody else (Howells). So, in this case, the subject may be just buying a gift for somebody else.
On the other hand, in the first sentence, somebody (Twain again) buys something (a book again as well) but now that something was intended for somebody else (a book for Howells) yet the buyer is keeping it for himself.
The explanation is that, when parsing this sentence, the for-prepositional phrase is consumed when it is attached to the closest noun (book). Therefore, it's no longer available later to become the for-phrase beneficiary argument of the verb buy, which therefore can no longer have its gift-making meaning and switches instead to its purely transactional transfer-of-ownership meaning.
The branching of the trees represents these two interpretations, that is, the different syntactic ways in which the same linear order of constituents can be arranged. This phenomenon is one of the traditional examples that linguistic structures do not map exactly to the observable temporal sequences we perceive.
The interesting thing in your example is that the "less typical" interpretation (the first one, corresponding to the "not-a-gift" meaning) is being assigned a higher probability, while I would expect it to be much less likely, since the probability of the "gift" meaning should be much higher when the for-phrase has a person name as the head (Howells) –people just don't go around buying books that belong to other people, but rather for them.
Hope this helps!


Precision, recall and accuracy metrics significantly different between training/validation and actual predictions

Your model is overfitted. It means you are like "memorizing" from training data, when you should "learn". A good model must have similar scores for Training and validation data, but it's not your case, scores are super different. To avoid that:

Use more data to train. Why you use only 50% for training? I will
recommend you use about 90% almost. Anyway, you will be keeping
around 5000 row to test your model, more than enough. With more data, more difficult to overfit. Moreover you can use techniques like data augmentation to obtain more data.
Increase complexity of the model. Adding more layers or convolutions.
You can try also to use some extra techniques like drop out or regularization.
Decrease the value of learning rate, maybe you can improve your model
with this step.

If you use a combination of these point I commented, you should see an improvement on your validations scores.