0% found this document useful (0 votes)
2 views

Ass1_SetB1

The document outlines a Python script for predicting fish weight using linear regression. It includes data preprocessing steps, such as dropping the species column and handling missing values, followed by splitting the dataset into training and testing sets. The model is trained, predictions are made, and the performance is evaluated using the R^2 score, with a visualization of the results.

Uploaded by

ashwinpunmagr99
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ass1_SetB1

The document outlines a Python script for predicting fish weight using linear regression. It includes data preprocessing steps, such as dropping the species column and handling missing values, followed by splitting the dataset into training and testing sets. The model is trained, predictions are made, and the performance is evaluated using the R^2 score, with a visualization of the results.

Uploaded by

ashwinpunmagr99
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# Import necessary libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

# Load the dataset


df = pd.read_csv('Fish.csv')

# Preprocess the data


df = df.drop(['Species'], axis=1) # Drop the species column as it's categorical
df = df.dropna() # Drop any rows with missing values

# Split the dataset into training and testing sets


X = df.drop(['Weight'], axis=1) # Features
y = df['Weight'] # Target variable
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,random_state=0)

# Train the model


regressor = LinearRegression()
regressor.fit(X_train, y_train)

# Make predictions on the test set


y_pred = regressor.predict(X_test)

# Evaluate the model using the coefficient of determination (R^2 score)


r2 = r2_score(y_test, y_pred)
print("R^2 score:", r2)

# Plot the regression line and data points


plt.scatter(X_test, y_test, color='gray')
plt.plot(X_test, y_pred, color='red', linewidth=2)
plt.show()

You might also like