Python Forum
Evaluate Calculations - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Evaluate Calculations (/thread-40182.html)



Evaluate Calculations - yrstruly - Jun-17-2023

Python Linear Regression Project
In the Python file, write a script that will perfom a linear regression on the data in data.txt and then calculate the coefficient of determination of the prediction.

The first line in data.txt represents the X column, and the second line represents the Y column.
3.
import numpy as np
from sklearn.linear_model import LinearRegression

# Read data from file
with open(r'C:\Users\Anthony.DESKTOP-ES5HL78\Downloads\abalone.data', 'r') as file:
lines = file.readlines()

# Extract X and Y data
X = []
Y = []
for line in lines:
data = line.strip().split(',')
X.append(data[1:-1])
Y.append(data[-1])

# Convert data to numpy arrays
X = np.array(X, dtype=float)
Y = np.array(Y, dtype=float)

# Create a linear regression model
model = LinearRegression()

# Fit the model to the data
model.fit(X, Y)

# Calculate the coefficient of determination (R-squared)
coefficient_of_determination = model.score(X, Y)

# Print the coefficient of determination
print("coefficient:", coefficient_of_determination)
Here is my answer:
https://drive.google.com/file/d/1eNJQJBr6DqGW2sVr-X61Iq4-KPIawvDW/view?usp=sharing

Data for this question: https://drive.google.com/file/d/16VdNW4ZlBZkA6uPdtlkjwk66KbWJkT6L/view?usp=sharing