Contact information

PromptCloud Inc, 16192 Coastal Highway, Lewes De 19958, Delaware USA 19958

We are available 24/ 7. Call Now.
Abhisek Roy

Python is one of the easiest and most user-friendly codes that can be written by coders. Factor analysis of python is one of the popular methods of discovering underlying factors and latent variables in data. What do I mean by latent variables? Let’s find out. Say we have a dataset with 6 variables-

  1. Age
  2. Weight
  3. Height
  4. Salary
  5. Bank Balance
  6. Number of Credit Cards

And we have collected these 6 data points for 1000 users. Now after thorough analysis we find out that there exist two latent or hidden variables that are dependent on some of the data points present here.

The figure above gives a good understanding of how factor-analysis helps find hidden variables. In this specific example, I chose data points that one can easily separate and analyze, but when you have a data set with headings like x1, x2, y1, y2, z1, z2, you will need the proper tools to get this job done. When it comes to exploratory factor analysis, which is by far the most popular factor analysis approach amond researchers, the basic assumption made at the very beginning is that every variable at hand is directly associated with a factor.

What Is The Aim Of Factor Analysis Of Python?

The main aim of factor analysis is to reduce the number of data-points by extracting unobservable variables. The new variables make it easier for a market researcher or a statistician to complete his study, and also makes the data more consumable. This conversion of

Observable variables  →  Unobservable variables

needs to be done through Factor Extraction followed by Factor Rotation.

In Factor Extraction, we decide the number of factors that we want and how we want to extract it. This is done using variance partitioning methods, of which common factor analysis and principal components analysis are common. Next comes Factor Rotation, which is done to reduce the complexity of the solution.

How to find these latent variables using Python?

Python comes with multiple third-party libraries that can help you in statistical analysis. And today we will be using its factor_analyser library. We will also be using pandas to handle our data and convert it to a data frame, that makes it more usable. Another library matplotlib is being used for creating graphs. One point to note is that certain commands in this code may not print the result directly if you are running this in the terminal. I would recommend you to use Jupyter notebooks since it helps visualize data well.

In the first step, we import the libraries that are required for the task and load our dataset. Once that is done, we will view the columns.

Factor Analysis Of Python

Once you see the columns, you will get a fair idea of the number of features. You can also use df.head to get a snapshot of the data.

Index([‘Unnamed: 0’, ‘A1’, ‘A2’, ‘A3’, ‘A4’, ‘A5’, ‘C1’, ‘C2’, ‘C3’, ‘C4’,
      ‘C5’, ‘E1’, ‘E2’, ‘E3’, ‘E4’, ‘E5’, ‘N1’, ‘N2’, ‘N3’, ‘N4’, ‘N5’, ‘O1’,
      ‘O2’, ‘O3’, ‘O4’, ‘O5’, ‘gender’, ‘education’, ‘age’],

Before we try out an exploratory analysis of the dataset, we need to prepare the data. For this, we will be dropping the first and the last three columns of the dataset and also removing all values that are “Nan”. After data preparation completion, we shall view the schema of the data.

#Steps required for data preparation
df.drop([“gender”, “education”, “age”], axis =1 , inplace = True)
df = df.iloc[0:,1:26]

Once you have viewed the schema of the data, you can also see the data itself – a few rows of it.

#View the data

Fig: Output of df.head()

After this, we shall create a Factor analyzer object and extract the eigenvalues. This used to create a Scree plot.

#Create an object of FactorAnalyser with number of factors 6 and varimax rotation
fa=FactorAnalyzer(n_factors=6, rotation=‘varimax’)
#Extract eigenvalues and create the Scree Plot
eigen_values, vectors = fa.get_eigenvalues()

plt.title(‘Scree Plot’)

From the scree plot, we can see that the number of eigenvalues greater than one is 6. Hence our guess of setting n_factors as 6 was correct. In case of a mismatch, we could have reinitialized the FactorAnalyser object before continuing to the next step. There can be a possibility that a different value of n_factor can perform better, but we shall know that only after generating the loading values.

#Extract factor loadings for each variable and convert the 2d array to a dataframe
loadings = fa.loadings_
df_new = pd.DataFrame(loadings)

In the final step, we will extract the factor loadings for each variable corresponding to each factor. We have converted the final 2d array to a Pandas data frame so that it is easier to understand.


Fig: Output of fa.loadings_

What do the results tell us?

So you got a 2-D array of 25 rows and 5 columns as the final output. What does that mean? If you take a closer look at the data, you will spot that specific factors have high loading values for specific variables.

Factor 0N1, N2, N3, N4, N5 
Factor 2E1, E2, E3, E4, E5 
Factor 3C1, C2, C3, C4, C5
Factor 4A1, A2, A3, A4, A5
Factor 5O1, O2, O3, O4, O5
Factor 6No high loading value for any variable.

So in a way, our estimate missed the mark, and our results will be better if redone with just 5 factors instead of 6. You can try that out on your system and share the results!

Sharing is caring!

Are you looking for a custom data extraction service?

Contact Us