IOT DA 21bee0309
IOT DA 21bee0309
21BEE0309
Date:16/03/24
Aim:
To perform Linear regression and KNN using Machine Learning
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:
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()
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.