Jun-17-2023, 06:51 PM
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
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