# PySpark MLlib – Algorithms and Parameters

Stay updated with latest technology trends
Join DataFlair on Telegram!!

## 1. Objective

In our last PySpark tutorial, we discussed PySpark StorageLevel. Today, we will discuss PySpark MLlib. Moreover, we will see different algorithms and parameters of PySpark MLlib. PySpark has this machine learning API.
So, let’s start PySpark MLlib.

## 2. What is PySpark MLlib?

As we know, Spark offers a Machine Learning API which we call MLlib. Though, in Python as well, PySpark has this machine learning API. Also, there are different kind of algorithms in PySpark MLlib, such as:

### a. mllib.classification

For binary classification, various methods are available in the spark.mllib package such as multiclass classification as well as regression analysis. Moreover, in classification, some of the most popular algorithms are Naive Bayes, Random Forest, Decision Tree

### b. mllib.clustering

An unsupervised learning problem is clustering, here we try to group subsets of entities with one another on the basis of some notion of similarity.

### c. mllib.linalg

This algorithm supports PySpark MLlib utilities for linear algebra.
Let’s revise PySpark Spark Context

### d. mllib.recommendation

For recommender systems, collaborative filtering is commonly used. So, to fill in the missing entries of a user item association matrix is the main aim of these techniques aim.

### e. spark.mllib

Recently, this PySpark MLlib supports model-based collaborative filtering. By a small set of latent factors,. Here all the users and products are described, which we can use to predict missing entries. However, to learn these latent factors, spark.mllib uses the Alternating Least Squares (ALS) algorithm.

### f. mllib.regression

Basically, linear regression comes from the family of regression algorithms. To find relationships and dependencies between variables is the main goal of regression.
Although, PySpark MLlib package also covers other algorithms, classes, and functions.
Well to understand it better, here is the following example
Read PySpark Career Scope With Salary Trends
Alternating Least Squares Matrix Factorization

```def train(cls, ratings, rank, iterations=5, lambda_=0.01, blocks=-1, nonnegative=False,
seed=None):```

“””
Train a matrix factorization model given an RDD of ratings by users
for a subset of products. The rating matrix is approximated as the
product of two lower-rank matrices of a given rank (number of
features). To solve for these features, ALS is run iteratively with
a configurable level of parallelism.
:param ratings:
RDD of `Rating` or (userID, productID, rating) tuple.
:param rank:
Rank of the feature matrices computed (number of features).
:param iterations:
Number of iterations of ALS.
(default: 5)
:param lambda_:
Regularization parameter.
(default: 0.01)
:param blocks:
Number of blocks used to parallelize the computation. A value
of -1 will use an auto-configured number of blocks.
(default: -1)
:param nonnegative:
A value of True will solve least-squares with nonnegativity
constraints.
(default: False)
:param seed:
Random seed for initial matrix factorization model. A value
of None will use system time as the seed.
(default: None)
“””
model = callMLlibFunc(“trainALSModel”, cls._prepare(ratings), rank, iterations,
lambda_, blocks, nonnegative, seed)
return MatrixFactorizationModel(model)

## 3. Parameters of PySpark MLlib

Below discussing are some main parameters of PySpark MLlib:

• Ratings

This is RDD of Rating or (userID, productID, rating) tuple.

• Rank

It shows Rank of the feature matrices computed (number of features).
Learn PySpark SparkConf – Attributes and Applications

• Iterations

These are the number of iterations of ALS. (default: 5).

• Lambda

It is Regularization parameter. (default: 0.01).

• Blocks

To parallelize the computation some number of blocks used. (default: -1).

• Nonnegative

With nonnegativity constraints, a value of True will solve least-squares. (default: False).
So, this was all about PySpark MLlib. Hope you like our explanation.

## 4. Conclusion

Hence, we have seen all about PySpark MLlib. Moreover, in this PySpark tutorial, we discussed different algorithms and parameters for PySpark MLlib. Still, if any doubt, ask in the comment tab. Hope it helps!
See also –
Pyspark Profiler
For reference