Dataconomy
  • News
    • Artificial Intelligence
    • Cybersecurity
    • DeFi & Blockchain
    • Finance
    • Gaming
    • Startups
    • Tech
  • Industry
  • Research
  • Resources
    • Articles
    • Guides
    • Case Studies
    • Whitepapers
    • AI Models Leaderboard
  • AI toolsNEW
  • Newsletter
  • + More
    • Glossary
    • Conversations
    • Events
    • About
      • Who we are
      • Contact
      • Imprint
      • Legal & Privacy
      • Partner With Us
Subscribe
No Result
View All Result
  • AI
  • Tech
  • Cybersecurity
  • Finance
  • DeFi & Blockchain
  • Startups
  • Gaming
Dataconomy
  • News
    • Artificial Intelligence
    • Cybersecurity
    • DeFi & Blockchain
    • Finance
    • Gaming
    • Startups
    • Tech
  • Industry
  • Research
  • Resources
    • Articles
    • Guides
    • Case Studies
    • Whitepapers
    • AI Models Leaderboard
  • AI toolsNEW
  • Newsletter
  • + More
    • Glossary
    • Conversations
    • Events
    • About
      • Who we are
      • Contact
      • Imprint
      • Legal & Privacy
      • Partner With Us
Subscribe
No Result
View All Result
Dataconomy
No Result
View All Result

Machine Learning using Spark and R

byAndrew Tait
May 30, 2017
in Articles, Artificial Intelligence
Home Resources Articles
Share on FacebookShare on TwitterShare on LinkedInShare on WhatsAppShare on e-mail
Google Preferred Source

R is ubiquitous in the machine learning community. Its ecosystem of more than 8,000 packages makes it the Swiss Army knife of modeling applications.

Similarly, Apache Spark has rapidly become the big data platform of choice for data scientists. Its ability to perform calculations relatively quickly (due to features like in-memory caching) makes it ideal for interactive tasks—such as exploratory data analysis.

Spark provides APIs for four programming languages:

Stay Ahead of the Curve!

Don't miss out on the latest insights, trends, and analysis in the world of data, technology, and startups. Subscribe to our newsletter and get exclusive content delivered straight to your inbox.

  • Scala
  • Java
  • Python
  • R

R (SparkR) is the latest addition and support for it certainly lags the other three languages. In Spark 1.x there was no support for accessing the Spark ML (machine learning) libraries from R. The performance of R code on Spark was also considerably worse than could be achieved using, say, Scala.
These were major barriers to the use of SparkR in modern data science work.

However, Spark 2.x has improved the situation considerably. Crucially, Spark’s new primary data structure (DataSet/DataFrame) is inspired by R’s data frame. This means that all four languages can use this abstraction and obtain performance parity.

In addition, with Spark 2.1, we now have access to much of Spark’s machine learning algorithms from SparkR.

In this article, we’ll see how we can build a random forest classifier in (Spark)R. The code can be run from the industry-standard RStudio or any other R IDE.

machine learning with spark and r

Predicting wine quality with Machine Learning

We’re going to look at using machine learning to predict wine quality based on various characteristics of the wine.

The UCI Machine Learning Repository has a dataset we can use to train our prediction model.

This dataset contains the following features:

  • fixed acidity
  • volatile acidity
  • citric acid
  • residual sugar
  • chlorides
  • free sulfur dioxide
  • total sulfur dioxide
  • density
  • pH
  • sulphates
  • alcohol
  • quality (score between 0 and 10)

There are almost 5000 wines in this dataset, but very few high or low-quality wines. For example, only 5 wines are of the highest quality.

The lack of data at the extremes makes it difficult for the algorithm to learn about these wines, so we’ll follow the approach used in an article by Teja Kodali and classify the wines into

  • good (quality is 6–10)
  • bad (quality is 0–5)
  • average (quality is 6)

We chose 6 as “average” as this is the mode of the quality scores—representing over 2000 observations.

Data preparation

We’re going to start by obtaining the data and preparing it. To do this we use the readr and dplyr packages.

library(readr)  
library(dplyr)  
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv"  
df <-  
 read_delim(url, delim = ";") %>%  
 dplyr::mutate(taste = as.factor(ifelse(quality < 6, "bad", ifelse(quality > 6, "good", "average")))) %>%  
 dplyr::select(-quality)  
df <- dplyr::mutate(df, id = as.integer(rownames(df)))

In this code, we:

  • load the data into a data frame
  • bin the quality values to create a “taste” categorical feature (as previously discussed)
  • discard the quality values
  • add an integer ID column so we can identify the observations more easily

Connect to the Spark cluster

For the purposes of this example, we’re connecting to a local Spark cluster. If you have a remote cluster this can be referenced when configuring the session.

When running RStudio (or another IDE) from an application launcher, it may not pick up the SPARK_HOME environment variable (I’m using Linux here), so you need to configure this directly. Alternatively, launch your IDE from a terminal window.

Sys.setenv(SPARK_HOME="/home/andrew/spark-2.1.0-bin-hadoop2.7")

We need to load the Spark R package. This is distributed with the Spark installation (in $SPARK_HOME/R/lib/)_.

library(SparkR, lib.loc=c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib")))

Now we can set the Spark session to use our local master node.

sparkR.session(master="local[*]")

SparkR doesn’t use normal data frames. It makes use of a distributed data frame that can be spread across the nodes of the cluster. This would normally be loaded from distributed storage such as HDFS or Amazon S3. However, we’ll just convert our small wine data frame to a distributed data frame.

ddf <- createDataFrame(df)

Let’s split this into training (70%) and test (30%) datasets. We set a seed so we can get replicable results.

seed <- 12345  
training_ddf <- sample(ddf, withReplacement=FALSE, fraction=0.7, seed=seed)  
test_ddf <- except(ddf, training_ddf)

Note that Spark has not actually computed these datasets yet. It’s lazily building up an execution plan that will be executed when the data is required.

Now we train a model to predict the “taste”.

model <- spark.randomForest(training_ddf, taste ~ ., type="classification", seed=seed)

The cluster will now start to do some work. To examine the model parameters—including definitions of all the trees in the forest—use the summary function.

 summary(model)

We can use our newly trained model to make some predictions on the test dataset. These can be retrieved as a normal (non-distributed) data frame.

predictions <- predict(model, test_ddf)  
prediction_df <- collect(select(predictions, "id", "prediction"))

Now let’s join the actual “taste” scores to the predicted scores and see whether our model is accurate.

actual_vs_predicted <-  
 dplyr::inner_join(df, prediction_df, "id") %>%  
 dplyr::select(id, actual = taste, predicted = prediction)

mean(actual_vs_predicted$actual == actual_vs_predicted$predicted)

table(actual_vs_predicted$actual, actual_vs_predicted$predicted)

In my run, I achieved an accuracy of around 62% (your numbers may vary slightly due to changes to the libraries).

Looking at the tabulated data, very few bad wines were classified as good (6) and vice versa (8). However, the model has trouble separating the average wines from good or bad ones. This is a challenge with our quality encoding approach as wines with a quality score of 7 or 5 are still average.

If we want to use this model in future, we can save it so we don’t have to retrain it every time.

model_file_path <- "home/andrew/wine_random_forest_model"  
write.ml(model, model_file_path)  
saved_model <- read.ml(model_file_path)  
summary(saved_model)

Machine Learning using Spark and R

The script that formed the basis of this tutorial is available as a Gist.

 

Like this article? Subscribe to our weekly newsletter to never miss out!

Follow @DataconomyMedia

Tags: Apache SparkMachine LearningRSparksurveillance

Related Posts

Meta releases Pocket app for generative AI games

Meta releases Pocket app for generative AI games

July 3, 2026
Android Halo will place AI agent updates in status bar

Android Halo will place AI agent updates in status bar

July 2, 2026
Anthropic launches Claude Science workbench for researchers

Anthropic launches Claude Science workbench for researchers

July 1, 2026
ChatGPT Plus users can now connect financial accounts

ChatGPT Plus users can now connect financial accounts

July 1, 2026
Google rolls out Gemini Spark for macOS subscribers in the US

Google rolls out Gemini Spark for macOS subscribers in the US

July 1, 2026
Google expands Gemini’s personalized image generation to all U.S. users

Google expands Gemini’s personalized image generation to all U.S. users

June 30, 2026
Please login to join discussion

LATEST NEWS

Samsung confirms One UI 9 Beta 4 release for next week

Sony to keep producing discs for pre-2028 PlayStation games

$TRUMP memecoin investors face $3.8 billion in losses

Tesla brings long-wheelbase Model Y to the US

Opera adds protection against copy-paste ClickFix attacks

Cloudflare will block AI crawlers unless sites opt in

BEST AI MODELS LEADERBOARD

See the best AI models, ranked by intelligence, benchmark results, speed and token price. Find the most suitable LLMs, Text-to-Image, Image Editing, Text-to-Speech, Text-to-Video and Image-to-Video  artificial intelligence model for your tasks and business.

LATEST TOOLS

Kaiber

KitchenGPT

Dupdub

Solvely

Typecast

Swimm

Instantchapters

Intellectia

ZipWP

Copyleaks – Plagiarism detector

Dataconomy

COPYRIGHT © DATACONOMY MEDIA GMBH, ALL RIGHTS RESERVED.

  • About
  • Imprint
  • Contact
  • Legal & Privacy

Follow Us

  • News
    • Artificial Intelligence
    • Cybersecurity
    • DeFi & Blockchain
    • Finance
    • Gaming
    • Startups
    • Tech
  • Industry
  • Research
  • Resources
    • Articles
    • Guides
    • Case Studies
    • Whitepapers
    • AI Models Leaderboard
  • AI tools
  • Newsletter
  • + More
    • Glossary
    • Conversations
    • Events
    • About
      • Who we are
      • Contact
      • Imprint
      • Legal & Privacy
      • Partner With Us
No Result
View All Result
Subscribe

This website uses cookies to improve your experience. You can choose to accept or reject them. Visit our Privacy Policy.