Permalink
Cannot retrieve contributors at this time
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?
5011CEM_COURSEWORK_CLINTON-EKWUGHA_13293446/2.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38 lines (30 sloc)
1.36 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |