Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors

A program that reads data from a tab-delimited numeric data series file as shown below and obtains the variance-covariance matrix, eigenvalues, and eigenvectors.

MultipleRegressionAnalysis_Data2.txt


23.3	19.8	14.7	19.7	16.9	14.4
5.24	-5.23	-7.95	11.70	-2.44	-2.14

MultipleRegressionAnalysis_3.py


import numpy as np
import math

def load_data(filename):
    with open(filename) as lines:
        return [[float(data) for data in line.strip().split('\t')]
                for line in lines]
    
def calc_sum_of_square(data_list1, data_list2):
    return (len(data_list1) * sum([data1 * data2 for data1, data2 in zip(data_list1, data_list2)]) - sum(data_list1) * sum(data_list2))/(len(data_list1)*(len(data_list1)-1))

def calc_variance_covariance_matrix(data_list):
    return [[calc_sum_of_square(data_list1, data_list2)/(len(data_list1)-1) for data_list1 in data_list]
            for data_list2 in data_list]
            
def calc_engenvalue_2d(mat):
    lambda1 = ((mat[0][0] + mat[1][1]) + math.sqrt((mat[0][0] - mat[1][1])**2 + 4*(mat[0][1]**2))) / 2
    lambda2 = ((mat[0][0] + mat[1][1]) - math.sqrt((mat[0][0] - mat[1][1])**2 + 4*(mat[0][1]**2))) / 2
    return [lambda1,lambda2]
    
def calc_engenvector_2d(mat, engenvalue):
    a11 = math.fabs(mat[0][1]) / math.sqrt((engenvalue[0] - mat[0][0])**2 + mat[0][1]**2)
    a12 = (engenvalue[0] - mat[0][0])*a11 / mat[0][1]
    a21 = math.fabs(mat[1][0]) / math.sqrt((engenvalue[1] - mat[0][0])**2 + mat[0][1]**2)
    a22 = (engenvalue[1] - mat[0][0])*a21 / mat[0][1]
    return [[a11, a12],[a21, a22]]
            
if __name__ == "__main__":
    data = load_data('MultipleRegressionAnalysis_Data2.txt')
    variance_coveriance_matrix = calc_variance_covariance_matrix(data)
    print("-Covariance matrix-")
    print(np.array(variance_coveriance_matrix))
    eigenvalue = calc_engenvalue_2d(variance_coveriance_matrix)
    print("-eigenvalue-")
    print(eigenvalue)
    engen_vector = calc_engenvector_2d(variance_coveriance_matrix, eigenvalue)
    print("-Eigenvector-")
    print("z1 = " + str(engen_vector[0][0]) + " x1 " + ("+" if engen_vector[0][1] > 0 else "") + str(engen_vector[0][1]) + " x2")
    print("z2 = " + str(engen_vector[1][0]) + " x1 " + ("+" if engen_vector[1][1] > 0 else "") + str(engen_vector[1][1]) + " x2")

result


> python MultipleRegressionAnalysis_3.py
-Covariance matrix-
[[  2.36693333   2.99921333]
 [  2.99921333  10.61296533]]
-eigenvalue-
[11.58843357427205, 1.3914650923945988]
-Eigenvector-
z1 = 0.30929366776230544 x1 +0.9509665751655736 x2
z2 = 0.9509665751655736 x1 -0.3092936677623057 x2

Recommended Posts

Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Python --Read data from a numeric data file and find the multiple regression line.
Python-Read data from a numeric data file and calculate covariance
[Python] How to read data from CIFAR-10 and CIFAR-100
Find the Hermitian matrix and its eigenvalues in Python
Build a Python environment and transfer data to the server
Find the eigenvalues of a real symmetric matrix in Python
Read a file in Python with a relative path from the program
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
How to read a CSV file with Python 2/3
[Python] Find the transposed matrix in a comprehension
Read line by line from a file with Python
Various ways to read the last line of a csv file in Python
Find eigenvalues and eigenvectors
Template of python script to read the contents of the file
Read the xml file by referring to the Python tutorial
Python script to create a JSON file from a CSV file
[Python] Start a batch file from Python and pass variables.
[python] Change the image file name to a serial number
How to switch the configuration file to be read by Python
Change the standard output destination to a file in Python
[Python] Read the csv file and display the figure with matplotlib
Write a python program to find the editing distance [python] [Levenshtein distance]
Read and write a file
[Introduction to matplotlib] Read the end time from COVID-19 data ♬
Write and read a file
Python> Read from a multi-line string instead of a file> io.StringIO ()
[Python] A program to find the number of apples and oranges that can be harvested
Pass OpenCV data from the original C ++ library to Python
Extract the value closest to a value from a Python list element
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
Read zipline brand data from csv file and perform back test
Process Splunk execution results using Python and save to a file
How to get followers and followers from python using the Mastodon API
Create a shell script to run the python file multiple times
How to make a command to read the configuration file with pyramid
From a book that programmers can learn (Python): Find the mode
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
[Python3] Save the mean and covariance matrix in json with pandas
[Python] How to store a csv file as one-dimensional array data
[Python] Read a csv file with a large data size using a generator
Find all patterns to extract a specific number from the set
[Python] A simple function to find the center coordinates of a circle
Find the intersection of a circle and a straight line (sympy matrix)
Read and use Python files from Python
[Python] About creating a tool to create a new Outlook email based on the data of the JSON file and the part that got caught
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Remove and retrieve arrays from fasta according to the ID list file
[Python Kivy] How to get the file path by dragging and dropping
How to divide and process a data frame using the groupby function
Read and format a csv file mixed with comma tabs with Python pandas
[Introduction to Python] What is the difference between a list and a tuple?
Tips: [Python] Randomly restore and extract an array from a fasta file
Extract lines that match the conditions from a text file with python
Command to find the desired header file from the GCC, Clang include directories
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Connect to postgreSQL from Python and use stored procedures in a loop.