Skip to content

This Python code represents a machine learning project that builds a simple linear regression model using experience and salary data. It plots the data, constructs the regression model, and visualizes the results.

License

Notifications You must be signed in to change notification settings

Prometheussx/Linear-Regression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Linear Regression with Python

This Python script demonstrates simple linear regression using the scikit-learn library. It reads a dataset containing 'experience' and 'salary' data, visualizes the data, fits a linear regression model, and provides predictions. This README will explain each part of the code.

Table of Contents

Prerequisites

Make sure you have the following libraries installed:

Getting Started

  1. Clone this repository to your local machine.
  2. Ensure you have the required libraries installed.
  3. Place the dataset file, "linear_regression_dataset.csv," in the project directory.

Code Explanation

  • import pandas as pd and import matplotlib.pyplot as plt: Import necessary libraries.
  • df = pd.read_csv("linear_regression_dataset.csv", sep=";"): Read the dataset with 'experience' and 'salary' data, separated by a semicolon.
  • plt.scatter(df.experience, df.salary): Create a scatter plot of the data.
  • from sklearn.linear_model import LinearRegression: Import the Linear Regression model from scikit-learn.
  • linear_reg = LinearRegression(): Create a Linear Regression model.
  • x = df.experience.values.reshape(-1, 1): Prepare the 'experience' data for the model.
  • y = df.salary.values.reshape(-1, 1): Prepare the 'salary' data for the model.
  • linear_reg.fit(x, y): Fit the Linear Regression model.
  • b0 = linear_reg.predict([[0]]): Calculate the intercept (b0) of the regression line.
  • b1 = linear_reg.coef_: Calculate the slope (b1) of the regression line.
  • trial = 1663.89519747 + 1138.34819698 * 10: Make a manual prediction.
  • array = np.array([0, 1, 2, ..., 15]).reshape(-1, 1): Create an array for prediction values.
  • y_head = linear_reg.predict(array): Predict salaries based on experience values.
  • plt.scatter(x, y, color="green") and plt.show(): Plot the dataset and display the plot.
  • plt.plot(array, y_head, color="red"): Plot the regression line.

Usage

In the code, make sure to adjust the dataset filename if needed:

df = pd.read_csv("your_dataset.csv", sep=";")
You can customize the script further by changing the parameters, such as experience values in the 'array' variable.

Contributing

Contributions and improvements are welcome. Feel free to submit pull requests or open issues to enhance this project.

License

You can embed this README into your GitHub repository by adding it as a README.md file in your project's root directory. This detailed README will help users understand and use your code effectively.

About

This Python code represents a machine learning project that builds a simple linear regression model using experience and salary data. It plots the data, constructs the regression model, and visualizes the results.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages