Is Elastic Search recommended if attribute getting search is not a huge text document?

As you mentioned you are searching  just names, emails or ids, etc which is not large text.

So consider a case where you have 6 documents/records having names as below then you can understand better whether how much the large text could matter.

Rohit kumar Bhatnagar
Shilpa Shinde
Manoj kumar
Rohit Bhatnagar Kandaswamy
Rohit kumar shindey Bhatnagar
Rohit Bhatnagar

If user comes and perform a search for Rohit Bhatnagar, then using the regex you will be showing results in two ways:

Case I: Where regex is strict match

Rohit Bhatnagar Kandaswamy
Rohit Bhatnagar

Case II: When regex is relax

Rohit kumar Bhatnagar
Rohit Bhatnagar Kandaswamy
Rohit kumar shindey Bhatnagar
Rohit Bhatnagar

If we examine case Ist, you are missing two things relaxation(Record 1 & 5 will be missed) and in ranking exact match should come on top that might be more relevant.

In case 2, we relaxed but still exact match is below in ranking

So if need a relevant search then yes you can use a search engine. You could also tweak like whether you need the record 5 or not because if you see that might looks irrelevant so you can control like how many words should be in between to consider a document match. If we say consider 1 word in between then record 5 will be eliminated from results.

Other than search relevance, you can horizontal scale your search if QPS is high. You could also use machine learning techniques(learn to rank), also you could apply synonyms, stemming etc. There are still many other benefits join, streaming, sharding, etc which you could learn from the documentation

Solr: http://lucene.apache.org/solr/guide/7_6/

ES: https://www.elastic.co/guide/en/elasticsearch/reference/6.4/index.html

If above mentioned is required or in near Future then you can use use ElasticSearch or Solr as search engine otherwise you should be good with the current system.


How do I get the values of y_true and y_pred in the confusion matrix?

https://www.cs.waikato.ac.nz/~ml/weka/
The screencap in the Evaluate predictive accuracy of Machine Learning without Programming shows the Correctly Classified Instances and the corresponding percentage.


This is indeed expected behavior, because of the way tree models handle multioutput problems.  The nodes contain some number of samples, and the score for each output is the average of those samples' corresponding output.  Since averaging commutes with sums, the property of summing to 1 is preserved.  I'm not sure if this will help, but in symbols:
$$ \sum_{\text{output }i} p_i = \sum_{\text{output }i} \left(\operatorname*{avg}_{\text{sample }j}(p_i^j)\right) = \operatorname*{avg}_j \left(\sum_{\text{output }i} p_i^j\right) = \operatorname*{avg}_j 1 = 1.$$
Then for the entire forest, you're just applying another averaging, and so the property is again maintained.


Using max-pooling is not a good idea on its own. The reason is that by employing that, you ignore $75$% of the information each time. If your input is a signal which is quite a small pattern, as the number you have referred, it is better not to use max-pool that much. Although the use of max-pooling adds relative spatial invariance to the objects in the input and is useful for classification tasks, the main reason it is employed nowadays is to reduce the number of parameters to train. For instance, for images which belong to $R^{224\times224}$, it is wise to use them for some layers due to lessening the number of training parameters significantly. For images with smaller sizes, the input signal on its own has a smaller number of entries, features, and by employing max-pooling, you are chiefly discarding information which may be necessary and prominent for generalisation. You are actually shrugging them off!


R Programming rearranging rows and colums from timeline data

This would do the job:

#install packages
library(qdapTools)
library(dplyr)
#set working directory
setwd("C:/Users")

#read csv
test <- read.csv("test.csv")

#create dummy variable
#test.new <- dummy.data.frame(test$Sale.Type, sep = ".")
test.new <- cbind(test, mtabulate(test$Sale.Type))

#rename column
names(test.new)[names(test.new) == 'Cash Sale'] <- 'cashsale'

#making new columns
test.new1 <- test.new %>%
                group_by(Vehicle)

test.new2 <-  test.new1 %>%
                  mutate(sum_Cashsale = sum(cashsale)) %>%
                  mutate(sum_Ins = sum(Installment)) %>%
                  mutate(sum_Lease = sum(Lease))

#select few necessary colums
test.new3 <- subset(test.new2, select=c("Vehicle", "sum_Cashsale", "sum_Ins","sum_Lease"))

#select the distict rows
test.new4 <- dplyr::distinct(test.new3)

Just copy paste the code.
saved your sample data to test.csv
Give your working directory details, do let me know if you have any issues.


Get number of correct predictions for each class in Keras

That's true. For understanding how many correct decision your classifier has made, confusion matrix can be used. The main diagonal illustrates that. It depicts how many data samples are correctly classified and how many are mislabeled to which class. you can take a look at here.


If you want to test whether your algorithm works as expected, I'd use sklearn datasets. They allow you to create simple synthetic 2D data with certain properties: circles, half moons, etc.

If you want "real" datasets, here is an interesting resource found after a brief search:

https://uni.hi.is/helmut/2019/06/20/datasets-for-dbscan-evaluation/

It seems to be a collection of datasets used in the literature.

Otherwise, I'd recommend you to look for image segmentation datasets, for instance, maps, as they make good candidates for DBSCAN. Kaggle is good place to search, so is the Google Dataset Search tool


Creating new columns based on 3 column and create new data frame

The question could have been framed better. Checkout the code below, in which your final dataframe will be in output.

import pandas as pd

input_array = [["rs123", "T", "C", 0, 0, 1, 1, 0, 0, 1, 0, 0], ["rs124", "A", "G", 0, 0, 1, 0, 1, 0, 0, 0, 1]]

raw_pd = pd.DataFrame(input_array).astype(str)

def change(a):
    if list(a)[2]+list(a)[3]+list(a)[4] == "100":
        return list(a)[0] + list(a)[0]
    elif list(a)[2]+list(a)[3]+list(a)[4] == "010":
        return list(a)[0] + list(a)[1]
    else:
        return list(a)[1] + list(a)[1]

output = pd.DataFrame()

output['S1'] = raw_pd[[1, 2, 3, 4, 5]].apply(lambda x: change(x), axis = 1)

output['S2'] = raw_pd[[1, 2, 6, 7, 8]].apply(lambda x: change(x), axis = 1)

output['S3'] = raw_pd[[1, 2, 9, 10, 11]].apply(lambda x: change(x), axis = 1)

output['SNP'] = raw_pd[0]

Hope this helps ;) Mark this as the correct answer if you have no other doubts.


How to find anomalies in (almost) constant stream of data?

Try looking into the Robust Random Cut Forest Algorithm (RRCF). There's a python library implementation that supports streaming data where you create rolling windows (called shingles): https://github.com/kLabUM/rrcf
RRCF builds a binary decision tree by randomly picking a number between the min and max of your variable and splitting the data there. If a point is alone after the split it becomes a leaf in the tree. The more separate from the rest of the data a point is (ie the more of an outlier/anomaly it is), the more likely it is to become a leaf higher in the tree. This works well for higher-dimensional data since it will just split all variables randomly.
Here's a video that explains RRCF better than I could in a written post: https://youtu.be/yx1vf3uapX8?t=355


Train at 99% and Validation split accuracy is not more than 70%

This means your model is overfitting the train set. Try simplifying the model by reducing the number of neurons in each layer. That will reduce your train accuracy but may allow your validation accuracy to increase.
You know that you are not overfitting when both train and validation accuracy hover around the same value.


deepAR RNN from AWS Sagemaker - should I clean the data first?

I wouldn't do much to the data before using as input to DeepAR. I would just make sure the timeseries I'm using as input have some variance (are not stationary for long).

DeepAR uses a simple yet efficient sampling technique to handle timeseries of different velocity.

DeepAR's paper state some of the capabilities the architecture should handle. https://arxiv.org/abs/1704.04110

In any case, it is always a responsible thing to try the simple first and use that as a baseline.


NLP: What are some popular packages for phrase tokenization?

Spacy can do this. Spacy's semantic parser is based on Language models trained on large corpus of text.

This parser can break sentence into lower level components such as words / phrases.

More details and examples :

https://spacy.io/usage/linguistic-features

Example with the first sentence from  questions: https://explosion.ai/demos/displacy?text=I%20think%20you%27re%20cute%20and%20I%20want%20to%20know%20more%20about%20you&model=en_core_web_sm&cpu=0&cph=0


Does Box plot with many outliers effects the result?

As the comments suggest, it's not always helpful to think of points outside the whiskers as "outliers". What you are seeing in these boxplots is a strong positive skew. And yes, a strongly skewed target is typically harder to predict than a less-skewed target.

In traditional survival analysis, it's common to model this skewed data using a parametric probability distribution that naturally produces positive and positive-skewed data, e.g. the Weibull distribution.

Otherwise, you can also try a Box-Cox or inverse hyperbolic sine (IHS) transformation on the survival time to reduce the skew.

For visualizing strongly-skewed data, you can either apply one of the above skew-reducing transformations, or use an "adjusted" boxplot, which have an R implementation.


Machine Learning: creating a prediction without previous trends

At first sight I don't see the need for ML, you just need a similarity measure which would return a score between two objects:

Given the input object, say "12345", compare it against any known object using the similarity measure. The result is a similarity score for every known object.
Rank the known objects according to their similarity against the input object, or just obtain the object with the highest similarity. for example "12445" is the most similar with score 0.8.
Apply a threshold $t$: if the max similarity is higher than $t$, then the corresponding known object is returned; otherwise the system says that the input object is "completely new".

Of course ML could still be used, for instance to tune the similarity measure or to find the optimal threshold.


Instead of filtering single points I would suggest that you smooth your data using established techniques, e.g. Savitzky–Golay filter. Another option would be to employ Kernel Density Estimation, where you can then visualize the curves using a reduced, regular set of supporting points.


Should we use one hot encoder class in data having 2 as maximum numeric representation of categorical feature in each column?

The purpose of one-hot encoding is to binarize categorical labels so that your model doesn't learn a spurious ordinal relationship.

For example if you had categories Red, Yellow, Blue, it would be bad to encode Red as 0, Yellow as 1, and Blue as 2 because your model might accidentally "learn" that Red < Yellow < Blue.  In this case you should use one-hot encoding.

But for some categorical features, an ordinal encoding makes sense.  For example, your dataset has the feature "Temperature", which can be Cool, Mild, or Hot.  In this case, the relationship Cool < Mild < Hot is semantically meaningful, so you can probably get away with ordinal encoding if you want.

However, you still have to ensure that the ordinal relationship is preserved in your encoding, and scikit-learn's LabelEncoder does not take care of this for you.  To illustrate, look at how "Temperature" is being encoded:

Hot => 1
Mild => 2
Cool => 0

That's no good!  You want Hot to have the highest value.  Instead you should use scikit's OrdinalEncoder and explicitly tell it how to order the categories:

from sklearn.preprocessing import LabelEncoder, OrdinalEncoder

outlook_encoder = OrdinalEncoder(categories=['Rainy', 'Overcast', 'Sunny'])
X['Outlook'] = outlook_encoder.fit_transform(X['Outlook'])

temp_encoder = OrdinalEncoder(categories=['Cool', 'Mild', 'Hot'])
X['Temp'] = temp_encoder.fit_transform(X['Temp'])

# humidity and wind are binary features, so we don't need the ordinal encoder

humidity_encoder = LabelEncoder()
X['Humidity'] = humidity_encoder.fit_transform(X['Humidity'])

windy_encoder = LabelEncoder()
X['Windy'] = windy_encoder.fit_transform(X['Windy'])

Although there do exist ordinal relationships in most of your categorical features, you should still feel free to use one-hot encoding.  It's always a safe bet when you're dealing with categorical features.

Since your dataset is small, you should experiment with both and see which gives the best results!


Does convergence of loss function is always guarnteed?

(iii), if you add this clause

provided an optimum or smaller than optimum learning rate and the training dataset is shuffled

Why
When we get the Gradient of the full batch, it's towards the global minima. So with a controlled LR, you will reach there.
With stochastic GD, the individual gradients will not be towards the global minima but it will be with each set of few records. Obviously, it will look a bit zig-zag. For the same reason, it might miss the exact minima point and bounce around it.
In a theoretical worse case, if the dataset is sorted on Class, then it will move in the direction one Class and then the other and most likely miss the global minima.
Reference excerpt from Hands-On Machine Learning

On the other hand, due to its stochastic (i.e., random) nature, this algorithm is much less regular than Batch Gradient Descent: instead of gently decreasing until it reaches the minimum, the cost function will bounce up and down, decreasing only on average. Over time it will end up very close to the minimum, but once it gets there it will continue to bounce around, never settling down (see Figure 4-9). So once the algorithm stops, the final parameter values are good, but not optimal."

When using Stochastic Gradient Descent, the training instances must be independent and identically distributed (IID) to ensure that the parameters get pulled toward the global optimum, on average. A simple way to ensure this is to shuffle the instances during training (e.g., pick each instance randomly, or shuffle the training set at the beginning of each epoch). If you do not shuffle the instances—for example, if the instances are sorted by label—then SGD will start by optimizing for one label, then the next, and so on, and it will not settle close to the global minimum.


You're doing something wrong. I can query a 100K word dict in nanoseconds

word_list = open('/usr/share/dict/words').read().split()
len(word_list)

> 99171

word_dict = {word: hash(word) for word in word_list}
%timeit word_dict['blazing']

> 10000000 loops, best of 3: 33.8 ns per loop


Getting Scikit-Learn RandomForestClassifier to output Top N results

As there isn't too much code/context you're giving us I assume that you're working with a binary classification problem:

import numpy as np
X = ... # input for classification, shape (n_samples, n_features)
y_pred = rf.predict_proba(X)[:, 1] # index slicing to retrieve a 1d-array of probabilities
y_pred
# array([  0.18, 0.21, 0.1, 0.2, 0.3])

# Now lets see what the 2 biggest probabilities are
np.argsort(y_pred)
# [2, 0, 3, 1, 4] => lowest probability is at index 2 (the 3rd probability as classified by the RandomForestClassifier)
# Now lets see what the top 5 samples look like (remember, argsort sorts in ascending order, so we take the last 5 indices of argsort):
X[np.argsort(y_pred)[-5:]]

Now you should see the rows in X with the 5 most confident predictions.


If it's exactly zero, you might classify it in either class. When using floating point numbers, there's a nearly 0% probability that an observation would ever have a predicted value of exactly zero. There's always some chance that observations will get the wrong label - that's how statistical models work, if you can find some rule that will with 100% certainty give you the right result, you shouldn't be using a statistical model in the first place.

I'm not sure why you want to "label test data". Test data is by definition labeled data that you set apart in order to get a hopefully unbiased evaluation of your model performance. Perhaps you mean making new predictions?


Keras: How to connect a CNN model with a decision tree

You can make the feature extraction to an intermediate model in keras. It could look something like this:

# Build a model for only the feature extraction layers
feature_extractor = Sequential()
feature_extractor.add(Conv1D(15,60,padding='valid', activation='relu',input_shape=(18000,1), strides = 1,  kernel_regularizer=regularizers.l1_l2(l1=0.1, l2=0.1)))
feature_extractor.add(MaxPooling1D(2,data_format='channels_last'))
feature_extractor.add(Dropout(0.6))
feature_extractor.add(BatchNormalization())
feature_extractor.add(Conv1D(30, 60, padding='valid', activation='relu',kernel_regularizer = regularizers.l1_l2(l1=0.1, l2=0.1), strides=1))

# Keep adding new layers for prediciton outside of feature extraction model
x = feature_extractor.output
x = MaxPooling1D(4,data_format='channels_last')(x)
x = Dropout(0.6)(x)
x = BatchNormalization()(x)
x = Flatten()(x)
prediction_layer = Dense(3, activation = 'softmax')(x)

# Make a new model combining both
cnn_model=Model(inputs=feature_extractor.input, outputs=prediction_layer)
cnn_model.compile(optimizer=opt,loss=loss)

Then you train using the full cnn_model but you call predict only on your feature extraction model with feature_extractor.predict(X). Then you can use the output of the prediction to train your decision tree like this:

# Train full network, both feature extractor and softmax part
cnn_model.fit(X, y_one_hot) # y needs to be one hot for keras

# Predict only the output of the feature extraction model
X_ext = feature_extractor.predict(X)

dtc = DecisionTreeClassifier(criterion = 'entropy')

# Train the decision tree on the extracted features
dtc.fit(X_ext, y) # y should be one-dimensional for sklearn


I used a MinMax scaler in the range between (0, 1) applied to the closing price of S&P500. The RNN consisted of a single LSTM layer with a lookback window of 10 days to predict the next day's closing price. The following figure shows RNN prediction of the next day's closing price (in red).

For code used to generate the figure, have a look at the following ipython notebook.


RNN: Multiple inputs per time step with categorical variables

I would recommend using embedding in this case so that each unique category within a categorical feature is represented as a vector of floats. Then, you can concatenate all vectors coming from different categorical features, and append this long vector along with all other numerical feature values at each timestep.
Besides, if your categorical features are time-invariant (meaning not change over time, e.g. gender), then it might have some issues incorporating static feature values along with other numerical values since RNN is trying to capture the temporal information within the given sequence. But, there is a very helpful link regarding how to add non-temporal features into RNN.
Hope this helps.


What's the best way to do classification basing on two given datasets (annual data and daily data)?

Since the daily dataset does not contain labels, you could aggregate the daily data into annual and then do the join. It sounds like a (binary) classification problem, which can be done using methods such as logistic regression. You will however have to handle missing values caused by the left join, one method would be imputing them. Or just doing an inner join if the missing data is random without patterns (e.g. companies of certain type don't have missing data more often than the other types) and if there's enough data that is not missing.


Python uses conda-base tensorflow-gpu instead of conda environment installation

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

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


How to compare fast the value with the ones of neighbours in a 3D array

NumPy performs operations in a vectorised manner, you should try and operate on the whole array and avoid explicit loops.

e.g. np.argwhere( (test == 1) & np.roll( test==2 , shift=-1 , axis=0 ) )

can be done for different shift and axis values and results combined


When is a neural network better "traditional" models like decisions trees and lassos?

Neural network are "just" a way of estimating a numerical function. Their power is that this comes out of simple matrix multiplications with non linear functions that can be efficiently parallelized. As such, they are perfectly described by calculus.

The question on whether they are biased or not is then related to how they are trained and their black-box behavior. You can put stats at the end of a neural network as well.

The problem IMHO is that generalization for them is a complex topic. Presented with new data, they can have unexpected behavior compared to more traditional techniques for which we understand the behavior at the edge.

If you consider simple regression, like least squares, then neural networks are not actually in play. We use the frameworks to build equations, but we don't build neural networks. As such, we just benefit from the capability of these to process huge amount of data.

We are still going to estimate $y=ax+b$ if we want to estimate the coefficients. For logistic regression, it's still the same boundaries as if we were doing a logistic regression without neural networks.

Neural networks are only in play when the function is unknown and needs to be estimated.


Is it normal for F1 scores to be lower on a binary classification task as compared to a 3-class classification task?

In general the lower the number of classes the easier it is for a classifier to assign the right category. However this completely depends on the characteristics of the data, in particular how well the features match the classes.

For example if one tries to somehow classify pictures of dogs, cats and rabbits into two classes, it's possible that the performance will be poorer than into 3 classes. This is an obvious example but sometimes the data might contain patterns which fit more easily into 3 groups than 2.


the size of training data set in the context of computer vision

Your second hypothesis is on the right track. Try comparing the information content of the training set with the information content of the network parameters. Of course most images are compressible, but they don't compress down to a single floating-point number, which is how network parameters are usually encoded.


where does kmeans store its trained model parameters in scikit learn?

K-means stores 4 model parameters namely cluster_centers_, labels_, inertia_, n_iter_. For details on what they store you can look at the official documentation. Also, Inertia, or the within-cluster sum of squares criterion, can be recognized as a measure of how internally coherent clusters are. You can see a full description here under the section: 2.3.2. K-means.


Your linked paper appears to be wrong about feature subsetting.  I couldn't find it in the documentation for randomForest, but the source for randomForest uses randomTree for the base models, and in that documentation it says

  a tree that considers K randomly chosen attributes at each node.

So the selection seems to happen at each split.
(Note that xgboost has feature subsetting at each of the tree, the level (depth), and the node.  I don't see any obvious reason that one or more of these options should always be preferable...)

For default number of features, sqrt(m) is the most common, but it looks like Weka uses lg(m).  See option -K at
https://weka.sourceforge.io/doc.dev/weka/classifiers/trees/RandomForest.html

Yes, Weka uses the Quinlan family of decision trees, which split using information gain (as opposed to CART, which uses gini).


Frequent Item-Set Mining is what you are looking for. You can see the tree structure of your frequent itemsets and the association rules afterwards.

For your data I'd suggest to look at the whole for a while to get a sense on what you have in hand. Playing with concepts like Probability Distributions, Entropy, etc would be really helpful in case you can reduce the size of your features.

PCA also gives you the opportunity of projecting your data into a low-dimenstional space and you can see also plots showing first several PCs in 2-D or 3-D and get an impression about your data.

Before all above I strongly suggest to see if you have Missing Values and if yes try to cope with them.


@RISK Vs R. Monte Carlo simulation in R?

Monte Carlo simulations are very easy in R. The simplest approach is to write your own scripts that carry out the steps you need for your simulations. To construct these scripts you will need to understand what you are simulating, that is what is the distribution of outcomes, and what are you measuring about those outcomes. If you understand what you are simulating than writing these scripts is easy, but will require programming in R. If you are new to R and/or to programming then it will take some effort to get up to speed. In the end, you will probably understand your data, simulations, and results better. There are packages for Monte Carlo in R, but these are not packages that will do the simulations for you and would require as significant learning of R to be able to use (as much as writing your own scripts).

I suggest reading more about conducting Monte Carlo simulations in R. There are many resources online and offline, such as this book or this tutorial.


Predictive Model for Customer Payment Pattern

Here are some things that come to mind:

1) ARMA/ARIMA models: These may show if there is any seasonal trends in their payment history.

2) Logistic Regression: Will allow you model the conditional probability of a missed payments based off of number of previous missed payments.

From here you need to determine what the threshold for risk is since that's a little more subjective.


Simple explanation with images

We know that an activation is required between matrix multiplications to afford a neural network the ability to model non-linear processes.

A classical LSTM cell already contains quite a few non-linearities: three sigmoid functions and one hyperbolic tangent (tanh) function, here shown in a sequential chain of repeating (unrolled) recurrent LSTM cells:

Images borrowed from "colah's blog"

So far this is just a single LSTM layer, and here we see that the cell output is already the multiplication of two activations (a sigmoid and a hyperbolic tangent). In this case, you could agree there is no need to add another activation layer after the LSTM cell.

You are talking about stacked layers, and if we put an activation between the hidden output of one layer to the input of the stacked layer. Looking at the central cell in the image above, it would mean a layer between the purple ($h_{t}$) and the stacked layer's blue $X_{t}$. You will notice then, that the output in this case, like with the sequential output, has already been activated, as it is the exact same output as the black left-to-right arrow (know as $f_{t}$). What is more, the first thing the input would do in the stacked layer is to be passed through the sigmoids and hypoerbolic tangents of the forget/input/output gates.

So there are plenty of non-linearities being used, meaning it is unnecessary to add yet another between the stacked LSTM layers. You might like to think of it as simply applying two ReLU layers after a fully-connected layer. The results might be slightly different compared to just using one, but not much; as in your experiements with stacked LSTMs.

Documentation

If you look at the Tensorflow/Keras documentation for LSTM modules (or any recurrent cell), you will notice that they speak of two activations: an (output) activation and a recurrent activation. It is here that you can decide which activation to use and the output of the entire cell is then already activated, so to speak. PyTorch doesn't seem to (by default) allow you to change the default activations.

Real world stacked models

Common applications of recurrent networks are found in NLP, for example the ELMo model. If you look through the network design code, you see only basic LSTM cells being used, without additional activation laters. They only mention adding activations for the fully-connected layers (namely a ReLU) at the final output.

The first usage of stacked LSTMs (that I know of) was applied to speech recognition (Graves et. al), and the authors also do not mention the need for activation layers between the LSTM cells; only at the final output in conjunction with a fully-connected layer.


what arguments should I pass to dbscan or optic in order to divide the data in a specific way

Start with what the parameters mean.

$\varepsilon$ is the search radius around each point. You need this search radius to be small enough that it can't fully "bridge the gap" between the clusters. If the gap width is variable, $\varepsilon$ needs to be small enough to accommodate the narrowest gap. Note that we can exclude the occasional straying point from consideration when determining the gap width as minPoints can handle a small amount of "bleed" into the gap. However, by inspecting the data you can see that making $\varepsilon$ smaller than the smallest gap would cause the points on the far right side to be excluded from the right cluster.

minPoints is the minimum number of points within $\varepsilon$ to point $x$ in cluster $c$ for $x$ to be included in $c$. When we determined the minimum gap width, we allowed a few points from each cluster to cross into the gap. minPoints must be large enough that those stray points don't bridge the clusters. Specifically, if there are $n$ points from cluster $c1$ within $\varepsilon$ of a point in cluster $c2$, minPoints must be greater than $n$. Note that like with $\varepsilon$, setting minPoints large enough to keep the primary clusters separate would cause some points on the edges to be excluded from these clusters.

DBSCAN can't cleanly cluster all points into the two clusters that you want because it connects using local connectivity, not global coherence. Even one bridge from one bunch of points to the other groups them into a single cluster. In order to separate the two clusters, there will be outliers. If outliers are unacceptable, you could join them to the two main clusters using a simple heuristic, such as joining to whichever cluster has a closer center to the outlier. If post-processing is unacceptable, you might want to use a global clustering method such as spectral clustering; you might also have success with mean shift.


Transformer based architectures are some of the most popular in NLP right now. You can check this blog post for more information:
https://ai.googleblog.com/2017/08/transformer-novel-neural-network.html
Other than performance, one major advantage of transformers is that operations can be parallelized, making it much faster than RNNs/LSTMs.


Site packages location not shared between Python and Jupyter Lab

A new version of Python was released, and yet again I lost all my libraries by the the time Brew dropped in the new release.

I, therefore, spent some time to look into the best options out of there, and I believe the best is to use a Python Environment management package.

I decided to stick on PyEnv.

A simple %pip -V demonstrates how the command is now run from the shim's directory, which makes me confident whatever I'm now going to install will persist in the 3.8.2 folder and will be movable at a later stage.

Guess that moving packages will be not as easy as 1-2-3, but at least I may not  necessarily have to download all of them rather than copy/move. Any more expert advice here is welcome.


Understanding of Convolutional Neural Network (CNN)

Following blog posts would be helpful for Visualization and intuitive understanding:

Understanding Convolutions - To understand the basic operation involved, Convolution in 1-D setting
Conv Nets: A Modular Perspective - To understand its usage in construction of CNN ( 2-D setting )


Reinforcement Learning - What's the formula for the value function

The formula you have quoted is a bit unwieldy, precisely because the R function as defined needs to "look ahead" to all possible outcomes and their probabilities, but how to do that is not included explicitly.

There are actually a few variants of the Bellman equation that express more  or less detail. A good place to start for a truly generic version is Sutton & Barto (2nd Edition):

$$v^*(s) = \text{max}_{a \in \mathcal{A}}[\sum_{r,s'}p(r,s'|s,a)(r + v^*(s')]$$

Where $\sum_{r,s'}$ is over all possible reward and next state pairs.

The above equation changes your transition function that only handles next state, to a similar function that handles successor state and reward:

$$p(r,s'|s,a) = Pr\{ R_{t+1} = r, S_{t+1} = s' | S_{t} = s, A_{t} = a \}$$

Usually this does not increase the number of items to sum over, or add much complexity, because reward will most often associated with the transition.

The benefit is that this approach removes the need for a reward function that works with expected reward, and just works with specific rewards. Other variants are possible too, such as an expected reward function based on $(s,a)$ or $(s,a,s')$ - the difference is just a little bit of juggling with the expression so that it remains effectively the same given the subtle differences in definition for $r$.


How do I interpret the output of linear regression model in R?

So, the question is centred around the meaning behind a confidence interval.
The main principle behind confidence intervals is the following:
It is very costly and time-inefficient (if not impossible) to sample the whole population (i.e. all UCLA students from China and Hong Kong) and measure their cultural adjustment. Therefore, we can take a sample from this population (i.e. 200 students).
From this sample we can then develop a linear model between input features (i.e. country) and the level of cultural adjustment and establish the slope of the model.
The slope alone only tells us the slope for the linear model trained on these 200 students. We want to have an estimate of the slope that we have at least 95% confidence that it lies in a particular range. Hence, confidence intervals.
Here is an article on confidence intervals for further reference: https://www.simplypsychology.org/confidence-interval.html


In first visit monte carlo are we assuming the environment is the same over episodes?

Yes, an assumption in the MDP formulation in RL is stationarity: the dynamics of the environment do not change over time (within or across episodes).

Note that this almost always false in the real world, but this assumption simplifies matters, so we have it.


Transformer-based architectures for regression tasks

In the simplest case, doing regression with Transformers is just a matter of changing the loss function.
BERT-like models that use the representation of the first technical token as an input to the classifier. You can replace the classifier with a regressor and pretty much nothing will change. The error from the regressor will get propagated to the rest of the network and you can both train the regressor and fine-tune/train the underlying Transformer.
Also, I don't think that layer normalization causes severe information loss. It is already there when the network is trained, so the rest of the network parameters need to take care of that, which should not be a problem because the gradients "know very well" that there was a normalization layer.


Is there an AI service that could be used to classify 30,000 different tools and parts?

Use pre-trained models and transfer learning.

take keras as example:

https://keras.io/applications/

you just need prepare dozens of pictures for each category and can have a pretty good performance.


NMT, What if we do not pass input for decoder?

The currently dominant NMT paradigm is based on an encoder-decoder architecture where the translation is generated autoregressively. This means that each token in the translation is generated conditioning not only on the source sentence but also on the previously generated tokens. The encoder generates a representation of the source sentence in one go and then the decoder takes this representation and generates the target side tokens one by one. This way, the decoder is responsible for the autoregressive part.

When you have an architecture where there is no decoder, you have non-autoregressive (NAR) NMT. It is possible to build a NAR model, but most attempts to train models directly in such a way have failed. They usually repeat the same words once and again, utterly generating garbage instead of actual translations.

Nevertheless, if NAR NMT were possible, it would lead to huge speedups ($O(1)$ instead of $O(n)$ complexity). This is currently a very active line of research. Most of the proposed attempts rely on creating some sort of intermediate (latent) representation to feed to the decoder, which then decodes non-autoregressively.

These are some of the latest articles from that area:

Non-Autoregressive Neural Machine Translation
Non-Autoregressive Machine Translation with Auxiliary Regularization
Latent-Variable Non-Autoregressive Neural Machine Translation with Deterministic Inference using a Delta Posterior


Attention for time-series in neural networks

It is an interesting question. I would not completely agree with you though when you say that most time-series models dont use attention. However there is not as much documentation available on the web as there is for other applications.
LSTNet was one of the first papers that proposed using an LSTM + attention mechanism for multivariate forecasting time series. Temporal Pattern Attention for Multivariate Time Series Forecasting by Shun-Yao Shih et al. focused on applying attention specifically attuned for multivariate data.
Attend and Diagnose leverages self attention on medical time series data. This time series data is multivariate and contains information like a patient’s heart rate, SO2, blood pressure, etc.
A good link to further study this would be: https://towardsdatascience.com/attention-for-time-series-classification-and-forecasting-261723e0006d
Further quoting form the above paper: "self-attention and related architectures have led to improvements in several time series forecasting use cases, however, altogether they have not seen widespread adaptation. This likely revolves around several factors such as the memory bottleneck, difficulty encoding positional information, focus on pointwise values, and lack of research around handling multivariate sequences. Additionally, outside of NLP many researchers are not probably not familiar with self-attention and its potential."
I dont completely agree with the last statement, nonetheless I do agree that the benefits of attention have not yet captured the attention of researchers outside of NLP to the extent that it should have


Improving accuracy of Text Classification

First of all good job done in processing the data and coming up with your base model. I would suggest few things that you can try:

Improve your model my adding bigrams and tri-grams as features.
Try doing some topic modelling like latent Dirichlet allocation or Probabilistic latent Semantic Analysis for the corpus using a specified number of topics - say 20.  You would get a vector of 20 probabilities corresponding to the 20 topics for each document. You could use that vector as input for your classification or use it as additional features on top of what you already have from your base model enhanced with bigrams and trigrams.
Another thing I would say is try using a tree based classifier ensemble to capture non-linearity and interactions between the features. Either of random forest or gradient boosting would be fine. In gradient boosting you can use  xgboost as its a pretty good package that gives good classification.
If you are familiar with Deep Learning you can give a try with a Recurrent Neural network architecture(mostly the LSTM versions).

Hope this helps and let me know if this improves your classification accuracy.


Looks like you have a classification problem. A simple way to solve this is with a linear regression model. Here is how I would do that with the data you've provided:

1) Determine a "unit" of time, for example 1 minute of keystrokes. Once this process is complete you can tweak the unit of time to see if different intervals give you better results (5 minutes, 10 minutes, etc...)

2) Split all keystrokes into 1 minute chunks, and categorize those chunks as work, study, play, etc... Now you basically have a bunch of character strings that are categorized into activity types.

3) Create variables to describe your chunks. Word count, character count, wpm, # of times "lol" was typed, etc...Get creative.

4) Create a test set and training set of chunks.

5) Create a linear regression model to predict type of activity based upon the variables you created.

This is a pretty simple solution but if you can come up with good variables to distinguish your activity types it can work.


Downsampling means you sample from the majority class (the 98.5%) to reduce the imbalance between majority and minority class. If you keep the ratio constant you simply reduce your number of trainings examples. This doesn't make sense. However, you don't have to sample down to a ratio of 50:50. If you have a ratio of 98:2, you can sample to 80:2 instead of 2:2.

The main goal of downsampling (and upsampling) is to increase the discriminative power between the two classes. Ideally, you would have a classifier that outputs a decision surface that is not simply binary (e.g. logistic regression (where you don't have to select a cut-off point of 0.5)) but gives you a continuous decision value. You can then order the data and set a decision threshold that gives you the best outcome.

Since downsampling (or upsampling) changes your training distribution from your true distribution, you only want to downsample (or upsample) so much that your classifier can start discriminating between the two classes. You then fine-tune the results by selecting an appropriate decision threshold. (Also, in my experience, upsampling is often a better choice over downsampling.)


Adam optimizer for projected gradient descent

tl;dr: All gradient descent-based methods have an update rule similar to Adam's, so I think they'd all give you some grief. I don't know of any optimizers tailored to your application (but maybe you could invent one!)

Long answer:

According to this, Adam has four hyperparameters that typically don't require much tuning (although of course model hyperparameters will still need to be tuned, such as number of hidden units per layer, etc.)

alpha. Also referred to as the learning rate or step size. The proportion that weights are updated (e.g. 0.001). Larger values (e.g. 0.3) results in faster initial learning before the rate is updated. Smaller values (e.g. 1.0E-5) slow learning right down during training
beta1. The exponential decay rate for the first moment estimates (e.g. 0.9).
beta2. The exponential decay rate for the second-moment estimates (e.g. 0.999). This value should be set close to 1.0 on problems with a sparse gradient (e.g. NLP and computer vision problems).
epsilon. Is a very small number to prevent any division by zero in the implementation (e.g. 10E-8).

There are several other optimizers out there, such as AdaGrad and RMSprop. The idea from the 2015 paper on Adam was that by doing a combination of AdaGrad and RMSprop, you get a better optimizer (Adam).

The algorithm for Adam is explained in the 2015 paper (Algorithm 1). The exponential smoothing portion applies specifically to the (biased) estimates of the first and second moment. The only time your actual parameter value plays a role is in the calculation of the gradient, and then in the update.

In the update step (last part of the while loop in Algorithm 1), you perform this calculation:

x_t = x_{t-1} - alpha*(unbiased estimate of the first moment)/(sqrt(unbiased estimate of the second moment) + epsilon)

If you restrict x to be greater than or equal to 0, then if x_{t-1} ever becomes zero, you can no longer update (the subtraction of alpha*(stuff) would result in something less than 0, which you then clip to be 0, which means you'd get 0 as the result in each subsequent update step).

Maybe that's the source of the issue? If so, I would recommend restricting your entire parameter space to be above zero. That is, in your initializer, make sure the initial values are all positive (i.e. don't use Glorot/Xavier initialization, because you could get initial values less than 0, as this initializer is based on the Normal distribution).


Is using samples from the same person in both trainset and testset considers being a data leakage?

Yes, this is a form of data leakage. The testing data should not be linked to the training data in any way.
Another way to think of it is, if someone were to try replicating your results with their own test set, would your test set have given you an advantage such that your results are generally better than theirs?


Do the filters in deconvolution layer same as filters in convolution?

Here, I think, you can find good visuals and explanations for convolution/deconvolution arithmetic.


I recommend you to take a look to Oryx (https://github.com/OryxProject/oryx). Oryx is based on Apache Mahout (actually one of the creators of Mahout Sean Owen built it) and provides recommendation using collaborative filtering. Oryx is a very practical tool for implementing recommendation. I have used it in several projects: recommending products in retail stores (small businesses), building an e-commerce recommender and user similarity from mobile app interaction.

You just have to represent data in the form:
UserId ItemId Value

Where value is a measure (subjective) of the importance or influence of the interaction between that user and the item. User and item can be anything actually, and the same procedure can be used for tagging. For example, for recommending songs, finding similar songs and bands, and finding similar users according to their music tastes you can represent data as

UserId SongId NumberOfPlays

Where NumberOfPlays is the amount of times a song has been played by user (in an online music service for example). This exampl was given in Myrrix the predecessor of Oryx. They also show how to recommend tags to questions in StackOverflow.

The github site is not that well documented but it will be enough to get it running (and working :))


[Keras][LSTM] error due to shape mismatch

LSTM gives 2 outputs:-

Sequences
States.

Now as you are working with Keras, by default the LSTM layer is designed to return states over Sequence (Which can to altered by turning return_sequence argument to True in keras layer).

Looking at the shape of your output, (2,3) is incorrect for the states and is actually looks like it should be for the sequence. This is the reason why your code is throwing an error.


It is in order, but not in the order that you want.

It is currently treating the indices as a string.

To get your desired index as numerics, here are some codes that hopefully can help you.

> c = c("1", "2", "13")
> c = sort(c)
> c
[1] "1"  "13" "2"
> c = strtoi(c)
> c
[1]  1 13  2
> sort(c)
[1]  1  2 13


How can you include information not present in an image for neural networks?

Other answers suggest to put an additional channel, I disagree. I think it's a very computationally intensive, time consuming process. Moreover, it forces non-pixel data to be processed by Conv filters, which doesn't make much sense IMHO.

I suggest you to establish a multi-input model. It would be composed by three parts:

A Convolutional part, to process pixel data,
A Feed-forward part to process non-image data,
Another Feed-forward part that elaborates the prediction based on the concatenation of the two outputs above.

You will need to instantiate them separately, then combine together in a Keras Model(). You will also need Concatenate() layers to combine the two different sources of data.

You can read more about the implementation of multi-input Neural Networks here.


Reusing a portion of a neural network (with shared weights) in Keras

When you build your Keras model using the functional interface, you can also build additional models on any subset of the paths through the network by reusing the intermediary functions. Then you can train on just portions of the network (given that you have targets for the outputs). I haven't tried to train on sub-networks of a network, but I do use these intermediary models to propagate activations between internal layers. The Python package conx that is built on top of Keras will build these intermediary models for you, by the way. For more, see: http://conx.readthedocs.io/en/latest/Getting%20Started%20with%20conx.html?highlight=propagate_from#Propagation-functions


Predicting yearly income with linear regression using Python

Try:

from sklearn.linear_model import LinearRegression
import numpy as np
regression_model = LinearRegression()
Year = np.array([[1970], [1971], [1972]]) #your data
Income = np.array([3399.299037, 3768.297935, 4251.175484]) #your data
regression_model.fit(Year, Income)
regression_model.predict([[1973]]) #make predictions


I have not been able to reproduce the blank model fit results.  But, I know that the forecast function will only forecast up to known xreg inputs. For instance, your test period starts at 1/2019 and ends 12/2019 (12 periods) however you're attempting to forecast  72 periods.  Try setting the h parameter in the forecast function equal to 12 and see if you get the same result.

To get around this problem in the past, I have forecasted the xreg series into the future (in your case an additional 60 periods for each of the four xreg variables).  You'll then need to add these 60 periods to the end of the test set for each respective variable.


What is the state-of-the art ANN architecture for MNIST?

Actually, what I'm going to discuss is not an architecture and is like a module used in networks. It performs really well on character based data-sets like MNIST although there is other functionality for them. There is a paper called Spatial Transformer Networks written by Max Jaderberg et al. It tries to introduce an alternative for pooling layers. What it does is tries to find the canonical shape of its input by reducing transformations, like translation and rotation, or even diminishing the distortion of the inputs. It introduces a module which helps convolutional networks to really be spatial invariant. The work is amazing and the paper is easy to be read. Based on experience, using these modules may reduce the number of neurons and layers, because networks employing them won't try to learn extra stuff of the inputs, like applied transformations to the inputs. The reason is that the net will try to learn the canonical shape of inputs.

(a) shows the arbitrary input to the network (b) shows what spatial transformer has done and finally (c) is the output of the spatial transformer which can be used through other layers of the networks.

One of the significant achievements of this module is that it tries to enhance distorted inputs.

Its performance can be seen here.


How to determine precision level of outcome in ML models?

The reason you get predictions without an exact time is because that is how models are trained. They are not trained to predict the exact time but a window for it.
The reason a model is not trained to predict exact time is because it introduces a lot of problems starting with data imbalance and the huge number of classes it would introduce. Also, DL/AI is not God or Laplace's demon, so to predict the exact is not going to happen.


Find frequent segments of GPS trajectories

With the initial assumption that the trajectories can follow existing routes that a number of all possible sub-trajectories is limited and can be also discreet. I would discretise the map and on all sub-paths compter a frequency analyses:

Create a map of all possible paths such that you overlay all trajectories which gives you the full map.
Dicretise the map by creating paths and path segments defined that each path segment is a path between two closest branching on the particular path, you can also see this task as generating a graph.
Represent each trajectory as sequence of this path segments and compute a frequency characteristic for each segment in the map
You can extend this task with discretization levels such that some short by-passes could be still counted as the main path...


Where exactly does $\geq 1$ come from in SVMs optimization problem constraint?

First problem: Minimizing $\|w\|$ or $\|w\|^2$:

It is correct that one wants to maximize the margin. This is actually done by maximizing $\frac{2}{\|w\|}$. This would be the "correct" way of doing it, but it is rather inconvenient. Let's first drop the $2$, as it is just a constant. Now if $\frac{1}{\|w\|}$ is maximal, $\|w\|$ will have to be as small as possible. We can thus find the identical solution by minimizing $\|w\|$.

$\|w\|$ can be calculated by $\sqrt{w^T w}$. As the square root is a monotonic function, any point $x$ which maximizes $\sqrt{f(x)}$ will also maximize $f(x)$. To find this point $x$ we thus don't have to calculate the square root and can minimize $w^T w = \|w\|^2$.

Finally, as we often have to calculate derivatives, we multiply the whole expression by a factor $\frac{1}{2}$. This is done very often, because if we derive $\frac{d}{dx} x^2 = 2 x$ and thus $\frac{d}{dx} \frac{1}{2} x^2 = x$.
This is how we end up with the problem: minimize $\frac{1}{2} \|w\|^2$.

tl;dr: yes, minimizing $\|w\|$ instead of $\frac{1}{2} \|w\|^2$ would work.

Second problem: $\geq 0$ or $\geq 1$:

As already stated in the question, $y_i \left( \langle w,x_i \rangle + b \right) \geq 0$ means that the point has to be on the correct side of the hyperplane. However this isn't enough: we want the point to be at least as far away as the margin (then the point is a support vector), or even further away.

Remember the definition of the hyperplane,

$\mathcal{H} = \{ x \mid \langle w,x \rangle + b = 0\}$.

This description however is not unique: if we scale $w$ and $b$ by a constant $c$, then we get an equivalent description of this hyperplane. To make sure our optimization algorithm doesn't just scale $w$ and $b$ by constant factors to get a higher margin, we define that the distance of a support vector from the hyperplane is always $1$, i.e. the margin is $\frac{1}{\|w\|}$. A support vector is thus characterized by $y_i \left( \langle w,x_i \rangle + b \right) = 1 $.

As already mentioned earlier, we want all points to be either a support vector, or even further away from the hyperplane. In training, we thus add the constraint $y_i \left( \langle w,x_i \rangle + b \right) \geq 1$, which ensures exactly that.

tl;dr: Training points don't only need to be correct, they have to be on the margin or further away.


class StrayCommaRemover(TransformerMixin):
    def __init__(self): //Initialize self by setting some variables here which can be passed as a input to transformer

def fit(self, X, y=None):
    self.columns = X.columns //Setting context based on input Data
    return self

def transform(self, X, y=None): // Actual transformation logic
    X= X.withColumn('Company', regexp_replace('Company',',','')
    return X

You can add above transformer as a step in your pipeline and can call init() and fit() on it.


Applying dimensionality reduction on OneHotEncoded array

Following your example, you have different points in a 4-dimensional space. So, yes! you can use any dimensionality reduction technique, from PCA to UMAP.

In general, if your data is in a numeric format (and one-hot actually is), all the elements have the same dimensionality, and you don't have undefined values (NAN, inf), you can always use dimensionality reduction.


How to load this data from .dat into dataframe using python

Given just one line of the data, it's a little hard to go off of, but I'm assuming you're trying to get at the number after each colon, and the number before it refers to the column name?

If so, you can use read_csv with a little tweaking:

import pandas as pd
from pandas.compat import StringIO

temp='1 1:31080.410200 2:2.871828 3:5.862267 4:7.100850 5:8.283706 6:-5.427875 7:-6.667087 8:-8.888233 9:28898.943400'
#after testing replace StringIO(temp) to filename

df = pd.read_csv(StringIO(temp),
                 sep="\s+", #separator whitespace
                 index_col=0,
                 header=None)

for c in df.columns.values:
    df[c] = df[c].apply(lambda x: float(str(x).split(':')[1]))

df.head()

Which will output:

    1           2           3           4       5           6           7           8           9
1   31080.4102  2.871828    5.862267    7.10085 8.283706    -5.427875   -6.667087   -8.888233   28898.9434


What pre processing should I use on data to feed into a CNN?

You should normalize your input data for different purposes. As you can read from here, Normalizing data is done for accelerating optimization. If you have features with different scales, it will take too much time for your optimizer function to find optimal points. Suppose you have age feature which can change between 0 to 150 (!) and salary which can be changed from 0 to whatever, like 500,000,000 $. your optimization algorithm used in your ML model will take too much time, if possible, to find appropriate weights for each feature. Moreover, if you don't scale your data, your ML algorithm may take too much care to features with large scales.

You have not explicitly specified your task, if its classification, you may need to turn the labels into one-hot encoded versions or if possible you may need to use word embeding for adding logical distance to different classes. If your task is a regression one, you may need to normalize your outputs. In regression problems it is customary to normalize the output too, because the scale of output and input features may differ.


Is it possible to get worse model after optimization?

Is it possible that after running the optimization my score won't get better (and even worse?) ?

Yes, theoretically, by pure luck, it is possible that your initial guess, before optimization of hyper-parameters, provides better results than the best of parameter combination found in the parameters grid. However, assuming you have enough data and your parameter grid is wide enough it is rather unlikely that the tuning of hyper-parameters would not be able to find better results. Such behavior rather indicates that something is wrong with your approach or your data.
If understand correctly, the choice of the best parameters is based on the cv results on training data, while in your final run the performance is assessed based on test dataset. If the distribution of training and test data differ significantly it could lead to the situation when the parameters providing the best results on the training data perform poorly on test data.

Where is my mistake?

As already mentioned by others, the parameters you are testing after the tuning were not included in the parameter grid. In this case it is incorrect to talk about the model performance "after running the optimization".
I suggest the following in order to investigate and fix the problem

Instead of using the hard-coded parameters in the XGBClassifier  call, use the optimal parameters found by tuning process, i.e. grid_search.best_params_. Furthermore, if you think that subsample and cilsample_bytree (a typo?) are relevant parameters include them in the parameters grid.
Increase the cv parameter to e.g. 5-10, the results with cv = 3 might be very unstable. You can assess the stability of your current results by using different random seeds and repeating the entire exercise.
Make sure that you use the consistent parameters in tuning process and in the final evaluation, or just include these parameters in the parameters grid if possible. In particular, check early_stopping_rounds and eval_metric.

Are there other parameters that could influence or improve my model?

From your code it is unclear how many rounds you use. Either increase n_estimators or include it in the parameters grid.
Given that you use AUCPR you might need to explicitly set the parameter maximize=True, otherwise in your final run you could minimize the AUCPR, which could explain poor results.


The Bayesian Network Generated (BNG) datasets are a set of artificially generated datasets openly available on OpenML. These datasets were generated to fill the need for a large heterogeneous set of large datasets. This paper describes the BNG generator best:
Algorithm Selection on Data Streams.

Small quote from the paper about the BNG data generator:

  The generator takes a dataset as input, and outputs a data stream containing
  a similar concept, with a predefined number of instances. The input dataset is
  preprocessed with the following operations: all missing values are first replaced by the majority value of that attribute, and numeric attributes are discretized using Weka’s binning algorithm.

A personal note: For general Machine Learning studies, I would refrain from using BNG (or any other kind of artificially generated) datasets, as the concept is generally simpler than the original dataset. Instead, it is recommendable to use a per-defined benchmark suite, such as the OpenML-100.


What you're describing is indeed the traditional approach for building a sentiment analysis system, so I'd say it looks like a reasonable approach to me.
I'm not up to date with the sentiment analysis task at all, but I think it would be worth studying the state of the art for several reasons:

There might be more recent, better approaches
There might be datasets in the languages you're interested in, and if there is that could save you a lot of time. Check if there are any shared tasks about this, they often provide annotated datasets.


Calculating error from accuracy score in Sklearn

From sklearn's documentation for the score function

Returns the coefficient of determination R^2 of the prediction.

R^2 is a measure of how well the variability of the data is explained by the model. So, at 0.97, the model is able to explain that really well. And yes, it is a measure of accuracy for regression models. However, you cannot construct confidence intervals with that estimate.

accuracy_score is a measure of accuracy for classification models. Again, you cannot construct confidence intervals from the accuracy metric.


An option could be using the output of the i-th image as an input for classifying the (i+1)-th image.


Can a convention convolution neural network train correctly with different training image size and ratio?

Do not do this.

You are providing the algorithm with examples your training data should conform with the same dimensions otherwise you will skew the weights with alternate examples that do not reflect a true positive as like the original batch.

Your data preparation is important, if you have the ability to standardise the size then you should do this at the start before training.

If you have a library of images that you want to train on that are all different sizes; I suggest that you do the following:

Use opencv for the following
1) Write a script that cycles through the images
2) use the script to clip the images on the x:y axis making a square
     2b)you can automate this by using the click point for centre which then clips and save the new sized image

obviously save all the files in another directory but they should all be standardised now.


Train Test Split for overlapping samples

You can shuffle the samples first with a serial number attached to all the samples. Now, randomly select p% of the samples as test data and leave the (100-p)% samples as the training data. During this random selection, make sure that if a sample of serial #n has been selected as test, then #n-1 & #n+1 are not picked.

In that way,

You are not testing the classifier on a sequence of the data.
Avoid overlap between train & test samples.


How do I predict continuous value from time series data?

I will first list some examples of models you could look into.

  You can suggest me things I should learn about too.

You can look at more classical regression type models, or go deeper in to deep neural networks, utilising Long Short Term Memory cells, which model relationships over time.

Classical

The main term that will help you search for related tutorials/documentation will be autoregressive, which is a fancy name for saying a target value is predicted from its recent history. How recent, is a parameter you can tune your model for, called lags.

Have a look at the options in the statsmodels module for Python. More specifically, you might want to try out the ARIMA model class. For more details on ARIMA, check out this thread.

There are many traditional models that you could use for a time-series problem. Terms you might consider searching for include:

Here is an introduction article about these Generalised Linear Model (GLM) -
Here is a variant, called General Additive Models (GAM) with a good walkthtough using R

Deep Learning

This is a much newer topic and is overall a fair bit complexer than the classical models discussed above. If you go this route, you would probably make progress quickest by using a Deep Learning framework such as Keras, which allows you to build complex models without too much time investment. It is a wrapper library that uses either Tensorflow, Theano or CNTK in the background (these are the available backends).

In a deep learning approach involving time-series analysis, you almost certainly want to start with an LSTM model. This stands for Long Short Term Memory, and uses a complex cell to monitor various states as you pass in your time-series data. It is a really big topic, so I will just provide:

an introduction to the logic behind LSTM, and
a nice tutorial on how to get things working

If you decide to go this way (as opposed to classical models), then yes, you should understand backprop.

This is a really broad problem that you have described and there are many many approaches. It is also likey very active in research within the fields of (self-)localisation.


Given a single discrete data set, how should I divide it into training data and test data?

In machine learning, it is important to test out the model that you have built on your training data. This is to prevent overfitting. This is why you must split your data into testing and training. There are many different ways to split testing and training. You can randomly split the data set so that 80% of the samples are training and 20% are testing. Something else you may want to consider is using stratified sampling so that the positive labels occur in both testing and training. This is especially important if you only have a few positively labeled samples, as you could easily end up with a test set without any positive samples. In python there is an argument ‘stratify’ that you can use so that the split has balanced classes.


Find the consecutive zeros in a DataFrame and do a conditional replacement

Consider the following approach:

def f(col, threshold=3):
    mask = col.groupby((col != col.shift()).cumsum()).transform('count').lt(threshold)
    mask &= col.eq(0)
    col.update(col.loc[mask].replace(0,1))
    return col

In [79]: df.apply(f, threshold=3)
Out[79]:
       col1  col2
names
A         1     0
B         1     0
C         1     0
D         1     0
E         1     1
F         1     1
G         0     1
H         0     1
I         0     1
J         1     0
K         1     0
L         1     0

Step by step:

In [84]: col = df['col2']

In [85]: col
Out[85]:
names
A    0
B    0
C    0
D    0
E    1
F    0
G    1
H    0
I    1
J    0
K    0
L    0
Name: col2, dtype: int64

In [86]: (col != col.shift()).cumsum()
Out[86]:
names
A    1
B    1
C    1
D    1
E    2
F    3
G    4
H    5
I    6
J    7
K    7
L    7
Name: col2, dtype: int32

In [87]: col.groupby((col != col.shift()).cumsum()).transform('count')
Out[87]:
names
A    4
B    4
C    4
D    4
E    1
F    1
G    1
H    1
I    1
J    3
K    3
L    3
Name: col2, dtype: int64

In [88]: col.groupby((col != col.shift()).cumsum()).transform('count').lt(3)
Out[88]:
names
A    False
B    False
C    False
D    False
E     True
F     True
G     True
H     True
I     True
J    False
K    False
L    False
Name: col2, dtype: bool

In [89]: col.groupby((col != col.shift()).cumsum()).transform('count').lt(3) & col.eq(0)
Out[89]:
names
A    False
B    False
C    False
D    False
E    False
F     True
G    False
H     True
I    False
J    False
K    False
L    False
Name: col2, dtype: bool


One way to detect overfitting is to monitor train- and validation error. If both are diverging during training an overfitting situation is given. With Keras you could set verbose=1 while training to monitor the metrics or you could plot your model history:

History = model.fit(...)
History.history #holds val and train error for all trained epochs


Does the score output of a classification model has a global meaning?

The output of the final layer, before softmax activation, is the log odds of the classes. Softmax isn't a linear function, so [10,20,30] going into softmax won't give the same answer as [1,2,3].


Transfer learning for a regression problem

The question is if you can provide the trained model with the data it expects to provide a good output.

In your example, for the model to provide an output, you will have to provide 15 inputs. If you have only 14, can you put a value for input 15 that will make sense to the model? If feature 15 is a person's age, can you make an estimate? If you trained a model on 16x16 images (256 features) but now you have images that are 4x4 (16 features), you may be able to just provide 256 - 16 = 240 black pixels and the model may work fine. Or you could reformat your data to multiply each pixel into 4x4 patch, and get a 16x16 image that way.

What you cannot do, is simply reshape the input so that it's 15 columns wide. If you do that, the first example you provide to your model will contain 14 features from the first real example, plus feature number 1 from the next case. That way, the model is using data from two different examples to make a prediction and it's getting a value for feature 1 on the input node where it expects a value feature 15, and feature 1 and feature 15 can have very different meaning.


Is the maximum BDeu Bayesian Network always the empty network?

Does it mean that the best network is always the empty network?

No.

The empty DAG (i.e. the DAG with no arcs) implies the factorization is simply $$P(X_1, \dots, X_n) = P(X_1) \dots P(X_n),$$ which is telling you all variables are independent.

This will be the best network (DAG) if all the variables are actually independent (Remember that independence for a dataset entails a certain tolerance), because the penalization coefficient will make the score lower as any arc is added.


Since we know that sin(x) can have -ve values as well,
Relu will kill all those mercilessly..

Also we can't have relu just before the output layer as it doesn't makes any sense..(Atleast I have never seen this architecture before..)

Also you can make that 100 drop to around 40

Check this link also


How can I get a forecasting model to improve its forecasts over time instead of fitting to training data?

Like you stated, you get good predictions based on the information in the model.

The model conflates demand and supply in the case of under-supply, since it has no information about unfulfilled demand: how many products were requested but not sold due to lack of supply. Instead it tracks $sold = min(demand,supply)$.

Extend the Model

The obvious solution is to have a separate feature for the true demand rather than the conflating it with unit sold. However, this data may not be available.

Learn from Correlations

Another option is to use richer model: if you have multiple products where most of them don't sell out (i.e. where $sold$ reflects the true demand) and their sales are correlated, then it can predict that a particular product needs to be resupplied at a higher rate than the observed sales. This prediction would be based on the correlations with products that don't hit the $min(demand,supply)$-cap and .

Reinforcement Learning

Finally, another approach is to look to Reinforcement Learning where the agent learns the best resupplying strategy by experimenting.

In a simple setup, your agent will select between two actions: one that orders according to its prediction in the normal case and another action that orders more than predicted. In the case where the prediction matches actual demand the probability of the over-ordering action will be quite low, but it will still be selected with some small probability $\epsilon$.

Reinforcement Learning allows the agent to learn the relative probability of the two, and based on the cases where the actual demand was higher than what you had in stock, it will slowly learn to order more with a higher probability until the prediction based on past sales increases to a point where there is no extra sales to gain from ordering more and the probability of taking the over-ordering action starts to fall again.


How can I handle missing categorical data that has significance?

Imputation and dealing with missing data a broad subject; you should start by researching standard material on this subject.

The first question to figure out is Why is some data missing?  and What is the process that causes data to be missing?  It's important to understand how this happens, because this will affect what solution is appropriate.

Randomly missing data

If data is missing totally at random (whether a value is missing does not depend on any of the feature values of that item), then imputation can be appropriate.  It should not create bias, if you do it appropriately.  There are many techniques for imputation.  You don't mention what you tried or why you think it will bias your results, but in general, if you use an appropriate method of imputation, there is no reason why it needs to bias your data.

Alternatively, you can use a classifier that can tolerate missing data.  Some classifiers are designed to handle missing data and can tolerate it.  However, I don't know of any reason to use them over imputation.

Non-randomly missing data

In contrast, if the chance for data to go missing for some object depends on the value of the features of that object, then you have a bigger problem.  In that case imputation can create bias -- as can any other method.  Your best hope is to understand in greater depth the random process that causes data to be missing and the probability distribution (probability that data goes missing, as a function of feature values), and try to design a procedure that is appropriate for that process.

Your specific situation: all features missing

Your specific situation is especially weird: it appears in your case either all features are missing, or none are.  That's a weird one.  For instances where the features are missing, you have absolutely no information about those instances.  So, the best classification decision in that case is probably a very simple rule: take whichever class appears most frequently in your training set (or, most frequently among instances with missing data).  Run the classifier on the remaining instances, i.e., the instances with no missing data.

But in real life this situation is pretty rare.  It's more typical that some features are missing and others are present, and that requires more work to handle.


It depends on the meaning of the classes, and whether they have any meaningful order.
If they are ordinal or a scale, then there is a meaningful ordering, and it can potentially be reasonable to order them and then assign labels $1, 2, \dots, n$ in order to the classes.  This is what you call "LabelEncode".  In some cases, some form of regression might also be appropriate and worth trying.
If they are categorical or nominal, with no meaningful ordering, then LabelEncode makes no sense and will be no better than a one-hot encoding -- and potentially worse.  So, the default is one-hot encoding.
If they are categorical but have some hierarchy, there are smarter methods.  You can build a tree that represents the hierarchy, then have a multi-hot encoding.  If there are $n$ classes, you will have a tree with $n$ leaves and (say) $m$ nodes in total  (where $m > n$).  Then you can encode the class as a $m$-vector, whose $i$th entry is 1 if the class is a descendant of node $i$ in the tree, or 0 otherwise.
If the classes are categorical but have various attributes, you can try an encoding that has one element for each possible attribute.  For instance, if there are $k$ binary attributes, you can have a $k$-vector with a 0 or 1 in the $i$th entry according to whether the corresponding class has the $i$th attribute or not.  You can generalize from there for more general situations.
See this and this for more about different types of measurements.


Next year forecasting with monthly data from many, correlated, non-monotonic trends

One approach could be to simply use standard time series models, like ETS/TBATS/ARIMA for each department. These methods will, with proper care, take into consideration specific trends you might see (as well as seasonality) albeit predicting when a trend will fall/how long it will last is an incredibly difficult task. This wouldn't be an invalid approach, but the problem here is that each time series will be fit in isolation and you seemingly have large amounts of similar time series along with valuable exogenous data, much of it that does not depend on time. The approach above would mean fitting a potentially large amount of time series models which is largely impractical and tedious (in my opinion) and in the case of ARIMA you would not be able to include variables that don't vary with time. There are multivariate extensions of ARIMA methods that could help, but if you have many different time series convergence is often a problem.

This is where I would suggest you look into regression algorithms (statistical or ML) with time (in some fashion) as a covariate. Your dataset would basically be expanded so that one row is the observed response variable at any specific month + year, for any given department. Your time covariates could be two columns; one with the year, another with the month, or simply a continuous variable like 2019 + 1/12, 2019 + 2/12, etc.

You would then use "department" as a categorical variable, along with perhaps lagged values of the response (to perhaps incorporate current trend information) or perhaps variables like "average health service production in last k month(s) by department" or "median health service production in past year over all departments = over all the data", etc. The choice of how many months to take, along with the level at which you aggregate to (over a department? over all departments? over specific departments only?) will be determined based on what you think is reasonable.

If you have time related events, you could model this with indicator variables, and also perhaps use variables like "months since this time related event has started/ended", etc. to perhaps capture lagged effects. Seasonality, if it exists, could be easily captured with indicator variables/Fourier series transforms. To obtain more trend features, you might also run another regression algorithm using the past couple months of data (from the time period for that specific observation/row of data) to get the overall current trend (at that point in time). Non-time related continuous variables (like "how much our patients have hip surgery in our hospitals versus other hospitals") is a simple join to the expanded dataset by department.

In many of these feature engineering ideas, you will end up with NA values at the start of the series which implies giving up maybe a few months of data at the beginning (I think this shouldn't be too big of a deal if you have time series' that are 120 units long). Regardless, I'd recommend using time series cross validation, using the most recent data as testing indices, and walking forward until you run out of data. In your case, each test set would be comprised of 12 months worth of data, and you would add one month of data at a time to the training set (moving the test sets time period one month ahead).

I hope this helps.


Dot product for similarity in word to vector computation in NLP

The reference points to the word2vec library (source code).
It does not use normalised vectors during training (although it indeed uses the cosine similarity metric for semantic comparisons on already trained vectors).
The reasons for using only dot product instead of cosine similarity during training can be due to:

Dot product is a variation of cosine similarity.
Length captures some semantic information in the sense that length can correlate to frequency of occurance in a given context, so using dot product only captures this information as well (although for strict similarity testing cosine metric is still used)
When vectors are normalised the two metrics coincide.
Efficiency (doing less computations)

References:

Why does the word2vec objective use the inner product (inside the softmax), but the nearest neighbors phase of it uses cosine? It seems like a mismatch.
Should I normalize word2vec's word vectors before using them?
Measuring Word Significance using Distributed Representations of Words
word2vec Parameter Learning Explained


Finding a proper method for an online driving style classification using acceleration data

Supervised vs. Unsupervised Learning

You will first need to decide whether to approach this as a supervised or unsupervised learning problem.

For supervised learning, you would need to take some portion of the data and hand score it as harsh/aggressive and normal.  I suggest you not go this route to start.  But if you do, I suggest using a support vector classifier for your initial testing.

I suggest approaching this as an unsupervised learning problem. Assuming you tend to be a normal/safe driver you can view this behavior as normal and harsh/aggressive driving as an anomaly or an outlier.  This is known as outlier detection.

Feature Engineering

Feature engineering will play a significant roll in this problem. Despite your attention to the longitudinal and lateral axes of the car, the phone will have arbitrary orientation within the car i.e. it will just be in your pocket.  Thus its probably best to factor out the direction somewhat.

First, I would find the length of the acceleration vector (magnitude of acceleration without regard to its directional orientation).  Since you have three orthogonal axes, you can just use:

$$||\mathbf{a}||=\mathbf{a}_x^2+\mathbf{a}_y^2+\mathbf{a}_z^2=a$$.

Additionally, you can also integrate the acceleration vector over time and find its magnitude to find the vehicle speed:

$$speed=||\mathbf{v}||=\mathbf{v}_x^2+\mathbf{v}_y^2+\mathbf{v}_z^2=v$$

where $$\mathbf{v}(t)=\int_{start-of-trip}^t\mathbf{a}(t)$$

The speed is very useful in determining the difference between different modes of acceleration e.g. driving around in a circle will yield constant acceleration, but also constant speed vs. holding down the gas pedal and driving straight, which could yield constant acceleration with changing speed.

An important aspect of this method lies in the fact that integrals of your data will tend to damp noise, where as derivatives (differences) will tend to amplify noise.  Don't take derivatives of your data.

Outlier Detection

At this point you can normalize the speed and acceleration-magnitude and find the mean and standard deviation of both values.  When your normalized speed and acceleration-magnitude lies outside of some z-value, say two standard deviations, then you can classify your driving as dangerous.

Don't forget to think of speed and acceleration as orthogonal values and form a circular function out of the respective z-values for proper outlier detection.  You can read about 2D or nD outlier detection and also here

Remember that the equation for a circle is:

$$r^2=a^2+v^2$$

We can therefore solve for $r$ and set it equal to $2$ standard deviations.

As an aside, note that since variance is just the square of the standard deviation, you could alternatively think of this in terms of variance.

Now for two standard deviations, the driving is aggressive if:

$$\sqrt{\big(\frac{v-\mu_v}{\sigma_v}\big)^2+\big(\frac{a-\mu_a}{\sigma_a}\big)^2} \gt2$$

The online learning component simply requires that you continuously update the means and standard deviations.  Though, I suggest that this be done in some sort of batch mode rather than continuously.

Hope this helps!


Business-related metric for an optimization problem

On historical data, you can try to determine what would have happened if you add the model before. For each client you proposed some product, calculate the average gain (propensity*cost) of said product and compare with the average gain of said of best product. Say each product cost 100$.
Last year you offered product1 to client1 but he refused. Using your model you know : he only had 20% chances of buying product1 and he had 25% chances of buying product 2. On average you missed out on (25%-20%)*100 = 5 dollars. You can calculate a similar metric on all your clients, than aggregate the total. That would give you an estimation of the amount of dollars you would have made last year if you used your model. There rarely is more business-related metric than money.


What does online learning mean in Topic modeling (LDA) - Gensim

Yes, your intuition about the definition of online learning in Topic modeling(LDA) is correct :

  "The model can also be updated with new documents for online training."

However, I would quote the standard definition of online learning in machine learning :

  It is a method in machine learning in which data becomes available in sequential order and is used to update our best predictor for future data at each step, as opposed to batch learning techniques which generates the best predictor by learning on the entire training dataset at once. It is a very useful technique in areas of machine learning where it is computationally infeasible to train over the entire dataset, requiring the need of out-of-core algorithms.

You can find more about it here.

In your case (nlp), online learning would -

Add vocabulary as the data comes.
Train the model on newly added corpus.
update the results

All of these would keep happening as the new data comes.


Problem faced when collect data randomly from cluster

The problem was understanding the index between train_unlabeled data and the cluster. There are no connection between index of train_unlabeled data set and the data that belongs to cluster. As I am using jupyter notebook, so the indexes given to itself. Now when I clustered those unlabelled data (index start from 344 to 863) into 2, then which data belongs which cluster it already takes its new index for cluster which is not same as previous index.

For better understanding I'm giving an example:

suppose for unlabelled data (train_unlabeled) the data those indexes are 344,345,346,347 and 348. After clustering 344 and 346 took their place at cluster 0 and 345,347,348 at cluster 1. While clustering the new index of 344 will be 0, for 345 will be 1, for 346 will be 2 and so on.

That's why when I print the indexes of data, belongs to cluster 0 and 1 (by using print (i, sample_i) this command) it just shows the index of data of its own cluster.

My mistake was I though that both indexes are same.

Now for keep similarity of indexes before and after cluster, I reset the the index of train_unlabeled data.

The command is: train_unlabeled = train_unlabeled.reset_index()

Here comes another problem, that is a new column named:index is added.

So, after resetting index (now, index starts from 0), I drop the extra index column by using this command : train_unlabeled =train_unlabeled.drop('index', axis=1, inplace=True)

And at last I cluster that unlabeled data (train_unlabeled) and would be able to take 95% data randomly from cluster by using cluster index.


What is the role of logistic function in logistic regression?

Because it makes the network learn the desired output (usually 1 or 0) much easier. It is possible to omit the logistic function (aka sigmoid function) and achieve desired results, but the network has to learn to specifically map each input to one or zero which is hard when it has the ability to map it to any real number.
The logistic function makes it easier because the network just has to learn to map it to a positive or negative number.

Now the network can map an input to -6 if it wanted to and still produce the desired output -1, whereas before it has to output specifically -1.

Also, @Media logistic regression doesn't need a non-linearity for this purpose. It will still only be able to separate the data in a linear fashion.


How to give name to topics created using LDA?

I can suggest several papers on this topic:

Automatic Labelling of Topic Models
Automatic Labeling Hierarchical Topics
Representing Topics Labels for Exploring Digital Libraries

You can find more by looking at their citations.


This line of code should do it for you :

df.groupby(["order number", "working area"])['time'].agg(sum)


Binary classfication vs One-class classification

Binary classification is needed when requirement is to capture data into two classes. if you can't capture data in two classes where you need only one you go for One-class.
you can check this link for better explanation
http://rvlasveld.github.io/blog/2013/07/12/introduction-to-one-class-support-vector-machines/

If you take binary classification, svm tries to find best possible space between A and B. If there is only one class A model tries to create a boundary around it and classify. Take for example patient disease classification: For +ve some symptoms t1, t2, t3, t4, t5 for -ve he has t1, t2, t7. in the above case it is difficult to classify using one class because model classifies patient having t1, t2 as +ve because of proximity to +ve class. The second label gives you more info for better classification.


The short answer is yes.

What you are looking at would be developing a model that would regress an input of 6 integers to a single integer between 0 and 100.  $f(x) : \mathbb{Z}^6 \rightarrow \mathbb{Z}^1$

This could also be done as as 100 class classification problem.

The regression approach would be better in my opinion.

One big question that you need to know to better determine if machine learning is the correct approach:  are the sequences (e.g., 0 3 3 5 3 2) the only sequences that = the given ranks (e.g.,1).   If so you only need to match sequences, no need for M L.

If no, and you chose to develop an ML approach, an issue you have is that you only have 1 observation for each integer.

Depending on the variability of the input integers and their output rank {1 - 100} you would want multiple observations corresponding to each integer.

One last thing, I would start with one linear and one non-linear algorithm to see which method fits this data best.


I think I found the reason: the correct description of the 2nd layer is:

for the 2nd layer:

input size: (1, 26, 26, 32)
weights size: (3, 3, 32, 64)
output size: (1, 24, 24, 64)

So for each one of the 64 features, the (26,26,32) input is convoled with a (3,3,32)-sized kernel, producing a (24,24) output.

Since this is for each one of the 64 features, the output will finally be (1, 24, 24, 64).

Code to display the shape of the weights:

for l in model.layers:
    if len(l.get_weights()) > 0:
        print(l.get_weights()[0].shape)        # ...[1].shape  would be for the biases


Weighted clustering of lat lon coordinates

Leader clustering is so simple, the weight does not make a difference.

It assigns points to a cluster if the distance is less than a threshold. It does not matter whether the point has 1 weight or is a "square" of 100.


Acurracy = 0 using tensorflow on tabular data

Accuracy is for classification problems (and usually not the best metric anyway). Now, with integer outputs you could treat this as a multiclass classification problem, but you'd need to encode things properly for tensorflow to understand. I'd guess (without really knowing your data, mind) that you'd be better off with regression (so no cross-entropy either).

You also can't use a sigmoid activation on the last layer; it's forcing output predictions between 0 and 1. If you go for regression, you won't need to adjust you true labels, you can use MSE as your metric, and you can just put a linear activator on the last layer.


Can I accurately call sklearn.model_selection.train_test_split multiple times when data doesn't fit into memory?

This should be fine. If the data is independent of its position in the list, this should give basically identical results.

If the data depends on order, then grabbing some data for training and testing from each batch will have a smoothing effect, making the training and test sets more representative than the results you would get with a raw train_test_split.


Tensorflow (or Keras) vs. Pytorch vs. some other ML library for implementing a CNN

If you are looking for something easy to use and to read, definitely go for Keras.

Example of CNN in Keras :

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

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

So easy to read !

Source, literally the first link when searching for "keras CNN" on Google.

I really enjoy Keras, because it's easy to read, easy to use, great documentation, and if you want to mess up things at lower level you can do it by touching the back-end of Keras (Tensorflow or Theano)

EDIT (following your comment)

Excellent blog : Keras vs Tensorflow


Getting to other parameter sets from GridSearchCV in sklearn

If the parameter refit is set to True, the GridSearchCV object will have the attributes best_estimator_, best_score_ etc. ("Best" measured in terms of the metric provided through the scoring parameter.)

For other models you can only access the cross-validation scores from cv_results_. The models are not stored. If you want to use them to make predictions, you have the re-train the model where you explicitly set the parameters.

If you want to use all the models to make predictions, it would be better to write your own code and simply loop over the parameter constellations without using GridSearchCV.


Welcome to the site!

I think the 53 category issue is specific to R's randomForest package. If you're set on using a random forest, try other packages like ranger and Rborist.


Visualize the predicted and actual class after training and testing

The whole essence of cross-validation is to check the model with various sets of data and to know how well it predicts with 'unseen' data. So, with the 10 folds, you create 10 different training and test/validation set. But, the model remains the same.

What you are trying to ask is which model to choose. There is only one model. We are only checking the model that's already built using cross-validation.

After checking the model you have built, use the complete dataset to train and predict the labels on test set using this final model.

Usually, the dataset is split into training, validation and test set. Test set is not touched till the final model has been built. The test set you create during cross-validation is actually the validation set. The ability of the model to predict well is validated on this set.

Hope it helps!

Update:
fitcsvm() is what trains the data. This is the modelling bit. What you are doing is right. What you need to understand is that cross-validation is used for checking the model. There would be cases where one model wouldn't predict as well as another one. Testing with only one test would not give good insights on how well the model is working. This is why, we use cross-validation - to test the model with different training and test sets.

10 folds for only 50 records might be an overkill. 2-3 folds would suffice.


Reinforcement learning: understanding this derivation of n-step Tree Backup algorithm

The slides and the book are consistent. Notice how in the slides there is a restriction in the summatory: i.e.  $a \neq A_{t+1}$. For $G^{(2)}$, you need to "remove" from $V_{t+1}$ the term that should not be there, i.e. $A_{t+1}$.

Now, why this term is removed?

If you keep this term you will be adding $A_{t+1}$ twice. In 1-step backup, it is part of the expectation of step $S_{t+1}$.

When you calculate 2-step backup you want to replace $(S_{t+1}, A_{t+1})$ in the 1-step expectation with the discounted expected value of $S_{t+2}$.
So you substract the term and add the discounted expectation for $S_{t+2}$


How to replace short words into full words from tweets using python

I have worked on lexicon level token normalization on twitter data, which seems to be very similar to what you are trying to do. In our task, we were not only normalizing short word, but also "welllll"->"well", "lysm"->"love you so much", "2"-> to, etc. Refer to the task description here. At that time we did not find a good library directly for this job.

In our approach, we resort to buidling a lookup table to replace suspicious words. The lookup table is compiled from the following resources:

Huge List of Texting and Online Chat Abbreviations

Twitter Dictionary: A Guide to Understanding Twitter Lingo

Lexical normalisation of short text messages: makn sens a #twitter

Insertion, Deletion, or Substitution? Normalizing Text Messages without Pre-categorization nor Supervision

The latter two links are two relevant papers. They both include a dataset that we used in our lookup table.

The suspicious words that need to be replaced can be simply identified by whether they are OOV, or using a more complicated machine learning approach to determine based on context.