0% found this document useful (0 votes)
10 views3 pages

IOT DA 21bee0309

The document discusses performing linear regression and K-nearest neighbors (KNN) machine learning models using Python code. It describes the algorithms, provides code examples for implementing them, and shows the output and results of running the models on randomly generated data.

Uploaded by

Sparsh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

IOT DA 21bee0309

The document discusses performing linear regression and K-nearest neighbors (KNN) machine learning models using Python code. It describes the algorithms, provides code examples for implementing them, and shows the output and results of running the models on randomly generated data.

Uploaded by

Sparsh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sparsh Singh

21BEE0309

Date:16/03/24

Machine learning models-Linear regression and KNN models

Aim:
To perform Linear regression and KNN using Machine Learning

Description about the experiment:

Linear regression is a supervised learning algorithm used for predicting continuous values by
establishing a linear relationship between input features and the target variable. It aims to
minimize the difference between actual and predicted values by fitting a line to the training data.
In contrast, K-Nearest Neighbors (KNN) is a non-parametric algorithm used for both classification
and regression tasks. KNN determines the output by identifying the majority class among the
knearest neighbors (for classification) or computing the average of the target values of those
neighbors (for regression). Unlike linear regression, KNN does not require an explicit training
phase and stores the entire training dataset for predictions. The choice between linear regression
and KNN depends on factors such as the nature of the data, the problem domain, and the
tradeoff between model complexity and interpretability.

Python code:

FOR LINEAR REGRESSION


import numpy as np from sklearn.linear_model
import LinearRegression from
sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt np.random.seed(42)
x=2*np.random.rand(100,1)
y=4+3*x+np.random.randn(100,1)

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)
model=LinearRegression() model.fit(x_train,y_train)
y_pred=model.predict(x_test)

plt.scatter(x_train,y_train,color='blue',label='training data')
plt.scatter(x_test,y_test,color='red',label='test data')
plt.plot(x_test,y_pred,color='black',linewidth=3,label='reg')
plt.xlabel("x") plt.ylabel("y") plt.title('regression')
plt.legend() plt.show()

FOR KNN
import matplotlib.pyplot as plt from sklearn.neighbors
import KNeighborsClassifier x = [4,3,2,5,6,7,8,1,2,9] y=
[11,12,13,31,32,21,22,23,14,15] classes =
[0,1,0,1,0,1,1,1,0,0] data = list(zip(x,y))
knn=KNeighborsClassifier(n_neighbors=1)
knn.fit(data,classes) new_x=9 new_y=21 new_point=[(new_x,
new_y)] prediction=knn.predict(new_point)
plt.scatter(x+[new_x], y+[new_y], c=classes+[prediction[0]])
plt.show()

Steps involved and output (snapshots):

1. Install PyCharm: If you haven't already installed PyCharm, you can download and install it
from the official JetBrains website: [Download
PyCharm](https://www.jetbrains.com/pycharm/download/).

2. Create a New Project: Open PyCharm and create a new Python project. Give your project a
name and specify the location where you want to save it.

3. Create a Python File: Inside your project, create a new Python file (.py) where you'll write
your code.

4. Generate Random Data: Use libraries like NumPy or scikit-learn to generate random data
for your machine learning task. For example, you can use NumPy to generate random numbers or
scikit-learn to create synthetic datasets.

5. Perform Machine Learning: Write code to perform machine learning tasks such as Linear
Regression or KNN using the generated random data. You can use scikit-learn, one of the most
popular machine learning libraries in Python, to implement these algorithms.

6. Run and Test: Once you've written your code, you can run it within PyCharm to see the
results. PyCharm provides a convenient interface for writing, running, and debugging Python code.
Result and Inference:

The linear regression model captures the underlying trend in the data, indicating a positive
relationship between the input feature and the target variable. On the other hand, K-Nearest
Neighbors (KNN) focuses on the similarity of data points to make predictions. Evaluating both
models' performance provides insights into their effectiveness in capturing patterns and making
accurate predictions based on the given dataset.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy