Adventures in Machine Learning

Uncovering Hidden Insights: Implementing Factor Analysis in Python

Introduction to Factor Analysis

Data analysis is an essential component of data science and provides crucial insights into data trends, patterns, and relationships. Data scientists often use unsupervised machine-learning techniques such as factor analysis to gain an in-depth understanding of data variability.

Factor analysis is a dimensionality reduction technique used to identify common features that lie behind a set of measured features. This technique assesses variations in data by investigating the correlation among variables.

In doing so, factor analysis aims to find a way to represent variables using fewer dimensions while preserving the essential information.

Types of Factor Analysis

There are two types of factor analysis: exploratory factor analysis (EFA) and confirmatory factor analysis (CFA). EFA is used to explore the structure of relationships among variables and assess how many underlying factors or dimensions can explain the data’s variability.

CFA, on the other hand, is used to test the hypothesized factor structure and ensure that it fits the data. CFA helps researchers confirm or verify the structure of a pre-determined variable relationship structure.

Application of Factor Analysis

Factor analysis is widely used in data science and various other fields such as psychology, sociology, and marketing. In data science, one of the primary applications of factor analysis is to reduce the number of variables to a more manageable number, making modeling more effective.

It enables data scientists to narrow down the variables to a few “factors” that influence the data’s variability. Additionally, factor analysis is used to understand the relationships between variables and identify which variables are correlated in a particular dataset.

Implementing Factor Analysis in Python

Python is an open-source programming language that provides powerful tools for data analysis. To implement factor analysis in Python, you must first install the necessary modules.

These modules include Pandas, pydataset, sklearn, matplotlib.pyplot, and NumPy.

Installing Pydataset

To install Pydataset, you need to have pip installed on your system. Pip is a package manager for Python.

After installing pip, you can install pydataset using the following command: “pip install pydataset.”

Data Preparation

Once you have installed the necessary modules, you can proceed to prepare your data. For this article, we will be using the BioChemists dataset, which contains data on five variables – age, sex, education, occupation, and region.

To load the dataset into Python, you can use the following code:


import pandas as pd

from pydataset import data

biochemists_df = data(‘BioChemists’)


This code loads the BioChemists dataset into Pandas DataFrame format, making it easier to manipulate the data.

Variable Selection

After loading the dataset, you can select the variables that you want to use for factor analysis. For this article, we will use the “age,” “education,” “occupation,” and “region” variables.

You can create a new DataFrame containing only the selected variables as follows:


selected_variables_df = biochemists_df[[‘age’, ‘education’, ‘occupation’, ‘region’]]


You can now proceed to implement factor analysis on the selected variables.


In conclusion, factor analysis is a powerful dimensionality reduction technique that allows data scientists to investigate the structure of relationships among variables, assess how many underlying factors or dimensions explain the data’s variability, and reduce the number of variables to a more manageable number. Python provides several powerful tools for implementing factor analysis, including Pandas, pydataset, and sklearn.

By following the steps outlined in this article, you can perform factor analysis on your dataset and gain crucial insights into your data’s variability.

Model Development

Once you have selected your variables, you can use factor analysis to create factors for your dataset. A factor is a new variable that is created by combining the selected variables that have high correlation.

The number of factors created can be determined using the “n components” parameter in factor analysis. It is important to note that factors do not have a specific meaning but instead represent latent constructs that underlie the observed variables.

To create factors, you can use the following code:


from sklearn.decomposition import FactorAnalysis

fa = FactorAnalysis(n_components=4)

factors = fa.transform(selected_variables_df)


The “n_components” parameter specifies the number of factors that need to be created. In this case, we have specified four factors.

The “fit” method is then used to apply the factor analysis to the selected variables. Finally, the “transform” method is used to apply the factor analysis to the entire dataset, creating the factors required.

The output of the factor analysis is an array containing the factors.

Factor Output

The output of the factor analysis is an array with the same number of rows as the original dataset and a column for each of the factors created. Each value in the array represents the factor score for that row of the dataset.

Factor scores represent the extent to which each observation aligns with the specific factor. For example, an observation that scores high on the first factor represents that it is strongly associated with the underlying construct that the first factor represents.


After creating factors, visualizing the results is recommended for better interpretation and understanding of the factor analysis. One of the ways to visualize the factor analysis result is to use a scatter plot.

In this case, we will use the “age” and “education” variables to create a scatter plot of the factors. We will also use different colors to differentiate between different marital statuses.

Creating a Dictionary for Conversion

Before we can create a scatter plot, we need to convert the “marital status” variable into a numerical representation that can be plotted. To do this, we can create a dictionary that maps the “single” and “married” values to 0 and 1, respectively.

This will enable us to plot the different marital statuses on the same plot. “`

marital_status_dict = {

‘single’: 0,

‘married’: 1


selected_variables_df[‘marital_status’] = selected_variables_df[‘marital_status’].replace(marital_status_dict)


Plotting the Data

Now that we have converted the marital status variable into a numerical representation, we can plot the data using a scatter plot. “`

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

scatter = ax.scatter(factors[:, 0], factors[:, 1], c=selected_variables_df[‘marital_status’])


This code creates a scatter plot of the “age” and “education” factors, with different colors representing different marital statuses.

The first factor is plotted on the x-axis, while the second factor is plotted on the y-axis.


In conclusion, factor analysis is a powerful technique used to uncover latent constructs underlying observed variables. Python provides several libraries, such as Pandas, pydataset, and sklearn, that can be used to perform factor analysis.

By following the steps outlined in this article, you can perform factor analysis on your dataset, create factors, and visualize the results. This knowledge can enable data scientists to gain a better understanding of their data and uncover important insights.

Factor analysis is a valuable technique that enables data scientists to explore and understand the underlying constructs of their dataset. This technique creates factors to represent the data variability and reduce the number of variables to a manageable number.

Python libraries such as Pandas, pydataset, and sklearn provide the tools to implement factor analysis. Through data visualization using scatter plots, a data scientist can better interpret the factor analysis results.

The benefits of factor analysis include the ability to pinpoint underlying themes in data, streamline data for modeling, identify correlated variables, and gather insights into data relationships. Factor analysis is essential for eliminating redundant information and transforming complex datasets into meaningful data analysis.

Popular Posts