Simple Linear Regression with Scikit-learn

 


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Load data
data = pd.read_csv('data.csv')

# Prepare the data
X = data[['YearsExperience']]  # Feature
y = data['Salary']  # Target

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create a linear regression model
model = LinearRegression()

# Train the model
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)

# Print the coefficient and intercept
print("Coefficient:", model.coef_[0])
print("Intercept:", model.intercept_)

-

Explanation:

This code demonstrates a simple linear regression model using Scikit-learn. It loads data, prepares the features (X) and target (y), splits the data into training and testing sets, creates a linear regression model, trains the model on the training data, makes predictions on the testing data, and evaluates the model using mean squared error.

Requirements:

  • Scikit-learn library installed (pip install scikit-learn)
  • Pandas library installed (pip install pandas)
  • A CSV file named data.csv with 'YearsExperience' and 'Salary' columns. Example:


YearsExperience,Salary
1,45000
2,50000
3,60000
4,70000
5,80000
6,90000
7,100000
8,110000
9,120000
10,130000

Additional Notes: Scikit-learn is a comprehensive machine learning library in Python. It provides a wide range of algorithms for classification, regression, clustering, dimensionality reduction, and model selection. It's known for its consistent API and ease of use.

Comments