Can the χ² test be used without a contingency table? I thought that the chi squared (χ²) test is to be used when one has an r × c contingency matrix, i.e., when the dependent variables are nonnegative, span the same r dimension, and typically represent a count, frequency, or boolean value (cf. for example, p. 124 of Practical Statistics for Data Scientists by Bruce and Bruce).
I'm therefore confused by this scikit-learn tutorial which uses the χ² test in ranking the features of the Iris dataset. Aren't those feature continuous? Where then is the rectangular contingency matrix from which the Pearson residuals, and hence the chi-squared value are derived?
PS: Here, I'd consider the c flower categories as the independent variables and the four features are the dependent variables. Would that be accurate?

This question is in part about programming: How does sklearn.feature_selection uses the chi2 criterion to rank features? It's a good question, so not surprisingly it already has a good answer on Stack Overflow: How SelectKBest (chi2) calculates score?
So let's consider another interesting question, this one more appropriate for Cross Validated: In what cases is scikit-learn's chi2 criterion useful for feature ranking and selection, if at all?
The other answers discuss at length that the Pearson's chi-squared test is a test (for goodness of fit, homogeneity or independence) on contingency tables of counts. So if the features are not counts, then the chi2 criterion is not applicable.
A couple of observations that might help to explain the procedure as a heuristic rather than as an appropriate application of statistical theory. (The intended use of SelectKBest?)

*

*The ranking of the features won't change if instead of raw counts we use relative frequencies as long as the counts are normalized by the same total. Since SelectKBest chooses a fixed number of features, the result will be the same. So at least on paper the procedure is fine for counts or frequencies (with the same total) though not positive continuous variables in general.٭

*The chi-squared statistic is more sensitive to small differences in large counts. Intuitively, if some features have small total count and other features have large total count (eg. dummy variables vs term counts in document classification), the large-count features are more likely to be selected. The feature totals would act a bit like feature weights.

So an appropriate use of the chi2 criterion is when the features are counts and of the same type; for example: all dummy variables or all term frequencies. (If there are different types of features, it may be better to SelectKBest from each type first and FeatureUnion the selected features, not the other way round.)
The applicability of the procedure may still be limited though. As @EdM points out, automated feature selection, whatever the selection criterion, has many pitfalls.
٭ SelectKBest is designed to be generic, so it uses the scores not the pvalues to rank and select the top k features. We can define our own scoring functions as well.
