Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Evaluate Calculations
#1
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/1eNJQJBr...sp=sharing

Data for this question: https://drive.google.com/file/d/16VdNW4Z...sp=sharing
Larz60+ write Jun-16-2023, 09:14 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What to use with simple calculations Milfredo 6 2,669 Sep-24-2020, 06:42 AM
Last Post: Milfredo
  huge and weird values after applying some calculations karlito 2 2,168 Dec-13-2019, 08:32 AM
Last Post: karlito
  Can I evaluate a Chebyshev polynomial using a function player1681 1 2,011 Nov-22-2019, 06:33 AM
Last Post: scidam
  Evaluate dataset with logistic regression chisox721 6 3,616 Jun-06-2019, 03:01 PM
Last Post: chisox721
  Pandas .rolling() with some calculations inside irmscher 5 6,234 Apr-04-2019, 11:55 AM
Last Post: scidam
  Pandas, cryptocurrency and some calculations vvinni 0 2,043 Mar-10-2019, 03:52 PM
Last Post: vvinni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020