Code Implementation of Simple Linear Regression 🧑‍💻🖥️

  Рет қаралды 37

Nerchukundam raa Bro..🤝

Nerchukundam raa Bro..🤝

Күн бұрын

Simple linear regression:
Simple linear regression aims to find a linear relationship to describe the correlation between an independent and possibly dependent variable.
Importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
`numpy` is used for numerical operations.
`pandas` is used for data manipulation and analysis.
`matplotlib.pyplot` is used for plotting graphs.
`train_test_split` from `sklearn` is used to split the dataset into training and testing sets.
`LinearRegression` from `sklearn` is used to create and train the linear regression model.
Loading the Dataset
data = pd.read_csv('/content/drive/MyDrive/Data sets ml/Salary.csv')
print(data.head())
The dataset is loaded using `pd.read_csv`.
`data.head()` displays the first few rows of the dataset to understand its structure.
Extracting Dependent and Independent Variables
x = data.iloc[:, :-1].values
y = data.iloc[:, 1].values
print(x)
print(y)
`x` contains the independent variable(s) (in this case, years of experience).
`y` contains the dependent variable (salary).
`data.iloc[:, :-1]` selects all columns except the last one for `x`.
`data.iloc[:, 1]` selects the second column for `y`.
Splitting the Dataset into Training and Testing Sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
The dataset is split into training and testing sets.
`test_size=0.2` means 20% of the data is used for testing, and 80% for training.
`random_state=0` ensures reproducibility of the split.
Fitting the Model
regressor = LinearRegression()
regressor.fit(x_train, y_train)
A `LinearRegression` model is created.
The model is trained using the `fit` method on the training data (`x_train` and `y_train`).
Predicting on the Testing Set
y_pred = regressor.predict(x_test)
The trained model predicts salaries (`y_pred`) based on the testing set (`x_test`).
Plotting the Training Data Graph
plt.scatter(x_train, y_train, color="red")
plt.plot(x_train, regressor.predict(x_train), color="green")
plt.title("Salary vs Experience (Training set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
A scatter plot of the training data (`x_train`, `y_train`) is created, with red dots representing actual data points.
A green line represents the predicted salaries based on the training data.
Titles and labels are added for clarity.
Plotting the Testing Data Graph
plt.scatter(x_test, y_test, color="red")
plt.plot(x_train, regressor.predict(x_train), color="green") # Use x_train for the line
plt.title("Salary vs Experience (Testing set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
A scatter plot of the testing data (`x_test`, `y_test`) is created, with red dots representing actual data points.
The green line from the training data is plotted to compare the actual testing data points against the model's predictions.
Titles and labels are added for clarity.
Summary:
1. **Libraries**: Imported necessary libraries.
2. **Data Loading**: Loaded and displayed the dataset.
3. **Variable Extraction**: Extracted independent (`x`) and dependent (`y`) variables.
4. **Data Splitting**: Split the data into training and testing sets.
5. **Model Fitting**: Created and trained a linear regression model.
6. **Prediction**: Predicted salaries using the test set.
7. **Visualization**: Plotted graphs for both training and testing sets to visualize the model's performance.
#ml #machinelearningbasics #machinelearningtutorialforbeginners #linearregression #code #python #machinelearning

Пікірлер: 1
@LR-EduSphere
@LR-EduSphere 2 ай бұрын
Dataset: drive.google.com/file/d/13n-nQF6oSmLucFKZQ8KAYvTseUVbi-qg/view?usp=drivesdk
Noam Chomsky on Israel
7:41
FanCradle Video
Рет қаралды 3,6 М.
Why Vertical LLM Agents Are The New $1 Billion SaaS Opportunities
37:06
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 69 МЛН
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
Двое играют | Наташа и Вова
Рет қаралды 3,4 МЛН
Spongebob ate Michael Jackson 😱 #meme #spongebob #gmod
00:14
Mr. LoLo
Рет қаралды 11 МЛН
Кәсіпқой бокс | Жәнібек Әлімханұлы - Андрей Михайлович
48:57
Ch08 Lect2
1:01:49
Suheir Harb
Рет қаралды 512
Learn Particle Swarm Optimization (PSO) in 20 minutes
19:08
Ali Mirjalili
Рет қаралды 304 М.
Iran: the Octopus of Terror | Naftali Bennett
10:55
Jordan B Peterson
Рет қаралды 25 М.
Maths Olympiad | A Tricky Maths Olympiad Question | Algebra Problems |
8:06
Complete Dockers For Data Science Tutorial In One Shot
1:19:29
Krish Naik
Рет қаралды 123 М.
Data Modeling for Power BI [Full Course] 📊
2:34:41
Pragmatic Works
Рет қаралды 3,3 МЛН
Huffman codes
29:05
Design and Analysis of Algorithms
Рет қаралды 14 М.
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 69 МЛН