Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import numpy as np
from sklearn.linear_model import LinearRegression
# Load data using NumPy arrays
data_file1 = "/Users/thecreator/Downloads/Trips_Full Data.csv"
data_file2 = "/Users/thecreator/Downloads/Trips_by_Distance.csv"
# Load data from the first file
data1 = np.genfromtxt(data_file1, delimiter=",", skip_header=1, usecols=(10,), dtype=float)
x = data1[~np.isnan(data1)] # Filter out NaN values in x
# Load data from the second file
data2 = np.genfromtxt(data_file2, delimiter=",", skip_header=1, usecols=(19,), dtype=float)
y = data2[~np.isnan(data2)] # Filter out NaN values in y
# Check how many valid samples are remaining
print(f"Number of valid samples in x: {len(x)}")
print(f"Number of valid samples in y: {len(y)}")
# Check if there are still samples left after filtering NaN values
if len(x) == 0 or len(y) == 0:
print("No valid samples remaining after filtering NaN values.")
else:
# Create and fit the model
x = x.reshape(-1, 1) # Reshape x to be a 2D array (required by scikit-learn)
model = LinearRegression()
model.fit(x, y)
# Print the model coefficients
print(f"Coefficient of determination (R^2): {model.score(x, y):.2f}")
print(f"Intercept: {model.intercept_:.2f}")
print(f"Coefficient(slope): {model.coef_[0]:.2f}")
# Make predictions
y_pred = model.predict(x)
print("Predicted responses:")
print(y_pred)