Policy Gradients - gradient Log probabilities favor less likely actions? Assume we work with neural networks, with the policy gradients method. The gradient w.r.t to the objective function $J$, is an expectation.

In other words, to get this gradient $\nabla_{\theta} J(\theta)$, we sample N trajectories, then average out their gradient contribution to obtain a more precise value that can "begin flowing into our network" during backprop.
$$\nabla_{\theta} J(\theta) \approx \frac1N \sum_{i=1}^N \left( \sum_{t=1}^T \nabla_\theta \log \pi_\theta (a_{i,t}|s_{i,t}) \right) \left( \sum_{t=1}^T r(s_{i,t}, a_{i,t}) \right)$$

Looking with more detail at some specific trajectory $i$, we sum the gradient at each timestep in this trajectory, then multiply by the the total reward obtained from running that trajectory.

Assuming we use softmax as the network's final layer, the values for each action belongs to the range $[0,1]$

If I remember correctly, the gradient wrt $\theta$  at each timestep of a trajectory is going to be:
$$\nabla_{\theta}\log \pi_{\theta}(a_{t}|s_{t}) = \frac{1}{\pi_{\theta}(a_{t}|s_{t})} \nabla \pi_{\theta}(a_{t}|s_{t})$$

where $\nabla \pi_{\theta}(a_{t}|s_{t})$ is then simply a derivative of softmax wrt its inputs, with the rest of the backprop towards the weight $\theta$

Question:

Let's say at timestep $t_2$ our softmax has output something like this:

[0.5,  0.1,  0.1,  0.1,  0.1,  0.1]

and the total reward for the trajectory was 50

Ignoring the rest of the backprop at the current timestep, $\frac{1}{\pi_{\theta}(a_{t}|s_{t})}$ will give us:

[2,  10,  10,  10,  10,  10]

This means we are already favoring other actions instead of the first action.  This seems counter-intuitive to me.  What if we only got such a large large reward because we took the first action? But the formula encourages us to strengthen the other 4 actions.

Yes, you are right that the term $\frac{1}{\pi_\theta(a_t|s_t)}$ itself is favoring the less likely actions. But don't forget how $\nabla \pi_\theta(a_t|s_t)$ is calculated, and you will see that $\nabla \pi_\theta(a_t|s_t)$ is over-emphasizing on the first action and $\frac{1}{\pi_\theta(a_t|s_t)}$ is correcting it.

Recall that the derivative for y =softmax(x) is $\frac{\partial y_i}{\partial x_j} = y_i(\delta_{ij}-y_i)$. Notice that $y_i$ is multiplied in the front, i.e. over-emphasizing the more likely $y_i$. By multiplying $\frac{1}{y_i}$ in the front, the over-emphasizing effect is being corrected.

In your example, when ${\pi_\theta(a_t|s_t)}=[0.5,  0.1,  0.1,  0.1,  0.1,  0.1]$, we can write down that
$$\nabla \pi_\theta(a_t|s_t)=\left(\begin{array}{cccccc}
0.5\times0.5 & -0.5\times0.1 & -0.5\times0.1 & -0.5\times0.1 & -0.5\times0.1 &  -0.5\times0.1\\
 -0.5\times0.1 & 0.1\times0.9 &  -0.1\times0.1 & -0.1\times0.1 & -0.1\times0.1 & -0.1\times0.1 \\
 -0.5\times0.1 & -0.1\times0.1 & 0.1\times0.9  & -0.1\times0.1 & -0.1\times0.1 & -0.1\times0.1 \\
-0.5\times0.1 &  -0.1\times0.1 & -0.1\times0.1 & 0.1\times0.9 &-0.1\times0.1 & -0.1\times0.1 \\
 -0.5\times0.1  &  -0.1\times0.1 & -0.1\times0.1 & -0.1\times0.1 & 0.1\times0.9 & -0.1\times0.1 \\
 -0.5\times0.1  &  -0.1\times0.1 & -0.1\times0.1 & -0.1\times0.1 & -0.1\times0.1 & 0.1\times0.9\\
\end{array}\right)$$
And by multiplying it with $\frac{1}{\pi_\theta(a_t|s_t)}=[\frac{1}{0.5},\frac{1}{0.1},\frac{1}{0.1},\frac{1}{0.1},\frac{1}{0.1},\frac{1}{0.1}]$, you effectively get
$$\nabla \log\pi_\theta(a_t|s_t)=\frac{1}{\pi_\theta(a_t|s_t)}\nabla \pi_\theta(a_t|s_t)=
\left(\begin{array}{cccccc}
0.5 & -0.5, &-0.5, &-0.5, &-0.5, &-0.5, \\
-0.1 & 0.9 & -0.1 & -0.1 & -0.1 & -0.1 &  \\
-0.1 & -0.1 & 0.9 & -0.1 & -0.1 & -0.1 &  \\
-0.1 & -0.1 & -0.1 & 0.9 & -0.1 & -0.1 &  \\
-0.1 & -0.1 & -0.1 & -0.1 & 0.9 & -0.1 &  \\
-0.1 & -0.1 & -0.1 & -0.1 & -0.1 & 0.9 &  \\
\end{array}\right)$$

In case you are still thinking that because $0.9>0.5$ in the above result, we are slightly favoring the less likely action (although not 5 times more favorable), I would say that's the desired behavior. Because when we already have a relatively high score for action 1 (pre-activation 0.5) and now increase the score a little bit, we don't want the output probability to increase that much because otherwise the probability would more easily explode out of the range of $[0,1]$.

This kind of saturating gradient effect is very common for anything using the log derivative trick, not only in RL. You can also think of the example in the original REINFORCE algorithm
paper (on page 6) where a Bernoulli random variable
$$g_i(y_i,p_i)=\left\{
\begin{array}{c l}
     1-p_i & \text{if } y_i=0\\
     p_i & \text{if } y_i=1
\end{array}\right.$$

has a log derivative as

$$\frac{\partial \log g_i}{\partial p_i}=\left\{
\begin{array}{c l}
     -\frac{1}{1-p_i} & \text{if } y_i=0\\
     \frac{1}{p_i} & \text{if } y_i=1
\end{array}\right.$$
Here when $p_i$ is already large and $y_i=1$, the derivative would be relatively small.

Finally, regarding your question what if we only got such a large large reward because we took the first action? Don't worry, here you are estimating the expected reward (and its gradient) of your current policy $\pi$, not exploiting this policy, so you just want to have a fair (unbiased) estimate, not a best possible reward.


Human activity recognition using smartphone data set problem I'm new to this community and hopefully my question will well fit in here.
As part of my undergraduate data analytics course I have choose to do the project on human activity recognition using smartphone data sets. As far as I'm concern this topic relates to Machine Learning and Support Vector Machines. I'm not well familiar with this technologies yet so I will need some help.
I have decided to follow this project idea (first project on the top)
The project goal is determine what activity a person is engaging in (e.g., WALKING, WALKING_UPSTAIRS, WALKING_DOWNSTAIRS, SITTING, STANDING, LAYING) from data recorded by a smartphone (Samsung Galaxy S II) on the subject's waist. Using its embedded accelerometer and gyroscope, the data includes 3-axial linear acceleration and 3-axial angular velocity at a constant rate of 50Hz.
All the data set is given in one folder with some description and feature labels. The data is divided for 'test' and 'train' files in which data is represented in this format:
  2.5717778e-001 -2.3285230e-002 -1.4653762e-002 -9.3840400e-001 -9.2009078e-001 -6.6768331e-001 -9.5250112e-001 -9.2524867e-001 -6.7430222e-001 -8.9408755e-001 -5.5457721e-001 -4.6622295e-001  7.1720847e-001  6.3550240e-001  7.8949666e-001 -8.7776423e-001 -9.9776606e-001 -9.9841381e-001 -9.3434525e-001 -9.7566897e-001 -9.4982365e-001 -8.3047780e-001 -1.6808416e-001 -3.7899553e-001  2.4621698e-001  5.2120364e-001 -4.8779311e-001  4.8228047e-001 -4.5462113e-002  2.1195505e-001 -1.3489443e-001  1.3085848e-001 -1.4176313e-002 -1.0597085e-001  7.3544013e-002 -1.7151642e-001  4.0062978e-002  7.6988933e-002 -4.9054573e-001 -7.0900265e-001

And that's only a very small sample of what the file contain.
I don't really know what this data represents and how can be interpreted. Also for analyzing, classification and clustering of the data, what tools will I need to use?
Is there any way I can put this data into excel with labels included and for example use R or python to extract sample data and work on this?
Any hints/tips would be much appreciated.

The data set definitions are on the page here:

Attribute Information at the bottom

or you can see inside the ZIP folder the file named activity_labels, that has your column headings inside of it, make sure you read the README carefully, it has some good info in it. You can easily bring in a .csv file in R using the read.csv command.

For example if you name you file samsungdata you can open R and run this command:

data <- read.csv("directory/where/file/is/located/samsungdata.csv", header = TRUE)

Or if you are already inside of the working directory in R you can just run the following

data <- read.csv("samsungdata.csv", header = TRUE)

Where the name data can be changed to whatever you want to call your data set.


Calculate average Intersection over Union I want to have a global IoU metric for each class in a segmentation model with a neural net. The idea is, once the net is trained, doing the forward pass over all training examples an calculate the IoU, I'm thinking in two approaches (for each class):
1) Calculate IoU for each training instance, and finally, calculate the mean IoU (per class)
2) Accumulate the intersections and unions along all the training instances, (per class) and finally taking the ratio.

To illustrate the problem, let's take two training instances in which for class=0, intersection_1 = 2, intersection_2 = 3, union_1=7, union_2=6. The mean IoU (approach 1) wil be 0.3929 and the second approach will be 5/13 = 0.3846. What method do you think will give better/unbiased result?

In my experience, the two approaches can give quite different results. It doesn't really show in the example you provided because the sizes are similar. However, in some cases in object detection, you can have the same object appear with very different sizes in two images. Your first approach will weigh the IoUs equally but the second will give more weight to the larger object. The scripts I find online to calculate the average IoU seem to do the second approach but in my opinion, each instance of a class should be weighed the same regardless of its size.
Edit:
The first approach is: $\frac{1}{n}\sum\frac{i_i}{u_i}$
The second is: $\frac{\sum i_i}{\sum u_i}$.
Let's assume that we are detecting two objects the first object is very small and is well detected. It has an intersection of 20 pixels and a union of 21 pixels. The second object is very large and is poorly detected. It has an intersection of 20 pixels and union of 200. The first method will give an average IoU of 0.52 the whereas the second will give an IoU of 0.18. The first approach treats every object detection individually and averages the IoUs effectively giving each object detection the same weight. The second approach is biased toward the larger object.


How to apply Content-based image retrieval for scanned images? Is there a way to differentiate between scanned images(with only text) that are well-lit and with good contrast, from the ones that are not(that have poor quality), making use of Content-based image retrieval? More exactly, is there a feature extraction method that can help to query a database to retrieve the well-lit, good contrast, scanned images?

Thank you!

Your problem is studied under the rubric of "image (Quality) assessment" and, more specifically, "document image assessment". Here are links to and abstracts from some relevant surveys:

Document Image Quality Assessment: A Brief Survey

  To maintain, control and enhance the quality of document images and minimize the negative impact of degrada- tions on various analysis and processing systems, it is critical to understand the types and sources of degradations and develop reliable methods for estimating the levels of degradations. This paper provides a brief survey of research on the topic of document image quality assessment. We first present a detailed analysis of the types and sources of document degradations. We then review techniques for document image degradation modeling. Finally, we discuss objective measures and subjective experiments that are used to characterize document image quality.

Subjective and Objective Quality Assessment of Image: A Survey

  With the increasing demand for image-based applications, the efficient and reliable evaluation of image quality has increased in importance. Measuring the image quality is of fundamental importance for numerous image processing applications, where the goal of image quality assessment (IQA) methods is to automatically evaluate the quality of images in agreement with human quality judgments. Numerous IQA methods have been proposed over the past years to fulfill this goal. In this paper, a survey of the quality assessment methods for conventional image signals, as well as the newly emerged ones, which includes the high dynamic range (HDR) and 3-D images, is presented. A comprehensive explanation of the subjective and objective IQA and their classification is provided. Six widely used subjective quality datasets, and performance measures are reviewed. Emphasis is given to the full-reference image quality assessment (FR-IQA) methods, and 9 often-used quality measures (including mean squared error (MSE), structural similarity index (SSIM), multi-scale structural similarity index (MS-SSIM), visual information fidelity (VIF), most apparent distortion (MAD), feature similarity measure (FSIM), feature similarity measure for color images (FSIMC), dynamic range independent measure (DRIM), and tone-mapped images quality index (TMQI)) are carefully described, and their performance and computation time on four subjective quality datasets are evaluated. Furthermore, a brief introduction to 3-D IQA is provided and the issues related to this area of research are reviewed.

No-reference image quality assessment algorithms: A survey

  valuation of noise content or distortions present in an image is same as assessing the quality of an image. Measurement of such quality index is challenging in the absence of reference image. In this paper, a survey of existing algorithms for no-reference image quality assessment is presented. This survey includes type of noise and distortions covered, techniques and parameters used by these algorithms, databases on which the algorithms are validated and benchmarking of their performance with each other and also with human visual system.

Objective image quality assessment: a survey

  Image quality assessment (IQA) is critically important for the image-processing field. IQA aims to build a computational model to predict human perceived image quality, accurately and automatically. Until now, great efforts have been employed to design IQA metrics. In this paper, we systematically and comprehensively review the fundamental, brief history, and state-of-the-art developments of IQA, with emphasis on natural image quality assessment (NIQA). First, the definition of image quality is discussed, which contains three aspects and lead to different philosophies of designing IQA metrics. Afterwards, classic NIQA metrics are presented with some further discussions. Widely used databases and the performances of classic NIQA metrics on them are also listed. We highlight the most significant works and some open issues about the developments of IQA, and provide the benchmarks for the researchers and scholars who work on IQA.

Research papers specifically on document image assessment

Document Image Quality Assessment Using Discriminative Sparse Representation

  The goal of document image quality assessment (DIQA) is to build a computational model which can predict the degree of degradation for document images. Based on the estimated quality scores, the immediate feedback can be provided by document processing and analysis systems, which helps to maintain, organize, recognize and retrieve the information from document images. Recently, the bag-of-visual-words (BoV) based approaches have gained increasing attention from researchers to fulfill the task of quality assessment, but how to use BoV to represent images more accurately is still a challenging problem. In this paper, we propose to utilize a sparse representation based method to estimate document image’s quality with respect to the OCR capability. Unlike the conventional sparse representation approaches, we introduce the target quality scores into the training phase of sparse representation. The proposed method improves the discriminability of the system and ensures the obtained codebook is more suitable for our assessment task. The experimental results on a public dataset show that the proposed method outperforms other hand-crafted and BoV based DIQA approaches.

Discrete Orthogonal Moments Based Framework for Assessing Blurriness of Camera Captured Document Images

  One of the most widely used tasks in the area of image processing is automated processing of documents, which is done using Optical Character Readers (OCR) from document images. The most common form of distortion in document images is blur which can be caused by defocus, motion, camera shake etc. In this paper we propose a no reference image sharpness measure framework using discrete orthogonal moments and image gradients for assessing quality of document images and validated the results against state of the art image sharpness measures and accuracy of three well known Optical Character Readers.

Document Image Quality Assessment Based on Texture Similarity Index

  In this paper, a full reference document image quality assessment (FR DIQA) method using texture features is proposed. Local binary patterns (LBP) as texture features are extracted at the local and global levels for each image. For each extracted LBP feature set, a similarity measure called the LBP similarity index (LBPSI) is computed. A weighting strategy is further proposed to improve the LBPSI obtained based on local LBP features. The LBPSIs computed for both local and global features are then combined to get the final LBPSI, which also provides the best performance for DIQA. To evaluate the proposed method, two different datasets were used. The first dataset is composed of document images, whereas the second one includes natural scene images. The mean human opinion scores (MHOS) were considered as ground truth for performance evaluation. The results obtained from the proposed LBPSI method indicate a significant improvement in automatically/accurately predicting image quality, especially on the document image-based dataset.

No-reference document image quality assessment based on high order image statistics

  Document image quality assessment (DIQA) aims to predict the visual quality of degraded document images. Although the definition of “visual quality” can change based on the specific applications, in this paper, we use OCR accuracy as a metric for quality and develop a novel no-reference DIQA method based on high order image statistics for OCR accuracy prediction. The proposed method consists of three steps. First, normalized local image patches are extracted with regular grid and a comprehensive document image codebook is constructed by K-means clustering. Second, local features are softly assigned to several nearest codewords, and the direct differences between high order statistics of local features and codewords are calculated as global quality aware features. Finally, support vector regression (SVR) is utilized to learn the mapping between extracted image features and OCR accuracies. Experimental results on two document image databases show that the proposed method can accurately predict OCR accuracy and outperforms previous algorithms.

A deep learning approach to document image quality assessment

  This paper proposes a deep learning approach for document image quality assessment. Given a noise corrupted document image, we estimate its quality score as a prediction of OCR accuracy. First the document image is divided into patches and non-informative patches are sifted out using Otsu’s bina- rization technique. Second, quality scores are obtained for all selected patches using a Convolutional Neural Network (CNN), and the patch scores are averaged over the image to obtain the document score. The proposed CNN contains two layers of convolution, location blind max-min pooling, and Rectified Linear Units in the fully connected layers. Exper- iments on two document quality datasets show our method achieved the state of the art performance.

Metric-based no-reference quality assessment of heterogeneous document images

  No-reference image quality assessment (NR-IQA) aims at computing an image quality score that best correlates with either human perceived image quality or an objective quality measure, without any prior knowledge of reference images. Although learning-based NR-IQA methods have achieved the best state-of-the-art results so far, those methods perform well only on the datasets on which they were trained. The datasets usually contain homogeneous documents, whereas in reality, document images come from different sources. It is unrealistic to collect training samples of images from every possible capturing device and every document type. Hence, we argue that a metric-based IQA method is more suitable for heterogeneous documents. We propose a NR-IQA method with the objective quality measure of OCR accuracy. The method combines distortion-specific quality metrics. The final quality score is calculated taking into account the proportions of, and the dependency among different distortions. Experimental results show that the method achieves competitive results with learning-based NR-IQA methods on standard datasets, and performs better on heterogeneous documents.


Training a Variational Autoencoder (VAE) for Random Number Generation I have a complicated 20-dimensional multi-modal distribution and consider training a VAE to learn an approximation of it using 2000 samples. But particularly, with the aim to subsequently generate pseudo-random numbers behaving according to the structure of the distribution. However, my problems are the following:

Is my approach fundamentally or logically flawed? Specifically, because unlike image data, the random numbers are of geometric nature and thus take negative values and could also be considered noisy.
How do I find the right architecture aside from simple trial and error? Obviously, I do not necessarily need 2D-Convolutions. But instead, 1D-Convolutions could be considered a good choice to capture the correlations (i.e. modalities of the distribution). I'm also not sure about how I properly decide on the number of hidden layers and nodes.

You are describing surrogate modelling.
Because your situation is well studied, I recommend looking at what others have published. See, this paper for example.


Can I apply survival analysis to predict if a user will revisit the website? I have one business problem in hand which is to predict if a user will revisit the website or not within 6 months. I need to majorly understand what are the factors which make the user return and also need to give business recommendations on what can be done to make a new user return to the website.
My initial idea was to do logistic regression. Lately, I read about survival analysis. I want to know if I can use survival analysis for this problem.
Also, my dataset has 20k users; each user having multiple transactions; the target variable was not given
I aggregated the dataset to one record per user and did some feature engineering to come up with a target variable.
If I want to use survival analysis in this problem, shall I consider only the last transaction of each user or shall I use the aggregated dataset?

If you want to use survival analysis (which can be more flexible and insightful), I'd recommend this package and this great tutorial. Speaking shortly, as a result, you'll get "probability of being alive" for each customer.

If you want to use logistic regression I think it's trickier. Why I think so - Like any other churn problem, it's hard to define it properly. The definition depends on your task and where the model outcome will be used. Let's say churn is a particular amount of inactivity, e.g. 30 days. You can do an initial analysis of how to find this number. Just pick a particular date (you can do it multiple times) and check % of people who made next transaction. Important thing is - your time period from both sides should be same for all users:

if it's a new users he's not able to be inactive for a long period, right?
if it's a last date in the dataset (e.g. yesterday) - not all users are not able to perform transaction within 1 day. And you'll get high churn rates. So be attentive to dates.

So you need to understand from your data - which inactivity is "normal" for the average user and define it as N. After that you can label users binary like "if inactivity > N then "churned (1)" else "not churned (0)"". And you can use this label with any classification model.


What is the standard model for CNN to compare against other classifier technique? I have created a new method to do binary image classification. I think it would be interesting to compare it to the convolutional neural network that would do the same binary classification given the same data sets. The question I have is what would be a standard architecture of such CNN model? How much should I tune it to increase the accuracy of the CNN model so it is fair to compare it to my methodology? I want this to be as fair as possible with the chance of maybe having my new methodology published. I tried googling my question but I could not stumble upon any blog about it.

I would appreciate some guidance or links.

If your model is simple and you don't have a lot of training data, then you need a model with few parameters to compare to, or else you won't be able to train it. Using standard CNN architectures may not be a good option in this case, because even if the point of using a CNN rather than a full-connected network is to reduce the parameters, they still have many millions of parameters. Depending on the details of the problem, I think you have to design something yourself. Have a look at this Kaggle guide on choosing a CNN architecture.

If you have a lot of input data and a complex model that you're comparing to, I would recommend using a standard architecture that people are familiar with, such as VGG-16 or VGG-19, so that a performance comparison means something to the reader.

You can get these models with pretrained weights, so you don't need to do any training necessarily. However, they are set up for multiclass classification and typically trained on ImageNet, so you need to adapt the classification part. The simplest way is to replace the last (output) layer with a single output node for your binary classification.

To make a fair comparison you then need to train the entire model on the same training dataset used for your model, and evaluate on the same test set. But I would start by retraining the final FC layers first, to get a quick preview of how it's going. Then retrain the full model for the fair comparison.


Do weights of keywords for each topic add up to 1 in topic modeling? When you run a topic modeling (say LDA), you can get outputs for some number of topics with corresponding keywords and their weights. Based on my understanding, people usually output top 10 or top 20 keywords for each topic. For these keywords, they also have weights which represent how important each keyword is for a certain topic.
For example, if I decided to draw out top 10 keywords for each topic, then the example output will be shown below.
topic 0: 0.2*keyword1 + 0.15*keyword2 + 0.09*keyword3 +... + 0.005*keyword10

topic 1: ...
...

topic n

I'm not sure how many maximum keywords I can draw out for each topic but do these weights for each topic add up to 1?

Yes, the weights would add up to 1. This is a dirchlet random variable - review documentation on scipy here - https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.dirichlet.html


Pyspark Pipeline Custom Transformer I'm having some trouble understanding the creation of custom transformers for Pyspark pipelines.

I am writing a custom transformer that will take the dataframe column Company and remove stray commas:

from pyspark.sql.functions import *

class DFCommaDropper(Transformer):

    def__init__(self, *args, **kwargs):
        self.name = CommaDropper

    def transform(self,df):
        df = df.withColumn('Company', regexp_replace('Company',',','')
        return df

The above code is obviously wrong. I'm unsure what/how to initialize this and then how to use the initialized class instance in the transform function.

Thanks in advance for your help.

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

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

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

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


Can I use cosine similarity as a distance metric in a KNN algorithm Most discussions of KNN mention Euclidean,Manhattan and Hamming distances, but they dont mention cosine similarity metric. Is there a reason for this?

Short answer: Cosine distance is not the overall best performing distance metric out there

Although similarity measures are often expressed using a distance metric, it is in fact a more flexible measure as it is not required to be symmetric or fulfill the triangle inequality. Nevertheless, it is very common to use a proper distance metric like the Euclidian or Manhattan distance when applying nearest neighbour methods due to their proven performance on real world datasets. They will therefore be often mentioned in discussions of KNN.

You might find this review from 2017 informative, it attempts to answer the question "which distance measures to be used for the KNN classifier among a large number of distance and similarity measures?" They also consider inner-product metrics like the cosine distance.

In short, they conclude that (no surprise) no optimal distance metric can be used for all types of datasets, as the results show that each dataset favors a specific distance metric, and this result complies with the no-free-lunch theorem. It is clear that, among the metrics tested, the cosine distance isn't the overall best performing metric and even performs among the worst (lowest precision) in most noise levels. It does however  outperform other tested distances in 3/28 datasets.

So can I use cosine similarity as a distance metric in a KNN algorithm? Yes, and for some datasets, like Iris, it should even yield better performance (p.30) compared as to Euclidian.


Orange (Data Mining) : How to start using "Orange" from Python Anaconda Environment? I have install "Orange Data Mining v3.4.1" in Anaconda Python v3 environment using commands : "conda install orange3" sucessfully.

However, I do not know how to call "Orange" as Application to start using it. There is also NO icon on the desktop as well.

Please help. Thank you very much in advance

Regards
Pearapon S.

Instructions can be found on their Github page: https://github.com/biolab/orange3#starting-orange-gui


Non-linear Regression For example suppose I've data set which looks like:

[[x,y,z],
 [1,2,5],
 [2,3,8],
 [4,5,14]]

It's easy to find the theta parameters from those tiny data set. Which is
theta = [1,2,0]

z = 1*x + 2*y + 0

But if my data set are non linear. Suppose:

[[x,y,z],
 [1,2,6],
 [2,3,15]]]

If i choose the mapping function to be of:
z = xy+yy

It would return the theta parameter :

theta = [1,1,0]

So my deal is how to choose such mapping function for data sets which varies over time. As in recommender system  the user rating varies as per the time, to reduce the cost. I've recently gone through regularization. Is there any other ideas for reducing the cost.

To answer you first question about Non linear regression:

I believe your problem of choosing mapping function for non linear regression can be solved by using Support Vector Machines.

SVMs can learn non linear mapping functions in a kernel-induced feature space.
What this means is in svms , the basic idea is to map the input data X into some high dimensional feature space f using a non linear mapping (kernel) and then doing linear regression in this feature space.

To learn more about non-linear regression and kernels, you can read this.

Secondly, Regularization is a technique that is used to solve over-fitting problem. This usually happens when you use a very dense model for your training set or you train the model for far too many steps. In this case, while the accuracy on your train set is high,but it performs very poorly in case of unseen data. Hence when you add regularization, it helps reduce the cost function.

Regularization is of two types, L1 and L2. The difference lies in the power of weight-coefficients.These should be enough for your SVM based models.

To reduce overfitting induced high cost, you can also use BatchNormalization and Dropout algorithms.

Hope this helps :)


Data produced as an output to Dumbo API of Python not getting distributed to all the nodes of cluster On the node from which I run Dumbo commands, all the files produced as output are produced on the same node. For example, suppose there is a node having name hvs on which I ran the script:

dumbo start matrix2seqfile.py -input hdfs://hm1/user/trainf1.csv -output hdfs://hm1/user/train_hdfs5.mseq -numreducetasks 25 -hadoop $HADOOP_INSTALL

When I inspect my file system, I find that all the files produced are accumulated only in the hvs node.

Ideally, I'd like the files to get distributed throughout the cluster--my data is not getting balanced throughout the cluster. Can anyone advise me on how to fix this?

In hadoop etc folder,hdfs-site.xml was having number of replication units as 1.That is why,all the files were getting saved on one single node.I changed it and the problem resolved.


Is sequential pattern mining possible with machine learning? I am curious if sequential pattern mining algoritmhs fill a unique gap or the same thing can be achieved with alternative methods for example wih machine learning or something else. Do you know any alternative methodology that can achieve the same thing? (For me this would be relevant, because if there is an alternative method I can compare them in my thesis.)

Seq2Pat: Sequence-to-Pattern Generation Library might be relevant to your case.
The library is written in Cython to take advantage of a fast C++ backend with a high-level Python interface. It supports constraint-based frequent sequential pattern mining.
Here is an example that shows how to mine a sequence database while respecting an average constraint for the prices of the patterns found.
# Example to show how to find frequent sequential patterns
# from a given sequence database subject to constraints
from sequential.seq2pat import Seq2Pat, Attribute

# Seq2Pat over 3 sequences
seq2pat = Seq2Pat(sequences=[["A", "A", "B", "A", "D"],
                             ["C", "B", "A"],
                             ["C", "A", "C", "D"]])

# Price attribute corresponding to each item
price = Attribute(values=[[5, 5, 3, 8, 2],
                          [1, 3, 3],
                          [4, 5, 2, 1]])

# Average price constraint
seq2pat.add_constraint(3 <= price.average() <= 4)

# Patterns that occur at least twice (A-D)
patterns = seq2pat.get_patterns(min_frequency=2)

Notice that sequences can be of different lengths, and you can add/drop other Attributes and Constraints. The sequences can be any string, as in the example, or integers.
The underlying algorithm uses Multi-valued Decision Diagrams, and in particular, the state-of-the-art algorithm from AAAI 2019.
Another source that might be relevant is SPMF
Hope this helps!
Disclaimer: I am a member of the research collaboration between Fidelity & CMU on the Seq2Pat Library.


Proper Understanding of Condensed Nearest Neighbor I have a question regarding the Condensed Nearest Neighbors algorithm:

Why am I returning Z, which if I understand correctly, is the array of all of the misclassified points? Wouldn't I want to return the points that were classified correctly? What benefit does this give me in returning all the points I got wrong?

Condensed Nearest Neighbors algorithm helps to reduce the dataset X for k-NN classification. It constructs a subset of examples which are able to correctly classify the original data set using a 1-NN algorithm.

It is returning not the array of misclassified points, but a subset Z of the data set X.

CNN works like that:

1) Scan all elements of X, looking for an element x whose nearest prototype from Z has a different label than x

2) Remove x from X and add it to Z

3) Repeat the scan until no more prototypes are added to Z

Z used instead of X for kNN classification.

An advantage of this method is decreasing of execution time, reducing a space complexity


dealing with HuggingFace's model's tokens I have a few questions regarding tokenizing word/characters/emojis for different huggingface models.
From my understanding, a model would only perform best during inference if the token of the input sentence are within the tokens that the model’s tokenizer was trained on.
My questions are:

is there a way to easily find out if a particular word/emoji is compatible (included during model training) with the model? (in huggingface context)

if this word/emoji is not was not included during model training, what are the best ways to deal with these words/emojis, such that model inference would give best possible output considering the inclusion of these word/emoji as input. (for 2. it would be nice if it could be answered in the context of my huggingface setup below, if possible)

My current setup is as follows:
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
pre_trained_model = 'facebook/bart-large-mnli'
task = 'zero-shot-classification'
candidate_labels = ['happy', 'sad', 'angry', 'confused']
tokenizer = AutoTokenizer.from_pretrained(pre_trained_model)
model = AutoModelForSequenceClassification.from_pretrained(pre_trained_model)
zero_shot_classifier = pipeline(model=model, tokenizer=tokenizer, task=task)

zero_shot_classifier('today is a good day ', candidate_labels=candidate_labels)

Any help is appreciated

For your first question, you can check if the tokenizer covers a certain string with the following:
text = 'today is a good day '
ids2string = lambda ids: tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(ids))
ids2string(tokenizer(text)['input_ids'])
> <s>today is a good day </s>

If emoji is not included in the tokenizer creation, the tokenizer will replace it with the unknown special token. You can access that with tokenizer.special_tokens_map['unk_token']. You can drop or keep them, shouldn't make much of a difference.
Alternatively, if you're going to fine-tune, you can add your own tokens to the existing tokenizer with tokenizer.add_special_tokens. However, in this case the embeddings for that token will be random. And you need to train them.


Fake News Detection problem I would like to work on a project for Fake News Detection especially for Indians news which are in different languages and different formats.

Fake news as image with no or very less text
Fake news on a blog site
Fake news as Tweets
Fake news in Hindi
Fake news in the watsapp group and shared across.

Need your help on the approach. One approach I can think of is using OCR we can read the content of the post, then search those content in the google. If the news is not present in any of the famous print media then we can tag it as fake. However there can be many challenges in this. What if the print media itself gives any fake news shared by someone.

How to handle the scenario where there is no text in the image but the information shown as image is fake.

How to handle posts written in Hindi. ?

And even if we detect fake news, is there any way to make the person accountable for sharing it. ? I know it is little difficult problem to solve. But is there any work currently done by any company on this. ?
Any starting point for me to get into this domain ?

This is a very ambitious project. First it's important to realize that ML cannot really solve this kind of problem in general, it can only help detect the posts which are likely fake news (see for example this other post about measuring credibility, i.e. the same question seen the other way around).

Assuming you work on the text of the message (I'm not competent about images), the first step would be to collect a corpus of data from all your sources and manually label all of it as fake or not (you could also decide to use a score of "fakeness" for instance). From there you could train a model: at first I would suggest something simple like Naive Bayes (it's the traditional model used for spam detection).

In any case it's important to keep in mind that what the model would learn is not whether the input text is a fake news or not, it would only learn to recognize the tell tale signs: for instance fake news tend to use scary words whereas real news tend to be worded more neutrally. But of course there can real news using scary words and fake news using neutral language, so it's not going to be perfect. In order to really reach the goal the only way would be to have the potential fake news checked by humans, who would be able to give a more informed judgement.

The last part of the question is more a legal question. As far as I know it's often very hard on social media to discover who is really the author, only the police can investigate this but they do it only in very serious cases.


Using deep-learning on graph data for binary classification The data:

I have certain data that I decided to represent it as a graph (I thought it would suit).
So I have the weighted graph data that includes a numeric attribute for each node. (networkx graphs).
Each graph represents a session.
Each session label is either good (1) or bad (0).

The mission:

I need to predict given an unlabeled graph, whether it's good (1) or bad (0).

What did I do so far:

I've made the ML method that calculates features (using networkx excellent algorithms) over those graphs.
For example I took the networkx algorithms for calculating betweenesss, degree_centrality, closeness_centrality, etc

I've received better results than the currently available results (which didn't use a graph for representing the data): F1-Score ~ 65%, ROC_AUC ~ 90%.

My intuition:

Maybe I shouldn't randomly choose networkx function. What if I could do something smarter using deep-learning. The model should understand how bad graph looks like, a good graph looks like, and make the classification

Problems:

I'm not sure if my intuition is correct. Maybe feeding the graph as is wouldn't be enough in order for the model to learn. I feel that I need advice regarding this approach, and especially previous similar works if they exist.

Relevant previous work

http://openaccess.thecvf.com/content_cvpr_2017/papers/Monti_Geometric_Deep_Learning_CVPR_2017_paper.pdf
http://proceedings.mlr.press/v48/niepert16.pdf
https://arxiv.org/pdf/1803.03324.pdf
https://arxiv.org/pdf/1709.05584.pdf

Questions:

Is anyone familiar code implementations of those previous work / other previous works?
Is someone familiar for methods that do it with a weighted graph that includes node attributes?
Do you agree/disagree with my intuition?
Do you suggest alternate ways with handling this problem? (DL or not DL)

Thank you :)

Feeding arbitrary graphs as inputs to any current general purpose ML algorithm, unless your problem and graphs are very specific (e.g., all graphs on a handful of nodes, or the size of your training set is of the same scale as the number of possible graphs of that size, or there is some very simple dependency - e.g. output is determined by the presence of some particular edge, etc) seems a rather pointless approach.

You can encode all NP-complete problems or e.g. the halting problem by a graph or a directed graph and few other inputs, and a 0/1 label.

One of very few successful ML algorithms that applied neural networks for a combinatorial problem was AlphaGo/AlphaZero, but it relied heavily on some specific properties of the game, the possibility to generate infinite amount of training data via self-play and enormous resources.

What you did so far (trying to construct features based on your graphs and your particular problem) makes much more sense in practice, I would explore this path further. There is also some recent literature that tries to assign graph nodes vectors of numbers, or "node embeddings", but this might work better for a specific type of graphs (sparse networks, where some additional data is available per node).


Error 'Expected 2D array, got 1D array instead:' While performing a simple fitting operation on the Titanic dataset. The following is my code:

data = pd.read_csv(r'.\Desktop\DS\Titanic\train.csv')
sex_train = data['Sex'].map({'male':0,'female':1})
survived_train = data['Survived']
sex_survivor_tree = GaussianNB()
sex_survivor_tree.fit(sex_train,survived_train)

This is a bit tricky. Using pandas data, sklearn only accepts input variables (features) with type pandas.Dataframe. In your code variable sex_train in pandas.Series type. Try the following code :

sex_train = data['Sex'].map({'male':0,'female':1}).to_frame()


Efficient environment for machine or deep learning in Python I am getting very frustrated working with either Google Colab or Azure notebooks (they are very slow and glitchy). Usually, I work with Jupyter notebooks to perform Machine Learning or Deep Learning tasks in Python.

Does anyone have any recommendations for a truly good performing alternative? It does not matter if it is free or not.

Try Kaggle, they have kernels with GPUs (free but limited 9 hours runtime I think)

Google cloud is currently beta testing jupyter notebooks on their infrastructure. You do not need to spawn your own compute instance, or even start a Docker: you have your jupyter kernel in your browser in a matter of minutes (https://cloud.google.com/ai-platform-notebooks/). There runtime is illimited but if your connection is glitchy the notebook might disconnect and you can loose your data.

But as a rule of thumb if your runtime is several hours, it might be better to prototype on a subset in notebook and then train on your full dataset in command line.


how to reshape xtrain array and what about input shape? from keras.datasets import mnist
from keras.layers import Activation,Dense,Convolution2D
from keras.models import save_model,load_model,Sequential
from keras.callbacks import TensorBoard
import matplotlib.pyplot as pl

(xtrain,ytrain),(xtest,ytest)=mnist.load_data()
model = Sequential()
model.add(Convolution2D(32,3,activation='relu',input_shape=(60000,28,28)))
model.add(Dense(10, activation='relu'))

model.summary()

model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              <space>metrics=['accuracy'])

model.fit(xtrain, ytrain, epochs=10, batch_size=60000)

Keras requires you to set the input_shape of the network. This is the shape of a single instance of your data which would be (28,28). However, Keras also needs a channel dimension thus the input shape for the MNIST dataset would be (28,28,1).

First we load the data as you did,

from keras.datasets import mnist
import numpy as np

(x_train, y_train), (x_test, y_test) = mnist.load_data()

print('Training data shape: ', x_train.shape)
print('Testing data shape : ', x_test.shape)

  Training data shape:  (60000, 28, 28)
  Testing data shape :  (10000, 28, 28)

Then we reshape the examples in the MNIST dataset to have the additional channel dimension

# Input image dimensions
img_rows, img_cols = 28, 28

# Channels go last for TensorFlow backend
x_train_reshaped = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test_reshaped = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

And now we can define our model as you did. Do note the necessary Flatten layer between the Convolutional layers and the Dense layer.

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

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

model.fit(xtrain, ytrain, epochs=10, batch_size=60000)


What is the best way to classify data not belonging to set of classes? I am building a multi-class support vector machine (8 classes to be precise) on an image dataset of pre-defined classes. And then I thought of a question:

  What if I have an image that doesn't belong to the set of predefined classes, what would be the outcome?

So I decided to experiment with it and the result was very bad. I got a higher accuracy for images that don't belong to any of the classes. Some images gave 98% accuracy, that they belonged to a particular class, even though my expectation should be that they should should have a very low accuracy.

I also tried using OnceClass SVM to first predict if it's part of the class or not. If yes, then what's the label? (Meaning I have 2 models).

But this doesn't seem to work as the OneClass SVM couldn't classify the "other" images well. Now I am running out of ideas of how to go about it.

How can I approach this problem?

Just a proposal of a method to try out.

Stage $1$: Use one-class SVM to assign those images that do not belong to the set of predefined classes as the $9$-th class.

Stage $2$: For those images that passes through your filter, let the multi-class SVM assign them to one of the $8$ classes.


Rationale behind most published works in medical imaging trying to reduce false positives In medical image processing, most of the published works try to reduce false positive rate (FPR) while in reality, false negatives are more dangerous than false positives. What is the rationale behind it?

TL;DR: diseases are rare, so the absolute number of false positives is a lot more than that of false negatives.

Let's assume that our system has the same false positive and false negative rate of 1% (pretty good!), and that we're detecting the presence of new cancers this year: 439.2 / 100,000 people, or 0.5% of the population. [source]

No cancer, no detection: 99.5% x 99% = 98.5% (98.505%)
No cancer, detection: 99.5% x 1% = 1.0% (0.995%)
Cancer, detection: 0.5% x 99% = 0.5% (0.495%)
Cancer, no detection: 0.5% x 1% = 0.005%

So we can see that we have a problem: for everyone who has cancer, two people who didn't have cancer wind up with invasive surgery, chemotherapy or radiotherapy.

For every person who fails to have a present cancer detected, two hundred people receive actively harmful treatment they didn't need and can't really afford.


How do I find the count of a particular column, based on another column(date) using pandas? I have a dataframe with 3 columns, such as SoldDate,Model and TotalSoldCount. How do I create a new column, 'CountSoldbyMonth' which will give the count of each of the many models sold monthly?

Date        Model  TotalSoldCount
Jan 19        A          4
Jan 19        A          4
Jan 19        A          4
Jan 19        B          6
Jan 19        C          2
Jan 19        C          2
Feb 19        A          4
Feb 19        B          6
Feb 19        B          6
Feb 19        B          6
Mar 19        B          6
Mar 19        B          6

The new df should look like this.

Date      Model     TotalSoldCount     CountSoldbyMonth
Jan 19     A               4                    3
Jan 19     A               4                    3
Jan 19     A               4                    3
Jan 19     B               6                    1
Jan 19     C               2                    2
Jan 19     C               2                    2
Feb 19     A               4                    1
Feb 19     B               6                    3
Feb 19     B               6                    3
Feb 19     B               6                    3
Mar 19     B               6                    2
Mar 19     B               6                    2

I tried doing
df['CountSoldbyMonth'] = df.groupby(['date','model']).totalsoldcount.transform('sum')
but it is generating a different value.

Try this:

df['CountSoldbyMonth'] = df.groupby(['Date','Model']).transform('count')

You don't need to select the third column as you only need the counts of groupby items.


Is is possible to see the input labels for a randomforest model? I have a random forest model built like this:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
model = RandomForestClassifier(random_state=0)

X_train,X_test,y_train,y_test = train_test_split(X, y, test_size = 0.2, random_state=42, stratify=y)

model.fit(X_train, y_train)

Is there any method I can call (or attribute I can use) to see the column names used to make the  model?
Thanks

It is impossible to retrieve column names from a trained Random forest classifier from my experience, there is also a previous answer for an identical question.


Decision Tree split error During the split function of the decision tree, I am getting an assertion error stating:

Error > Assert: type(C) == dict

I couldn't find any errors in the code up until now. Could you please correct it if there is any problem with it?

My code:

def split(X,Y,i):

    def __find_best_split(self):
                best_split = {}
                for column in self.X:
                information_gain,split =
    self.__find_best_split_for_column(column)
                    if split: continue
                    if not best_split or best_split["information_gain"]
       <information_gain:
                       best_split =
    {"split":split,"col":column,"information_gain":information_gain}

                    C= (best_split)

                return C

def __find_best_split(X, Y, i):
best_split = {}
for column in X:
    information_gain,split = (2,3)#self.__find_best_split_for_column(column)
    if not split: continue
    if not best_split or best_split["information_gain"] <information_gain:
        best_split = {"split":split,"col":column,"information_gain":information_gain}
C= (best_split)
return C

I ran this code in python 3.7 and it gave me the correct output: {'split': 3, 'col': 'a', 'information_gain': 2}. Also as pointed out by Ben Reiniger, it should be if not split: continue rather than if split: continue. In your case based on the indentation, C should never get defined or if indented properly, it should give empty dictionary. In both the scenario, getting error assert type(C) == dict does not makes sense.


Vectorizing text data for ML models Here is the sample data I have:

Tag 1(Val: X), Tag 2(Val: Y), Tag 3(Val: Z), Label (Val: P)

Tag 1(Val: A), Tag 2(Val: B), Tag 3(Val: C), Label (Val: Q)

Tag 1(Val: D), Tag 2(Val: E), Tag 3(Val: F), Label (Val: R)

Tag 1(Val: G), Tag 2(Val: H), Tag 3(Val: I), Label (Val: S)

All the values are strings and I need to encode them into vectors for training ML models using this data.
How do I make sure that the strings are always converted to the "same" vectors everytime?
I notice that when I try a test input with the same value as the training data, it gets vectorized into a different integer.
What is the standard procedure for preserving the mapping between String <--> Hashed Integer representation so that i get the same hash everytime?

Have you looked into a tokenizer? By your question, I can't tell if either (1) you don't know about tokenizing or (2) you are looking for an alternative to tokenizing.

I will assume the former and suggest that you read about tokenizing and go down that road. Keras provides a very simple approach to tokenizing and sequences that you can implement quickly and should get what you're looking for.


Calculate whether datapoints are part of a larger distribution I have some normally distributed variables (~800) and some variables that are in some way special (~30). I need to find out whether the special ones can be considered normal members of the distribution.

I drew a histogram of the full distribution with the special members as scatter. In addition to that, the normal distribution of all variables and that of the special members. The plot shows these graphics for the strongest difference between the calculated distributions. Green is the normal distribution of all members, orange that of the special ones.

How do I calculate the likelihood of whether the special members are part of the larger ensemble, i.e. they don't stand out?

One way addressing your question is to ask if your two datasets(regular points and irregular points) come from the same distribution?
The kolmogorov-smirnof test is a common tool for putting a number on the 'likelihood' of two datasets being from the same distribution.
python scipy has an implementation of this test.

Be aware that this test is more sensitive to differences around the means of each distribution, than to differences around the edges. From the looks of the graph in the question, its not an issue in your case, so the test should work.

This is a very concise explanation of the test


Why am I getting crossvalidation scores of 0 only I am trying Catboost package with iris dataset with following code:

from sklearn.datasets import load_iris
iris = load_iris()
from catboost import CatBoostClassifier
model = CatBoostClassifier(iterations=50,
                      learning_rate=0.1, depth=4,
                      loss_function='MultiClass')
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, iris.data, iris.target)
print(scores)

The output is:

[0. 0. 0.]

Why are the scores 0 only? I expected them to be close to 1. I tried adjusting parameters but results are still the same. Are these errors rather than classification accuracies? Thanks for your insight.

Edit: It appears that with CatBoostClassifier, cross_val_score() uses KFold() rather than StratifiedKFold(), since adding cv=StratifiedKFold() in cross_val_score function solves this problem.

With sklearn's classifiers such as LogisticRegression or SVC, cross_val_score uses StratifiedKFold as default (see here).

I think that perhaps the problem is that cross_val_score() in its default options for the parameter cv the documentation says:

  cv : int, cross-validation generator or an iterable, optional
  Determines the cross-validation splitting strategy. Possible inputs for cv are:

  None, to use the default 3-fold cross validation,
  integer, to specify the number of folds in a (Stratified)KFold,
  An object to be used as a cross-validation generator.
  An iterable yielding train, test splits.
  For integer/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.

So my guess is that, if cv not specified the split is being done without stratification. This, coupled with the fact that by default in the iris dataset the targets are perfectly sorted (50 label 0, then 50 label 1 and then 50 label 2) means that in each 3 k-fold you are training with two classes and predicting the third one and that's why the scores are 0.

Two solutions:

A) Shuffle the data:

from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
from catboost import CatBoostClassifier
model = CatBoostClassifier(iterations=50,
                      learning_rate=0.1, depth=4,
                      loss_function='MultiClass')
from sklearn.model_selection import cross_val_score
df = pd.DataFrame({'X0':iris.data[:,0],'X1':iris.data[:,1], 'X2':iris.data[:,2],'X3':iris.data[:,3],'Y':iris.target})
df = df.sample(frac=1).reset_index(drop=True)
scores = cross_val_score(model, df[['X0', 'X1', 'X2', 'X3']], df['Y'])
print(scores)

Out:

[0.96 0.94 0.94]

B) Modify the cv parameter:

from sklearn.datasets import load_iris
iris = load_iris()
from catboost import CatBoostClassifier
model = CatBoostClassifier(iterations=50,
                      learning_rate=0.1, depth=4,
                      loss_function='MultiClass')
from sklearn.model_selection import cross_val_score

scores = cross_val_score(model, iris.data, iris.target, cv = 4)
print(scores)

Out:

[1.         0.92105263 0.91891892 0.78378378]


Formulation of a reward structure I am new to reinforcement learning and experimenting with training of RL agents.

I have a doubt about reward formulation, from a given state if a agent takes a good action i give a positive reward, and if the action is bad, i give a negative reward. So if i give the agent very high positive rewards when it takes a good action, like 100 times positive value as compared to negative rewards, will it help agent during the training?

Intuitively I feel, it will help the agent training, but will there be any drawbacks of such skewed reward structure?

TL;DR: Relative scale of multiple different rewards can be important. However, granting +10 for a win and -1 for a loss in a game will not improve speed of learning how to win any better than tuning the learning rate.

  from a given state if a agent takes a good action i give a positive reward, and if the action is bad, i give a negative reward.

Usually you do not know what a "good action" or "bad action" are, and the reward system is based on the immediate outcome of the action from a certain state.

You may already know that, and I will phrase the rest of this answer as if that is what you meant by "good action". However, that is an important detail, so if you are not sure what the difference is, you could ask another question about it.

  So if i give the agent very high positive rewards when it takes a good action, like 100 times positive value as compared to negative rewards, will it help agent during the training?

The ideal reward scheme is based on easy to measure outcomes that you care about. E.g. reaching a destination, solving a puzzle, how many items are collected, the score in a game, winning a game against an opponent.

If you have both negative and positive rewards, they usually need to be scaled relative each other in a way that makes sense given the problem. It is quite common to have a low negative reward for every time step for instance, if some resource such as time, fuel or money is being used up just from the agent acting and not solving the task. In that case it may make sense to have e.g have a small -0.1 reward for "bad actions" and a larger +10 reward for "good action = completing the task".

Scaling up rewards is like increasing the learning rate. There is usually some optimal learning rate where the agent learns fastest. Too high and the learning is not stable. Too low and the learning is too slow. Given that is the case, usually you just care about getting relative sizes of rewards correct, then you can scale the learning rate to get the best learning speed.

There is no specific benefit to scale up positive rewards only. You should generally do so only when the problem definition allows you to. It might help with speed that the agent learns when you test it, but most of that effect will be the same as scaling the learning rate.

This is different to training animals, teaching small children or rewards for humans in general, where you may be advised to use positive rewards and positive signals more often than negative. However, this is likely to be related to problem domain of general survival and generalist learning in living creatures, which most RL does not replicate. It definitely is not advice that extends to the simpler statistical agents built using RL such as Q-learning.


Why is convolution filter used instead of correlation filter in CNN? When I saw the two results of applying convolution filter and correlation filter, the results have the same distribution and are just flipped. Why is convolution filter used instead of correlation filter in CNN?

Convolution (as a term) matches with what we are trying to do in a CNN: we are building a (sort of) filter to extract certain feature from a signal. What is the output of the filter when a given image (vector) is given as input? Best thought of as convolution between the input and the filter.

Correlation (as a term) would be preferred when we are dealing with two signals/inputs. Probably bit misleading applied to CNN feature extraction operation.

I think this is more to do with terminology. The magnitude will be same as you suggest. Flipping happens because convolution is generally calculated after flipping the filter vector representation.


How to define a custom resampling methodology I'm using an experimental design to test the robustness of different classification methods, and now I'm searching for the correct definition of such design.

I'm creating different subsets of the full dataset by cutting away some samples. Each subset is created independently with respect to the others. Then, I run each classification method on every subset. Finally, I estimate the accuracy of each method as how many classifications on subsets are in agreement with the classification on the full dataset. For example:

Classification-full     1    2    3    2    1    1    2

Classification-subset1  1    2         2    3    1
Classification-subset2       2    3         1    1    2
...

Accuracy                1    1    1    1  0.5    1    1

Is there a correct name to this methodology? I thought it can fall under bootstrapping but I'm not sure about this.

Random subsampling seems appropriate, bootstrapping is a bit more generic, but also correct.

Here are some references and synonyms: http://www.frank-dieterle.com/phd/2_4_3.html


Train-Test split for a recommender system In all implementations of recommender systems I've seen so far, the train-test split is performed in this manner:

+------+------+--------+
| user | item | rating |
+------+------+--------+
| u1   | i1   |    2.3 |
| u2   | i2   |    5.3 |
| u1   | i4   |    1.0 |
| u3   | i5   |    1.6 |
| ...  | ...  |    ... |
+------+------+--------+

This is transformed into a rating matrix of the form:

+------+-------+-------+-------+-------+-------+-----+
| user | item1 | item2 | item3 | item4 | item5 | ... |
+------+-------+-------+-------+-------+-------+-----+
| u1   | 2.3   | 1.7   |   0.5 |   1.0 | NaN   | ... |
| u2   | NaN   | 5.3   |   1.0 |   0.2 | 4.3   | ... |
| u3   | NaN   | NaN   |   2.1 |   1.3 | 1.6   | ... |
| ...  | ...   | ...   |   ... |   ... | ...   | ... |
+------+-------+-------+-------+-------+-------+-----+

where NaN corresponds to the situation where a user has not rated that particular item.

Now, from each row (user) of the matrix, a certain percentage of the numeric (non-NaN) values are removed and set aside into a new matrix, representing the test set. The model is then trained on the initial matrix, with test samples removed, and the goal of the recommender is to fill-in the missing values, with the smallest possible error.

My question is, can the train-test split be somehow done user-wise? For example to keep a set of users separate, train the recommender on the rest of the user set and then try to predict the ratings for the new users? I know this goes a bit against the idea that "if a recommender does not know you, it cannot recommend something you like", but I am wondering if some k-NN can be done.

My two cents,
Evaluating results of a recommendation engine where test-set is unseen users only, will allow you doing exactly that.
Evaluating results on unseen users only.
If this is indeed the motivation(product/business wise) behind your recommendation engine I would suggest trying to tackle this problem directly.
If evaluating performance on 'unseen' users is an additional metric and not the only one I would keep with current scope of train/test splitting(which should create some unseen users in the test set).

Regarding methods on how to best recommend to unseen users, your intuition(K-nn) is one common solution.
 See this for example


Creating data model out of .csv file using Python I want to create a data model out of a .csv file using Python. I mean to create dependencies, for example the primary key and stuff such that I can check if the new .csv complies with the given data model. I would appreciate some suggestions regarding how to do that, the libraries, frameworks etc

In my opinion this is probably achieved fastest with a rule set. You can easily load and manipulate data via pandas. Then you just have to determine what are rules. But without any details its difficult to go more into details. Regular expressions might be a powerful tool to use but that is speculation given the sparse info you provide.


How to generate bootstrapping samples in R? Here is my original dataset from a voting activity. Each participant voted for one option (A,B,C,D,E,F) listed below. The numerical value is the total number of participants who voted for each option.

I want to bootstrap the voting for 1000 times (sample with replacement) and make a comparison between the pre-event and post-event voting for each category using independent sample t-test.

However, I don't know how to start with generating the 1000 bootstrapping samples...I have the "Boot" packages installed on R. Could anyone give me a hint what kind of steps I should take in order to generate 1000 bootstrap samples based on the original data I provide above?

Creating samples

You can create samples using sample and replicate functions.
I create smp with the first given row.

> smp <- c(rep('A', 8), rep('B', 4), rep('C', 2), rep('D', 11), rep('E', 21), rep('F', 0))
> table(smp)
smp
 A  B  C  D  E
 8  4  2 11 21
>
> samples <- replicate(1000, sample(smp)) # creating 1000 samples
> dim(samples) # 1000 samples of 46 votes
[1]   46 1000
>
> samples[,1:3]
      [,1] [,2] [,3]
 [1,] "E"  "A"  "E"
 [2,] "D"  "E"  "E"
 [3,] "E"  "D"  "C"
 [4,] "D"  "E"  "E"
 [5,] "A"  "D"  "D"
 [6,] "B"  "E"  "E"
 [7,] "E"  "A"  "D"
 [8,] "B"  "C"  "D"
 [9,] "B"  "A"  "E"
[10,] "E"  "E"  "E"
[11,] "A"  "E"  "E"
[12,] "D"  "E"  "D"
[13,] "D"  "E"  "B"
[14,] "D"  "B"  "E"
[15,] "E"  "E"  "D"
[16,] "E"  "E"  "E"
[17,] "E"  "B"  "B"
[18,] "A"  "D"  "E"
[19,] "E"  "E"  "E"
[20,] "E"  "E"  "C"
[21,] "E"  "E"  "E"
[22,] "E"  "D"  "A"
[23,] "D"  "D"  "B"
[24,] "E"  "E"  "E"
[25,] "D"  "D"  "A"
[26,] "E"  "E"  "D"
[27,] "C"  "A"  "E"
[28,] "D"  "E"  "D"
[29,] "D"  "E"  "E"
[30,] "D"  "A"  "E"
[31,] "A"  "D"  "E"
[32,] "E"  "E"  "A"
[33,] "E"  "E"  "D"
[34,] "E"  "E"  "A"
[35,] "A"  "E"  "E"
[36,] "D"  "C"  "A"
[37,] "A"  "D"  "A"
[38,] "E"  "D"  "D"
[39,] "E"  "D"  "E"
[40,] "A"  "E"  "A"
[41,] "B"  "A"  "A"
[42,] "C"  "A"  "D"
[43,] "A"  "A"  "D"
[44,] "E"  "B"  "E"
[45,] "E"  "D"  "B"
[46,] "E"  "B"  "E"

If all you need are samples, sample solves your problem.

Bootstraping

To bootstrap you need to compute a statistic. For example, I compute the weighted mean for a table of votes.
I'm using boot::boot to bootsprap.
You must pass the original sample and a handler which receives the original sample s and a vector with the indexes shuffled (idx).
This function returns a boot object that shows the bootstrap statistics.

> boot.smp <- boot::boot(smp, function(s, idx) {
+ tt <- table(s[idx])
+ weighted.mean(tt, as.numeric(factor(names(tt), labels=1:6, levels=LETTERS[1:6])))
+ }, 1000)
> boot.smp

CASE RESAMPLING BOOTSTRAP FOR CENSORED DATA

Call:
boot::boot(data = smp, statistic = function(d, w) {
    tt <- table(d[w])
    weighted.mean(tt, as.numeric(factor(names(tt), labels = 1:6,
        levels = LETTERS[1:6])))
}, R = 1000)

Bootstrap Statistics :
    original    bias    std. error
t1*     11.4 0.4126077    1.274827

The boot object has other methods to compute confidence interval and return much more information.


Showing the response time on a graph I have several computing devices. I have used an algorithm to balance the load between these devices. There is a central coordinator which controls the load on each device and if one device is overloaded, it will transfer some load of overloaded device to a less-loaded device. I have considered the number of tasks on each device as the load of that device.

I have measured the response time with load balancing and without load balancing and now I want to show the difference between these situations on a graph. I know that y-axis represents "response time", but the problem is that I can't figure out what I should consider for x-axis. Each device may have several tasks, for example device1 = 3, device2 = 1 and device3 = 10 tasks. Should I use the mean of these values for x-axis value? If not, what should I can use for x-axis?

You Can do one thing as per my knowledge take the difference Between two response times and plot them in a line graph which ever have the more deviation from the 0 line you can say they are taking more time


What is the appropriate evaluation metric for RandomForest with probability in R? In order to build a predict model with two categories (buy or not buy),I want to use RandomForest and predict with type='prob', so I can have a prob of someone buy or not buy. So, with this outcome I can clusterize and make groups, like this:

group A: costumer who has [100 to 80]% of buy.
group B: costumer who has [81 to 60]% of buy.
...

But I don't know the appropriate evaluation metric to measure the accuracy of this model. I guess that I can't use a confusion matrix.

Maybe I can use a ROC curve, and or measure KS between the buy group with the not buy group. But I'm not sure about this metrics.

Group the entires by the probability score you assigned them (e.g. 100-95%, 95-90%,....) and then measure the likelihood of their purchases from ground truth.  You can then perform a Chi-squared calculation between your predicted and measured outcomes.


Normalize weekly data - Python I have a weekly dataset and I have to normalize this data.
Data is something like this :

1. week   50
2. week   51
3. week   50
4. week   54
5. week   150
6. week   155
7. week   ...

The important thing is, the difference between week 3 and week 4 (50-54) is not same with week 5 and week 6. And also there is a huge different between week 4 and week 5.

My question is how can i handle all of this things ?

Is the standard normalization functions(for example scikit normalization) can do it for me and should I normalize this data 0-1 or -1 to 1 ?

Sklearn normalization page

NOTE I am working with python and generally scikit-learn library.

Any help is appreciated.

I would find the unit variance of the all the weeks and then divide by that. Scikit can do this for you using scale.


automatic feature selection I have a lot (thousands are possible)  of automatically-generated ordinal features that i'd like to exploit , to differentiate between two classes. I'm looking for some measure that will select the most-likely-to-be-useful ones .  For instance I could look at the frequency of occurrence of each feature for one class vs. the other class, and go for those features that have the greatest delta .
If the features are ngrams from given names, and the classes are latin-american vs. american names , the frequency histograms look like this : latin ngrams,
english ngrams

Is there some better way to go about this than looking for large frequency deltas? The scikit-learn classifiers allow you to check for feature usefulness post-facto, I was looking to do this pre-classifier.

Frequency delta is not necessary a sound method. A better approach would be to use a statistical test for independence, like chi square independence test which can tell you if two categorical samples does not come from the same distribution. A similar construction can be done with G statistic. See more on wikipedia.

One problem you will certainly encounter is that some categorical distribution for ngrams have a large number of categories and the test is heavily applicable. To avoid that you can group the frequencies which are very small in both distributions into a single category: other. That way you limit the number of categories but comparison remains significant.

Simply comparing differences is not enough since you do not have some threshold to use which have also a meaning, while a statistical test produces p-values which are probabilities in the end.

On the other hand, you should consider the following issue with both approaches (the one you proposed and the one I presented you): they test the individual contribution of one feature, independent of others. As such, they will not catch situations where two features have insignificant contribution, but together they have a significant one.


Dynamic Images in Tableau A Tableau Workbook I'm working on has different users logging in to see the data for their company. In the dashboard I have created a dynamic image that shows the customer logo depending on who is logged in. I created this by going to the Map drop down and using Background Images. The field that controls which image is seen, is a customer dimension which lists all of the customers in the database.
The issue I'm having is that some users, internal users, can see all of the customers. When these users log in I need the logo to either disappear or show the main company's logo. The customer column doesn't these users listed because they aren't customers. This means I don't have a column to use as a filter.
I followed every step in this website and hopefully it will provide enough of an example since I can't share the original workbook. Here is the website I used to build this: https://www.thedataschool.co.uk/nora-ann-weststrate/tableau-creating-filter-dependent-images/

Found the fix by creating this calculated field:
{EXCLUDE [ColumnName] : COUNTD([ColumnName]) = 23}
This would exclude all of the customers when all were shown. Then I added this to the Filters card and the color marks card.


How to sort numbers using Convolutional Neural Network? Recently, in an interview I got this question:

Design a convnet that sorts numbers. Operators are ReLU, Conv, and Pooling. E.g. input: 5, 3, 6, 2; output: 2, 3, 5, 6

I am not sure how can you sort a list of numbers using CNN. I know it is possible using RNN. Is it even possible?

I have a solution however I use a densely connected layer at the output to simplify the reshaping. If you can manipulate the sizes of this model such that you have 4 output parameters this should work as well.

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv1D, MaxPooling1D, Reshape
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K

Preparing the data

We will generate some random lists containing integers between [0,49], we will take a random permutation of the list and then take the first 4 values. We will then set our targets $y$ as the sorted rows of $x$.

import numpy as np

n = 100000
x_train = np.zeros((n,4))
for i in range(n):
    x_train[i,:] = np.random.permutation(50)[0:4]

x_train = x_train.reshape(n, 4, 1)
y_train = np.sort(x_train, axis=1).reshape(n, 4,)

n = 1000
x_test = np.zeros((n,4))
for i in range(n):
    x_test[i,:] = np.random.permutation(50)[0:4]

x_test = x_test.reshape(n, 4, 1)
y_test = np.sort(x_test, axis=1).reshape(n, 4,)

print(x_test[0][0].T)
print(y_test[0])

  [ 44.  36.  13.   0.]  [  0.  13.  36.  44.]

The model

I tried different combinations of parameters. This worked out not bad.

input_shape = (4,1)

model = Sequential()
model.add(Conv1D(32, kernel_size=(2),
                 activation='relu',
                 input_shape=input_shape,
                 padding='same'))

model.add(Conv1D(64, (2), activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=(2)))
model.add(Reshape((64,2)))

model.add(Conv1D(32, (2), activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=(2)))

model.add(Flatten())
model.add(Dense(4))

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

epochs = 10
batch_size = 128
# Fit the model weights.
history = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))

  Epoch 10/10  100000/100000 [==============================] - 6s
  56us/step - loss: 0.9061 - acc: 0.9973 - val_loss: 0.5302 - val_acc:
  0.9950

Results

So for a new list of values, I get the predicted output. Then I determine which value in the original list is closest to each of these and replace them. I could have just rounded the predicted values, however this caused so +/-1 errors due to rounding the wrong way.

test_list = [1,45,3,18]
pred = model.predict(np.asarray(test_list).reshape(1,4,1))
print(test_list)
print(pred)

print([np.asarray(test_list).reshape(4,)[np.abs(np.asarray(test_list).reshape(4,) - i).argmin()] for i in list(pred[0])])

  [1, 45, 3, 18]  [[  0.87599814   3.43058085  17.36335754  45.21624374]]
  [1, 3, 18, 45]

And for the sequence you suggested as a test case

test_list = [5,3,6,2]
pred = model.predict(np.asarray(test_list).reshape(1,4,1))
print(test_list)
print(pred)

print([np.asarray(test_list).reshape(4,)[np.abs(np.asarray(test_list).reshape(4,) - i).argmin()] for i in list(pred[0])])

  [5, 3, 6, 2]  [[ 1.85080266  2.95598722  4.92955017  5.88561296]] [2,
  3, 5, 6]


Error in using sklearn's GridSearchCV on Word2Vec I am using the sklearn_api of gensim to create an estimator for a Word2vec model to pass it to sklearn's gridsearch . My code is as follows :
from gensim.sklearn_api import W2VTransformer
from sklearn.model_selection import GridSearchCV

s_obj = W2VTransformer(size=100,min_count=1,window=5)

parameters = {'size':(100,150,200),'min_count':(1,2,4),'alpha':(0.025,0.015)}

s_model = GridSearchCV(s_obj,parameters,cv=2)
s_model.fit(sentences)

print(s_model.best_params_)

Running the above code, I get the following error:
"If no scoring is specified, the estimator passed should have a 'score' method. The estimator W2VTransformer(alpha=0.025, batch_words=10000, cbow_mean=1,
               hashfxn=<built-in function hash>, hs=0, iter=5,
               max_vocab_size=None, min_alpha=0.0001, min_count=1, negative=5,
               null_word=0, sample=0.001, seed=1, sg=0, size=100,
               sorted_vocab=1, trim_rule=None, window=5, workers=3) does not."

I do not know how to resolve this. I tried using scoring='accuracy' or scoring='hamming' but they don't seem to work either.
Can someone please help me get rid of this error?

Do:
from sklearn.metrics import accuracy_score, make_scorer

s_model = GridSearchCV(s_obj,parameters,cv=2, scoring=make_scorer(accuracy_score))


Input Normalization for Transfer Learning If I am training a deep neural net with input features that are physical in nature (e.g. temperature, precipitation, etc), and I want to be able to perform some kind of transfer learning where I train on multiple instances to see how they perform on a different set of inputs entirely. How do I make sure that the inputs being normalized in each instant don't conflict with one another?

For example, Mean temperature of 0 degrees with a standard deviation of 10 will be the same as mean temperature of 80 degrees with a standard deviation of 10 after normalization to a mean of 0 and a std dev of 1.

You should select a single preprocessing scheme and keep it constant for all experiments!

In your case, you want to scale your input features to $0$ mean and a standard deviation equal to $1$:

$$x_{scaled} = \frac{x-μ}{σ}$$

During the initial training compute the mean $μ$ and standard deviation $σ$ of each feature and store them. Then use the same values to scale the data for all subsequent experiments.


Q-learning with a state-action-state reward structure and a Q-matrix with states as rows and actions as columns I have set up a Q-learning problem in R, and would like some help with the theoretical correctness of my approach in framing the problem.

Problem structure For this problem, the environment consists of 10 possible states. When in each state, the agent has 11 potential actions which it can choose from (these actions are the same regardless of the state which the agent is in). Depending on the particular state which the agent is in and the subsequent action which the agent then takes, there is a unique distribution for transition to a next state i.e. the transition probabilities to any next state are dependant on (only) the previous state as well as the action then taken.

Each episode has 9 iterations i.e. the agent can take 9 actions and make 9 transitions before a new episode begins. In each episode, the agent will begin in state 1.

In each episode, after each of the agent's 9 actions, the agent will get a reward which is dependant on the agent's (immediately) previous state and their (immediately) previous action as well as the state which they landed on i.e. the agent's reward structure is dependant on a state-action-state triplet (of which there will be 9 in an episode).

The transition probability matrix of the agent is static, and so is the reward matrix.

I have set up two learning algorithms. In the first, the q-matrix update happens after each action in each episode. In the second, the q-matrix is updated after each episode. The algorithm uses an epsilon greedy learning formula.

The big problem is that in my Q-learning, my agent is not learning. It gets less and less of a reward over time. I have looked into other potential problems such as simple calculation errors, or bugs in code, but I think that the problem lies with the conceptual structure of my q-learning problem.

Questions

I have set up my Q-matrix as being a 10 row by 11 column matrix i.e. all the 10 states are the rows and the 11 actions are the columns. Would this be the best way to do so? This means that an agent is learning a policy which says that "whenever you are in state x, do action y"
Given this unique structure of my problem, would the standard Q-update still apply? i.e. Q[cs,act]<<-Q[cs,act]+alpha*(Reward+gamma*max(Q[ns,])-Q[cs,act]) Where cs is current state; act is action chosen; Reward is the reward given your current state, your action chosen and the next state which you will transition to; ns is the next state which you will transition to given your last state and last action (note that you transitioned to this state stochastically).

Is there an open AI gym in R? Are there Q-learning packages for problems of this structure?

There is a problem in your definition of the problem.

Q(s,a) is the expected utility of taking action a in state s and following the optimal policy afterwards.

The expected utility of a certain state (based on your definition) is different after taking 1, 2 or 9 steps. That means that the reward of being in state s_0 and taking action a_0 is different in step 0 from what you get in step 9

To adequate model the problem, you should reframe it and consider the state to be both the 'position'+'step'. You will now have 90 states (10pos*9steps).


What does it mean: "Everything looks OK but loss won't decreases!" I have written a LSTM network. It seems all the things are OK but when I train the network, I get the same loss amount about 4.9e-4 for every iterations! What is the problem? Why my network can't decrease the loss amount?

IF EVERYTHING ELSE IS OKAY then ideally, your loss should decrease every epoch which means your model is learning and updating weights accordingly.

Steady loss might mean that your model has over-fitted data. Your model has memorized data and there is no room for improvement. Maybe

1) You have less data with very complex LSTM. Try having very simple LSTM.
2) Apply regularization layer in between.


How can I replace some words in my text file by keywords provided in another file using python? I'm new to Python.
I have two files. one file contains text, the second contain keywords.Here is a sample of the keywords file :

Parental authority

Rights and Responsibilities

Health and Safety

I want my code to parse my text, and whenever he finds a derivation of one of the keywords, he would replace them with the keyword as it is in my file.

Exemple : if "healthy and safe" is found in the text, he would replace it with "Health and Safety" mentioned in the keywords file.

I already have a function "Vocabulary" that does the lemmatization:
print(j --> Vocabulary[j]) would display a word and its derivations. Exemple :

Safety --> Safely, Safe

Here is my code :

import pandas as pd
import numpy as np
import spacy
from itertools import product
import re

text = pd.read_csv(name_file, sep=';', header=None, index_col=0)
text.shape
for i in text.index:
  document = i.lower
    for j in vocabulary:
       for t in vocabulary[j]:
        document.replace(t,j)

As a result, I'm having the following error :

AttributeError: 'builtin_function_or_method' object has no attribute 'replace'

Try i.lower().

It might be that document is not a string, but a function at the time in which you call document.replace(t, j)


How to define the input channel of a CNN model in Pytorch? In pytorch, we use:
nn.conv2d(input_channel, output_channel, kernel_size)

in order to define the convolutional layers.
I understand that if the input is an image which has size $\text{width} \times \text{height} \times 3$ we would set the input_channel = 3. I am confused, however, what if I have a data set that has dimension: $3 \times 3 \times 30$ or $30 \times 4 \times 5$?
Which number should I use to define the input_channel for these?
Thanks in advance.

The defining factor is which dimensions you want your 2-dimensional convolution sweep over, e.g.:

In images, you want the 2D convolution to sweep over the height and width dimensions, and the extra dimension (the color space) is the channels; for grayscale images, you have a single channel.

In a spectrogram, you want the 2D convolution to sweep over the time and frequency dimensions. As there are no further dimensions, there is only one channel, like with grayscale images.

In the cases you propose, e.g. "3 * 3 * 30", if we want the 2D convolution to happen in the two first dimensions, then the number of input channels would be 30. If we wanted the 2D convolution to sweep over two other dimensions, then the remaining one would be the number of input channels. The same for "30 * 4 * 5".
We should note, however, that 2D convolutions follow a strict convention in the ordering of dimensions. As described in the pytorch documentation, the convention is $(N,C_{in},H,W)$, which means that we should rearrange the dimensions in our input tensor (e.g. with torch.Tensor.permute) to ensure that the dimensions over which we want the 2D convolution to sweep are in the correct order (i.e. the last 2 dimensions).


Computing adjusted p-values in batches Data
For simplicity I propose the following scheme:

I have two DataFrames, one with genes, the other with miRNA (it is a simple example, it is not the case what the DF are), the data are continuous:

             Gene_1    Gene_2    Gene_3
Patient_1    220.43    12,959    12,311
Patient_2    270.27    12,870    13,234

             miRNA_1   miRNA_2    miRNA_3
Patient_1    220.43    12,959     12,311
Patient_2    270.27    12,870     13,234

I must draw correlations all the genes against all the miRNAs (using either Pearson, Kendall or Spearman correlation, it doesn't matter) establishing as final result the following structure:

Gene     miRNA      Correlation  P-value        Adjusted P-value
Gen_1    miRNA_1    0,959        0.00311        0.00014
Gen_1    miRNA_2    -0,039       0.00311        0.00014
Gen_1    miRNA_3    -0,344       0.00311        0.00014
Gen_2    miRNA_1    0,1333       0.00311        0.00014
Gen_2    miRNA_2    0,877        0.00311        0.00014
...

Problem
The result of the Cross Join (all against all) can result in a DataFrame with billions of rows. To give a dimension of the required space, leaving aside the columns of the gene and the miRNAs and considering a small result of 300 million rows would need 300000000 * (16 bits of the correlation in floating pt + 64 bits of p-value + 64 bits of adjusted p-value) = 5 GB approx.
In order to optimize the memory usage, I do the computation in batches, the problem is with the adjusted p-values since I use the method of Benjamini & Hochberg (1995) with the function p.adjust of R (using a Python wrapper) that requires the complete p-value array, which makes me run out of memory.
Is there any way to compute, either from another library or another similar statistical method, the p-value adjusted in batches?
I already tried the FastLSU technique to be able to filter out some p-values that are not significant, but as I understood it, when I get the adjusted p-value for each row I need to know all the total p-values. If I could get for each row the adjusted p-value my problem would be solved since I could download the results of the batches to disk and goodbye to complications.
If someone could shed some light on the subject I would be very grateful

Can you explain further about how you are computing in batches? The entire p-value array itself should not be large enough to cause the memory problem. So it seems like your main problem is that you are trying to hold the cross-joined DF in memory and do the computation you need. Therefore these are my thoughts:

You might be able to solve this problem the way you are currently doing it if you have access to a cluster which has more memory.
There is another way to solve it that will use less memory and could work locally but will take a long time using nested loop (see below). You can also write results intermediately instead of doing a union at the end of each loop. Afterwards you can take the initial DFS out of memory, and then load in all of the intermediary results and create the final df.
You could use the method from prior bullet point in cluster which will be a bit better.
It would be even better if you could use a cluster and parallelize it. This problem would be well suited for Spark.
If you don't have access to a cluster, you can still parallelize it on your own computer which may help a little with speed. Again, I think if you use Spark and parallelize it on your computer it would be a bit faster.

Here I will explain the first bullet which I think will at least allow you to get your final data frame without the memory issue:
I would first try making this problem into a nested loop of the columns of the data frames where you find the pvals for each gene --> all miRNAs and then subset this after computing q-values and using a cutoff. Then you will only be storing in memory the pairs which have met your q-value threshold. I would not suggest trying to create a joined DF with all of this data as that will take up too much memory. I wrote pseudo-code for a loop below. I feel like this would help with you running out of memory, however it will still be pretty slow. If you are still running out of memory, do you have a cluster available to you? Also it would be even better to parallelize this. You could write it in python but do you have Spark? If you will be continuing to work with 'big data' I would try to get access to a cluster at your institution or pay for it using AWS or something.
I am going to call your DF with the genes: geneDF and your DF with microRNA mirnaDF. Keep in mind this is python pseudo-code, and is pretty messy. I can clean it up if you think it would be useful to you. I more-so just wrote it to make what I was writing about clearer.
top_adjusted_pairs = pd.DataFrame(columns=['gene', 'miRNA', 'qval'])
for i in range(len(geneDF)):
    # initialize array for the distances between all microRNAs with just gene X
    miRNA_geneX_pvals = []*len(microRNAsDF)
    geneCol = geneDF[i]
    for j in range(len(mirnaDF)):
         mirnaCol = mirnaDF[j]
         # compute distances and write pvalue into array
         pval = dist(geneCol, mirnaCol).pval
         miRNA_geneX_pvals[j] = [miRNA.name, gene.name, pval]
    # now that you have the array of distances between gene X and all miRNAs you can use multiple hypothesis correction.
    qvals = adjust(miRNA_geneX_pvals['pval'])
    # pick q-val cut off
    top_mirna_geneX = miRNA_geneX_pvals[qvals<0.1]

top_adjusted_pairs.union(top_mirna_geneX)

```


Cross Validation for Different Metrics - Sklearn When I am doing cross validation using Python's Sklearn and take the score of different metrics (accuracy, precision, etc.) like this:

result_accuracy = cross_val_score(classifier, X_train, y_train, scoring='accuracy', cv=10)
result_precision = cross_val_score(classifier, X_train, y_train, scoring='precision', cv=10)
result_recall = cross_val_score(classifier, X_train, y_train, scoring='recall', cv=10)
result_f1 = cross_val_score(classifier, X_train, y_train, scoring='f1', cv=10)

Did every execution of cross_val_score() function for different metrics made the same 10 folds of the training data or not? If not, do I need to make the general 10-folds first using KFold, like this:

seed = 7
kf = KFold(n_splits=10, random_state=seed)

result_accuracy = cross_val_score(classifier, X_train, y_train, scoring='accuracy', cv=kf)
result_precision = cross_val_score(classifier, X_train, y_train, scoring='precision', cv=kf)
result_recall = cross_val_score(classifier, X_train, y_train, scoring='recall', cv=kf)
result_f1 = cross_val_score(classifier, X_train, y_train, scoring='f1', cv=kf)

It only really matters if you want to shuffle your data in the cross-validation. The default for both cross_val_score and KFold is to NOT shuffle.

If you do want to shuffle your second option is best if you want to make sure that you are comparing scores across the same splits on the data.

Keep in mind that in the default KFold(shuffle=False), so if you want to shuffle your data make sure that you set Kfold to:

kf = KFold(n_splits=10, shuffle=True, random_state=seed)