GithubHelp home page GithubHelp logo

tcp-knn's Introduction

TCP-KNN

A third part of an ongoing project about vector classification.

Description

In this part we were asked to implement a TCP clientSocket connection between a client and a server, that will run in an endless loop and will run our verctor classification algorithm which was implemented in the previeus part.

image

The classification algorithm is implemented with the KNN algorithm, which is among the most common of machine learning algorithms and is used in a variety of fields. The algorithm works as follows:

  • Calculation of the distance between the input vector and all classified vectors.
  • Finding K nearest neighbors.
  • Classify the input vector according to the classification of the majority of neighbors. (using the distance metrics we implemented in the previous part of the assignment)

image

Because this project is a continious one, our implementation derived from a wide prespective that Lacks of dependence on implementation, in order to expand the project in the future and leverage it.

We used encapsulation for the distance classes and additionally made distance abstract. Thanks to this, our KNN calculation function that received an object of type Distance could use any of the distance functions independently of them. In addition, since in the future we may want to change the internal implementation of the vector classification or add additional attributes to it, we implemented the relationship between the client and the server in such a way that does not create a dependency between the implementation of the classification algorithm and the communication process between them.

While planning the task and out of an ambition to improve the running time, we noticed that we don't need to sort the entire database, but it is enough for us to find the K members whose distance is the smallest from the newly inserted vector, and therefore we chose to use the Select algorithm which has an average time complexity of O(n).

Course of the program

The client sends the information received from the user to the server. The information is received as follows: "vector distance k"

  • Vector - a new vector to classify.
  • Distance - the distance metric we want to use.
  • K - the number of neighbors to acount in the calculation.

The server classified the resulting vector with the KNN algorithm and according to the received metric distance and K (neighbors), and then sends the classification back to the client.

The client prints the received answer to the screen on a separate line and receives the next input from the user.

The client is connected to the same port during the entire program run, but when the client sends "-1" the client will close the socket, on the other hand, the server will continue to listen to the next client.

Dealing with edge cases

  • If the user entered an input not in the order discussed above, the server will return an invalid input message and request a new input.
  • In cases where a vector entered by the user does not contain numbers or alternatively is not the appropriate size for the vectors in the database, the server will return an invalid input message and request a new input.
  • If the customer enters a distance function corresponding to one of the five distance functions, the server will return an invalid input message and request a new input.
  • If the k inserted into the arguments is less than or equal to zero, we will exit the program. Or alternatively if k is greater than the number of vectors in the database, we will turn k into the number of vectors to prevent illegal access.
  • In cases where the client was unable to connect to the server's port or IP, we will exit directly from the client's program.
  • If the server fails to connect to any port, we will exit the program.
  • If the client sends -1 as input to the server, the server will close the communication with the client and wait for the next user, at this time the client will close its socket and end the program.

Distances

Installing And Executing

To clone and run this application, you'll need Git installed on your computer.

From your command line:

# Clone this repository.
$ git clone https://github.com/tomerp1812/TCP-KNN

# Go into the repository.
$ cd TCP-KNN

# Compile using makefile.
$ make
# Run the program on Linux:
$ ./server.out "path to a classified file" port
$ ./client.out ip port
# Run the program on Windows:
$ server.out "path to a classified file" port
$ client.out ip port

Authors

tcp-knn's People

Contributors

tomerp1812 avatar dansaada avatar

Stargazers

 avatar

Watchers

 avatar

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.