Python-Read data from a numeric data file and calculate covariance

A program that reads data from a tab-delimited numeric data series file and calculates covariance.

MultipleRegressionAnalysis_Data1.txt


65.7	67.8	70.3	72.0	74.3	76.2
3.27	3.06	4.22	4.10	5.26	6.18
69.7	69.7	71.3	77.6	81.0	78.7

correlation.py


import numpy as np

def load_data(filename):
    x = []
    for line in open(filename, 'r'):
        x.append([])
        for data in line.strip().split('\t'):
            x[len(x)-1].append(float(data))
    return x

def calc_correlation(data1, data2):
    ave1 = calc_average(data1)
    ave2 = calc_average(data2)
    sum = 0.0
    for i in range(len(data1)):
        sum += data1[i] * data2[i]
    return (sum / len(data1)) - (ave1 * ave2)
    
def calc_average(data):
    sum = 0.0
    for d in data:
        sum += d
    return sum / len(data)

if __name__ == "__main__":
    data = load_data('MultipleRegressionAnalysis_Data1.txt')
    corr = []
    for i in range(len(data)):
        corr.append([])
        for j in range(len(data)):
            corr[len(corr)-1].append(calc_correlation(data[i],data[j]))
            
    print(np.array(corr))

result


>python correlation.py
[[ 12.95583333   3.70208333  14.89666667]
 [  3.70208333   1.18114722   4.10327778]
 [ 14.89666667   4.10327778  20.94222222]]


Recommended Posts

Python-Read data from a numeric data file and calculate covariance
Python --Read data from a numeric data file and find the multiple regression line.
I tried reading data from a file using Node.js.
[Python] Start a batch file from Python and pass variables.
Read and write a file
Create a dummy data file
Write and read a file
Extract csv data and calculate
Read zipline brand data from csv file and perform back test
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
I made a subtitle file (SRT) from JSON data of AmiVoice
Get OCTA simulation conditions from a file and save with pandas
Clogged when getting data from DB and making it a return value
Tips: [Python] Randomly restore and extract an array from a fasta file
Make a decision tree from 0 with Python and understand it (4. Data structure)
Data handling 1 Data formatting and file input / output
Create a deb file from a python package
Read a character data file with numpy
Aggregate steps by day from iPhone healthcare data to create a CSV file
[Kaggle] From data reading to preprocessing and encoding
[Python] How to read data from CIFAR-10 and CIFAR-100
Data retrieval from MacNote3 and migration to Write
Generate a MeCab dictionary from Nico Nico Pedia data
Draw a graph with matplotlib from a csv file
Run a Python file from html using Django
Wav file generation from numeric text with python
Read line by line from a file with Python
Sort Fashion-MNIST data and save as PNG file
Extract data from a web page with Python
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_