from pyspark.sql import SparkSession
spark = (
SparkSession
.builder
.appName('Sport_Predict')
.getOrCreate()
)Week 10: Scalable Machine Learning with Spark MLlib, SparkNLP, and MLFlow
DSAN 6000: Big Data and Cloud Computing
Fall 2026
Class Sessions
Post-Midterm Roadmap
Just for this week and next week, we need to split our brains in half!
Class Material
This week (W10, Nov 9):
- Scalable ML with Spark
- HW7: Sun, Nov 15
Next week (W11, Nov 16):
- ETLing into Vector DB
- HW8: Sun, Nov 22
Final Project
This week (W10, Nov 9):
- Finalize topic \(\leadsto\) Proposal
- Proposal: Sun, Nov 15
Next week (W11, Nov 16):
- Exploratory Data Analysis
- EDA 1-pager: Sun, Nov 22
- Nov 23: No class! Spark cluster run (Spark 1-pager: Sun, Nov 29)
- W12, Nov 30: Informal technical presentation (Formal slides: Sun, Dec 6)
- [Formal] Final Presentations: Monday, December 7 (Report: Sun, Nov 13)
How Do I Pick A Project?
-
- Could just be “associated notes” (healthcare)
- “Intake notes” (NYC shelters)
- Or short descriptions (ACLED)
Today’s Goal
Combination of your skills > sum of its parts!

Numeric Features: VectorAssembler
- MLlib models expect features to be combined into a single column
- For numeric data,
VectorAssemblerdoes what we want!
Code
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.regression import DecisionTreeRegressor
sport_sdf = (
spark
.read
.option('header', 'true')
.csv("assets/country_sports.csv")
)
sport_sdf.show()+--------------+---------+----------+--------+
| Country|Continent|Population| Sport|
+--------------+---------+----------+--------+
| Argentina| SA| 44| Soccer|
| Australia| Aus| 34| Cricket|
| Brazil| SA| 211| Soccer|
| Canada| NA| 36| Hockey|
| Cuba| NA| 11|Baseball|
| Germany| Eur| 80| Soccer|
| India| Asia| 1342| Cricket|
| Italy| Eur| 59| Soccer|
| Russia| Asia| 143| Hockey|
| Spain| Eur| 46| Soccer|
|United Kingdom| Eur| 65| Cricket|
| United States| NA| 326|Baseball|
+--------------+---------+----------+--------+
Code
from pyspark.ml.feature import VectorAssembler, StringIndexer, OneHotEncoder
from pyspark.ml.regression import DecisionTreeRegressor
sport_sdf = (
spark
.read
.option('header', 'true')
.schema('Country STRING, Continent STRING, Population INT, Sport STRING')
.csv("assets/country_sports.csv")
)
va = VectorAssembler(
inputCols=['Population'],
outputCol='features'
)
va.transform(sport_sdf).select('Country','features','Sport').show()+--------------+--------+--------+
| Country|features| Sport|
+--------------+--------+--------+
| Argentina| [44.0]| Soccer|
| Australia| [34.0]| Cricket|
| Brazil| [211.0]| Soccer|
| Canada| [36.0]| Hockey|
| Cuba| [11.0]|Baseball|
| Germany| [80.0]| Soccer|
| India|[1342.0]| Cricket|
| Italy| [59.0]| Soccer|
| Russia| [143.0]| Hockey|
| Spain| [46.0]| Soccer|
|United Kingdom| [65.0]| Cricket|
| United States| [326.0]|Baseball|
+--------------+--------+--------+
- …What about categorical data?
(Example dataset from Leskovec, Rajaraman, and Ullman (2014); Notebook here
Categorical Features: StringIndexer + OneHotEncoder
StringIndexer
Code
si = StringIndexer(
inputCols=['Continent'],
outputCols=['Continent_Index'],
handleInvalid="skip"
)
fitted_si = si.fit(sport_sdf)
sport_ci_sdf = fitted_si.transform(sport_sdf)
sport_ci_sdf.select('Country','Continent','Continent_Index').show()+--------------+---------+---------------+
| Country|Continent|Continent_Index|
+--------------+---------+---------------+
| Argentina| SA| 3.0|
| Australia| Aus| 4.0|
| Brazil| SA| 3.0|
| Canada| NA| 1.0|
| Cuba| NA| 1.0|
| Germany| Eur| 0.0|
| India| Asia| 2.0|
| Italy| Eur| 0.0|
| Russia| Asia| 2.0|
| Spain| Eur| 0.0|
|United Kingdom| Eur| 0.0|
| United States| NA| 1.0|
+--------------+---------+---------------+
OneHotEncoder
Code
from pyspark.ml.linalg import DenseVector
from pyspark.ml.functions import vector_to_array, array_to_vector
ohe = OneHotEncoder(
inputCols=['Continent_Index'],
outputCols=['Continent_OHE'],
)
fitted_ohe = ohe.fit(sport_ci_sdf)
sport_ohe_sdf = fitted_ohe.transform(sport_ci_sdf)
sport_dense_sdf = (
sport_ohe_sdf
.withColumn(
"Continent_Dense",
array_to_vector(
vector_to_array("Continent_OHE")
)
)
)
(
sport_dense_sdf
.select('Country','Continent_Index','Continent_OHE','Continent_Dense')
).show()+--------------+---------------+-------------+-----------------+
| Country|Continent_Index|Continent_OHE| Continent_Dense|
+--------------+---------------+-------------+-----------------+
| Argentina| 3.0|(4,[3],[1.0])|[0.0,0.0,0.0,1.0]|
| Australia| 4.0| (4,[],[])|[0.0,0.0,0.0,0.0]|
| Brazil| 3.0|(4,[3],[1.0])|[0.0,0.0,0.0,1.0]|
| Canada| 1.0|(4,[1],[1.0])|[0.0,1.0,0.0,0.0]|
| Cuba| 1.0|(4,[1],[1.0])|[0.0,1.0,0.0,0.0]|
| Germany| 0.0|(4,[0],[1.0])|[1.0,0.0,0.0,0.0]|
| India| 2.0|(4,[2],[1.0])|[0.0,0.0,1.0,0.0]|
| Italy| 0.0|(4,[0],[1.0])|[1.0,0.0,0.0,0.0]|
| Russia| 2.0|(4,[2],[1.0])|[0.0,0.0,1.0,0.0]|
| Spain| 0.0|(4,[0],[1.0])|[1.0,0.0,0.0,0.0]|
|United Kingdom| 0.0|(4,[0],[1.0])|[1.0,0.0,0.0,0.0]|
| United States| 1.0|(4,[1],[1.0])|[0.0,1.0,0.0,0.0]|
+--------------+---------------+-------------+-----------------+
- Note 1: PySpark automatically chooses
SparseVectorif % non-zero is low - Note 2:
StringIndexeruses0for most common,1for next most common, etc. - In
scikit-learnwe’d be ready to go! In MLlib…
Map-Reduced vs. Serial Algorithms
- Decision Trees: Optimal split \(s^*\) \(\overset{\tiny\text{def}}{\small{=}}\) a constant value \(c^*\) for feature \(j^*\) such that «go left if \(x_{ij^*} \leq s^*\)» produces maximum reduction in uncertainty about label (entropy)
- Maximum over what? All data
- …But workers don’t have all data!
- \(\Rightarrow\)
spark.mlcan’t use same algorithm asscikit-learn! - Uses distributed DT learner instead: Planet
Why Do You Need To Know These Details?
from pyspark.ml.regression import DecisionTreeRegressor
dt = DecisionTreeRegressor(labelCol="price")
dt_model = dt.fit(train_sdf)java.lang.IllegalArgumentException: requirement failed: DecisionTree requires maxBins (= 32) to be at least as large as the number of values in each categorical feature, but categorical feature 3 has 36 values. Consider removing this and other categorical features with a large number of values, or add more training examples.scikit-learnhas nomaxBinsparameterspark.mldoes, and it’s important!- Should be power of 2, and the next one above the default value (
64) can massively increase computation time for datasets with many categorical features
(Many spark.ml algorithms also fail if # features \(p > 4096\)…)
Final Note: Different Algorithms for Long vs. Wide
- Note that this switching occurs when Spark hits its hard constraint: \(p \leq 4096\)
- You may want to switch it manually… When would this be helpful? In which Jeff sits in silence until his heart explodes
In-Class Demo 3
Spark NLP
MLlib: pyspark as normal:
from pyspark.sql import SparkSession
spark = (
SparkSession
.builder
.appName('My_ML_App')
.getOrCreate()
)
type(spark)
# >>> pyspark.sql.session.SparkSession
from pyspark.ml.feature import VectorAssemblerSpark NLP: Custom SparkSession with sparknlp.start():
import sparknlp
spark = sparknlp.start()
type(spark)
# >>> pyspark.sql.session.SparkSession
from sparknlp.base import DocumentAssembler
In-Class Demo 4
References
Abuzaid, Firas, Joseph Bradley, Feynman Liang, Andrew Feng, Lee Yang, Matei Zaharia, and Ameet Talwalkar. 2016. “Yggdrasil: An Optimized System for Training Deep Decision Trees at Scale.” In Proceedings of the 30th International Conference on Neural Information Processing Systems, 3817–25. NIPS’16. Red Hook, NY, USA: Curran Associates Inc.
Leskovec, Jure, Anand Rajaraman, and Jeffrey David Ullman. 2014. Mining of Massive Datasets. Cambridge University Press.
Thomas, Alex. 2020. Natural Language Processing with Spark NLP: Learning to Understand Text at Scale. "O’Reilly Media, Inc."

