Git Product home page Git Product logo

Comments (2)

sudharsansrini avatar sudharsansrini commented on September 27, 2024

KNN - K Nearest Neighbor

KNN algorithm is mainly used to classify and predicting the values in Machine Learning. We can use it for Supervised Machine Learning problems(Labeled) such that Classification and Regression.

KNN
K - constant value
NN - Nearest Neighbor

KNN is simply , Finding the K Nearest Neighbor of the data point and classifying the data points with maximum number of classes of the neighbors

20210831_155414_1

We can find the value of K by finding the mean of the error in each records and plotting as graph
K is the value where the mean error rate is lesser

error =[]
for i in range(1, 40):
    model = KNeighborsClassifier(n_neighbors=i)
    model.fit(x_train, y_train)
    pred_i = model.predict(x_test)
    error.append(np.mean(pred_i != y_test))

plt.figure(figsize=(15, 15))
plt.plot(range(1, 40), error, color = "red", linestyle = "dashed", marker="o", markerfacecolor="blue", markersize=10)
plt.title('Error Rate K value')
plt.xlabel('K value')
plt.ylabel('Mean Error')
plt.show()

Finding K -> Finding how many neighbors that we have to choose around the data point

Figure_1

Value of k = less error rate
After choosing K value,
You may have a question like,
How will it find the k nearest points ?

Distance of the data points will be calculated in KNN using Minkowski distance.

min

Based on Minkowski Distance metric, We gonna classify the data points.
p=1, Manhattan Distance
p=2, Euclidean Distance

Manhattan Distance

1_KDgfdK6SooXtaUvlnXdpaA

This is the distance between real vectors using sum of their absolute difference.

Euclidean Distance

1_9LeaMTcOXxeTPN-VCbKloQ

Euclidean distance is calculated as the square root of sum of the squared differences between a new point (x) & existing point y

Hamming Distance

1_1lXpSrDyY37V4Ke--GkRQQ

It is used for categorical variables. If the value x and the value y are the same, the distance d will be equal to 0. Otherwise distance equal to 1

The above methods are used to find the distance and to finding the k closest neighbor of that particular data point.
With the classes of neighbors, We can classify the data point with maximum neighbor's class

Thank you!

from onestep-electron-content.

AM1CODES avatar AM1CODES commented on September 27, 2024

Hey @sudharsansrini the content that you mentioned above looks really amazing and we could incorporate it in the main platform. I am pretty sure that writing this would have taken a good amount of time and i request you to make a PR for the same because otherwise it won't get counted as your contribution. Feel free to join our discord if you need any help with how to contribute and stuff like that.

from onestep-electron-content.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.