# A tibble: 1 × 1
prediction
<chr>
1 strawberry
Week 11
Machine Learning in
Python
SOCI 269
“Midterm” Assignment Deadline
Your “midterm” assignments are due by 8:00 PM tonight.
Term Paper Deadline
Your final paper is due on 8:00 PM on Friday, May 9th.
Click here to see when you’ll be presenting.
Grimmer and colleagues (2021:396, EMPHASIS ADDED)
A class of flexible algorithmic and statistical techniques for prediction and dimension reduction.
Molina and Garip (2019:28, EMPHASIS ADDED)
A way to learn from data and estimate complex functions that discover representations of some input (X), or link the input to an output (Y) in order to make predictions on new data.
Once deployed, SML algorithms learn the complex patterns linking X—a set of features (or independent variables)—to a target variable (or outcome), Y.
The goal of SML is to optimize predictions—i.e., to find functions or algorithms that offer substantial predictive power when confronted with new or unseen data.
Examples of SML algorithms include logistic regressions, random forests, ridge regressions, support vector machines and neural networks.
A quick note on terminology
If a target variable is quantitative, we are dealing with a regression problem.
If a target variable is qualitative, we are dealing with a classification problem.
# A tibble: 80,000 × 6
fruit shape weight_g colour texture origin
<chr> <chr> <dbl> <chr> <chr> <chr>
1 apple spherical 150 red crispy china
2 banana curved 120 yellow creamy ecuador
3 orange spherical 148 orange juicy egypt
4 watermelon spherical 4500 green juicy spain
5 strawberry conical 13 red juicy mexico
6 grape spherical 5 green juicy chile
7 mango ellipsoidal 240 yellow juicy india
8 pineapple conical 2100 yellow juicy costa rica
9 apple spherical 140 green crispy usa
10 banana curved 110 yellow creamy ecuador
# ℹ 79,990 more rows
# A tibble: 1 × 6
fruit shape weight_g colour texture origin
<chr> <chr> <int> <chr> <chr> <chr>
1 <NA> conical 6 red juicy china
# A tibble: 8 × 2
fruit probability
<chr> <dbl>
1 apple 0.03
2 banana 0
3 orange 0.05
4 watermelon 0
5 strawberry 0.71
6 grape 0.21
7 mango 0
8 pineapple 0
# A tibble: 1 × 1
prediction
<chr>
1 strawberry
UML techniques search for a representation of the inputs (or features) that is more useful than X itself (Molina and Garip 2019).
In UML, there is no observed outcome variable Y—or target—to supervise the estimation process. Instead, we only have a vector of inputs to work with.
The goal in UML is to develop a lower-dimensional representation of complex data by inductively learning from the interrelationships among inputs.
V-Party Name | Label | Underlying Question |
---|---|---|
v2paanteli |
Anti-Elitism | How important is anti-elite rhetoric for this party? |
v2papeople |
People-Centrism | Do leaders of this party glorify the ordinary people and identify themselves as part of them? |
v2paculsup |
Cultural Chauvinism | To what extent does the party leadership promote the cultural superiority of a specific social group or the nation as a whole? |
v2paminor |
Minority Rights | According to the leadership of this party, how often should the will of the majority be implemented even if doing so would violate the rights of minorities? |
v2paplur |
Political Pluralism | Prior to this election, to what extent was the leadership of this political party clearly committed to free and fair elections with multiple parties, freedom of speech, media, assembly and association? |
v2paopresp |
Demonization of Opponents | Prior to this election, have leaders of this party used severe personal attacks or tactics of demonization against their opponents? |
v2paviol |
Rejection of Political Violence | To what extent does the leadership of this party explicitly discourage the use of violence against domestic political opponents? |
Karim and Lukk’s The Radicalization of Mainstream Parties in the 21st Century
As Grimmer and colleagues (2021) note, “machine learning is as much a culture defined by a distinct set of values and tools as it is a set of algorithms.”
This point has, of course, been made elsewhere.
Leo Breiman (2001) famously used the imagery of warring cultures to describe two major traditions—(i) the generative modelling culture and (ii) the predictive modelling culture—that have achieved hegemony within the world of statistical modelling.
The terms generative and predictive (as opposed to data and algorithmic) come from David Donoho’s (2017) 50 Years of Data Science.
Quantity of Interest | Primary Goals | Key Strengths | Key Limitations |
---|---|---|---|
Generative (i.e., Classical Statistics) | |||
Inferring relationships between X and Y | Interpretability; emphasis on uncertainty around estimates; explanatory power | Bounded by statistical assumptions, inattention to variance across samples | |
Predictive (i.e., Machine Learning) | |||
Generating accurate predictions of Y | Predictive power; potential to simplify high dimensional data; relatively unconstrained by statistical assumptions | Inattention to explanatory processes, opaque links between X and Y |
Note: To be sure, the putative strengths and weaknesses of these modelling “cultures” have been hotly debated.
Advances in machine learning can provide empirical leverage to social scientists and sharpen social theory in one fell swoop.
Lundberg, Brand and Jeon (2022), for instance, argue that adopting a machine learning framework can help social scientists:
While ML is often associated with induction, van Loon (2022) argues that SML algorithms can help us deductively resolve predictability hypotheses as well.
Image can be retrieved here.
Bias emerges when we build SML algorithms that fail to sufficiently map the patterns—or pick up the empirical signal–linking X and Y. Think: underfitting.
Variance arises when our algorithms not only pick up the signal linking X and Y, but some of the noise in our data as well. Think: overfitting.
When adopting an SML framework, researchers try to strike the optimal balance between bias and variance.
We can use a training set to fit our algorithm—to find weights (or coefficients), recursively split the feature space to grow decision trees and so on.
Training data should constitute the largest of our three disjoint sets.
We can use a validation set to find the right estimator out of a series of candidate algorithms—or select the best-fitting parameterization of a single algorithm.
Often, using both training and validation sets can be costly: data sparsity can give rise to bias.
Thus, when limited to smaller samples, analysts often combine training and validation—say, by recycling training data for model tuning and selection.
We can use a testing set to generate a measure of our model’s predictive accuracy (e.g., the F1 score
for classification problems)—or to derive our generalization error.
This subsample is used only once (to report the performance metric); put another way, it cannot be used to train, tune or select our algorithm.
Unlike conventional approaches to sample partition, k or v-fold cross-validation allows us to learn from all our data.
k-fold cross-validation proceeds as follows:
Stratified k-fold cross-validation ensures that the distribution of class labels (or for numeric targets, the mean) is relatively constant across folds.
In SML settings, we automatically learn the parameters (e.g., coefficients) of our algorithms during estimation.
Hyperparameters, on the other hand, are chosen by the analyst, guide the entire learning or estimation process, and can powerfully shape our algorithm’s predictive performance.
How can analysts settle on the right hyperparameter value(s) for their algorithm?
GridSearchCV
from scikit-learn
.k-nearest neighbours (KNNs) are simple, non-parametric algorithms that predict values of Y based on the distance between rows (or observations’ inputs).
The estimation of KNNs proceeds as follows:
Note
The rest of today’s session will take place in Colab.