# PySpark MLlib – Algorithms and Parameters

Boost your career with Free Big Data Courses!!

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.

## 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.

### 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.

Technology is evolving rapidly!

Although, PySpark MLlib package also covers other algorithms, classes, and functions.
Well to understand it better, here is the following example

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)

## 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).

• 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.

## 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!