How to create correlation matrix in Python?
A correlation matrix has been created using the following two libraries:
- NumPy Library
- Pandas Library
Creating a correlation matrix using NumPy Library
NumPy is a library for mathematical computations. It can be used for creating correlation matrices that helps to analyze the relationships between the variables through matric representation.
Example 1
Suppose an ice cream shop keeps track of total sales of ice creams versus the temperature on that day. To learn the correlation, we will use NumPy library.
In the following code snippet, x and y represent total sales in dollars and corresponding temperatures for each day of sale and np.corrcoef() function is sed to compute the correlation matrix.
import numpy as np
# x represents the total sale in dollars
x = [215, 325, 185, 332, 406, 522, 412,
614, 544, 421, 445, 408],
# y represents the temperature on each day of sale
y = [14.2, 16.4, 11.9, 15.2, 18.5, 22.1,
19.4, 25.1, 23.4, 18.1, 22.6, 17.2]
# create correlation matrix
matrix = np.corrcoef(x, y)
print(matrix)
Create a correlation Matrix using Python
In the field of data science and machine learning, a correlation matrix aids in understanding relationships between variables. Correlation matrix represents how different variables interact with each other.
For someone who is navigating the complex landscape of data, understanding and harnessing the potential of correlation matrices is a skill that can significantly enhance their ability to drive meaningful insights. In this article, we will explore the step-by-step process of creating a correlation matrix in Python.
Contact Us