Python --Read data from a numeric data file and find the multiple regression line.

A program that reads data from a tab-delimited numeric data series file as shown below and obtains multiple regression lines. 1st line: Objective variable (y) 2nd line: Explanatory variable 1 (x1) Line 3: Explanatory variable 2 (x2)

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

MultipleRegressionAnalysis_2.py


from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D 

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_average(data):
    return sum(data) / len(data)
    
def calc_sum_of_square(data_list):
    ave = calc_average(data_list)
    return sum((d - ave)**2 for data in data_list)
    
def calc_sum_of_square(data_list1, data_list2):
    ave1 = calc_average(data_list1)
    ave2 = calc_average(data_list2)
    return sum((data1 * data2 - ave1 * ave2)**2 for data1, data2 in zip(data_list1, data_list2))
    
if __name__ == "__main__":
    data = load_data('MultipleRegressionAnalysis_Data1.txt')
    s = [[calc_sum_of_square(data1, data2) for data2 in data]
            for data1 in data]

    det = s[1][1]*s[2][2] - s[1][2]**2
    #Partial regression coefficient calculation
    b1 = (s[0][1]*s[2][2] - s[0][2]*s[1][2])/det
    b2 = (s[0][2]*s[1][1] - s[0][1]*s[1][2])/det
    b0 = calc_average(data[0]) - b1*calc_average(data[1]) - b2*calc_average(data[2])
    #Multiple regression expression display
    print("Y = " + str(b1) + " x1 + " + str(b2) + " x2 + " + str(b0))
    
    fig = plt.figure()
    ax = Axes3D(fig)
    #Plane display from the obtained multiple regression equation
    x1 = np.arange(0, 10, 0.25)
    x2 = np.arange(65, 80, 0.25)
    X1, X2 = np.meshgrid(x1, x2)
    Y = b1 * X1 + b2 * X2 + b0
    ax.plot_wireframe(X1, X2, Y)
    #Original data display
    ax.scatter3D(data[1], data[2], data[0], c='red')
    plt.show()

result


> python MultipleRegressionAnalysis_2.py
Y = -17.118276566578807 x1 + 1.0700625679745137 x2 + 65.58796752824318

MultipleRegressionAnalysis1.PNG

MultipleRegressionAnalysis2.PNG

MultipleRegressionAnalysis3.PNG

Recommended Posts

Python --Read data from a numeric data file and find the multiple regression line.
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Read line by line from a file with Python
Python-Read data from a numeric data file and calculate covariance
Read the file line by line in Python
Read the file line by line in Python
Read a file in Python with a relative path from the program
[Python] Read the specified line in the file
Read the file with python and delete the line breaks [Notes on reading the file]
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
[Implementation example] Read the file line by line with Cython (Python) from the last line
[Python] How to read data from CIFAR-10 and CIFAR-100
Various ways to read the last line of a csv file in Python
[Python] Start a batch file from Python and pass variables.
Read and write a file
Write and read a file
[Python] Read the csv file and display the figure with matplotlib
Build a Python environment and transfer data to the server
Python> Read from a multi-line string instead of a file> io.StringIO ()
[Python] Read command line arguments from file name or stdin
Read and use Python files from Python
Create a shell script to run the python file multiple times
Read the standard output of a subprocess line by line in Python
From a book that programmers can learn (Python): Find the mode
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Outputs a line containing the specified character string from a text file
[Python] Read a csv file with a large data size using a generator
Find the intersection of a circle and a straight line (sympy matrix)
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
[Note] Read a file from another directory
Read and format a csv file mixed with comma tabs with Python pandas
Create multiple line charts from a data frame at once using Matplotlib
Tips: [Python] Randomly restore and extract an array from a fasta file
Extract lines that match the conditions from a text file with python
Let's read the RINEX file with Python ①
Create a deb file from a python package
Read a character data file with numpy
Make a decision tree from 0 with Python and understand it (4. Data structure)
[python] Read html file and practice scraping
Create a Python image in Django without a dummy image file and test the image upload
A Python script that reads a SQL file, executes BigQuery and saves the csv
A python script that draws a band diagram from the VASP output file EIGENVAL
Fourier transform the wav file read by Python, reverse transform it, and write it again.
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
I want to get the file name, line number, and function name in Python 3.4
Visualize railway line data and solve the shortest path problem (Python + Pandas + NetworkX)
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
Read table data in PDF file with Python
[Python] Find coordinates from two angles and distance
Run a Python file from html using Django
[Python] Find the transposed matrix in a comprehension
Wav file generation from numeric text with python
A discussion of the strengths and weaknesses of Python
Extract data from a web page with Python
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
[python] Read data
[Python] Save the video data imported by OpenCV as a serial number jpg file
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
A note that runs an external program in Python and parses the resulting line