What is "GOAL" in terms of Reinforcement Learning specified in these papers? I have a question regarding Reinforcement Learning. I've been reading the Horde and the UVFA paper extensively.

Take the Horde paper, there is this GVF, General Value Function Approximators which have a pseudo termination function and a pseudo reward. If I understand correctly, multiple demons with different behavioral policies, each of them having a GVF, spawned and off-policy learning takes place with GQ probability distribution. What is the pseudo termination function here? Is it a state?

Similar to this, there is this "goal" in UVFA paper. Take this paragraph for example, at the end of the second page:

  A two-stream architecture, on the other hand, assumes that
  the problem has a factorized structure and computes its output
  from two components φ : S → R and ψ : G → R, that both map into an n-dimensional vector space of embeddings and output function h : R × R → R that maps two such embedding vectors to a single scalar regression output.

The novelty of this paper is that the value function generalizes not just over states but also goals, as introduced in the abstract. But I'm having hard time understanding the goal definition here. Again, is it a state that an agent wants to achieve?

So, can anyone give me a clue about the meaning behind these two concepts?

Thanks.

After some discussions with Kiuhnm in RL Group Discord channel, I understand it now.
In the Horde paper it is easy, i.e. the goal might be state or a time. This sub-agent (called demon in the paper) might have a goal of running for 10 time steps, and you might have a reward function specific for it.

In the UVFA paper, the example was to define goals as a state and that's just for simplicity. Let's say you are in a room and your state is 2-dimensional position. Your goal maybe going to next room, and that means another 2-dimensional position. UVFA proposes that, the network will generalize over goals, and make your goal achieving easier in the following tasks.

That pretty much sums it up. Thanks to everyone who did give a thought.


Trouble with Scrapy and Python: ever present unexpected indent I am trying to build a scrapy bot capable of ripping the data from my local craigslist for jobs as well as having a recursive functionality to allow for the contact data to be gathered as well. Ultimately I would like to have all of this data placed into a .CVS file. I have read Learning Python the Hard Way as well as a good portion of Automating the hard stuff with Python, however I am still quite a novice. I have referenced the following tutorials while trying to build this script:
the documentation for Scrapy & this blog post by Michael Herman. I feel as though my code simply has a dumb syntax error but I'm too ignorant of what to do to fix it.

Here's the code for the spider:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from craigslist_sample.items import CraigslistSampleItem

class MySpider(CrawlSpider):
name = "craigs"
allowed_domains = ["sfbay.craigslist.org"]
start_urls = ["http://salem.craigslist.org/search/jjj"]
#Initially grab all of the urls up to where craigslist allows
#In this case, it's 2400
for i in range(1, 24):
    start_urls.append(base_url + "s=" + str(i) + "00&")

rules = (
    Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//a[@class="button next"]',)), callback="parse_items", follow= True),
)

def parse_items(self, response):
    hxs = HtmlXPathSelector(response)
    titles = hxs.xpath('//span[@class="pl"]')
    items = []
    for titles in titles:
        item = CraigslistSampleItem()
        item["title"] = titles.xpath("a/text()").extract()
        item["link"] = titles.xpath("a/@href").extract()
        item["email"] = titles.xpath("@anonemail").extract()
        items.append(item)
    return(items)

The following is the exact error I am receiving:

*File "C:\Users\newfa\Documents\scripts\craigslist_sample\craigslist_sample\spiders\test.py", line 27
item["email"] = titles.xpath("@anonemail").extract()
^

IndentationError: unexpected indent*

I do understand that indentation is very important in Python, however I keep trying different methods of indentation and have tried several "beautify" code methods to try and get it right, which leads me to believe that it may be some other error.

After moments more of work, I was capable of solving my own problem. The error turned out to be a wild 'tab' mark that I had lost in the code there. Also, my 'allowed_domains' section was wonky. This is all corrected in the code below

 from scrapy.contrib.spiders import CrawlSpider, Rule
 from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
 from scrapy.selector import HtmlXPathSelector
 from craigslist_sample.items import CraigslistSampleItem

class MySpider(CrawlSpider):
name = "test"
allowed_domains = ["salem.craigslist.org"]
start_urls = ["http://salem.craigslist.org/search/jjj"]

rules = (
    Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//a[@class="button next"]',)), callback="parse_items", follow= True),
)

def parse_items(self, response):
    hxs = HtmlXPathSelector(response)
    titles = hxs.xpath('//span[@class="pl"]')
    items = []
    for titles in titles:
       item = CraigslistSampleItem()
       item["title"] = titles.xpath("a/text()").extract()
       item["link"] = titles.xpath("a/@href").extract()
       item["email"] = titles.xpath("A/@anonemail").extract()
       items.append(item)
    return(items)

Unfortunately, I am still not managing to acquire the email, phone number, or description data - however as these problems are not directly related to the question I posed its fair to say that it is answered.


Response variable with very high number of class levels in R I've got a dependent variable in R with about 11,000 class levels. There's ample data for modeling this many classes, however some statistical packages like h2o only support up to 1,000 classes or so.

What are the best options for classification models in R with this high number of class levels?

Cluster your n response classes into m clusters, where m < 1,000. With an incoming instance i assign it to one of the m clusters using your favorite classification algorithm. Once the instance has been assigned to a specific cluster, you just need to run it through a classifier trained on the classes that comprise that particular cluster.

I've used this particular architecture to assign texts to one of n possible "emotions", which I illustrate below. The incoming text was first scored with a binary classifier trained on positive & negative texts. Given the text's probability of being positive (as predicted by the classifier), the text was then routed to either a series of all positive or a series of all negative emotion classifiers where it was a assigned a specific emotion.

This same sort of design should work your case. First you assign your instance a cluster. Given that assignment, send your instance to the next stage - where the final classification occurs.


Why do we choose principal components based on maximum variance explained? I've seen many people choose # of principal components for PCA based on maximum variance explained. So my question is do we always have to choose principal components based on maximum variance explained? Is it applicable for all scenarios i.e text count vectors(BoW, tfidf..) where number of dimensions are really high.

Does maximum variance means most information about my data in higher dimension is captured into lower dimension?

Usually I'd plot something like this to see the variance explained.

plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('Principal Components')
plt.ylabel('Variance ratio')
plt.show()

do we always have to choose principal components based on maximum
  variance explained?

Yes. "Maximum variance explained" is closely related to the main objective as follows.

Our main objective is: for a limited budget K dimensions, what information $\mbox{a}=(a_1,...,a_K)$ to keep from original data $\mbox{x}=(x_1,...,x_D)$ ($D \gg K$) in order to be able to reconstruct $\mbox{x}$ from $\mbox{a}$ as close as possible?

If we only allow rotation and scaling of original data, i.e. $a_k := \mbox{x}.\mbox{v}_k$ for unknown set of vectors $V_K=\{\mbox{v}_k|\mbox{v}_k \in \mathbb{R}^D, 1 \leq k \leq K\}$, and define the reconstruction error as
$$loss(\mbox{x},V_K):=\left \| \mbox{x}-\underbrace{\sum_{k=1}^{K}\overbrace{(\mbox{x}.\mbox{v}_k)}^{a_k}\mbox{v}_k}_{\hat{\mbox{x}}} \right \|^2,$$
the solution $V^*_K$ that minimizes this error is PCA. For first dimension, PCA keeps the projection of data on vector $\mbox{v}^*_1$ in the direction of largest data variance, namely $a^*_1$. For second dimension, it keeps the projection on vector $\mbox{v}^*_2$ in the direction of second largest data variance, namely $a^*_2$, and so on.

In other words, when we try to find a K-vector set $V_K$ that minimizes $loss(X,V_K)=\frac{1}{N}\sum_{n=1}^{N}loss(\mbox{x}_n,V_K)$, the solution
 $V^*_K$ includes $\mbox{v}^*_k$ that is in the direction of $k\mbox{-th}$ largest data variance.

Note that "ratio of variance explained" is a measure from statistics. Using the previous notations, it is defined as:

$$\mbox{R}(X,V_K):=1 - \frac{loss(X,V_K)}{Var(X)}$$

Since variance of original data $Var(X)$ is independent of solution, minimum of $loss(X,V_K)$ is equivalent to maximum of $\mbox{R}(X,V_K)$. For example, if $K=2$, then $V^*_2=\{\mbox{v}^*_1, \mbox{v}^*_2\}$ minimizes $loss(X,V_2)$ and equivalently maximizes $\mbox{R}(X,V_2)$. Ideally, if original data $X$ can be perfectly reconstructed from $V_K$, then $R(X, V_K)$ would be $1$.

  Does maximum variance means most information about my data in higher
  dimension is captured into lower dimension?

Yes. If we agree that "keep as much information as possible" is equivalent to "be able to reconstruct the data as close as possible", then our objective $min_{V_K}loss(X,V_K)$ formalizes "keep as much information as possible",  and its solution is "maximum variance".


Which Big-Data Frameworks have most simple interfaces? I found that Apache-Spark has pretty much simple interface and easy to use. But I want to know about other interfaces.

Can anyone give me a ranking of Big-Data frameworks in base of simplicity of their interfaces. also this is useful to express most simple and complex interfaces in base of your experiences.

Definitely this question is about some frameworks with same tasks. For example a selection between Flink and Spark just in your opinion. Detailed comparison is so lengthy and this is not my purpose. Just a selection or ranking on your opinions is sufficient.

Thanks

I think that it is impossible to answer this question comprehensively, at least for the following reasons:

big data frameworks have different goals and target different knowledge domains, so the comparison simply doesn't make much sense;
most big data frameworks (and other programming frameworks, for that matter) have multiple interfaces, and frequently those sets of interfaces are significantly different (the intersection is small), so there is a risk of comparing apples and oranges;
trying to compare anything (in this case, interfaces), using simplicity as a criterion, involves a significant amount of subjectivity - what one person perceive as very simple, another person might find quite complex;
the variety and the number of big data frameworks is mind-boggling (for example, see https://github.com/onurakpolat/awesome-bigdata); the same applies to a related topic of machine learning frameworks (for example, see https://github.com/josephmisiti/awesome-machine-learning);
corollary from the points above: a comprehensive comparison (considering all the above-mentioned issues) would go far beyond the scope of a single answer on this site, in volume and effort - it would be more like a long research paper, a book chapter or even a book.


Question about BERT embeddings with high cosine similarity Under what circumstances would BERT assign two occurrences of the same word similar embeddings? If those occurrences are contained within similar syntactic relations with their co-occurrents?

It's really hard to say if the BERT assigns similar embeddings to the same words in the similar context. Most likely it will not. This is because, the embeddings, is not just function of context (other words in the sentence), but the position as well. Hence, even if the words occurs in similar context, with different positions, they can have different embeddings.


How does Decision Tree with Gini Impurity Calculate Root Node? I couldn't figure out how it selected the root node with with <=7.5 and it's gini impurity is 0.45 but I tried to manually calculate it but the weighted gini impurity I got for it was 0.27.

Can anyone explain me how the calculation is done here just for the Root Node?

Here's a small dataset that I've generated,

import pandas as pd
import numpy as np

a = [5, 6, 7, 8, 9]
a1 = [1, 0, 1, 0, 0]
df = pd.DataFrame(np.c_[a, a1], columns=['val','target'])

   val  target
0    5       1
1    6       0
2    7       1
3    8       0
4    9       0

Here my code,

from sklearn.tree import DecisionTreeClassifier
from IPython.display import Image
import pydot

dt = DecisionTreeClassifier()
dt.fit(df.val.to_frame(), df.target.to_frame())

data_dot = export_graphviz(dt, out_file=None, class_names=['0', '1'])
graph= pydot.graph_from_dot_data(data_dot)
Image(graph[0].create_png())

From ISLR:

  ... we consider all predictors $X_1$, . . . , $X_p$, and all
  possible values of the cutpoint s for each of the predictors, and then
  choose the predictor and cutpoint such that the resulting tree has the
  lowest RSS ...

Since it's a classification problem, the best split is chosen by maximizing the Gini Gain, which is calculated by subtracting the weighted impurities of the branches from the original Gini impurity.

For c total classes with the probability of picking a datapoint with class, i is p(i), then the Gini Impurity is calculated as:

\begin{equation}
G = \sum_{i=1}^{c} [ p(i) * (1 − p(i)) ]
\end{equation}

1. Gini Impurity

Here, c = 2 , P(0) = 3/5 and P(1) = 2/5

G = [P(0) * (1 - P(0))] + [P(1) * (1 - P(1))]

G = [3/5 * (1 - 3/5)] + [2/5 * (1 - 2/5)] = 12/25

G = 0.48

2. Gini Gain

Now, let's determine the quality of each split by weighting the impurity of each branch. This value - Gini Gain is used to picking the best split in a decision tree.
In layman terms, Gini Gain = original Gini impurity - weighted Gini impurities So, higher the Gini Gain is better the split.

Split at 6.5:

Gini Impurity G_left  = [1/2 * (1 - 1/2)] + [1/2 * (1 - 1/2)] = 0.50
Gini Impurity G_right = [2/3 * (1 - 2/3)] + [1/3 * (1 - 1/3)] = 0.44
Weighted Gini  = (1/5 * .50) + (4/5 * 0.44) = 0.45
Gini Gain = 0.48 - 0.45 = 0.03

Split at 7.5:

Gini Impurity G_left  = [2/3 * (1 - 2/3)] + [1/3 * (1 - 1/3)] = 0.444
Gini Impurity G_right = [2/2 * (1 - 2/2)] = 0
Weighted Gini  = (3/5 * 0.444) + (2/5 * 0) = 0.27
Gini Gain = 0.48 - 0.27 = 0.21

Split at 8.5:

Gini Impurity G_left  = [2/4 * (1 - 2/4)] + [2/4 * (1 - 2/4)] = 0.500
Gini Impurity G_right$ = [1/1 * (1 - 1/1)] = 0
Weighted Gini  = (4/5 * 0.5) + (1/5 * 0) = 0.40
Gini Gain = 0.48 - 0.40 = 0.08

So, the best split will be chosen 7.5 because it has the highest Gini Gain.


Data scaling before or after PCA I have seen senior data scientists doing data scaling either before or after applying PCA.

What is more right to do and why?

I once heard a data scinetist state at a conference talk: "Basically, you can do what you want, as long as you know what you are doing."

This also applies here. The more statistically sound way would be to transform all variables prior to additional steps such as PCA or factor analysis. Then you still know the scale of your variables and can interpret the rescaling in the context of your application. If you have no such interpretation, but good reasons for rescaling your principal components due to computational issues arising if some values are to close to zero while others are quite large, rescaling the components makes sense. However, reversing this process and still being able to interpret the effect of the rescaling operation in your context will become almost impossible.


Different approaches of creating the test set I came across different approaches to creating a test set. Theoretically, it's quite simple, just pick some instances randomly, typically 20% of the dataset and set them aside. Below are the approaches

  The naive way of creating the test set is

def split_train_test(data,test_set_ratio):
  #create indices
  shuffled_indices = np.random.permutation(len(data))
  test_set_size = int(len(data) * test_set_ratio)
  test_set_indices = shuffled_indices[:test_set_size]
  train_set_indices = shuffled_indices[test_set_size:]
  return data.iloc[train_set_indices],data.iloc[test_set_indices]

The above splitting mechanism works, but if the program is run, again and again, it will generate a different dataset. Over the time, the machine learning algorithm will get to see all the examples. The solutions to fix the above problem was (guided by the author of the book)

Save the test set on the first run and then load it in subsequent runs
To set the random number generator's seed(np.random.seed(42)) before calling np.random.permutation() so that it always generates the same shuffled indices

But both the above solutions break when we fetch the next updated dataset. I am not still clear with this statement.

  Can someone give me an intuition behind how do the above two solutions breaks when we fetch the next updated dataset ?.

Then the author came up with another reliable approach to create the test.

 def split_train_test_by_id(data, test_ratio, id_column):
   ids = data[id_column]
   in_test_set = ids.apply(lambda id_: test_set_check(id_, test_ratio))
   return data.loc[~in_test_set], data.loc[in_test_set]

  Approach #1

 def test_set_check(identifier, test_ratio, hash=hashlib.md5):
    return bytearray(hash(np.int64(identifier)).digest())[-1] < 256 * test_ratio

  Approach #2

 def test_set_check(identifier, test_ratio):
    return crc32(np.int64(identifier)) & 0xffffffff < test_ratio * 2**32

  Approaches #1,#2, why are we making use of crc32, 0xffffffff, byte array?.

Just out of curiosity, I passed different values for identifier variable into hash_function(np.int64(identifier)).digest() and I got different results.

  Is there any intuition behind these results ?.

It gets a little complicated, I've attached links at the end of the answer to explain as well.

def test_set_check(identifier, test_ratio, hash=hashlib.md5):
    return bytearray(hash(np.int64(identifier)).digest())[-1] < 256 * test_ratio

The hash(np.int64(identifier)).digest() part returns a hash value of the identifier (which we cast as an int of 8 bytes with np.int64()).

The bytearray method stores the hash value into an array. The [-1] represents getting 1 byte/the last byte from the array/hash value. This byte will be a # between 0 and 255 (1 byte=11111111, or 255 in decimal).

Assuming our test_ratio is .20, the byte value should be less than or equal to 51 (256*.20=51.2). If the byte is less than or equal to 51, the row/instance will be added to the test set.

Rest is explained well in these links:

https://stackoverflow.com/questions/50646890/how-does-the-crc32-function-work-when-using-sampling-data

https://github.com/ageron/handson-ml/issues/71

https://docs.python.org/3/library/hashlib.html


Why should softmax be used in CNN In the last layer of CNNs and MLPs it is common to use softmax layer or units with sigmoid activation functions for multi-class classification. I have seen somewhere, I don't remember where, that softmax is used whenever the classes are mutually exclusive and the layer with units containing sigmoid activation function are used in tasks with multiple labels, e.g. recognizing animals in an image which can contain numerous animals. Am I right? Is there any other deduction for distinguishing between them?

I have seen here and here but they do not contain what I want.

Yes, you are right. The soft-max layer outputs a probability distribution, i.e. the values of the output sum to 1. The sigmoid function outputs marginal probabilities and therefore can be used for multiple-class classification, when the classes are not mutually exclusive. Additionally the soft-max layer is soft version of the max-output layer so it is differentiable and also resilient to outliers. A problem with sigmoids is that as you reach saturation (values get close to 1 or 0), the gradients vanish. This is detrimental to optimization speed and soft-max doesn't have this problem. Another interpretation is soft-max as a generalization of sigmoid, actually when there are two classes they are the same.

Wrapping up you should use soft-max when the classes are mutually exclusive and sigmoid when the classes are independent. This can be summarized in the following table:

Soft-Max                            |                 Sigmoid
-------------------------------------------------------------------------
Used for multi-classification       |  Used for binary classification in
in logistic regression model.       |  logistic regression model.
-------------------------------------------------------------------------
The probabilities sum will be 1     | The probabilities sum need not be 1
-------------------------------------------------------------------------
Used in the different layers of     | Used as activation function while
neural networks.                    | building neural networks
-------------------------------------------------------------------------
The high value will have the higher | The high value will have the high
probability than other values.      | probability but not the higher

For more information on soft-max look at the following links: 1, 2 and 3. For a step-by-step guide, including usage in Python see this reference 4.

For more on soft-max vs sigmoid check this: 5, 6 and this 7.

If you want a more reliable source on why to use soft-max regression for mutually exclusive classes you can look here. This page is part of Unsupervised Feature Learning and Deep Learning Tutorial of Stanford University, it contains material contributed by Andrew Ng and others.


Recognize Street View House Numbers I am new to deep learning and I am trying to train a NN to recognize house numbers gathered from street view. I have already managed to recognized MNIST sequence of hand written digits by means of a CNN. In that case I have trained the CNN with a thousands of examples consisting of 1 from up to 5 digits sequences created by me.

I have thus thought to do the same thing for SVHN, I have cropped the numbers and I now have a large dataset of single digits from street view. However the MNIST data set is very homogenous (i.e. images have same size, same angle, same color, etc.) while SVHN is obviously not. Trying to create sequences of digits from different SV photos led to weird examples that are actually not representative of the real word objects I want to model.

So I am wondering if it was better to train a NN on the single digits and then use it to recognize sequences. Would it be possible? How?

Have you seen this blog post? It is doing exactly what you are trying to do.

Convolutional neural network
Use a fixed sliding window to crop plate number
Train on a fixed-length number plate

If you want to train on the digit-level, you should define a fixed rectangle size, large enough to fit a single digit in your data set. If your convolutional neural network is good, it should be able to detect your digit.

If you haven't you should take a look at the Google paper: https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/42241.pdf


Find the mode value and frequency in R I'm trying to come up with a function in R that gives the mode value of a column along with the number of times (or frequency) that the value occurs. I want it to exclude missing (or blank) values, and treat ties by showing both values. When there are no repeating values I want it to return the first-appearing value that is found along with its frequency 1.

"Name         Color
 Drew         Blue
 Drew         Green
 Drew         Red
 Bob          Green
 Bob          Green
 Bob          Green
 Bob          Blue
 Jim          Red
 Jim          Red
 Jim          blue
 Jim          blue

mode of Drew = Blue, 1
mode of Bob = Green, 3
mode of jim = Red, Blue, 2

Here's the function code i have so far, it excludes NAs but does not show both values when there is a tie and does not show frequency. Any help appreciated!

mode <- function(x) {
  if ( anyNA(x) ) x = x[!is.na(x)]
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

You do not need a custom function to do this. Let dplyr handle it. Assuming your data is in a dataframe named df, here is what it might look like:

df %>%                                       # Set up the pipe
subset(complete.cases(df)) %>%               # Removes rows with NA values
group_by(Name) %>%                           # Groups by the Name column
count(Color) %>%                             # Counts each Color by Name, creates a new column n
mutate(max = max(n)) %>%                     # Creates a new column for the max(n) by Name
subset(n == max(n)) %>%                      # Keeps only those rows where n equals max(n)
mutate(Keep == case_when(                    # Creates a dummy logical column named 'Keep'
   n > 1 ~ TRUE,                             # That is TRUEfor n > 1 to keep ties
   n == 1 & Color == head(Color, 1) ~ TRUE,  # That is TRUE for the first row of n = 1
   TRUE ~ FALSE)) %>%                        # That is FALSE for all other cases
subset(Keep) %>%                             # Keeps only those rows where Keep is TRUE
select(Name, Mode = Color, n)                # Keeps only the Name, Color, and n columns and
                                             # renames Color as Mode

Here is the output

 # A tibble: 3 x 3
 # Groups:   Name [3]
   Name  Mode   Count
   <fct> <fct>  <int>
 1 Bob   Green      3
 2 Drew  Blue       1
 3 Jim   Blue       2
 4 Jim   Red        2

If you want a function, then wrap this up in a function definition:

my_mode_func <- function(df){
df %>%
   subset(complete.cases(df)) %>%
   group_by(Name) %>%
   count(Color) %>%
   mutate(max = max(n)) %>%
   subset(n == max) %>%
   mutate(Keep = case_when(
      n > 1 ~ TRUE,
      n == 1 & Color == head(Color,1) ~ TRUE,
      TRUE ~ FALSE)) %>%
   subset(Keep) %>%
   select(Name, Mode = Color, Count = n)
}


multiple linear regression with 5 records and 25 features X1     X2    X[...]    X25    Y

Q1_2019    23     65    18        32     1,6
Q2_2019    87     32    23        46     1,2
Q3_2019    34     15    63        78     3,2
Q4_2019    85     45    43        65     3,9
Q1_2020    85     43    78        35     1,1
Q2_2020    37     78    54        78     1,5

I have a very expensive dataset which shows aggregated survey data.
These are probably means. I am trying to get the individual data but
at the moment that is all I have.
The shape of data frame is 5x26
Y data so far is collected data calculated at the end of each quarter
via other means The survey is done at the beginning of the quarter.
Y is my dependent variable and I would like to derive a polynom to
predict the exact number based on future X data or at least the
probable trend it will be going in the next quarter once new survey
data is available. Up, down, stable would be enough
I have done correlation analysis (all vs all) and there are strong
pairwise    correlation between several X and Y

Questions

Y comes as a one digit before the comma and one digit after the comma. Since all other values are 2 digits before the comma I would like to multiply it with 10 to transform it into 2 digits before the comma.Is that ok from math/data science perspective?
5 records is not much but there are a lot of features. I would like to do multiple linear regression. Do you think this feasible with this data set? What would be objections and risks doing that?
Would upsampling the dataset help me with anything here? Or could I just work with the five records?
With the strange shape of the dataset especially the low number of records do you think that sufficient precision can be reached?
How could I calculated the maximum possible precision/discriminative power possible with this dataset? (I am looking for strong arguments why they should give me access to the complete dataset)

Y comes as percent in the format. To put it into the same
dimension as  X I multiplied it with 10. Is that ok from math/data
science perspective?

As far as I can tell there's no reason to do that, and why multiply by 10?

5 records is not much but there are a lot
of features. I would like to do multiple linear regression. Do you
think this feasible with this data set? What would be objections and
risks doing that?

The fact that there are lots of features makes it harder to work with few instances, not easier. There is a very high risk of overfitting, that is of the model catching patterns which appear by chance in the features. This leads to predictions being also affected by chance, so bad performance.

Would upsampling the dataset help me with
anything here? Or could I just work with the five records?

Upsampling is unlikely to work since it's going to reproduce the patterns in the small dataset, so it's also going to reproduce patterns which appear by chance.

With
the strange shape of the dataset especially the low number of records
do you think that sufficient precision can be reached?

It depends what the data represents, if the features happen to be really good predictors for the dependent variable and are not affected by chance, it might work. But these are very optimistic assumptions, in general it's not reasonable to expect good predictions from such a small set of instances.

How could
I calculated the maximum possible precision/discriminative power
possible with this dataset? (I am looking for strong arguments why
they should give me access to the complete dataset)

In general I would suggest doing a leave-one-out experiment: use 4 instances as training set, 1 instance as test set, repeat 5 times with a different instance as test set every time. Measuring the average performance should give you an idea how far off the predictions are going to be (you could use a very simple evaluation measure such as mean absolute error).
However what you have is actually a time series apparently, so it might be worth looking at methods which take time evolution into account.


what does eta constant mean in numpy I am using numpy to implement some neural network tutorials. There is a constant("eta") used in codes. What does it means and what it stands for?

Usually "eta" means learning rate, but it would be better if you could show an example.


Is it possible to create a neural network with two inputs, with sequential layers? Is there a natural way, in terms of structure of the layers of a NN, in order to pass 2 inputs vectors to the NN?

Example: text authorship identification

Input #1: sentence1 by unknown author encoded, as list of words from a dictionary

"The sky is blue" => x1 = [2, 23, 7, 76, 0, 0, 0, ..., 0] (zero-padded to have a length of 1000 items)
Input #2: sentence2 by unknown author, idem

"The cat is sleeping" => x2 = [2, 65, 7, 121, 0, 0, 0, ..., 0]
Output: y = a single number in [0, 1].
0 = different authors
1 = same author
0.9 = high probability of same author, etc.

Of course I could build the layers like this:

  Input-size: None, 1000, 2    (x1, x2 stacked into a 1000x2 matrix)
  CNN ...
  ...
  Dense: None, 1

Then I could train with a dataset with 10,000 pairs of sentences of the same author (desired output: 1), and 10,000 pairs of sentences with different authors (desired output: 0).

But I don't know if it would work by just stacking x1 and x2 as a 1000x2 matrix.

TL;DR: What is the classical approach to build a NN taking 2 inputs and a single number as output which is a similarity index from 0% to 100%? (if possible with Sequential structure)

Sure!  There's all sorts of ways you can pass in two inputs.  Your idea of stacking the inputs like a 2-channel image is one idea.

Another possibly simpler idea would be something like this:

The intuition is that the network learns intermediate representations for the two sentences, then compares the representations.

Of course, you have an enormous amount of flexibility in how you configure the network connections. There are endless variations that allow you to have two (or more) separate inputs.  For another example, check out DeepMind's pysc2 paper.  On page 10 you can find diagrams for networks that deal with three different inputs.  There's a flat vector of non-spatial features along with two different spatial inputs.


Which is the fastest image pretrained model? I had been working with pre-trained models and was just curious to know the fastest forward propagating model of all the computer vision pre-trained models. I have been trying to achieve faster processing in one-shot learning and have tried the forward propagation with few models over a single image and the results are as follows:

VGG16: 4.857 seconds
ResNet50: 0.227 seconds
Inception: 0.135 seconds

Can you tell the fastest pre-trained model available out there and the drastic time consumption difference amongst the above-mentioned models.

The answer will depend on some things such as your hardware and the image you process. Additional, we should distinguish if you are talking about a single run through the network in training mode or in inference mode. In the former, additional parameters are pre-computed and cached as well as several layers, such as dropout, being used, which are simply left out during inference. I will assume you want to simply produce a single prediction for a single image, so we are talking about inference time.

Factors

The basic correlation will be:

more parameters (i.e. learnable weights, bigger network) - slower than a model with less parameters
more recurrent units - slower than a convolutional network, which is slower than a full-connected network1
complicated activation functions - slower than simple ones, such as ReLU
deeper networks - slower than shallow networks (with same number of parameters) as less run in parallel on a GPU

Having listed a few factors in the final inference time required (time taken to produce one forward run through the network), I would guess that MobileNetV2 is probably among the fastest pre-trained model (available in Keras). We can see from the following table that this network has a small memory footprint of only 14 megabytes with ~3.5 million parameters. Compare that to your VGG test, with its ~138 million... 40 times more! In addition, the main workhorse layer of MobileNetV2 is a conv layer - they are essentially clever and smaller versions of residual networks.

Extra considerations

The reason I included the whole table above was to highlight that with small memory footprints and fast inference times, comes a cost: low accuracies!

If you compute the ratios of top-5 accuracy versus number of parameters (and generally versus memory), you might find a nice balance between inference time and performance.

1 Have a look at this comparison of CNNs with Recurrent modules


Is it possible to use Generative Adversarial Networks (GANs) for text classification? I am working on the classification of fake and real news. I did use a CNN Model for this problem and got satisfactory results. But, I was just wondering if it's at all possible to use any type of GAN for this sort of a problem.

(without philosophically adding some layers on top of GANS) answer is NO.

You can expect to generate images, news etc (generative adverserial network) but not to actually perform classification


Probability or score on how likely an article is motivated by content marketing Given a webpage url and the extracted article text from the given page, I want to calculate a probability of the article to be content marketing.

I consider content marketing as differentiated from spam, because it is to admit, content marketing makes some serious efforts to deliver real information to the reader. But the true reason behind the effort of writing an article in the context of content marketing is to sooner or later guide the reader to a newsletter subscription, a product landingpage or to contact the author. In the language of content marketers this kind of qualified reader is considered a "lead".

1. call to actions

I could by using Knn or Bayes simply test the scentences in the article text against a training set of known CTAs.

Okay, this where NLP comes in and this is all nothing else than spam detection.

2. link structure

I possibly can find strong evidence on content marketing motivation, in the outgoing links of a certain page or article. If it links to a landing page styled product page (some heuristics to identify these kind of webpage are needed as well) and a lot of other sites of the website link to a "near by" page.

By "near by" I mean text similarity, optimized for a (and the same) keyword.

question
So what do you think about this? How could scoring look like. How should a probability model look like? Are there aspects, that I miss? Does anyone have some experiences in this field? Is a paper out there? Does anyone know some resources for this?

If you have a broad set of testing data, I think this is feasible. I've had luck in getting basic models to identify abstract concepts like someone's politics or real news vs fake news, so I think this could work if people write content marketing different from normal news.

I just used this link in another post, but here is a tutorial in Python of taking unstructured articles, passing them through an NLP pipeline, and classifying them into multiple groups.


Does a larger action space take longer to train an RL agent? I am playing around with the openai gym to try and better understand reinforcement learning. One agent parameter you can modify is the action space i.e. the specific actions an agent can take in an environment at each state e.g. "Left", "Right", "Up" or "Down" if the environment is a game with 4 discrete actions.

In my research, I have not found anywhere that explicitly states that an RL model, specifically PPO2, will take longer to train if the action space is larger.

All else being held the same; same data, same environment, same hyperparameters, same hardware, will a model with a larger action space (more possible actions) take longer to train (1 episode) than a model with a smaller action space?

(e.g. will an agent with 100 possible actions take longer to train 1 episode than an agent with 2 possible actions?)

Intuitively, I would have thought that the more actions an agent has, the more "choice" it has at each state, and therefore it would take longer in choosing in one of those actions. But again, I haven't found anything proving or disproving this.

I think there are two things in your question:

The number of parameters of your network. So if you have more actions to predict the action layer of your network will have more parameters and it will take a little bit longer theoretically to update it (the time difference thought might be negligible depending your computational resources).
The total training time. Yes, having more choices it will take longer for the algorithm to generate some good estimations of expected reward per action in order to lead to a satisfactory performance. If for Critic you are using a state value $V(s)$ instead of $Q(s,a)$ still your algorithm will take longer to explore the action space and map your input state to a probability distribution over your action space.


Censored output data, which activation function for the output layer and which loss function to use? I am building a neural network to solve a regression problem. The output is a single numerical value.

Unfortunately, the output is censored: the values below 0 were recorded as 0, and postive values remained unchanged.

What activation function should I use for the output layer (maybe ReLU)? How to define the loss function, should I just use RMSE? (because the output is censored, we want the neural network to be able to generate 0 output, and positive values).

Edit 1:

The problem is to predict the electricity demand time series based on multiple input variables. Only the values above a certain threshold are being recorded, hence the output is censored. We have a lot of numerical/categorical input variables: time of the day, air temperature, days in a week, holiday/workday, etc. We want to build a neural network model to predict electricity demand (0 or positive) based on the input variables.

We can't tell you what loss function to use.  That is based on business needs.  In particular, what is the cost of being wrong?  Is the cost of being wrong proportional to relative error?  absolute error?  Something else?  That will drive the choice of loss function.  You should try to choose a loss function where the value of the loss function is proportional to the cost of the error (e.g., the monetary cost to your company).

Using ReLU as the activation function in the final layer would be sensible given that your data is censored to replace all negative outputs with 0.


How to train millions of doc2vec embeddings using GPU? I am trying to train a doc2vec based on user browsing history (urls tagged to user_id). I use chainer deep learning framework.

There are more than 20 millions (user_id and urls) of embeddings to initialize which doesn’t fit in a GPU internal memory (maximum available 12 GB). Training on CPU is very slow.

I am giving an attempt using code written in chainer given here

Please advise options to try if any.

One option is to switch to a deep learning framework that supports distributed training.


For the line $w^Tx = 0$, how do we know the direction of the vector $w$ On page 17/28 of Lecture 3: Linear Classification, why the slope of $w$ must be positive?

I don't see anything in the slides that suggests the line defined by w must have a positive slope. It happens to be in this picture, but w could be any vector.


R summarise with condition I have customer data with the products they purchased and the purchase date.

I want to extract a result that shows each customer and the first two fruits they purchased.

My actual set has 90000 rows with 9000 unique customers. I have tried groupby and summarise functions but I would like to be able to use summarise with condition like we use select with a where clause.
Thanks for your suggestions

Here is an example on the iris dataset

t(sapply(by(iris$Sepal.Length,iris$Species,function(x){x[1:2]}),as.numeric))

Where species is your customer, and Sepal.Length is your fruit.


How to get the probability/closeness of a sample belonging to a specific cluster? I'm new to this so please let me know if my logic of comparing cosine similarity and k-means is incorrect

I got a set of 4 clusters from k-means and now I'm interested in the Cluster No. 1. For this cluster, I take the average of all values for each column and keep it aside.

Now, I have a test sample, for which I run k-means prediction and I get output as 1, meaning it belongs to Cluster No. 1 which is good for me but my use-case here was to calculate that even if that sample didn't belong to Cluster 1, how close was it to falling in that Cluster No. 1

Hence, to resolve this I thought of doing a cosine similarity between my test sample and the one where I take average of all values for each column. Now, in this case, I get a similarity of just 5%

I'm not sure, for my use-case i.e.  (Getting the probability/closeness of a sample belonging to a specific cluster) which is a better interpretation for me?

I know I can use the cluster labels as y variables and make multi-class classification model but I want to keep it as un-supervised as possible. Please guide

Try Gaussian Mixture Model (GMM) as it is similar to KMeans, but differs in a few ways. In a nutshell think of KMeans as a hard clustering model where each sample is assigned to only one cluster, whereas GMM is a soft clustering technique that calculates the density (probability) of each Gaussian mixture (which can be considered as clusters) containing the data point in question. You can get both labels and probability scores from the model. Try it and see if it helps in your case. It is available from the SciKit Learn library.
Another approach in case if you have to stick to KMeans could be:

Take the cluster centers from the KMeans model.
Take your test sample vector and pass these as parameters to the softmax function to get the probability score for all the cluster centers per sample.


How to update weights in a neural network using gradient descent with mini-batches? [I've cross-posted it to cross.validated because I'm not sure where it fits best]

How does gradient descent work for training a neural network if I choose mini-batch (i.e., sample a subset of the training set)? I have thought of three different possibilities:

Epoch starts. We sample and feedforward one minibatch only, get the error and backprop it, i.e. update the weights. Epoch over.

Epoch starts. We sample and feedforward a minibatch, get the error and backprop it, i.e. update the weights. We repeat this until we have sampled the full data set. Epoch over.

Epoch starts. We sample and feedforward a minibatch, get the error and store it. We repeat this until we have sampled the full data set. We somehow average the errors and backprop them by updating the weights. Epoch over.

Let us say that the output of one neural network given it's parameters is $$f(x;w)$$
Let us define the loss function as the squared L2 loss (in this case).
$$L(X,y;w) = \frac{1}{2n}\sum_{i=0}^{n}[f(X_i;w)-y_i]^2$$
In this case the batchsize will be denoted as $n$. Essentially what this means is that we iterate over a finite subset of samples with the size of the subset being equal to your batch-size, and use the gradient normalized under this batch. We do this until we have exhausted every data-point in the dataset. Then the epoch is over. The gradient in this case:
$$\frac{\partial L(X,y;w)}{\partial w} = \frac{1}{n}\sum_{i=0}^{n}[f(X_i;w)-y_i]\frac{\partial f(X_i;w)}{\partial w}$$
Using batch gradient descent normalizes your gradient, so the updates are not as sporadic as if you have used stochastic gradient descent.


Binary classification of every time series step based on past and future values I'm currently facing a Machine Learning problem and I've reached a point where I need some help to proceed.

I have various time series of positional (x, y, z) data tracked by sensors. I've developed some more features. For example, I rasterized the whole 3D space and calculated a cell_x, cell_y and cell_z for every time step. The time series itself have variable lengths.

My goal is to build a model which classifies every time step with the labels 0 or 1 (binary classification based on past and future values). Therefore I have a lot of training time series where the labels are already set.

One thing which could be very problematic is that there are very few 1's labels in the data (for example only 3 of 800 samples are labeled with 1).

It would be great if someone can help me in the right direction because there are too many possible problems:

Wrong hyperparameters
Incorrect model
Too few 1's labels, but I think that's not a big problem because I only need the model to suggests the right time steps. So I would only use the peaks of the output.
Bad or too less training data
Bad features

I appreciate any help and tips.

You are facing a very common problem: handling imbalanced data. For neural networks, typical procedures are:

Having the proper metrics: global accuracy should not be used.
Oversampling the minority class: randomly generate replicas of the minority class until the imbalance disappears.You can also perform data augmentation on the minority class. Synthetic data can be generated from the feature space, using SMOTE algorithm, but I don't know how it applies to neural networks.
Under-sampling the majority class: randomly remove instances of the majority class. It can deteriorate the performance on the majority class
Include class-weights in the loss function: the idea is to penalize the misclassification of the minority class. The weights are usually inversely proportional to the occurrence frequency of each class.
Using different learning rates per class: you can use a bigger learning rate for the majority class, thus the net stops learning  earlier from the majority class than from the minority class

I'd recommend a combination of 1 (always), 2 and 4. For a higher insight in this topic, which is of very importance, I recommend reading:

https://dl.acm.org/citation.cfm?id=1592322
https://arxiv.org/pdf/1710.05381.pdf


ValueError: Expected 2D array, got 1D array instead: I was following this example online for simple text classification

And when I create the classifier object like this

from sklearn.datasets import fetch_20newsgroups
twenty_train = fetch_20newsgroups(subset='train', shuffle=True)

from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(twenty_train.data)
X_train_counts.shape

from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_train_tfidf.shape

from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_train_tfidf, twenty_train.target)

and run predict on the test data it gives an error

import numpy as np
twenty_test = fetch_20newsgroups(subset='test', shuffle=True)
predicted = clf.predict(twenty_test.data)
np.mean(predicted == twenty_test.target)

  ValueError: Expected 2D array, got 1D array instead:

But when I do the same thing using Pipeline it works as in the example

from sklearn.pipeline import Pipeline
text_clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', MultinomialNB())])

text_clf = text_clf.fit(twenty_train.data, twenty_train.target)
twenty_test = fetch_20newsgroups(subset='test', shuffle=True)
predicted = text_clf.predict(twenty_test.data)
np.mean(predicted == twenty_test.target)

  Out[37]: 0.7738980350504514

Why is that?

It seems to be because the predict method on your Pipeline object requires the input to match the input of the first object in your pipeline, which is the CountVectorizer. It any case, it only requires an iterable object, which your 1d array indeed is.

The classifier you train without the pipeline ends up being a MultinomialNB object, whose predict method actually requires a 2d array of shape num_samples * num_features.

Maybe you need to pass your test data through each of the individual steps manually before feeding anything into the final classifier?


Is the early stopping of xgboost using correct I'm using xgboost package in R with early stopping at 75 rounds. To monitor the progress the algorithm I print the F1 score from the training and test set after each round. After the algorithm has done 75 rounds, xgboost returns the model with the highest score on the test set, not the training set. My guess is that this has to do with the monitoring functionality and the watchlist parameter of xgboost

My intention of giving the algorithm access to the test set during training (using the watchlist parameter) was to monitor the training progress, and not to  select the best performing classifier with respect to the test set. That would be cheating right? My question is two-fold:

Is this really cheating?

and if it is cheating,

Why is it programmed into the xgboost package?

That's not cheating. If your test set is a representative sample of the future data you'll want to make predictions on, you'll want to have the lowest possible error there!

If you maximized performance on the training set, instead, you might overfit. It's fairly easy for a boosted algorithm to inadvertently memorize its training data rather than learn a meaningful mapping of inputs to output.

Per the comment below, the "test set" you describe is actually functioning like a validation set here.


How to distinguish informative and non-informative feature - Feature importance? I have a dataset with 5K records focused on binary classification problem. I have more than 60 features in my dataset. When I used Xgboost, I got the below Feature Importance plot. However I am not sure how to find out whether all of these are informative?

Questions

1) Yes, I can select top 15/20/25 etc. But is that how it is done? Is there any minimum F-score that we ought to look for?

2) Or is it like I select top 10 features, check for accuracy and again add 2-3 features in every round and verify accuracy manually. Is this how it is done?

3) How would you people go about it? I tried with full dataset, the accuracy is only about 86%. when I tried with 15-20 features, it's only about 84. So manual feature selection is the only way to improve further?

Can you help me please?

My approach is quite different. After the creation of feature importance, I usually formulate the question "Is a feature of low importance really non-informative or its informative nature is shadowed by other features?"

How do I answer this question?

I use a simple method - decreasing the number of features in each iteration* (a greater chance that the most important features are not between them), adding a dummy feature containing random numbers, and creating a new feature importance. The random dummy feature is non-informative by definition, so assumption, that features of importance less than this dummy feature are non-informative is IMHO not so strange. So, the next step is to remove these features.

Then I usually make a selection starting from the other side. Sometimes more valuable, more important features, being surely informative, cause overfitting and removing them increases accuracy.

*) The default partition of features in each iteration (colsample_bytree) is equal to 1, but it may be set in the stage of feature selection to a much lower value of the range 0.2-0.4.


Different approaches to label data I have a dataset of patient records but they don't have labels.

I would like to label them and would like to know what are the different approaches available that I can consider to label them.

For ex:  I have given two approaches below but is there any other approaches to it?

1) Manual labeling

2) Rule based

3) ..

4) ..

As you all are experienced in data science and AI

Can you please share some approaches that you know and resources that I can refer to learn about them?

1) Manual labeling--- this is not as bad as it sounds. Especially when you apply transfer learning, and for most of the datasets you have a lot of pre-trained models. There are products for that, but also inline python libraries

2) Rule based--- not advisible, since your model will just focus on these if-else rules itself. It would be the the best if these are the rules that are not that visible in the dataset, and the model cant catch it so easily. Which just implies it will than start to learn information in other features that also could be valuable.

3) Pseudo Labeling---- will add confident predicted test data to your training data. Therefore you will reinforce the clear labeled data and use them to help and label/predict other ones. Note, its a potential overfitting method.

4) Unsupervised Approach---- try to find good representation of data. The one thats discriminative enough, than apply clustering algos. If the resulting clusters are clear and different, you can look a couple of samples from every cluster and conclude what are the labels for every sample in your dataset.


Cross Validation in Keras Suppose I would like to train and test the MNIST dataset in Keras.

The required data can be loaded as follows:

from keras.datasets import mnist

digits_data = mnist.load_data()

Is there any way in keras to split this data into three sets namely: training_data, test_data, and cross_validation_data?

From the Keras documentation, you can load the data into Train and Test sets like this:
(X_train, y_train), (X_test, y_test) = mnist.load_data()

As for cross validation, you could follow this example from here.
from sklearn.model_selection import StratifiedKFold

def load_data():
    # load your data using this function

def create model():
    # create your model using this function

def train_and_evaluate__model(model, data_train, labels_train, data_test, labels_test):
    model.fit...
    # fit and evaluate here.

if __name__ == "__main__":
    n_folds = 10
    data, labels, header_info = load_data()
    skf = StratifiedKFold(labels, n_folds=n_folds, shuffle=True)

    for i, (train, test) in enumerate(skf):
        print "Running Fold", i+1, "/", n_folds
        model = None # Clearing the NN.
        model = create_model()
        train_and_evaluate_model(model, data[train], labels[train], data[test], labels[test])


What does 'contextual' mean in 'contextual bandits'? I recently read a lot about the n-armed bandit problem and its solution with various algorithms, for example for webscale content optimization. Some discussions were referring to 'contextual bandits', I couldn't find a clear definition what the word 'contextual' should mean here. Does anyone know what is meant by that, in contrast to 'usual' bandits?

A contextual bandit algorithm not only adapts to the user-click feedback as the algorithm progresses, it also utilizes pre-existing information about the user's (and similar users) browsing patterns to select which content to display.

So, rather than starting with no prediction (cold start) with what the user will click (traditional bandit and also traditional A/B testing), it takes other data into account (warm start) to help predict which content to display during the bandit test.

See: http://www.research.rutgers.edu/~lihong/pub/Li10Contextual.pdf


Model selection by Interpreting p-value of anova function I am trying to interpret the p-values for model selection. Here is a sample code taken from a book (An Intro. to stat. Learning, page 290, by Gareth James et al.)

￼
Null-hypothsis: Model M1 is sufficient to explain the data against the alternative hypothesis that a more complex model M2is required.

With this statement, I think p-value should be high so that null-hypotheis can be rejected and a model M2 is selected. Keeping this in mind, I think we should select Model 5 as it is having highest p-value, but authors mention either of Model 3 or Model 4 is a good fit. I don't understand why Model 5 is not a good fit.

To understand why, we should try to understand what we're doing here.

Let's start with the first p-value: 2e-16, what does that mean? This is the p-value under the null hypothesis that the linear model and the second polynomial model are statistically identical. We say that they are the same if the extra coefficient in the polynomial is statistically zero. This is exactly what the p-value is telling you. It reads like this: There is 2e-16 probability that the null hypothesis of the linear model and the polynomial are identical. This is a very small probability, so you can conclude that they are not identical. This means, the second-order polynomial is a better model than the linear model with a significance level of 5%.

(PS: a more correct statistical interpretation of the p-value relates to false-positive rejection, but let's not go into that deep)

Now, using the same logic, you can conclude the third-order polynomial fits better than the second-order polynomial.

Next, the p-value comparing the third and fourth order polynomial is about 0.05. You can reject it or you don't want to reject it, this is up to you. But If was you, I'd simply fail to reject it because it's larger than 0.05.

Finally, the final p-value is about 0.37 which is too high. This means although the fifth order polynomial fits better than the fourth order, the loss in your RSS is insufficient to justify the loss of the degree of freedom. Therefore, we say the fifth order polynomial is statistically no better than the fourth-order.

Conclusion: It's "bad" to have a large p-value because you really want to reject a null hypothesis. Statistically, we do it to control the false positive rate.

PS: In your example, R uses the F-test to compare the two models.


Improving a basic neural network with only 3 input columns I am just getting started with neural networks, and I was hoping someone could shed some light on an issue I keep facing with a model I am working on. I have a dataset that looks like this:
df:

x1     x2    x3      y1
70     3     0.25    0.4
80     4     0.5     1.0
70     4     1.25    0.5
...    ...    ...     ...

...with a shape of (400, 4)
My objective is to train a deep learning model to predict the y1 value using the x1, x2, and x3 inputs.
Here is my code so far adapted from this source:
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

# define base model
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(3, input_dim=3, kernel_initializer='normal', activation='sigmoid'))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_absolute_percentage_error', optimizer='sgd')
    return model

kfold = KFold(n_splits=10)

estimator = KerasRegressor(build_fn=baseline_model, epochs=100, batch_size=5, verbose=0)
results = cross_val_score(estimator, X_train, y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))

However, my results are a little unusual:
Results: -122.97 (87.82) MSE
y_pred = model.predict(X_test)
y_pred

array([[0.9994005],
[0.9994005],
[0.9994005],
[0.9994005], ...)
My main questions here are why are the final values always the same, in this case "0.9994005", and what can I do to improve this model? What the main things you look for in deep learning models that direct you in the right path?

There is no point in looking at the prediction at this point. Your model has not learnt the data yet.
Suggestions -

When copy from a source. Try copying everything first. Run it and then start tweaking for learning Or to map with your own need. You have changed a few things as compared to what Jason has in his post

Use the wisdom of researchers. So, you can keep a few things as default -
     1. Optimizer - Adam
     2. Activation - Relu for Hidden layer
     3. Loss - MSE(Regression), Cross-entropy(Classification). Beware of Cros-entropy variance.
     4. Activation for Output layer - Linear(Regression), Sigmoid/Softmax(Classification)
     3. Have Batch normalization layer when you have a deep network.

Try to know why behind every point that I have mentioned. It's will be an easy search on the Internet.

A good place to start can be - Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition, by Aurélien Géron
Further reading -
     Practical Recommendations for Gradient-Based Training of Deep Architectures, by Yoshua Bengio
    Andrej Karpathy blog
    Raúl Gómez blog


Keras : problem in fitting model I am trying to fit a keras classifier on a data matrix X_train

dummy_y = np_utils.to_categorical(y_train)
dummy_y_val = np_utils.to_categorical(y_val)

def baseline_model(optimizer='rmsprop', init='normal', dropout_rate =0.0):
    model = Sequential()
    model.add(Dense(100, input_dim = X_train.shape[1], activation='relu', init=init))
    model.add(Dropout(dropout_rate))
    model.add(Dense(50, activation = 'relu', init = init))
    model.add(Dropout(dropout_rate))
    model.add(Dense(15, activation = 'sigmoid', init = init))
    model.compile(loss = 'binary_crossentropy', optimizer = optimizer, metrics =['accuracy'])
    return model

   estimator = KerasClassifier(build_fn=baseline_model, nb_epoch=200, batch_size=5, verbose=0)
   model = baseline_model()

model.fit(X_train, dummy_y, batch_size=32,  verbose=1, callbacks=None, \
     validation_data=(X_val, dummy_y_val), shuffle=True, class_weight=None, \
    sample_weight=None, initial_epoch=0)

That's what I get as an error :

  Train on 582 samples, validate on 290 samples Epoch 1/10

  ValueErrorTraceback (most recent call last)
   in ()
        1 model = baseline_model()
  ----> 2 model.fit(X_train, dummy_y, batch_size=32,  verbose=1,

  ....

  ValueError: Cannot feed value of shape (582, 32) for Tensor
  u'dense_input_28:0', which has shape '(?, 18760)'

NOTE : X_train.shape = (580,18760) and y_train has 15 classes

The problem was : X_train and y_train were not numpy array. I transformed them to numpy arrays and it solved the problem.


Creating a neural network for predicting next vote in a series of votes We are working on a project for creating music based on crowd sourcing. People vote for every note until the vote is closed, and then move on to the next vote until the canvas for the music is filled. A similar project is crowdsound, if you want to get an idea of what it looks like.

Now the fun part is, based on all the votes we get from various people, we would like to be able to build a Neural Network that can build an entire song on its own. The idea is for it to take in account every preceding vote and predict the one that will follow. That way, when trained, we could give it one note and let it predict the rest of the votes on its own and thus create a song on its own.

So I've read a few things here and there about neural networks, but there are two things I don't understand:

How to build one that takes into account a dynamic number of inputs (all preceding votes).
How exactly should I decide the number of hidden layers (I still only vaguely understand what those hidden layers represent) I need for it to work well.

We are using Java for the project and we were planning on using Neuroph for the neural network.

Recurrent neural network may fit your needs.

Read about LSTM & GRU , which has been implemented into various NN libraires.

Here is the link to the keras documentayion of RNN :

https://keras.io/layers/recurrent/

Some interesting music project that takes advantage of RNN :

https://github.com/tensorflow/magenta

https://github.com/jisungk/deepjazz


Lower performace with same script on google cloud vs laptop So I want to test a lot of hyperparameters for an xgboost classification model and also do cross validation for all of these. To do this I use a gridsearch. To speed up the process I want to use as many cpu cores as possible, so I set the n_jobs parameter to the number of available cpu cores in the system. This all works perfectly fine, see code below.
    xgb_model = XGBClassifier(use_label_encoder=False, eval_metric='auc')
    njobs = os.cpu_count()
    gsearch = GridSearchCV(estimator=xgb_model,
                           param_grid=param_tuning,
                           scoring='roc_auc',
                           cv=3,
                           n_jobs=njobs,
                           verbose=10)

Now when I run this on my laptop, it takes around 1 second per core to complete a training cycle at a speed of around 2 GHZ. When I run this exact same script on a Google cloud N2 VM with 8 cores it takes around 1 minute per core to do the same, even though these cores are faster than 2GHZ. That's 60 times slower? Does one of you have any idea why it is so much slower on google cloud?
I already tried upgrading the harddrive to an SSD but that seems to have no impact.

After some experimentation I found out that reducing the amount of jobs actually increases the speed. I'm now running 8 machines with a single core where each machine is running faster than the 8 core machine.
I either did something wrong in my code or this library is not built for multi core processing..


What is missing from the following Curriculum Learning implementation in a Deep Neural Net? First of all we have a classification task.
So we use the typical softmax cross entropy to classify.

Current implementation of curriculum learning is as follows.

First we train our best version of the neural net
At the last epoch we get all of the cross entropies for all the instances. So smaller cross entropies are better classified and larger ones not so well.
Then we sort the instances based on the cross entropies.
Then we start training the instances from easier to harder as the curriculum learning theory suggests

Note that we have already experimented with various steps and repetitions. So in one example we took the first 200 batches and trained them two times before going to the next batch and so on until an epoch is completed.
In another example we took the first 10 batches and trained them only once  and then the next 10 and the next 10 and so on until the end of the epoch.

All of the experiments so far have concluded that the neural network is has a relatively ok accuracy at the beginning and this gets worsen as the more difficult instances come along.
The final accuracy is much worse than expected and in addition the maximum accuracy is still quite bad.

Why is this curriculum learning not working? Is anything missing?

You need a lot more information to figure out what's going on here. Some possibilities include:

Your "hard" examples are actually unlearnable. You can check this by seeing if you can at minimum overfit the hard examples, and if you as a human can label the hard examples correctly.
Your network is not large enough to learn the hard examples. That is, you are sure that the hard examples can be learned, it's just that the network isn't sufficiently complex to do it. Again you can check this by seeing if you as a human are able to figure out the correct labels for the hard instances.
Your curriculum learning may actually be hurting be the training. At the end of training, your network is only seeing examples that it has the most trouble with. These examples will cause the largest changes in the network, and you might have short-term gradient explosion. You can check if this is happening by seeing if you are getting significantly worse performance on "easy" examples at the end of training on the hard examples. One solution here might be something like slowly expanding the training set after you sort by difficulty. So if you have 10k easy examples and 10k hard examples, train on the 10k easy + 1k hard until you're satisified with the network performance, then grow the training set by adding the next 1k hardest, etc.


K-Fold Cross validation confusion? I am using K-Fold cross validation to test my trained model, but was amazed that for every K-fold the accuracy is different. For instance, if I use 5 K-fold, every fold has a different accuracy. So, which fold should I use? Is averaging all 5 of the folds the best choice? Secondly, why is the data-set split ratio (70/30) different in 5 fold Cross validation and 10 fold cross validation? Shouldn't it be the same?

The accuracy is different because there are k-classifiers made for each number of k-folds, and a new accuracy is found.

You don't select a fold yourself.  K-Fold cross-validation is used to test the general accuracy of your model based on how you setup the parameters and hyper-parameters of your model fitting function.

What you do select is the number of folds, so in your example of 5 folds, it will do the following:

split up your training set into 5 different subsets (folds)
create a classifier for each of the 5 folds by using k-1 folds for
fitting the model, and test the classifier accuracy using the fold
left out

After it's done you can see how your classifier fared over the average of these folds.

If you're trying to find the optimal parameters to configure your model for the best accuracy you should be using grid search.  Depending on your language the implementation will be different: python using sklearn.model_selection.GridSearchCV

, and R uses the 'carat' library and the train() function.  Once you've run grid search you can either use the resulting model if you're programming in R or in python, you can add the new hyperparameters to your model fitting function and re-fit your model.

Your train/test ratio will depend on the number of folds. If you have 100 rows in your training set and you have 5 folds, then you'll have an 80/20 split train/test.  If you have 10 folds it will be a 90/10 split.  The cross-validation function for k-folds uses k-1 folds for fitting the model, and the fold left out is used for testing.


Markov Chains: How much steps to conclude a Transition Matrix I have just learned Markov Chains which I am using to model a real world problem. The model comprises 3 states [a b c]. For now I am collection data and calculating transitional probabilities:-

T[a][b] = #transitions from a to b / #total transitions to a

However I am stuck at determining the correct Transition Matrix. As I am getting more data, the matrix is changing drastically. So when do I finalize Transition Matrix? Does that mean that my data is too random and cannot be modelled or I am doing some mistake here?

I expect you have, or can make, a matrix of transition counts. Consider the data in each row to be draws from a multinomial distribution. Then you should be able to use sample size calculations for the multinomial to get off the ground.

It is also possible that your data is not well described by a simple Markov chain. There are some available techniques for this, e.g. multistate modelling, but which may or may not fit your particular problem.


What are the tools to plot cluster results? I am clustering based on my cosine similarity matrix. Now I want to plot/visualize my very large clusters. I am interested in using a tool that is better than sklearn.

Please recommend me.

ELKI has some very nice cluster visualizations. You could also use tSNE or MDS.

But as you used cosine, I assume you have text data, and that is not easy to visualize. You probably first need to figure out how to visualize your input data, then you can think about adding cluster information to this.


Are view() in Pytorch and reshape() in Numpy similar? Are view() in torch and reshape() in Numpy similar?

view() is applied on torch tensors to change their shape and reshape() is a numpy function to change shape of ndarrays.

Yes, for most intents and purposes, they can do the same job. From this link, an example:

>>> import torch
>>> t = torch.ones((2, 3, 4))
>>> t.size()
torch.Size([2, 3, 4])
>>> t.view(-1, 12).size()
torch.Size([2, 12])

If you are concerned with memory allocation, here is another answer on StackOverflow with a little more information. PyTorch's view function actually does what the name suggests - returns a view to the data. The data is not altered in memory as far as I can see.

In numpy, the reshape function does not guarantee that a copy of the data is made or not. It will depend on the original shape of the array and the target shape. Have a look here for further information.


What is this formula, related to simple linear regression, called? This is my first post here. I hope I can make myself clear. Right now I'm learning linear regression as part of an introduction class to machine learning.

After going over the steps in the simple regression formulae, I realized that I had been doing something similar in the past to construct lines. In Python:

data = [{'x': 1, 'y': 2}, {'x': 5, 'y': 7}, {'x': 6, 'y': 8}]
coeff = sum([d['x'] / d['y'] for d in data]) / len(data)

Here, we're calculating the mean ratio between the variables, which we can use as a coefficient for constructing a line. Does this method have a name, and how does it relate to simple linear regression?

I do not know other terms than the  average of inverse slope or the inverse of the harmonic mean of slopes. This is also the negative of the average slope of the perpendiculars.

It gives you the inverse of the average slope of lines passing through $(0,0)$ and $(x_i,y_i)$. If the $(x,y)$ are almost aligned with $(0,0)$, this is an estimate of the inverse of the slope of a line passing through the points.

Linear regression is quite different, as it involves cross-products of sums of $x$ or $y$.


Pandas time series optimization problem: add year I have a pandas DataFrame containing a time series column. The years are shifted in the past, so that I have to add a constant number of years to every element of that column.

The best way I found is to iterate through all the records and use

x.replace(year=x.year + years)  # x = current element, years = years to add

It is cythonized as below, but still very slow (proofing)

cdef list _addYearsToTimestamps(list elts, int years):
    cdef cpdatetime x
    cdef int i
    for (i, x) in enumerate(elts):
        try:
            elts[i] = x.replace(year=x.year + years)
        except Exception as e:
            logError(None, "Cannot replace year of %s - leaving value as this: %s" % (str(x), repr(e)))
    return elts

def fixYear(data):
    data.loc[:, 'timestamp'] = _addYearsToTimestamps(list(data.loc[:, 'timestamp']), REAL_YEAR-(list(data[-1:]['timestamp'])[0].year))
    return data

I'm pretty sure that there is a way to change the year without iterating, by using Pandas's Timestamp features. Unfortunately, I don't find how. Could someone elaborate?

Make a pandas Timedelta object then add with the += operator:

x = pandas.Timedelta(days=365)
mydataframe.timestampcolumn += x

So the key is to store your time series as timestamps.  To do that, use the pandas to_datetime function:

mydataframe['timestampcolumn'] = pandas.to_datetime(x['epoch'], unit='s')

assuming you have your timestamps as epoch seconds in the dataframe x.  That's not a requirement of course; see the to_datetime documentation for converting other formats.


What circumstances causes two different classifiers to classify data exactly like one another Okay, here is the background:
I am doing text mining, and my basic flow is like this:
extract feature (n-gram), reduce feature count, score (tf-idf) and classify. for my own sake i am doing comparison between SVM and neural network classifiers. here is the weird part (or am i wrong and this is reasonable?), if i use 2gram the classifiers' result (accuracy/precision) is different and the SVM is the better one; but when i use 3-gram the results are exactly the same. what causes this? is there any explanation? is it the case of very separable classes?

Your results are reasonable. Your data brings several ideas to mind:

1) It is quite reasonable that as you change the available features, this will change the relative performance of machine learning methods. This happens quite a lot. Which machine learning method performs best often depends on the features, so as you change the features the best method changes.

2) It is reasonable that in some cases, disparate models will reach the exact same results. This is most likely in the case where the number of data points is low enough or the data is separable enough that both models reach the exact same conclusions for all test points.


Using ML to create unique descriptors? I have a problem that doesn't seem to fall into a common machine learning category, and I was wondering if this still could potentially be solved with ML.

Problem: I have two signals recorded from two sensors, and would like to determine whether they are correlated (i.e. record the same physical event) or not.

The catch: I don't have access to the full signal time series of both sensors, but only one at a time - I can only exchange a small descriptor on the order of 32 bits to see if the signals match or not.

Our current approach is to calculate a bunch of numerical signal features such as mean, derivative, zero-crossings, FFT etc. and see which ones provide the best correlation - but that seems to be a lot of guesswork and doesn't work very well in any case.

So now I had the following idea:

Start with a neural network which takes a fixed window out of the signal (+ possibly the FFT of that window) as an input, and produces a 32-bit output
Pick two random correlated samples out of the pool of examples, and run the network twice, once with each sample (and its FFT)
Take the difference between the two output values as error measure and perform backpropagation as usual
Repeat from 2. until the difference for all examples is below a threshold

Here are my questions:

Does this approach seem feasible at all?
As someone relatively new to machine learning, how would I implement this?
I've had a look at Keras - would this be a suitable starting point?

Thanks in advance, and best regards, Florian

Addendum: I've found this somewhat related post (Is it possible using tensorflow to create a neural network that maps a certain input to a certain output?), but I don't think that this is the same problem, as I don't actually care what the output looks like, just that it is as unique as possible for each matching pair of samples.

If I understood correctly your question, you want a function that takes a signal (a fixed window) and outputs a 32-bit representation in a way that the correlation between it and any other signal is preserved. Mathematically speaking given signals $s_1$ and $s_2$ in $S$ and a correlation function $corr(s_1, s_2)$ you want some function $f : S \rightarrow B$ (where $B$ is the space of 32-bit binary numbers) that you could use another correlation function for instance $corr_f(b_1, b_2) \approx corr(s_1, s_2)$.

If that is what you want you should at hashing techniques in particular learning to hash. Essentially in hashing want you do is you represent your input by binary numbers in a way that the hamming distance between the binary numbers preserves some target similarity (distance) function such as the correlation. In particular for cross-correlation (inner product) you have some methods based on random projections.

So once you have learned (designed) your hashing function $f$, what I would do is:

b1 = f(s1)
send b1
receive b1
b2 = f(s2)
return h(b2, b1) # this value is going to tell you if the signals are correlated


Logic behind SMOTE-NC? In the SMOTE paper here, the authors present the logic for creating synthetic examples when some of the features are nominal and some are continuous (section 6.1, SMOTE-NC).

This example is provided:

  $F_1$ = 1 2 3 A B C [Let this be the sample for which we are
  computing nearest neighbors]    $F_2$ = 4 6 5 A D E    $F_3$ = 3 5 6 A
  B K    So, Euclidean Distance between $F_2$ and $F_1$ would be:

  $Eucl$ = $\sqrt{(4-1)^2 + (6-2)^2 + (5-3)^2 + Med^2 + Med^2}$

  Med is the median of the standard deviations of continuous features
  of  the minority class.    The median term is included twice for
  feature numbers $5: B→D$ and $6:  C→E$, which differ for the two
  feature vectors: $F_1$ and $F_2$.

The paper lacks explanation about why the nominal features should be affected by the continuous ones.

Can anyone provide such explanation? Did I miss it in the paper?

I also thought about the very same question recently and I think I might have a possible explanation.

Since we need to calculate distances between k-nearest neighbors, we have to provide some synthetic value which denotes the difference between nominal features. As a matter of fact, it could be any value. For example, imagine you have your nominal features one-hot encoded. In this case the difference between two different nominal features would be 1.

However, you would also like to keep the calculated distances more or less on the same scale as if you would have by using only continuous features. I believe that using median of standard deviations of continuous features helps to achieve exactly that and is the reason why it was chosen as a measure of difference between nominal features.


finding the percentage > x with r Trying to locate the names of countries in my data.frame where the most spoken language is not an official language. My columns data.frame is World. The column country.y is the name column of all the countries. Column isOfficial has data in it that is 't' or 'false' representing that the official language is 'true' or 'false'. Another column is percentage representing what percentage of a language is spoken.  So far this is what I came up with.

subset(World$name.y, World$isOfficial != 't' & World$percentage > 50)

I would try the following R-Code for subsetting:

World[World$isOfficial != 't' & World$percentage > 50, "country.y"]

And in case isOfficial is type logical, I would try this R-code:

World[!(World$isOfficial) & World$percentage > 50, "country.y"]


What is the difference between cross_validate and cross_val_score? I understand cross_validate and how it works, but now I am confused about what  cross_val_score actually does. Can anyone give me some example?

cross_val_score is a helper function on the estimator and the dataset.
Would explain it with an example:
>>> from sklearn.model_selection import cross_val_score
>>> clf = svm.SVC(kernel='linear', C=1)
>>> scores = cross_val_score(clf, iris.data, iris.target, cv=5)
>>> scores
array([ 0.96...,  1.  ...,  0.96...,  0.96...,  1.        ])

This example demonstrates how to estimate the accuracy of a linear kernel support vector machine on the iris dataset by splitting the data, fitting a model and computing the score 5 consecutive times (with different splits each time)
The cross_validate function differs from cross_val_score in two ways -

It allows specifying multiple metrics for evaluation.
It returns a dict containing training scores, fit-times and score-times in
addition to the test score.

Note: When the cv argument is an integer, cross_val_score uses the KFold or StratifiedKFold strategies by default, the latter being used if the estimator derives from ClassifierMixin
You can go through this link for better understanding
Different examples using cross_val_score, you can go through about its different implementations.


How does a Q algorithm consider future rewards? I am trying to understand the underlying logic of Q learning (deep Q learning to be precise). At the moment I am stuck at the notion of future rewards.

To understand the logic, I am reviewing some of the present code samples. This one seemed quite interesting, so I went through it:

https://github.com/keon/deep-q-learning/blob/master/dqn.py

Here is the gist of the code that does the actual training of the underlying deep neural network:

def replay(self, batch_size):
    minibatch = random.sample(self.memory, batch_size)
    for state, action, reward, next_state, done in minibatch:
        target = reward
        if not done:
            target = (reward + self.gamma *
                      np.amax(self.model.predict(next_state)[0]))
        target_f = self.model.predict(state)
        target_f[0][action] = target
        self.model.fit(state, target_f, epochs=1, verbose=0)
    if self.epsilon > self.epsilon_min:
        self.epsilon *= self.epsilon_decay

In the 5th line of the code, (after the if not done line) we are adding the discounted reward of the next step, to the present step, and setting it as the target reward of the executed action to be trained. So, the way I see it, we have the reward of the executed action, and discounted possible reward of the following action, combined.

As far as I understand, in each iteration, Q-learning algorithm predicts the future reward of next step (and next step only) using the machine learning technique in use (be it the CNN, DNN etc.). And we are multiplying the reward of next step (and that specific next step only) with discount rate, to make it less important than the immediate reward (with the ratio we specified). So, my question is, how does the algorithm takes even further steps (say, 5 steps) ahead into account?

As far as I understand, in each iteration, Q-learning algorithm predicts the future reward of next step (and next step only) using the machine learning technique in use (be it the CNN, DNN etc.).

The Q values should eventually converge to the expected sum, future, discounted reward when taking action A in state S and following the optimal policy. Breaking it down:

Expected sum is not exactly the same as "predicted", but close enough for our purposes. And it really does mean sum of the rewards, not a single reward. To differentiate, this is often called the "return" or "utility"
Future -> from the step being evaluated onwards until end of episode, or the limit as time goes to infinity for continuous tasks with discounting.
Discounted -> a discount factor is only necessary for continuous tasks.

  And we are multiplying the reward of next step (and that specific next step only) with discount rate, to make it less important than the immediate reward (with the ratio we specified).

No, there is no multiplication of the reward. Let's take a look at the line:

target = (reward + self.gamma *
                  np.amax(self.model.predict(next_state)[0]))

The reward is not being multiplied by anything.

What is being multiplied by $\gamma$ is the Q value of the next state. That value represents the total sum of all rewards following on from that point - not a single reward value at all.

  So, my question is, how does the algorithm takes even further steps (say, 5 steps) ahead into account?

It is in the Q values. The pseudocode for the code you are looking at is not:

target_for_Q(s,a) = next_step_reward * gamma

It is:

target_for_Q(s,a) = next_reward + gamma * current_value_of_Q(s',a')

Or:

target_for_Q(s,a) = next_reward + gamma * estimate_all_future_return

This is closely related to the Bellman function for policy evaluation.

Intuitively what is happening is that you start with (really poor) estimates for expected return (not expected reward), and update them by inserting observed values of next_reward, s' and a' into the update rule above. The values always represent a learned estimate of total expected return.


Data Science Companies I'm interested in data science market. I was expecting that there would be a lot of companies who are making algorithms and models for companies like in kaggle competitions. But i struggle to find any. Practically every data science company is selling some automated software which makes algorithms/models by its own. So why is that so? Did i understand situation correctly? and if there are companies who make algorithms and models which are the leaders and best companies in that field. thanks p.s. sorry for my bad english

Some of the biggest companies in the world are 'data science' companies; facebook, for instance, performs targeted advertising based on analyzing users preference.  In essence, it's the most highly trained recommendation engine ever, as it knows a great deal about it's consumers.  At it's heart, Google is an advertisement firm, and similarly leverages user information to provide targeted messaging to consumers.  The same can be said for Amazon, Twitter, and most likely pokemon go.

If you're thinking of a more purely 'data science as a service' company, larger consultancy firms such as the Boston Consulting Group have always incorporated analytics in their services, and have certainly profited from the proliferation of data.  They don't say "we do data science", because people care more about getting the right answer, than how the answer is obtained.

As far as Kaggle goes, while it's an awesome resource, and it provides cool tutorials and data sets, if you're a professional data scientist, then you don't work for free ;-)  If a billion dollar company can harvest even a 1% gain in efficiency by offering a $50K prize, they can recoup that money very quickly.  If you're only asset is intellectual property, you need to be cautious about giving it away.


Creating categorical variable, without knowing true categories (through binning time series data)? I have a temperature dataset (data every 15 mins) to build a supervised classification/prediction algorithm, but only know one of the true classes (when data is nearly flatlining around 35deg)

However, given the academic literature on the subject and the data visualization it looks like there are three "true" classes: regulated, sub-ideal regulation, and no regulation. Here is a graph with vertical lines delineating 3 potential categories.
What is the best practice to build this classification? First attempt binned data every 6hrs and built categories based off stdev, but that seemed a bit arbitrary and did not align well with the three hypothesized categories.

So, if I understood you correctly, you want to classify your data based on the frequency and amplitude of the temperature change, right?

I think your attempt with stdev is not so far away, but your interval is a little off, 6 hours might be to much. Think about Shannon, your sample rate must be at least twice the highest frequency in your dataset, if you want to reconstruct your signal completely. If your sample rate is 6 hours you might miss the big leaps at the end of the image.

My recommendation would be to use a sliding window and use Fast Fourier Transformation to get to the frequency spectrum. With those datapoints you can try to cluster them with SVM or kNN approaches. I think SVM is the way to go, since your class should be cleanly seperateble. And since you know how many classes you have there is no need for hypertuning this parameter.

I certainly don't know if this is best practice, but I think this would be a feasible approach.


Feature extraction in audio spectrogram I have audio and its spectrogram of the words in English language. (A spectrogram is a frequency domain representation of a signal) Consider the words: chain, change, chair, chapter. As you can notice, the 'ch' sound is common among the words considered.

Is there an algorithm that will allow me to identify which part of the spectrogram (data) is common between the spectrograms of the words? In other words, is there an algorithm that can identify the part of the spectrogram that represents 'ch' sound in each of the words?

The "ch" sound is called a phoneme. Automatic speech segmentation on the phoneme level (as opposed to word or sentence) will allow you to extract each phoneme from the speech samples. This is sometimes referred to as phoneme segmentation. Several papers for this are available in the literature.

Once you have the phonemes extracted, you can compute distances/similarities between them (in some feature space). An example feature representation could be MFCC.


How to set hyperparameters in SVM classification I am studying image classification using SVMs and it is generally defined as so...

N = number of training examples

W = is the weights

f(x, W) = dot product

λ is explained to be set through cross-validation however no mention is made as to how Δ is set.

I understand that the SVM loss function wants the score of the correct class to be larger than the incorrect class scores by at least by Δ, but they don't explain how Δ is derived.

In most of the examples it is define to be Δ = 1.0, with no mention as to how 1.0 was calculated. Is this value determined through trial-and-error (cross-validation)? How does one determine what should be the value?

You could use cross validation with grid search as shown here


Does bias have multiple meanings in Data Science? What are the meanings of Bias?
And is Under fitting, which is used in machine learning contexts, the same as "Bias"?

I have faced biased data in sampling in statistics but it seems this is a different thing to bias in learning concepts.

I have heard that some data sets are biased, also have heard the model (for example neural network) has low bias or e.g. 'high bias' problem. Are these uses of bias different?

Bias can mean different things in statistics:

If your model is biased, it's likely your model is under-fitting.
Some data set is biased in sample collection. For instance, if you assume your sample responses are independent, but somehow it's not, this is a bias in your data set. If you want to sample everybody in the country, but you skip some cities for no reason, this causes bias in your data set.
Your estimators could be biased - the expectation of your estimator is not equal to the true value in the population.
"Bias" is also used to describe an learnable offset parameter when using transfer functions, e.g. in neural networks when calculating activation of an artificial neuron.


How to create an array from the list of arrays in python I was trying to write a python code that can set some neural network channels or neurons to zero at the inference; and I wrote the code below. The code generates 10 different arrays for different percentage of the channels or neurons that are set to zero. My challenge is how to combine these arrays into a single array or a list so that they can be individually accessed, but all I was getting was a list containing arrays with zero elements. Please help me.

import numpy as np
def three_steps(n):
    return step(n, steps=[1, 0.5, 0.25])

B = np.linspace(0.1, 1.0, 10)
print B
B = np.array(three_steps(100))
print "A = ", A

for i, value in enumerate(B):
    A[i:int(value*100)] = 0
    print A
list = [np.append(A, i) for k in range(i + 1)]
print list

This post from stack overflow should give you what you want. The magic code boils down to the following.

flat_list = [item for sublist in l for item in sublist]

You can also loop through the list of lists and merge them.

lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

merged_list = []

for l in lists:
    merged_list += l

print(merged_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]