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.
Make sure you have the following libraries installed:
- Clone this repository to your local machine.
- Ensure you have the required libraries installed.
- Place the dataset file, "linear_regression_dataset.csv," in the project directory.
import pandas as pd
andimport 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")
andplt.show()
: Plot the dataset and display the plot.plt.plot(array, y_head, color="red")
: Plot the regression line.
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.
Contributions and improvements are welcome. Feel free to submit pull requests or open issues to enhance this project.
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.