How to apply KNN algorithm using tf

This recipe helps you apply KNN algorithm using tf

Recipe Objective

How to apply KNN algorithm using tf?

So KNN means K-nearest neighbors, it is an supervised machine learning algorithm as it predicts the data labels by majority and distance rules. It is very simple algorithm when its predicting the data labels it takes the nearest K labels and by the majority rule the label of the data is being predicted. In our code we are going to take "iris" dataset which is a popular classification dataset and going to perform further operations on it.

Learn to Implement Customer Churn Prediction Using Machine Learning in Python

Step 1 - Import library

import matplotlib.pyplot as plt import numpy as np from sklearn import datasets import tensorflow as tf

Step 2 - Load the dataset

iris_data = datasets.load_iris() x_variable = np.array([x[0:4] for x in iris_data.data]) y_variable = np.array(iris_data.target)

Step 3 - Perform one hot encoding

y_variable = np.eye(len(set(y_variable)))[y_variable]

Step 4 - Normalize the data

x_variable = (x_variable - x_variable.min(0)) / x_variable.ptp(0)

Step 5 - Split the data in train and test

np.random.seed(59) train_data = np.random.choice(len(x_variable), round(len(x_variable) * 0.8), replace=False) test_data =np.array(list(set(range(len(x_variable))) - set(train_data))) x_variable_train = x_variable[train_data] x_variable_test = x_variable[test_data] y_variable_train = y_variable[train_data] y_variable_test = y_variable[test_data]

Step 6 - Define features

features = len(x_variable_train[0]) k = 5 x_new_train = tf.compat.v1.placeholder(shape=[None, features], dtype=tf.float32) y_new_train = tf.compat.v1.placeholder(shape=[None, len(y_variable[0])], dtype=tf.float32) x_new_test = tf.compat.v1.placeholder(shape=[None, features], dtype=tf.float32)

Step 7 - Define manhattan distance and nearest k points

# manhattan distance manht_distance = tf.reduce_sum(tf.abs(tf.subtract(x_new_train, tf.expand_dims(x_new_test, 1))), axis=2) # nearest k points _, top_k_indices = tf.nn.top_k(tf.negative(manht_distance), k=k) top_k_labels = tf.gather(y_new_train, top_k_indices) predictions_sumup = tf.reduce_sum(top_k_labels, axis=1) make_prediction = tf.argmax(predictions_sumup, axis=1)

Step 8 - Training and evaluation

sess = tf.compat.v1.Session() outcome_prediction = sess.run(make_prediction, feed_dict={x_new_train: x_variable_train, x_new_test: x_variable_test, y_new_train: y_variable_train}) accuracy = 0 for pred, actual in zip(outcome_prediction, y_variable_test): if pred == np.argmax(actual): accuracy += 1 print("This is final output:",accuracy / len(outcome_prediction))

This is final output: 0.9666666666666667

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.