Hyperparameter optimization, ensembling instead of selecting with CV criteria While burning CPUs performing a CV selection on a thin grid put on some hyperparameter space. I am using the `scikit-learn' API, for which the end result is a single point on the hyperparameter space, for which the performance is the best according to chosen metric. It looks like a lot of information is trashed, we are probing the performance landscape of the hyperparameters and keeping a single point estimate. Information such as having many modes, local maximas, maybe far apart. An ensembler method could make good use of such information. Is there a theory built over ensembling a diversity of hyperparameter estimates?

I think you are looking for hyperopts, Optuna and Gpopy for Hyperparameters search without burning much of CPUs.


How to train data by batch from disk? I am working on a convolutional neural network for image classification. The training dataset is too large to be loaded on my computer memory (4gb), on top of that I also need to try some augmentation to balance the classes.

I am using keras. I have looked into many examples but I have not found a solution to my problem. I am thinking about replacing the parameters of the model.fit call by passing a function or generator doing the 'batching'. This function I will design to import from disk and apply augmentation.

I have no idea how to implement that. Any suggestions?

As you are working on image classification and would also like to implement some data augmentation, you can combine the two AND load the batches directly from a folder using the mighty 'ImageDataGenerator` class.
Have a look at the execellent documentation!
I won't copy and paste the example from that link, but I can outline the steps that you go through:

create the generator class:   data_gen = ImageDataGenerator()

If you want it to perform on-the-fly augmentation for you, that can be    specified when creating the class: data_gen = ImageDataGenerator(samplewise_center=True, ...)

If you use augmentation processes that require some statistics about the dataset, e.g. feature_wise normalisation (not sample-wise), you must prepare the generator by showing it some of your data: data_gen.fit(training_data). This fit method simply precomputes things like the mean and standard deviation, which is later used for normalisation.

the generator goes into the model's fit_generator method, and we call the flow_from_directory method of the generator:
model.fit_generator(training_data=data_gen.flow_from_directory('/path/to/folder/'), ...)

You can also create a separate generator using ImageDataGenerator for your validation data, where you should then not apply the augmentation, so that validation tests are done on real data, giving you and accurate picture of the model's performance.
In any case, these generators will theoretically run forever, generating batches from your folder. Therefore I recommend using a callback function from Keras to stop when a certain criteria is met. See the documentation for the EarlyStopping class. You could also do this manually, but Keras makes it very simple!
If you want fine-grained control, you could do all of the above manually, loading enough samples from disk for a single batch, performing some augmentation and then running the model.train_on_batch() method. If you want to get into the details, you might be best first learning the Keras way, then progress to your own detailed models combing Tensorflow as required. The two can be used toegether very nicely!


Please help me find the mistake I'm trying to use Tensorlow on İris dataset using sublime as the text editor. I got this error "TypeError: float() argument must be a string or a number, not 'generator'"when running the session, but I haven't any Idea about it. Please help me out. Attached is the error screenshot.

I don't know precisely what's happening under the hood, but your list comprehensions on lines 31-33 are wrong and causing numpy to produce some funky arrays.

If you pull each of them out of the np.array calls and attempt to run them, you'll generate a nice SyntaxError (which is nice because it'll loudly tell us what's wrong!).
Right now, the error is getting suppressed by the fact Python is taking that funky list comprehension and creating a generator expression (I'm guessing due to how Python manages argument packing).

I'm unsure of the intent of lines 31 and 32 (are they supposed to be individual records from the data?), but line 33 can be rewritten as:

rand_y = np.array([binary_target[y_i] for y_i in rand_index])


Recurrent (CNN) model on EEG data I'm wondering how to interpret a recurrent architecture in an EEG context. Specifically I'm thinking of this as a Recurrent CNN (as opposed to architectures like LSTM), but maybe it applies to other types of recurrent networks as well

When I read about R-CNNs, they're usually explained in image classification contexts. Theyre typically described as "learning over time" or "including the effect of time-1 on the current input"

This interpretation/explanation gets really confusing when working with EEG data. An example of an R-CNN being used on EEG data can be found here

Imagine I have training examples each consisting of a 1x512 array. This array captures a voltage reading for 1 electrode at 512 consecutive time points. If I use this as input to a Recurrent CNN (using 1D convolutions), the recurrent part of the model isn't actually capturing "time", right? (as would be implied by the descriptions/explanations discussed earlier) Because in this context time is already captured by the second dimension of the array

So with a setup like this, what does the recurrent part of the network actually allow us to model that a regular CNN can't (if not time)?

It seems to me that recurrent just means doing a convolution, adding the result to the original input, and convolving again. This gets repeated for x number of recurrent steps. What advantage does this process actually give?

The recurrent part of a network allows you, generally speaking, to model long and short term dependencies. So your model can have some sense of state.

This is typically advantageous if you're using timeseries. For example, if you have data from a heart rate monitor and like to like to classify between resting, stress and recovering. If your datapoint says your heart rate is at 130, it depends on whether you are recovering from high loads or something else.

Edit:
I forgotten your second question.

  It seems to me that recurrent just means doing a convolution, adding the result to the original input, and convolving again. This gets repeated for x number of recurrent steps. What advantage does this process actually give?

I could think off a few possible answers. By convoluting the recurrent part you kind of filtering it. So you get a cleaner signal and errors won't stack as much. Vanilla rnn suffer from exploding vanishing gradients, so this could be his approach to overcome it.
Furthermore, you are embedding your features within the rcnn, which can lead, as he stated, to more paths to exploit. Which makes it less prone to overfitting, thus more generalizable.


Why is eulers number used as a constant in sigmoid I was asking myself why eulers number was used in the sigmoid function 1/(1+e^-x) instead of any other constant like for example 2 or 3?

I am pretty new to data science stuff, but I read somerwhere that eulers number is the natural growth of a curve, so would this mean, that eulers number is used in the sigmoid function because it makes it possible to output values that are evenly distributed between the values 0 and 1?

Euler's number pops up in a lot of places naturally; not quite something to do with growth rates but arises easily in common limits.

The form of the sigmoid function wasn't chosen because its derivative has a nice property, although that's true. It also wasn't chosen because it's a function with range (0,1); many functions do that.

The sigmoid function arises because it's the correct answer to a common type of problem. We think of logistic regression as a common classifier predicting a class probability, but it really is regression under the hood. It's not regressing the probability though, but log-odds of the probability (logit function). This is the right way to apply regression, given assumptions about the distribution of errors in a classification problem, which aren't the same as in a simple linear regression.

The sigmoid function is the inverse of the logit link function. That's why it's there. It gets from the regression output to the actual desired output, a probability. The logit function is there because it is implied by the assumption about the distribution of the 0/1 dependent variable.

That's actually it. It has to be there in certain types of common problems because it's the answer to those problems, not because it was chosen for nice properties.


Training a Graph model like an Artificial Neural Network I currently have a Graph model whereby I am mapping connections of different types between entities and attributing a weight to these connections based upon my own personal experience. Also, I would like to understand the connections between these entities in relation to a particular outcome. Looking at this problem, I can't help but notice it's similarity to a typical Artificial Neural Network (ANN) and am wondering if/how I can bake some of the theory there into my model.

Let me explain further; I have a set of entities for which I have sets of connections. Each different type of connection has it's own individual weight, as shown in this diagram:

The weights in this diagram are estimated based on my own experience and can then be summed to look at the strength of a relationship between these entities. It is this strength of relationship that I want to understand in relation to a particular Output/Outcome.

I can't help but notice when looking at this particular application of Graph Theory that it bears a large resemblance to an Artificial Neural Network (ANN), as shown in this diagram;

The difference here being that whilst in the first diagram the nodes represent entities, in the second diagram the nodes represent input values. In the ANN, input values, randomized weights and the process of back-propagation are are used to help determine what the weights need to be whilst in the first diagram, these values are estimated. What I want to be able to do is to use randomized weights and the process of back-propagation to determine the weights in my Graph model that maps the connections between entities.

Does anyone have any experience of doing anything like this or could maybe at lest discuss the possibility of doing something like this?

Thanks!

First, a note: don't let the graph structure make you think that this can be addressed with neural networks; this structure only suggests that this is a hierarchical model.

Second, let's take into account the following:

In your first figure, you don't connect A, B or C to the output. I assume that there is a relationship from A,B,C to output.
You mention that you have expert a priori knowledge on the relationships from A to B and from A to C.
I also assume that A, B, C and the output are numeric variables.
We don't know whether A, B, C and the output can be observed or not.
We don't know why there are 3 lines from A to C and 2 lines from A to B, so I will not take this into account.

You may want to provide more information if any of the assumptions above is not correct.

Now:

If only A and the output are observable, we are talking about a latent variable model. Latent variables are best suited for Hierarchical Bayesian methods. If the relations between variables are linear, you can model this as a bayesian linear model, e.g. hierarchical bayesian regression to model your variables and inject your prior knowledge as prior distributions over the weights. Here you can see the application of hierarchical bayesian regression to a latent variable model. This kind of methods are simple and interpretable, they handle prior knowledge graciously and they handle uncertainty.

If all variables are observable, you can define a combination of different extremely simple "neural network" models to approach your problem: Network 1 would receive A and output C; Network 2 would receive A and output B; Network 3 would receive A, B and C and generate the output. If the relationships among variables are linear, Network 1 and Network 2 are simple multiplications. You can then initialize their weights with your prior knowledge. The training would adjust the weights for everything to fit, but will not give you insight about the relationships except the fitted weights.

I suggest you think about whether bayesian hierarchical models can fit your problem.


Stata-style replace in Python In Stata, I can perform a conditional replace using the following code:

replace target_var = new_value if condition_var1 == x & condition_var2 == y

What's the most pythonic way to reproduce the above on a pandas dataframe? Bonus points if I can throw the new values, and conditions into a dictionary to loop over.

To add a bit more context, I'm trying to clean some geographic data, so I'll have a lot of lines like

replace county_name = new_name_1 if district == X_1 and city == Y_1
....
replace county_name = new_name_N if district == X_N and city == Y_N

What I've found so far:

pd.replace  which lets me do stuff like the following, but doesn't seem to accept logical conditions:

`

replacements = {   1: 'Male',   2: 'Female',   0: 'Not Recorded' }

df['sex'].replace(replacements, inplace=True)

`

df.where(condition, replacement, inplace=True)

Condition is assumed to be boolean Series/Numpy array. Check out where documentation - here is an example.


Why a restricted Boltzman machine (RBM) tends to learn very similar weights? These are 4 different weight matrices that I got after training a restricted Boltzman machine (RBM) with ~4k visible units and only 96 hidden units/weight vectors. As you can see, weights are extremely similar - even black pixels on the face are reproduced. The other 92 vectors are very similar too, though none of weights are exactly the same.

I can overcome this by increasing number of weight vectors to 512 or more. But I encountered this problem several times earlier with different RBM types (binary, Gaussian, even convolutional), different number of hidden units (including pretty large), different hyper-parameters, etc.

My question is: what is the most likely reason for weights to get very similar values? Do they all just get to some local minimum? Or is it a sign of overfitting?

I currently use a kind of Gaussian-Bernoulli RBM, code may be found here.

UPD. My dataset is based on CK+, which contains > 10k images of 327 individuals. However I do pretty heavy preprocessing. First, I clip only pixels inside of outer contour of a face. Second, I transform each face (using piecewise affine wrapping) to the same grid (e.g. eyebrows, nose, lips etc. are in the same (x,y) position on all images). After preprocessing images look like this:

When training RBM, I take only non-zero pixels, so outer black region is ignored.

A restricted Boltzmann machine (RBM) learns a lossy compression of the original inputs or in other words, a probability distribution.

Those are 4 different weight matrices are all reduced dimension representations of the original face inputs. If you visualized the weights as a probability distribution, the distributions value would be different but they would have the same amount of loss from the original image reconstruction.


ROC curves/AUC values as a performance metric I want to plot ROC curves using R. I have a prediction matrix, where each column shows the prediction values corresponding to different approaches. Also, I have a label vector. The column names of prediction columns are ccs,badaI,badaII and the column name of label vector is value. I am using ROCRlibrary for this as:

library(ROCR)
pred1 <- prediction(df$ccs,df$value)
roc <- performance(pred1,"tpr","fpr");
pred2 <- prediction(df$badaI,df$value)
roc2  <- performance(pred2,"tpr","fpr")
pred3 <- prediction(df$badaII,df$value)
roc3 <- performance(pred3,"tpr","fpr")
auc<- performance(pred,"auc")
auc = round(unlist(auc@y.values),2)
auc2<- performance(pred2,"auc")
auc2 = round(unlist(auc2@y.values),2)
auc3<- performance(pred3,"auc")
auc3 = round(unlist(auc3@y.values),2)
plot(roc,col="black",lty=1, lwd=4, cex.lab=1.5,axt="n")
axis(1,cex.axis=1.0);axis(2,cex.axis=1.0)
plot(roc2, add=TRUE,col="black",lty=3, lwd=4)
plot(roc3, add=TRUE,col="black",lty=2, lwd=4)
abline(0,1,col="gray60")
legend(0.3,0.30,c(paste0("CCS, ","AUC = ",auc),paste0("BADAI, ","AUC = ",auc2),paste0("BADAII, ","AUC = ",auc3)),
      lty=c(1,3,2), col=c('black','black','black'), lwd=4,cex=1.4,bty="n")

While using the above code,I am getting following plot:

I have doubt as:
 While looking at the data, It is obvious that ccs and badaII should have higher AUC values than badaI, but the results are somehow opposite. Can anyone help me in understanding why it is behaving like this?
The dput of the data used, df is:

structure(list(ccs = c(0.16, 0.04, 0.18, 0.09, 0.14, 0.14, 0.04,
0.04, 0.08, 0.76, 0.03, 0.03, 0.68, 0.06, 0.83, 0.15, 0.07, 0.02,
0.93, 0.22, 0.28, 0.11, 0.05, 0.01, 0.17, 0.15, 1, 0.13, 0.23,
0.44, 1), badaI = c(0.61, 0.11, 0.53, 0.79, 0.75, 0.82, 0.57,
0.67, 0.4, 0.95, 0.49, 0.61, 0.97, 0.52, 0.98, 0.7, 0.03, 0.18,
0.85, 0.94, 0.9, 0.77, 0, 0.37, 0.47, 0.88, 0.99, 0.55, 0.86,
0.96, 0.99), badaII = c(0.32, 0, 0.27, 0.12, 0.33, 0.12, 0.56,
0, 0.32, 0.18, 0.18, 0.11, 0.18, 0.54, 0.37, 0.33, 1, 0.39, 0.29,
0.11, 0.32, 0.53, 0.25, 0.21, 0.15, 0.16, 0.85, 0.31, 0.44, 1,
1), value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1)), .Names = c("ccs",
"badaI", "badaII", "value"), row.names = c(NA, -31L), class = "data.frame")

UPDATE
I use below figure to further explain my intuition. This figure is drawn using the same predictor values.

According to value column, observation with numbers 15, 27, 30, 31 have labels as 1 and the remaining observations have 0 value. While looking at above figure it is clear that CCS or badaIIare best in discriminating the difference between 0 and 1 as compared to badaI, which always provide higher predictor values.In other words, I mean with badaI, it is difficult to predict 1 as its values are higher for both 0 and 1.

I am not able to correlate my intuition with the ROC plot. @TBSRounder, I understood what you have mentioned, but I need to support the above figure with the ROC plot, which I find disappointing. Can anyone help me to correlate the above figure with the ROC plot?

The results you posted are correct, I did a quick check with library(pROC) and got the same thing.  The important feature for AUC/ROC is that the cutpoint of calling a sample "1" or "0" is not set at 0.5.  For each of your predictors, the range of the numbers do not matter at all.  What does matter is how often higher numbers are associated with true positive labels.  Example:

Method 1:[1, 3, 5, 6, 9, 14] (predictor)
Method 1:[0, 0, 1, 0 ,1 , 1] (true label)
AUC: 0.8889

Method 2:[[0.01, 0.02, 200 , 250, 300, 1000000] (predictor)
Method 2:[0, 0, 1, 0 ,1 , 1] (true label)
AUC: 0.8889

The AUC measure is just a measure of how often true "1" samples have a higher number than true "0" samples.  The predictor does not have to even be bounded by [0,1], it can be any range.

Your plot would be more informative if it was sorted.  Consider the below sorted plot (crude, sorry) for each of the three methods, with the "1" labels highlighted in blue.  badaI clearly has the closest grouping of "1"s towards the higher values.


Does a NB wrapper consider feature subset size? while comparing two different algorithms to feature selection I stumbled upon the follwing question:

For a given dataset with a discrete class variable we want to train a naive bayes classifier. We decide to conduct feature selection during preprocessing using naive bayes in a wrapper approach.
Does this method of feature selection consider the size of the used feature subsets?

When considering how NB classifies a given instance, the size of the feature subset being used for classification only influences the number of parts that the product of the conditional dependencies has but that does not make a difference, or does it?

It'd be great if someone could offer a solid explanation since for me it's more of a gut feeling at the moment.

Short Version

The difference is in the trade-off between complexity of model and how hard it will be to train.  The more states your variable can take, the much more data you'll need to train it.  It sounds like you're reducing the feature space through some front-end processing.

Example

Context:

Say you're using NBC to model if you go outside given the high temperature that day.  Let's say temperature is given to you as an int in Celsius which just happened to range between 0 and 40 degrees.  Over one year of running the experiment - recording the temperature and if you went outside - you'll have 365 data points.

Internal Representation:

The internal structure the NBC uses will be a matrix with 82 values (41 values for your one input variable * 2 because you have two states in your output class).  That means each bin will have an average of 365/82 = 8.9ish samples.  In practice, you'll probably see a few bins with lots more samples than this and a many bins with 0 samples.

A Pitfall

Say you saw 8 cases at a temperature of 5 C (all of which you stayed inside) and nothing at a temp of 3 or 4 C.  If, after the model is built, you 'ask it' what class a temp of 4 C should be in.  It won't know.  Intuitively, we'd say "stay inside", but the model won't.  One way to fix this is to bin the classes 0,1,2,... into larger groups of temperatures (i.e., class 0 for temp 0-3, class 1 for temp 4-7, etc).  The extreme case of this would be two temperature states "high" and "low".  The actual cut-off should depend more on the data observed, but one such scheme is converting temperatures within 0-20 to "low" and 21-40 is "high".

Discussion

This front end processing seems to be what you're talking about as the "wrapper" around the NBC.  The result of our "high" and "low" binning scheme will result in a much smaller model (2 input states and 2 output classes gives you a 2*2 = 4 number of classes.  This will be way easier to process and will take way less data to confidently train at the expense of complexity.  One example of a drawback of such a scheme is:  Say the actual pattern is that you love spring and fall weather and only go outside when it's not too hot or not too cold.  So your outside adventures are evenly split across the upper part of the "low" class and the lower part of the "high" class giving you a really useless model.  If this were the case, a 3-class bin scheme of "high", "medium", and "low" would be best.


Is it wrong if I cluster numerical attributes and categorical attributes separately? I have a dataset of credit customers containing mixed data types (numerical and categorical with several levels). I am trying to perform segmentation so that I can end up with k groups and then build definitions (based on attributes I have).

While there are solutions for clustering data with mixed data types (K-prototypes, hierarchical clustering with Gower's distance), why would it be wrong to cluster numerical attributes and categorical attributes separately and come up with definitions individually?

Generally, clustering on separate categorical and numerical features is wrong since it could lead to merging the otherwise separate clusters. Here is a visual example of why this may fail (drawn by myself):

If we cluster only on the categorical feature, clusters C1 and C2 would be merged. If we cluster only on the numerical feature, all three clusters would be merged. Therefore, clusters C1 and C2 could not be found separately.

As a side note, this blind separation is different than a careful feature selection (mentioned in this answer) which could end up with both categorical and numerical features.


Problem of finding best combination of features when desired feature is feature some_feature_A/some_feature_B Problem is stated: we have giant csv file with one target column and rest are inputs, we don't know these features impact target but we would like to use algorithm that besides using linear and non-linear transformations will also take into account that right solution would be some_feature_A/some_feature_B. Is there algorithm that will allow to take this case into account? One way is to craft these feature columns yourself but is there better way?

In theory, I think a deep neural net might be able to find features that are the product of two other columns.  There exist some nice mathematical results which guarantee the ability of a neural net (with certain activation functions) to approximate any function, so there's no theoretical reason why a neural net couldn't compute the division function $f(x, y) = \frac{x}{y}$.  That being said, it may be difficult to achieve in practice without a little preprocessing.

If you want to give it a try, I would suggest adding additional features obtained by taking the logarithm of any numerical features in the dataset.  This might make it easier for the network for learn features that are products of other features (since $\log{(x * y)} = \log{x} + \log{y}$ and $\log{x / y} = \log{x} - \log{y}$).


What's the correct form of use the real coded genetic algorithm? I'm new to genetic algorithms, but I haven't found specific info about real-coded GA's. I want to do antenna array optimization by using the real values of antenna position, phase, and amplitude, but I've only found binary encodings. Although the binary encodings seem easier during crossover, but I would like to use real continuous values, but I don't know how to apply crossover.
In MATLAB, I've been using an error value as a fitness function obtaining a vector with all the error data and using this to select the new population. I'd also appreciate a source describing the real continuous encoding process.

There are codifications in Genetic Algorithms which use continuous vectors instead of binary vectors, is not mandatory for all GA to work with binaries.

MATLAB also has a function called "ga" which works with these vectors.

Matlab's help on ga function


Is this overfitting? I read about the validations curves, and the following plot is similar to overfit, but in this case, the validation curve doesn't' growth again.
So is this overfit? why?
Thanks

So, overfitting occurs when the model is complex enough to fit very well with examples observed in the training data, such that the model is not able to generalise well over unseen instances (validation data).
Therefore, for overfitting, we expect the training F1 score to continually decrease, whilst the valid_1 F1 score increases.
Here, the plot shows that both training and validation F1-score has stabilised over epochs/iterations. Arguably though, we can see that valid_1 marginally increases as the training F1 score decreases. This can be indicative of (very mild) overfitting.


R vs. Python Decision Tree From my experiences the R Decision tree returns more accurate results than the python decision tree. Can anymore confirm this assumption and maybe knows the reason?

Decision trees involve a lot of hyperparameters -

min / max leave
size
depth of tree
criteria for splitting (gini/entropy) etc

Now different packages may have different default settings. Even within R or python if you use multiple packages and compare results, chances are they will be different.

There is nothing which suggests R is "better"

If you want to get the same results, you need to make sure the implicit defaults are similar. For instance, try running the following:

fit <- rpart(y_train ~ ., data = x_train,method="class",
             parms = list(split = "gini"),
             control = rpart.control(minsplit = 2, minbucket = 1, xval=0, maxdepth = 30))

(predicted5= predict(fit,x_test))
    setosa versicolor virginica
149      0  0.3333333 0.6666667

Here, the parameters minsplit = 2, minbucket = 1, xval=0 and maxdepth = 30 are chosen so as to be identical to the sklearn-options, see here. maxdepth = 30 is the largest value rpart will let you have;

sklearn on the other hand has no bound here. If you want probabilities to be identical, you probably want to play around with the cp parameter as well.

Similarly, with

model = tree.DecisionTreeClassifier(criterion='gini',
                                    min_samples_split=20,
                                    min_samples_leaf=round(20.0/3.0), max_depth=30)
model.fit(iris.data, iris.target)

I get

print model.predict([iris.data[49]])
print model.predict([iris.data[99]])
print model.predict([iris.data[100]])
print model.predict([iris.data[149]])
print model.predict([[6.3,2.8,6,1.3]])

[0]
[1]
[2]
[2]
[1]

which looks similar to your initial R output.

All in all, I believe the defaults in R are better suited for the dataset that you are working on, hence the "better" results. But rest assured, they are similar given the parameters are explicit and equal.

Hope this helps!


Limitations on the number of items to use in apriori algorithm? Are there any limitations on the number of items to use in a transaction for applying apriori algorithm.

I have a dataset with just 20 records, but the number of items extend upto 900. I am getting memory issue when I apply the apriori algorithm on the dataset.

Thank you

A first count on items can prune some of them. If you have only 20 records you can simply count the occurrence of items in dataset and prune those items which did not happen enough because their supersets also do not happen frequently and them make item-sets based on the rest of items.


The principle of LM deep model Language model(LM) is the task of predicting the next word.

Does the deep model need the encoder? From the ptb code of tensor2tensor, I find the deep model do not contains the encoder.

Or both with-encoder and without-encoder can do the LM task?

The goal of LM is to learn a probability distribution over sequences
  of symbols pertaining to a language.

That is, to learn $P(w_1,...,w_N)$ (resource).

This modeling can be accomplished by

Predicting the next word given the previous words: $P(w_i | w_1,...,w_{i-1})$, or
Predicting the neighbor words given the center word (Skip-gram): $P(w_{i+k}| w_i), k \in \{-2, -1, 1, 2\}$, or
Predicting the center word given the neighbor words (CBOW or Continuous Bag-of-Words): $P(w_i| w_{i-2},w_{i-1},w_{i+1}, w_{i+2})$, or other designs.

  Does the deep model need the encoder? From the ptb code of
  tensor2tensor, I find the deep model do not contains the encoder.

Yes. Modern LM solutions (all deep ones) try to find an encoding (embedding) that helps them to predict the next, neighbor, or center words as close as possible. However, a word encoding can be used as a constant input to other models. The ptb.py code calls text_encoder.TokenTextEncoder to receive such word encodings.

  Both with-encoder and without-encoder can do the LM task?

LM task can be tackled without encoders too. For example, we can use frequency tables of adjacent words to build a model (n-gram modeling); e.g. all pairs (We, ?) appeared 10K times, pair (We, can) appeared 100 times, so P(can | We) = 0.01. However, encoder is the core of modern LM solutions.


What are the most concrete and easiest to understand applications of deep learning in the industry? I am a researcher in deep learning, and have given courses on the theory. Recently I was approached to give the same course adding sections using some very concrete examples of deep learning for companies.

My audience will be industry managers never exposed to deep learning before. What are the most easily performed examples I could provide without having to develop millions of lines of code that can be of general interest to managers never exposed to deep learning before? These examples need to be quite concrete and easily applicable.

In particular, they look for examples that can have the label "big data" slapped on them.

I know about using ML for detecting digits, or all the other applications on kaggle, but they do not seem to care for those.

This seems like a matter of opinion to me, but here are some applications of machine learning that you seen very often in Silicon Valley:

Advertising: what to show whom and when? The data comes fast and furious so the engineering part is quite sophisticated.
Recommendation: similar to the advertising problem, except there are no advertisers, and the volume is slower, so you can afford to use more sophisticated machine algorithms.
Security: did somebody outside penetrate your system? did somebody inside abuse your system?
Text/audio/video processing: information retrieval on media. Requires finding good representations of the items using machine learning; e.g., fetch all pictures similar to the input, where the similarity can be construed in various ways, such as similarity of texture, style, color, content, etc.

These are not necessarily simple, but they're representative. If you want to give a demo, I suggest the last one. You could show them how to find similar images.


Preprocessing: StandardScaler() Do we really need mean to be zero? For instance, many elements used in the objective function of a
learning algorithm (such as the RBF kernel of Support Vector Machines
or the l1 and l2 regularizers of linear models) assume that all
features are centered around zero and have variance in the same order.

This is from the scikit-learn
Can someone please specify what elements they are referring when it is said the mean is expected to be zero.
I understand that the variance should be in similar range for the algorithms to give same significance to each feature. But is there anything also necessarily expecting zero mean in the data?
In other words, if I know variance of each feature is already in same range, can something still go wrong as it says some "elements" expect zero mean.

The reason is the same.
 I assume you understand how the Features at a very different scale can create issue
 But just scaling will not always bring them on the similar scale because Standard Deviation is dependent on the Range of the Feature.
So, if a feature is very large but in a small range then simply scaling it will not help.
Let's check an example with two features
import numpy as np
np.set_printoptions(precision=2)

feat_A = np.array([1,2,3,4,5])
feat_B = np.array([10000000,10000001,10000002,10000003,10000005])

# Case I  - Only Std
print(feat_A/feat_A.std())
print(feat_B/feat_B.std())

# Case II - Mean and Std
print((feat_A-feat_A.mean())/feat_A.std())
print((feat_B-feat_B.mean())/feat_B.std())

Output
Case - I - [You may see, this doesn't solve the problem]

[0.71 1.41 2.12 2.83 3.54]
[5812381.94 5812382.52 5812383.1  5812383.68 5812384.84]

Case - II - [This is what we were looking for]

[-1.41 -0.71  0.    0.71  1.41]
[-1.28 -0.7  -0.12  0.46  1.63]


What is considered short and long text in NLP (document similarity) What is considered short and long text in NLP?
I'm working on a dataset that contains documents from 10 to 600 words and I'm asking myself if I should treat them differently. Also, I haven't found a source which explicitly defines short and long text in NLP yet. The goal for my task is to find similar documents.

As Erwan said in the comments, it depends. In my experience, it depends specifically on two things:
Tokenization method: The length of a document in number of tokens will vary considerably depending on how you split it up. Splitting your text into individual characters will result in a longer document than splitting it into sub-word units (e.g. WordPiece), which will still be longer than splitting on white space.
Model: Vanishing gradients aside, an RNN doesn't care how long the input text is, it will just keep chugging along. Transformers, however, are limited. BERT can realistically handle sequences of up to 512 WordPiece units, while the LongFormer claims to handle sequences of up to 32k units (given sufficient compute resources). Thus your documents of 10 - 600 tokens would be long for BERT but short for the LongFormer.
Whether you should treat documents of length 10 differently from those of length 600 is not something I can answer without knowing the details of your specific task. Intuitively, I doubt a very short document would ever be very similar to a much longer one, simply because it likely contains less content.


why One-Hot Encoder can avoid the situation that the model will misunderstand the data to be in some kind of order if the data has been Label Encoding We know that we prefer to using One-Hot Encoding not Label Encoding when processing with non-ordinal data.
And I real a blog which give the difference between Label Encoding and One-Hot Encoding.
So I am wondering why One-Hot Encoder can avoid the situation that the model will misunderstand the data to be in some kind of order, $0 < 1 < 2$ if the data has been Label Encoding.
Does it have some provements and theories?
Or anyone just can explain it in intuition.
Thanks in advance.

From the blog, one hot encoding resolves the issue by explicitly showing that 1 category is true, while all others are false (1 vs 0). One column is turned into 3 columns which describe all categories as either true or false.

As opposed to letting a model see one column with 0, 1, and 2. One column to show this data with label encoding does make it seem as if the data is numerical so 0 < 1 < 2.


An appropiate data visualization? I want to include a plot in my thesis to highlight regional differences in bicycle collision outcomes. Now there are several plot visualizations I could use, and I am not sure which one could be the most appropriate. Having a "scale free" y axis better highlights the differences between each region, but it may also confuse the reader.

I am aware that there might not be an optimal solution to this, but I would appreciate to hear your thoughts.(If you have a completely other suggestion for an appropriate plot than the ones below, I would also be happy to hear those).

These are the plots I have made:

Plot 1

Plot 2

Plot 3

Plot 4

Looking into so many plots may miss the bigger picture.
We should keep the initial plot in "One Consolidated plot"
e.g.
Can add few other consolidated views - Pie on aggregated data on region and Vehicle


Sentiment retriving from text (Russian) Does anybody knows python library to retrieve sentiment from Russian text. The dictionary with sentiment parameterization will be ok to. The idea of library something like in GPOMS in article.

Take a look at the polyglot library. It has polarity lexicons for 136 languages, including Russian.

  The scale of the words’ polarity consisted of three degrees: +1 for
  positive words, and -1 for negatives words. Neutral words will have a
  score of 0.

You can use it like this:

>>> from polyglot.text import Text as T
>>> text = T("это очень плохо. А это намного лучше, даже хорошо!")
>>> text.polarity
0.33333333333333331

>>> for s in text.sentences:
...  s, s.polarity
...
(Sentence("это очень плохо."), -1.0)
(Sentence("А это намного лучше, даже хорошо!"), 1.0)

>>> for w in text.words:
...   print("{:<16}{:>2}".format(w, w.polarity))
...
это              0
очень            0
плохо           -1
.                0
А                0
это              0
намного          0
лучше            1
,                0
даже             0
хорошо           1
!                0


What should I do with the NaN values on this stock quote data? I concatenated 3 stock quote data-frames all with date-time indexes.
However, they differ in starting dates so the resulting data-frame contains NaN values for the stock quotes with more recent starting dates.

Should I just drop the rows with NaN and start the new data frame with the row where all have values or is there a way to fill them up? I'm planning on using the data to train a neural network that predicts future stock quotes.

Filling ups the nan based on the quote that already started is possible with time series models, but it won't bring new information that you can learn on.  At best it will obfuscate what the model has learned on that period.

Then there is the more general question about the performance : does the model perform better with or without that period of reconstructed data ? Only you can answer that by working it up... But I am not sure you want an answer to that question. The way to go imo is to just drop that period and see if the performance fits your need. If it isn't the case then you should consider some ways to add up more data.

In this cases i'd suggest either to just drop the NaN if you want to work with neural nets or work with a method that accept Nan as inputs (Xgboost for exemple).


MSE relevance as a metric when errors < 1 I'm trying to build my first models for regression after taking MOOCs on deep learning. I'm currently working on a dataset whose labels are between 0 and 2. Again, this is a regression task, not classification.
The low y values imply that the loss for each sample is quite low, always < 1. My question is then about the relevance of mse as a metric in such a case : since the loss is < 1, squaring it will result in an even smaller value, making the metric value drop very rapidly. In this case, would it be more relevant to use mae ? Or should I multiply the y values so that the order of magnitude of a sample loss would be > 1.
I found this nice article about regression metrics, but didn't find the answer in it. Thanks for your help.

I'd use relative RMSE
$\sqrt{\frac{1}{n} \sum \frac{(Preicted - True)^2}{True^2}}$.
In this case, close to 0 implies a good model, regardless of the scale of the true values.
Similarly, you can try relative MAE.


Comparing youtube view counts between historical release dates I've written a script to use youtube-dl to collect all view-counts and ratings from all listed videos on a youtube channel: https://gist.github.com/philshem/ca0b8f964458c63ee4f0e68c0f700f39#file-get_stats-sh
To compare ratings is relatively simple, because they are already averaged. But I would like to compare views, which is not clear to me because the number of views is related to how recently the video was released.
I am looking for a metric that normalizes somehow the age of the video with the number of views. I could do something simple, such as view_count * days_since_release, but I don't expect that to be statistically meaningful.
Is there an example metric for comparing historical, cumulative data?

https://twitter.com/philshem/status/1295725069268910081

A simple option would be to use the average number of views per day, this way the value is normalized and independent of how recent the video is. Of course averaging is simplistic, since the views don't follow a linear trend.
A more advanced option would be to model the evolution of views across time (for instance assuming a normal distribution), then estimate the parameters for every particular video and compare the predicted total number of views. This would be more complex due to the difficulty of estimating the parameters from an incomplete distribution.


Why isn't local averaging (including KNN) used often for regression? My professor said that the "holy grail of regression" is the function E(Y|X=x) i.e. the conditional expectation of Y on X. In practice, you'd take a small window of X and take the average value of Y for all observations that lie in the window.

The professor said that this is basically the best prediction you can make, but we don't usually do it because the curse of dimensionality reduces its effectiveness in when # of predictors is large. So it seems that local averaging (KNN regression is a type of this) is good with few predictors. However, in most articles and stats classes, I always see linear regression being used even in low dimensions. Why isn't local averaging used more often?

Local averaging regression is much more complicated model than a simple linear regression as it's a mixture of many individual smaller models. Local regression is actually quite popular in the literature, and has been tested extensively.

Local regression models require large dense sampling data set, which is not common for a statistical student. If you don't have complicated relationships to model, you probably don't need it.


Find Cluster Diameter and Associated Cluster Points with KMeans Clustering (scikit learn) I have done clustering using Kmeans using sklearn. While it has a method to print the centroids, I am finding it rather bizarre that scikit-learn doesn't have a method to find out the cluster diameter  (or that I have not seen it so far). Is there a neat way to obtain this for each cluster together with points associated with a cluster?

I currently have this rather kludgy code to do it

import numpy as np
from sklearn.cluster import KMeans
from sklearn import datasets

iris = datasets.load_iris()
X = iris.data
y = iris.target

estimator = KMeans(n_clusters=3)
estimator.fit(X)
print({i: np.where(estimator.labels_ == i)[0] for i in range(estimator.n_clusters)}) #get the indices of points for each cluster

For the time being, I've prepared a workaround solution:

#iris example
iris = datasets.load_iris()
x = iris.data
y = iris.target

estimator = KMeans(n_clusters=3)
y_kmeans = estimator.fit_predict(x)

To get the clusters' radii you can use the following code snippet:

#empty dictionaries

clusters_centroids=dict()
clusters_radii= dict()

'''looping over clusters and calculate Euclidian distance of
each point within that cluster from its centroid and
pick the maximum which is the radius of that cluster'''

for cluster in list(set(y)):

    clusters_centroids[cluster]=list(zip(estimator.cluster_centers_[:, 0],estimator.cluster_centers_[:,1]))[cluster]
    clusters_radii[cluster] = max([np.linalg.norm(np.subtract(i,clusters_centroids[cluster])) for i in zip(x[y_kmeans == cluster, 0],x[y_kmeans == cluster, 1])])

It will give you this:

Please note K-means:

Implicitly assumes all clusters have the same radius
Separates the data into Voronoi-cells (which can be seen from here as well).
Cluster points (circles) can overlap (it is how it is defined).

If you want to relax the shape of the clusters (not strictly spherical or circles like K-means), you should perform Gaussian mixture models.

Appendix (To Reproduce the above Visualization):

#Visualising the clusters and cluster circles

fig, ax = plt.subplots(1,figsize=(7,5))

plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Iris-setosa')
art = mpatches.Circle(clusters_centroids[0],clusters_radii[0], edgecolor='r',fill=False)
ax.add_patch(art)

plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Iris-versicolour')
art = mpatches.Circle(clusters_centroids[1],clusters_radii[1], edgecolor='b',fill=False)
ax.add_patch(art)

plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Iris-virginica')
art = mpatches.Circle(clusters_centroids[2],clusters_radii[2], edgecolor='g',fill=False)
ax.add_patch(art)

#Plotting the centroids of the clusters
plt.scatter(estimator.cluster_centers_[:, 0], estimator.cluster_centers_[:,1], s = 100, c = 'yellow', label = 'Centroids')

plt.legend()
plt.tight_layout()
plt.savefig('kmeans.jpg',dpi=300)


Inconsistency in training Iris dataset I am a noob in the field of ML. I have been trying to classify the iris dataset. I managed to do it with backpropagation, and with 3 neurons in the hidden layer. But the mean square error that I get when I rerun the entire code is not consistent. It varies between 0 - 8%. Is this expected, or am I doing something wrong here?

I am not an expert on Neural Networks, but if you are using Random Initialization then your MSE would vary slightly from each rerun.


Making Inference from a Correlation Heatmap I have constructed a Heatmap of my dataset for visualization. I have searched on various sites regarding what can I infer from the heatmap and I am unable to get any clear understanding from them. I want to select useful features for Exploratory Data Analysis and Visualization to get some insights. If you can provide any suggestion or advice it would be extremely helpful for me. Thank you

Assuming you're using df.corr(), the results from a heatmap are Pearson correlation coefficients which can be thought of as "the explainability between two arrays."

An effect score closer to 0 translates to there being no relationship. A score closer to 1 or -1 is a positive or negative relationship. A perfect score of 1 is a direct correlation.

Additionally, I would caution against taking action using these values without testing the normalcy/distribution of your data.


What does all zero mean decrease in accuracy (MDA/permutation importance) signify? I have a model I've trained on ~3400 features with ~500 samples (40:20:20 train:test:val) that I've calculated MDA on using the eli5 package. However, all the features are zero when I calculate this with the train or test dataset.
My dataset is a fairly sparse dataset with mostly zeros and I'm trying to classify disease. Here is a sample:

The model is a random forest that is predicting disease class (0 or 1). the dataset is imbalanced with appx (90% 0 and 10% 1)
My model accuracies are as follows:

train: 0.9923469387755102

test: 1.0

val: 0.9764705882352941

confusion matrix for test
[[76  0]
[ 0  8]]

these are my gini importances:

MDAs are all zero for testing and training. Why is that and what does it mean for the generalizability of the model?

I am assuming MDA means Mean Decrease Accuracy. Generally speaking, a good performance on a validation/testing dataset means that your model generalizes well.
On the other hand MDAs of all exactly zero means either the model always needs just one variable to get its best performance no matter what variable that is (very unlikely), or that you plain simply calculated the MDAs wrong. This is impossible to tell without code/examples/etc.


Recommendations for Data Visualization solution for large CSV file on a non-profit website I would like your advice and recommendations for a non-profit that is trying to turn a large CSV file into meaningful information.
I am part of a non-profit that would like to turn a 23,000 row x 23 column CSV file into charts and graphs.  The CSV file is published by the Georgia Bureau of Investigation several times per year and is publicly available here:
http://state.sor.gbi.ga.gov/SORT_PUBLIC/sor.csv
As you will see, the CSV contains over 23,000 individual records so grouping of those records by county would be necessary.  Keeping archive data for trends would be nice but isn't required.
The goal of the solution would be to visualize data for each of Georgia's 159 counties as well as an overall visualization.  I envision pie or donut charts representing demographic information like Race, Gender, and other data that the CSV collects.  I'd like for each of the 159 counties to have their own report and then an overall report for the state of Georgia as a whole.  The solution would need to be embedded on our website.  It would not need to track data in real time as I will probably upload a new CSV on a quarterly basis.
My website is running the Wordpress CMS with CiviCRM (PHP 7.3, MySQL 5.7).  I have full root access to the site so I'm able to install web apps if needed.
Does anyone have suggestions for accomplishing this?  I'm currently looking into GoodData.  We are a non-profit so the more affordable the solution, the better.  I don't mind installing an open source solution to save money over a SaaS hosted solution.
Thanks for your suggestions.  I'd be glad to provide additional information if you need it.

Shiny could be an interesting option: it's an R library which lets the programmer generate interactive web pages from an R program.

All the R libraries for data manipulation/visualization can be used (e.g. the great ggplot2 library for graphs)
The interactive pages are rendered with very little effort required on the programmer side
Very flexible, allows quite sophisticated visualizations (see the gallery) and the result looks very professional imho.
It requires the Shiny server to run on the site, it can be installed quite easily in my experience. There is a commercial version but the free version doesn't have any important limitation, as far as I can tell.

Drawbacks:

I'm not sure I would recommend this to somebody who doesn't have any experience with R, it could be a quite steep learning curve to learn the language just for using Shiny.
It might not scale well with a high amount of traffic, depending on the application and the memory resources.


Why does prediction by a consensus of classifier work better than prediction by a single classifiers? I have seen that consensus of classifiers (taking say 5 separate classifiers) and obtaining the final labeling of the unknown sample based on the voting method (whichever class gets the predicted the most is the class of the unknown sample) works better than taking a single classifier for predicting the class of a sample. Why so? Any articles which show why this happens?

For a very simple example, imagine you have three independent classifiers that each have 60% accuracy. If you use any one of them to classify a random sample, you have a 60% chance of getting it right.

Now use an ensemble classifier that takes the majority vote. Below are the probabilities that exactly N of the individual classifiers are correct.

0 correct - 6.4%
1 correct - 28.8%
2 correct - 43.2%
3 correct - 21.6%

Your ensemble classifier has the correct output when 2 or 3 of the component classifiers are correct, which happens 64.8% of the time. Your ensemble classifier outperforms each of the individual component classifiers! You'll get this advantage by combining any classifiers that perform better than random.


Neural network with fixed weights in Python lasagne I am trying to train a neural network with the lasagne module in Python. I do not want a fully connected network as defined by lasagne.layers.DenseLayer. Instead, I would like to fix some of the weight parameters to zero. Does anyone know how to do this?

The closest solution I have found is something like:

params = lasagne.layers.get_all_params(network, trainable=True)
layer1.params[layer1.W].remove("trainable")

However, this fixes the entire set of weight parameters to their initial values. How can I fix only a subset of these weights?

I'm not sure what your intention is by setting the weights to zero. Have you looked at dropout layers?

l_hid1 = lasagne.layers.DenseLayer(num_units=200)

l_hid1_drop = lasagne.layers.DropoutLayer(l_hid1, p=0.5)

This should drop 50% of your data from the l_hid1 layer.


What would be the target input for Transformer Decoder during test phase? The Transformer Decoder takes in two inputs, the encoder's output, and the target sequence. How the target is fed into the decoder has been provided in this answer
I am having confusion about what the target sequence will be when the trained model is evaluated?.
Is it that we start with a <SOS> tag for the first timestep and loop through the transformer decoder for each timestep like in RNN's?
It would be helpful if someone can clarify this for me.

At training time, the input to the decoder is the target sentence tokens, which are indeed unknown at the test time. What you call the second input are the desired outputs, which are not usually referred to as an input to the decoder, 1. for clarity, 2. they are technically input to the loss function.
At test time, we do not need the loss function, but we still need to pass some input to the decoder. The decoding proceeds autoregressively, i.e., at each decoding step, we execute the decoder layers and get a probability distribution over the target tokens. We select one token (typically the best-scoring one, but it gets trickier with beam search) and append it to the input of the decoder. It means that the input to the decoder is generated one token at a time, gradually as the sentence is decoded.


Recognize and predict a periodic pattern I need to design a controller that tries to collect as much falling objects (raindrops) from an unknown source. Beneficial to solving the problem could be the recognition of the seemingly random generation of the raindrops.

y  ^
   | 0 0 1 0 0 0 0
   | 0 0 1 0 0 1 0
   | 1 0 0 0 0 1 0
   | 0 0 0 1 1 0 0
   ----------------->
                  t

So, I have a binary vector signal (raindrop generated yes/no over 4 positions) over discrete time steps. I can only see the raindrops when they are 10 steps away from hitting the ground. The basket has a capacity of one raindrop. The control actions available are step left, step right or do nothing. Any source of pattern recognition could help me design a more advanced controller. Are there methods that can recognize periodic patterns in this type of data?

If it is really close to random (background noise, bits from random number generators or hash codes), I don't expect there is much you can do, except create a large collector. (Or are you only allowed one collector?)

You may want to throw in a few random checks. Those won't prove anything but may give you some insight into the pattern.

If you want to create a successful model, you'd have to come up with some assumptions about parameters that could affect the success of your model, and create and test that model.

For instance, one of my assumptions would be that there are rows with higher probability.
One other assumption would be that there is some hysteresis effect (either positive or negative), so the probability may depend on the actual outcome of the previous event(s).


Frequency for a time series What is meant by frequency of a time series? I am having data observations of energy consumption from 01/05/2016 - 31/05/2017 such that there are 96 values per day. What will be the frequency of the time series in such case?

The sampling frequency, or sample rate, is the number of equal-spaced samples per unit of time. For instance, if you have 96 equally spaced observation per day, then you sampling rate is 96/day, or 96/24/3600=0.0011 Hz. Hz, which means per second, is widely used for sample rate.

The frequency of your data is different. I you signal is periodic or approximately periodic (say, the similar energy consumption patterns repeat everyday), the the frequency is the number of occurrence of that repeating pattern per unit of time. More generally, if your data does not look like periodic, it would help to conduct Fourier Analysis to understand the frequency components in your data. The Fourier Analysis (Fourier Transform) is the operation to transform time-domain data into the frequency domain.


how to deal with two high correlations feature which both has a low correlation with target I am doing a prediction of house trade money.
Here is the correlation matrix of features whose correlations are larger than 0.3 as follows:

train_corr_full.unstack().sort_values(kind='quicksort', ascending=False).reset_index()
corr = corr[(corr['level_0'] != corr['level_1'])]
corr

    level_0             level_1              0
13  pv                  uv                   0.911879
14  uv                  pv                   0.911879
15  area                bathroom             0.721935
16  bathroom            area                 0.721935
17  area                tradeMoney           0.687447
18  tradeMoney          area                 0.687447
19  bathroom            tradeMoney           0.580745
20  tradeMoney          bathroom             0.580745
21  bathroom            room                 0.421762
22  room                bathroom             0.421762
23  remainNewNum        totalNewTradeMoney   0.417114
24  totalNewTradeMoney  remainNewNum         0.417114
25  buildYear           totalFloor           0.393571
26  totalFloor          buildYear            0.393571
27  remainNewNum        tradeMeanPrice       0.314611
28  tradeMeanPrice      remainNewNum         0.314611

corr[corr['level_0'] == 'tradeMoney']

    level_0     level_1             0
18  tradeMoney  area                0.687447
20  tradeMoney  bathroom            0.580745
34  tradeMoney  tradeMeanPrice      0.282720
45  tradeMoney  totalFloor          0.249755
47  tradeMoney  tradeNewMeanPrice   0.236713
55  tradeMoney  room                0.215041
79  tradeMoney  buildYear           0.123065
81  tradeMoney  totalTradeMoney     0.122407
95  tradeMoney  remainNewNum        0.100921
116 tradeMoney  pv                  0.072919
134 tradeMoney  uv                  0.040452
137 tradeMoney  totalNewTradeMoney  0.038420

  Features descriptions:
  area: the area of the house.
  room: the number
  of the room in this house.
  bathroom: the number of the bathroom in
  this house.
  totalFloor: total number of floors of this building.
  tradeMeanPrice: the mean price of second-hand housing transaction
  price this month.
  tradeNewMeanPrice: the mean price of new housing
  transaction price this month.
  buildYear: the age of this house.
  totalTradeMoney: the total price of second-hand housing transaction
  price this month.
  totalNewTradeMoney: the total price of the new
  housing transaction price this month.
  remainNewNum: the number of
  houses that haven't been sold this month.
  pv: the number of times that
  the website was viewed by tenants.
  uv: the number of tenants that
  viewed the website.

It shows that there is a high correlation with pv and uv but both of which has a low correlation with the target.
What I did before is that I would drop the features whose correlation is lower than about 0.15 and do more EDAs and feature engineerings in those high correlations feature.
And even if consider context I also think the pv and uv is not good enough for the prediction.

Could anyone give some hints on it?
Thanks in advance.

If two features have strong correlation together that could mean one of these features is redundant, and probably advised to delete it from the features list in your predictive model.

add to that if the correlation with the target is low then you may need to delete both.

How to decide?

there are many ways, one of them is Feature Selection Filter Method, or an algorithm such as FCBF.

which you try the correlation of the features before implementing your predictive model

The other one is the Wrapper method or Embedded method which will use your predictive model to decide which features to choose.


How to quantitatively evaluate raw neural network activations? Below are the activations for 2 different predictions. These predictions are for different labels/classes. They are being run through a dense Keras NN (96.6% accurate) with 2 hidden layers and adam optimizer.

What kinds of trends in the numerical values of the activations should I be looking for with regard to a NN that is being trained successfully?

Do you want most activations to be either high or zero... not somewhere in the middle?
What values would indicate that you need to add/ remove a layer?
What maximum activation value is too high? Like if I saw an 8 should I be worried when the rest of my high values are around 4?

1) It is difficult to interpret a given weight on a given node for a given instance, especially in hidden layer.

2) You can only deal with specifical cases :

Weights being very low for a given variable for all instance on the first layer means that you can remove the variable (If the variable is relatively uniform, standardized).
For some activation functions, you can remove 'dead' neurons that activate to few (or no) instances. More generally, regarding to your questions, answers highly depends on the kind of activation. I would reccomend you to read : http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf by Lecun to avoid common traps. It will also give you some idea about what problems to look for (vanishing / exploding gradient, dead neurons... etc.)

3) On the architecture : as a rule of thumb for the size of your networks I would advise to start big, get a performance benchmark and remove layers / units untill the performance drops. I am not sure if you plotted only hidden layer or not, but in most cases the numbers of layers shoud decrease.

4) You might want to look some explanatory tools, like DEEPLIFT or SHAP, that will give you variable importance, summarizing all variable impact for an instance. These methods have some limitations but they will be easier to read.


Renaming Column Elements of DataFrames I want to create a new DataFrame based upon the columns of another DataFrame, for which I would like to do the following:

Data Retrieval: Take partial strings from column elements from DataFrame1
Renaming Column Elements: Combine these strings and place into 2 new columns in DataFrame2

An example of the DataFrames can be seen below, where DataFrame1 is my actual data and the DataFrame2 is the type of data frame that I am interested in obtaining:

import pandas as pd
df1 = pd.DataFrame({'BGC Name': ['Hm106_120.region001', 'MBT13_26.region001',
'MBT23_64.region001', 'MBT36_100.region001'], 'GCF No': [1813, 1813, 1883,
1887], 'Organism': ['Streptomyces sp', 'Streptomyces sp', 'Listeria sp',
'Streptomyces sp'], 'BGC Class': ['PKSI', 'NRPS', 'PSKI', 'NRPS']})

df2 = pd.DataFrame({'GCF': ['PKSI_GCF1813', 'NRPS_GCF1813', 'PKSI_GCF1883',
'NRPS_GCF1887'],'Genome': ['Streptomyces_sp_Hm106', 'Streptomyces_sp_MBT13',
'Listeria_sp_MBT23', 'Streptomyces_sp_MBT36']})

print("DataFrame1 (Original DataFrame)")
print(df1)
print("DataFrame2 (Desired DataFrame)")
print(df2)

Snapshot can be found: [

The Code That I am trying to use

#Dataframe Copies
df1 = df1.copy() #self copy
df1.columns = df1.columns.str.strip().str.lower().str.replace(' ', '_').str.replace('(', '').str.replace(')', '')
df2 = df1.copy() #copy of original dataframe
#Change Column Names
df1.columns = ['BGC', 'GCF', 'Description', 'Product Prediction', 'BiG-SCAPE class', 'Organism']
df2.columns = ['BGC', 'GCF', 'Description', 'Product Prediction', 'BiG-SCAPE class', 'Organism']
#DataFrame Manipulation
df2['GCF'] = df1['BiG-SCAPE class'].astype(str) + df2['GCF'].astype(str)
df1[['BGC','BGC2']] = df1['BGC'].str.split('_',n=1,expand=True)
df2['Genome'] = df1['Organism'].astype(str) + df1['BGC'].astype(str)
df2.set_index('GCF')
df2 = df2[['GCF','Genome']]
df2['Genome'] = df2['Genome'].str.replace(' ', '_').str.replace('Unclassified','_').str.replace('__','_').str.replace('.','')
print(df1)
print("Updated table")
print(df2)

Here, first I created the column values as a list and then assigned them to be the new dataframe's columns. However, we could do it using the columns and without creating lists. It just seems easier this way.

#Initializing the new_df (df2) dataframe
import pandas as pd
new_columns =  ['GCF', 'Genome']
new_df  = pd.DataFrame(columns = new_columns)
#new_df
#df1.columns
#creating the column GCF of the new dataframe
GCF = [str(df1['BGC Class'][i])+ '_GCF' + str(df1['GCF No'][i]) for i in range(len(df1))]
#creating the new column 'Genome' of the new dataframe
organism = [df1['Organism'][i].replace(' ', '_') for i in range(len(df1))] #substituting ' ' by '_'
BGC_name = [list(df1['BGC Name'].str.split('_'))[i][0] for i in range(len(df1))]
genome = [organism[i] + '_' + BGC_name[i] for i in range(len(df1))]
new_df['GCF'] = GCF
new_df['Genome'] = genome

Output:

    GCF             Genome
0   PKSI_GCF1813    Streptomyces_sp_Hm106
1   NRPS_GCF1813    Streptomyces_sp_MBT13
2   PSKI_GCF1883    Listeria_sp_MBT23
3   NRPS_GCF1887    Streptomyces_sp_MBT36

The second way in which we do not create lists and change the columns' values sort of inplace, is as following:

#Initializing the new_df (df2) dataframe
import pandas as pd
new_columns =  ['GCF', 'Genome']
new  = pd.DataFrame(columns = new_columns)
#new_df
#df1.columns
BGC_name = [list(df1['BGC Name'].str.split('_'))[i][0] for i in range(len(df1))]
new['GCF'] = df1['Organism'].str.replace(' ', '_')+ '_' + BGC_name
new['Genome'] = df1['BGC Class']+ '_GCF'+ df1['GCF No'].astype('str')
#new


How to resume training of a model? I have not GPU support so it often happens that my model takes hours to train. Can I train my model in batches , for example if I want to have 100 epochs for my model,but due to power cut my training stops(at 50th epoch) but when I retrain my model I want to train it from where it was left (from 50th epoch).

It would be much appreciated if anyone can explain it by some example.

With tensorflow, currently the most straightforward and easy way to get persistence for your model is to use a tf.train.MonitoredTrainingSession. You just need to use it instead the normal tf.Session() that is frequently used. This an illustrative Python snippet:

with tf.train.MonitoredTrainingSession(checkpoint_dir='/tmp/mymodel',
                                       save_summaries_secs=600) as sess:
   _ = sess.run(train_op, feed_dict={...})

With this, your model is automagically saved every 600 secs in /tmp/mymodel and restored the next time you restart the program.


is this problem a multiclass case? I'm trying to classify my textile design patterns
(let's just think of it as medieval painting)
what I understand of "multilabel classification" is like this:
it outputs multiple possible result out of all those classes (let's say classes are of some artists, style and technique)
so one example could be

possible classes: leonardo, artist1, artist2, baroque, renaissance, whatever, oil, dessin, watercolor
prediction of img1: leonardo da vinci, artist1, renaissance, oilpainting, wtaercolor

but what I want to do is more like:
 - possible classes:

artist
style
technique

leonardo
baroque
oil

artist1
renaissance
dessin

artist2
whatever
watercolor

prediction: {artist: ['leonardo', 'artist1'], (maybe they drew it together)
style: ['renaissance'],
technique: ['oil', 'watercolor']}

classes are more strictly categorized and but also there could be multiple results from one category of class.
I'm not even sure what should it be called and having hard time to find articels for it.
Can someone please suggest?

It is not a multiclass problem.
It is a multilabel problem. Since, you have the clusters of classes you want to get. You just let the network predict multiple classes and segregate them afterwards. In this case, you will have single classification head.
Other way to do it, is to separately derive multiple classes of article, technique and style. In this case, you will have three classification heads.


datasets for fuzzy clustering By now i thought that fuzzy clustering can be applied to any kind of data-sets but now i have heard that it can be applied only to specific data-sets that involve the concept of probability, is that true?

First I would say the fuzzy clustering is not necessarily a clustering algorithm which uses fuzzy logic.

In machine learning terminology, a "soft" clustering algorithm is called fuzzy clustering as the intuition behind it is not "crisp" (like hard clustering algorithms in which a data point belongs ONLY to one cluster and not others) i.e. for each data point, they output a vector of "probabilities" corresponding to the membership of that point to each cluster (please note that fuzzy memberships are not necessarily probabilities). Even in FCM algorithm, you are just doing a soft version of k-means and it has nothing to do with fuzzy logic (except of the very general idea i.e. not being crisp in making decision)

So you can apply them to any dataset.


Value error while using linear regression | ML With reference to the below screen, I received the error:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

For reading purposes, i'm appending screen shot. Please advise where I have gone wrong. Thanks for your assistance.

My Code:

You have missing values in your DF (NaN) means no observations. You need to skip these values since linear regression cannot handle them. Looks like conversion of sqm failed. Check what the format is and try to get int or float from sqm column to avoid NaN.

If you want to skip NaNs read the docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html


When to use Stateful LSTM? I'm trying to use LSTM on time-series data in order to generate future sequences that looks like the original sequences in term of values and progression direction.
My approach is:

train RNN to predict a value based on 25 past values then use the model to recursively generate future predictions by appending the predicted values to the original sequence and shift the old values ...

Playing with LSTM cells, I found that the model is not able to learn to generate sequences that looks like the original data. It only predict next value then start converging to an 'equilibrium' or static value which is the same whatever the input sequence is.

I'm wondering if Stateful LSTM would help to learn better from past values and try to predict something close to what it have already? My goal here is to generate sequences that looks like something that the model have seen already.

Please let me know if I'm missing something or if you had a similar situation and you found the best approach to generate timeseries sequences that looks like what the model learned in the past.

As for stateful LSTM and its understanding, refer to here. Quoting an answer from there:

"I’m given a big sequence (e.g. Time Series) and I split it into smaller sequences to construct my input matrix X. Is it possible that the LSTM may find dependencies between the sequences?

No it’s not possible unless you go for the stateful LSTM. Most of the problems can be solved with stateless LSTM so if you go for the stateful mode, make sure you really need it. In stateless mode, long term memory does not mean that the LSTM will remember the content of the previous batches."

Therefore, stateful is useful if you wish to save the state of the neurons for the next training session instead of resetting it.

  "My goal here is to generate sequences that looks like something that
  the model have seen already."

Then maybe this sequence2sequence (seq2seq) LSTM encoder-decoder is exactly what you need.


LSTM loss value not change, accuracy stucked at 50% I'm using LSTM for time series prediction, my data is highly skewed, with class weight 197.16865807 : 0.50127117

With Label 0 : 25359 and Label 1 : 9974641

my model is shown below

n_input = 100
n_features = 36
class_weights = class_weight.compute_class_weight('balanced',
                                              np.unique(y_target),
                                              y_target)
model = tf.keras.Sequential([
        tf.keras.layers.LSTM(64, activation='tanh', input_shape=(n_input, n_features),return_sequences = True),
        tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
        tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
        tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(1,activation='sigmoid')])

model.compile(optimizer='adam', loss= 'binary_crossentropy' ,metrics=METRICS)
model.fit_generator(train_generator, epochs= 1,steps_per_epoch=len(train_generator),class_weight=class_weight)

I have tried the following method to dealing with my unchanged accuracy and loss value.

I tried to adjust class weight, but it seems not working, my predictions are all 1
I tried to use RandomSampler from imblearn to undersample my data, but the accuracy stucked at 50%
I tried to change the loss function to  weighted_cross_entropy_with_logits, but I did not find any examples show how to use it in Sequential model like the one above

I feel my model is not predict the result, since when I feed balanced dataset, the accuracy is around 50%, when I feed imbalanced dataset, the accuracy is 99%.

Can anyone help me with this? I wondering if it's the problem of my model, or the problem of my imbalanced dataset

Thank you!

The dataset contains ~25K class '0' samples and ~10M class '1' sample. This clearly tells us that the LSTM would be learning patterns more popular in class '1' instances. Here are some improvements you can try:

Instead of undersampling the class '1' labels, oversample the number of instances of class '0'.
Accuracy is not a very good metric in cases of unbalanced datasets. Use 'Macro-F score to evaluate the performance of your model.
Use tensorboard to see how the weights and the gradients of each layer are changing.


Model Predictive Power and better prediction of 1 or 0 in Python scikitlearn? I have 2 questions about Logistic Regression model in Python scikit-learn:

Which statistic can show me model predictive power ?
Which statistic can show me whether my model better predict event 1 or event 0 ?

Which statistic can show me model predictive power ? For example CV-f1 score
Which statistic can show me whether my model better predict event 1 or event 0 ? True negative and true positive rate


Concatenate dataframes Pandas I have three dataframes. Their shapes are (2656, 246), (2656, 2412) and (2656, 7025). I want to merge dataframes as above:

So It will result a (2656, 9683) Dataframe. Thanks for any help.

Typo on image: on Dataframe 3, it will 7025, not 5668.

Assuming that the rows are in same order that you wish to merge all of the dataframes, you can use the concat command specificying axis=1.

new_df = pd.concat([df1, df2, df3], axis=1)

If the row index for each of the data frames are different and you want to merge them in the current order, you can also apply ignore_index:

new_df = pd.concat([df1, df2, df3], ignore_index=True)

For details on the merge, join & concatenation operations, please refer to the pandas docs.


query category wise dara I have stored the some products and there respective category in sql.

Like this. Now I want fetch all product from table with respective category.
output should be=>

assuming your products table is called products the following sql statement would achieve this

SELECT DISTINCT id, category as name, 1 as flag_for_ca FROM products
UNION ALL
SELECT DISTINCT id, prodct as name, 0 as flag_for_ca FROM products


ZeroR as baseline for balanced / imbalanced datasets I have a dataset containing thousands of text posts. I am building a binary classifier that will classify posts as safe (0) or  risky (1). I randomly picked some of them and manually labeled. Label 1 is minority.

Imbalanced data results in skewed performance. To overcome this, I undersample to obtain a 50%/50% distribution for both train and test set.

If I use stratified strategy (in scikit-learn) for ZeroR, it acts like random guesser. Is this a good baseline?
If I use "guess 1" all the time, then its recall is 100% all the time. However recall is the most important metric for me. So other algorithms' predictions look unsuccessful. Should I ignore recall when using ZeroR and only compare accuracy?
I also would like to apply 10-fold cross validation, should I apply undersampling before splitting as well?
Or instead should I go with F1-Score without undersampling? This time ZeroR has high F1 score due to 100% Recall.

You should only undersample the data that is used for training. Test data should represent the true distribution.

Then you have at least two possible baselines. One is ZeroR (your second point), which always predicts the majority class. Given a true distribution of (0) 10% and (1) 90%, this would give you a recall of 100% for (1) and 0% for (0) and 90% accuracy.

If rows represent the true labels and the columns the predictions, the confusion matrix looks something like this:

$\begin{bmatrix}90 & 0\\10 & 0\end{bmatrix}$

The other would be to flip a coin with probability 90% for class (1) (your first point but without undersampling), which gives you a recall of 90% for (1) and 10% for (0), and 82% accuracy, which is a little more balanced:

$\begin{bmatrix}81 & 9\\9 & 1\end{bmatrix}$

If your classes are strongly imbalanced then recall and accuracy have the same problems as you noted above. F1 might be a better choice, but the best metric depends on your application.


Memory-efficient metric calculation for ultra high dimensional data I am preparing for clustering the data which can be only represent as a extremely sparse binary vectors. Each of the objects is represented by a large set the binary features ($10^3$ ~ $10^6$), each identified by a 32-bit hash code. We can assume that in order to represent the data object without loss of information we need to see it as a vector of $2^{32}$ bits - in which only the bits at positions identified by hash are set to one.

The problem is, having two data objects, each represented by a sorted set of hashes, how can I calculate the distance between them without storing two $2^{32}$ bits long vectors in memory?

The way to go was to use relative similarity metric:

\begin{equation*}
d(x,y)=\left(1-\frac{\left\vert{x \cap y}\right\vert }{\left\vert{x \cup y}\right\vert }\right)^k
\end{equation*}

Where x and y are sets containing hashes identifying features and k is experimentally selected factor that allows "spreading" the range of distances.

Given that data point, being really a binary vector of size 2^32 have bits set only on the positions denoted by the hashes, we can calculate the distance as size of intersection of hash sets divided by size of sum of hash sets.


the size of training data set in the context of computer vision Generally speaking, for training a machine learning model, the size of training data set should be bigger than the number of predictors. For a neural network, or even a deep learning model, the number of parameters are usually tens of thousands or even millions. It seems that in practice, the number of training data set, i.e., the number of images, is usually less than the number of parameters. How to explain this? I know, we can claim that the pre-trained model may remove the requirement of having that many images. Is this the only reason, or we should use number of pixels multiplied by the number of images to measure the size of training data set.

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.


What is a "good" sample size Let's say if I have 2 TB of data, what is the best sample size to pick? I understand that there is a limit on how much RAM/processing power I have and hence I should make my sampling decision around that. But let's say if the processing power is not a concern for me right now. What would be a good way to approach my sample size?

This is a tough question to answer without more information. I'm going to assume that this is for model building, but without more detail it's hard to recommend something.

However, there are some things which should generally be known:

Population size

How large is the population? Does your 2TB of data comprise the total population, or is this a sample of a given timeframe? What frame of data are you looking at - is this 2 days worth of data that is only representative for a given subset of the population, or is this everything? You'll need to know this to know what conclusions you can draw from this dataset.

Variance

What's the variance of the sample? If it's categorical data, how many unique values are there? Having a metric around this will help determine the number of samples you'll need. If this is a low variance set, you may only need a few hundred/thousand observations.

Stratification/grouping

Is your data grouped in a meaningful way? If so, you'll need to factor this into your sample. Depending on what you're doing, you'll want a meaningful representation of the population. If the data is not grouped, but has distinct groups within it that you care about, you may need to stratify or pre-process your data.

Model and goals

All of this ends up coming down to what you're trying to do. If you're trying to classify or parse a set of unique entities, you may be better off streaming a large set of your data rather than trying to sample it. If you're trying to classify images or customers based on behavior, you may only need a small subset depending on how these groups differ.


Conditional Statement to update columns based on range I have a dataframe called clients that has 5000+ rows with the following:

ID   Ordered   Days
1     101565    131
2     202546    122
3     459863    78
4     328453    327
5     458975    -27

I'm trying to create a loop that looks at the numbers of days and replace them with a new columns if it does meet the days critetaria based on the
following:

Days           NEW_COLUMNS
0-119          0-3 Months
120-209        4-6 Months
210-299        7-9 Months
300+           10+ Months
-280 to -196   Reach out clients
-195 to -104   Send promotion
-103 to -1     Close case
< -280         Plan

I have the following code but hasn't worked so far:

if(days <-280)(NEW_COLUMNS ="plan")
else if (days>-280 && days <-196)(NEW_COLUMNS=" Reach out clients";)
else if (days>-195 && days<-104)(NEW_COLUMNS =" Send promotion";)
else if (days>-103 && days <-1)(NEW_COLUMNS="Close case";)
else if(days> 0 && days <119)(NEW_COLUMNS="0-3 Months";)
else if(days > 120 && days <209)(NEW_COLUMNS="4-6 Mos";)
else if(days > 210 && day s<299)(NEW_COLUMNS="7-9 Mos";)
else if(days > 300)(NEW_COLUMNS="10+ Mos";)

Eventually I want a table that looks like this:

ID   Ordered   Days    New_Columns
1     101565    131     4-6 Months
2     202546    122     4-6 Months
3     459863    78      0-3 Months
4     328453    327     10+ Months
5     458975    -27     Close case

One elegant way to solve this is by using numpy.select. This function takes a list of conditions and a list of choices and then pick the choice where the first condition is true.

An advantage is that since the conditions are checked in order, only one side of the condition for the day value needs to be checked.

Assuming the input dataframe is called df:

import numpy as np
import pandas as pd

conditions = [
    df['Days'] < -280,
    df['Days'] < -195,
    df['Days'] < -103,
    df['Days'] < 0,
    df['Days'] < 120,
    df['Days'] < 210,
    df['Days'] < 300,
    True
]

outputs = [
    "Plan", "Reach out clients", "Send promotion", "Close case",
    "0-3 Months", "4-6 Months", "7-9 Months", "10+ Months"
]

df['New_Columns'] = np.select(conditions, outputs)

Result:

    ID  Ordered  Days New_Columns
0   1   101565   131  4-6 Months
1   2   202546   122  4-6 Months
2   3   459863    78  0-3 Months
3   4   328453   327  10+ Months
4   5   458975   -27  Close case


Reinforcement Learning: Policy Gradient derivation question I have been reading this excellent post: https://medium.com/@jonathan_hui/rl-policy-gradients-explained-9b13b688b146 and following the RL-videos by David Silver, and I did not get this thing:

For $\pi_\theta(\tau) = \pi_\theta(s_1, a_1, ..., s_T, a_T) = p(s_1) \prod_{t=1}^T \pi_\theta(a_t | s_t)p(s_{t+1}|a_t, s_t)$ being the likelihood of a given trajectory in a cycle, the derivative of the value function becomes
$$\nabla_{\theta}J = E[\nabla_{\theta}log\pi_{\theta} \cdot r ]$$

which then immediately becomes

$$={{1} \over {N}} \sum_{i=1}^{N}(\sum_t^T \nabla_\theta log \pi_\theta(a_{i,t}, s_{i,t})) r$$

i.e. summed over all N paths for $\tau$, while I expected

$$=\sum_\tau \pi_\theta(\tau) \sum_t^T \nabla_\theta log \pi_\theta(\tau) r$$

What I do not get: Where does the probability for the trajectories $\pi_\theta(\tau)$ (left-most sum) go or why did it get replaced by the mean over all paths? Is it assumed that all trajectories are equally likely, given that you start from a known starting position?

(You can find the equations in the blog-post linked above, at the end of the chapter "Optimization", right before the chapter "Intuition".)

Actually in the article there is a $\approx$ rather than an $=$. This is because you can approximate expectation values by sampling the respective distribution.

Assume you want to compute

$$ E \left[ f(x) \right] = \int p(x) f(x) dx$$

The integral might not be tractable, you might not even know the distribution $p(x)$. But as long as you can sample from $p(x)$, you can approximate quite well using the Monte Carlo estimator

$$ E \left[ f(x) \right] \approx \frac{1}{N} \sum_{i=1}^N f(x_i)$$

with $x_i \sim p(x)$ for all $i$. This approximation would get better for larger $N$. The distribution $p(x)$ is, in some sense, represented by the samples $x_i$ and their respective frequency.

This is what is going on in the article. You want to compute the expectation value over all possible trajectories, but that is infeasible. Luckily though, you can sample the distribution by running simulations of the environment. The expectation is then approximated using samples of trajectories. The $\pi_\theta(\tau)$ is represented in the sampled $a_{i,t}$ and $s_{i,t}$.

In short, the expectation over all possible paths is approximated by the mean over samples of paths.


Understanding general approach to updating optimization function parameters This question not related to a specific method or technique, rather there is a broader concept that I'm struggling to see clearly.
Introduction
In machine learning, we have loss functions that we're trying to minimize. Gradient descent is a general solution to minimizing the output of these loss functions. I understand the basic idea there, but it's the details that I'm getting stuck on.
Suppose that I have some loss function $J_\Theta(x)$, where I have some $x$ input, where $\Theta$ is just some matrix of arbitrary parameters.
In many examples, I see $\Theta$ written something like:
$\Theta =
\begin{bmatrix}
    \hat{x_0} \\
    \hat{x_1} \\
    \hat{x_2} \\
    \vdots \\
\end{bmatrix}
$
and each row of $\Theta$ is actually a vector.
Note that some concrete examples that I've run across include updating word embeddings in word2vec, or updating a softmax layer. I'm happy to elaborate if my explanation is too abstract.
In the examples of gradient descent that I've seen, typically the derivative of $J$ is taken w.r.t each row of $\Theta$, not individual elements.
So something like $\frac{dJ}{d\hat{x_0}}$, where each vector in $\Theta$ is updated according to the output of this gradient function.
Now for my point of confusion:
Suppose the parameters are initialized to zero, which is sometimes a thing. Wouldn't the updates be the same for each element of $\hat{x_i}$ in $\Theta$ during the update step? Wouldn't that lead to each vector having the same numbers for each element? I know that's the wrong conclusion, but I'm not able to see how each dimension would, in the end, become different values. I assume (hope) I'm missing something simple.

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

A general problematic case

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

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

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

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

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

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

A specific counterexample

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

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

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

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


Find the longest palindrome substring of a piece of DNA I have to make a function that prints the longest palindrome substring of a piece of DNA. I already wrote a function that checks whether a piece of DNA is a palindrome itself. See the function below.

def make_complement_strand(DNA):
    complement=[]
    rules_for_complement={"A":"T","T":"A","C":"G","G":"C"}
    for letter in DNA:
        complement.append(rules_for_complement[letter])
    return(complement)

def is_this_a_palindrome(DNA):
        if DNA!=(make_complement_strand(DNA)[::-1]):
            print("false")
            return False
        else:
            print("true")
            return True

is_this_a_palindrome("GGGCCC")

But now: how to make a function printing the longest palindrome substring of a DNA string?

The meaning of palindrome in the context of genetics is slightly different from the definition used for words and sentences. Since a double helix is formed by two paired strands of nucleotides that run in opposite directions in the 5’- to-3’ sense, and the nucleotides always pair in the same way (Adenine (A) with Thymine (T) for DNA, with Uracil (U) for RNA; Cytosine (C) with Guanine (G)), a (single-stranded) nucleotide sequence is said to be a palindrome if it is equal to its reverse complement. For example, the DNA sequence ACCTAGGT is palindromic because its nucleotide-by-nucleotide complement is TGGATCCA, and reversing the order of the nucleotides in the complement gives the original sequence.

Longest palindromic substring is a computer science problem.

One common solution is Manacher's algorithm.


Dose finding slope/intercept using the formula of m,b gives best fit line always In linear regression? In liner regression We have to fit different lines and chose one with minimum error so What is the motive of having a formula for m,b that can give slope and intercept value in the regression line ,when it cannot give best fit line directly ?

1.Consider i applied the value in dataset on the  formula of m,b and found the regression line  yhat = 17.5835x+6 and for example just assume   error calculated for this line was  3
2.Consider i fit another line randomly (i am not using the formula of m,b to find value of m,b  assume m,b value for this random line was 16,3) my 2nd regression line is yhat = 16x+3and  for example just assume   error calculated for this line was 1.5
Linear Regression Goal : to choose best fit line that has minimum error
so my second line is better than the 1st line in this case
What is the point of having a formula which gives value for slope "m", intercept "b" when it cannot give best fit line directly ?
OR is my understanding incoorect Dose finding slope/intercept using the formula of m,b  gives best line always ?
if its YES   then there is no need to try mulitple lines and calculate error  and choose line with min error
if its No  then whats the point of having a formula for slope m,intercept b when it cannot give the best fit line . dose that mean maths/stats community need to change this forumla for slope,intercept

The formulae you mentioned gives the coefficients of the line of best fit.The values are derived using the least squares method, where the goal is to minimize the sum of squared errors. Following is the derivation for the values of m and b.
Let the line of best fit be $$\hat{y} = m*x + b$$ We then  try to find the coefficients m and b which minimize the sum of squared errors between the actual value y and the observed value $\hat{y}$. \begin{align}
SSE &= \sum_{i=1}^{n}(y_{i}-\hat{y_{i}})^2 \\
&=\sum_{i=1}^{n}(y_{i}-m*x_{i}-b)^2
\end{align}
Taking the first derivative of SSE with respect to c and equating to zero.
\begin{align}
\frac{\partial SSE}{\partial b} &= \sum_{i=1}^{n}-2*(y_{i}-m*x_{i}-b)\\
0 &= \sum_{i=1}^{n}-2*(y_{i}-m*x_{i}-b)
\end{align}
Therefore we get c as $$ b = \bar{y} - m*\bar{x}$$ Similarly in order to find m we take the partial derivative of SSE with respect to m and equate it to zero.
\begin{align}
\frac{\partial SSE}{\partial m} &= \sum_{i=1}^{n}-2x_{i}*(y_{i}-m*x_{i}-b)\\
0 &= \sum_{i=1}^{n}-2x_{i}*(y_{i}-m*x_{i}-b)\\
0 &= \sum_{i=1}^{n}x_{i}*(y_{i}-m*x_{i}-b)\\
0 &= \sum_{i=1}^{n}x_{i}*y_{i} - \sum_{i=1}^{n}m*x_{i}^2 - \sum_{i=1}^{n}b*x_{i}
\end{align}
Substituting b and solving for m we get $$m = \frac{n\sum xy - \sum x\sum y}{n\sum x^2 - (\sum x)^2}$$