# Machine Learning: Iris Flower Classification using KNN

Project: Iris Flower Classification using KNN(K-nearest neighbors)

Iris flower classification is a classic machine-learning problem that involves categorizing iris flowers into different species based on their features. The Iris dataset is a popular dataset for practicing and learning machine-learning classification algorithms.

The Algorithm we are using is Knn K-Nearest Neighbors (K-NN) is a simple and intuitive supervised machine learning algorithm used for classification and regression tasks. It is particularly popular in cases where data is not linearly separable, and it falls under the category of instance-based or lazy learning algorithms.

In K-NN, predictions are made based on the majority class (for classification) or the average value (for regression) of the k-nearest data points to the query point in the feature space. K is a hyperparameter representing the number of nearest neighbors to consider when making a prediction. Choosing an appropriate K value is crucial, as it can greatly affect the algorithm's performance. A smaller K can lead to noisy predictions, while a larger K can smooth out predictions but might miss important local patterns.

**Let's code it:**

First, we need to import the necessary libraries:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694138871763/38a6c5b8-67b7-4a2c-8caa-a19ce853aadc.png align="center")

Now, we need to read the dataset, by using the pandas command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694138968298/40cb93c1-4061-4da8-9dd4-5e19d53c92f1.png align="center")

**Exploratory Data Analysis (EDA)** is a crucial step in the credit score classification process as it helps in understanding the dataset, identifying patterns, and gaining insights that can influence feature selection, model choice, and preprocessing techniques.

Now let’s have a look at the first five rows of this dataset:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140500321/3091b692-3f50-406a-9a3c-d685ba96f29a.png align="center")

Then last five rows of this dataset:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140522929/84f692f4-1032-419d-a701-03ab3859f80c.png align="center")

data.info() method will be used to obtain information about a data frame or data structure.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140542386/4ee3a314-023e-4f8a-8bf5-b73ebf33f5d4.png align="center")

shape attribute is used to determine the dimensions of a data structure in Python, typically with data manipulation libraries like NumPy or Pandas

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140556338/75a70208-7e12-4d9c-8ffc-fc043e0f68e1.png align="center")

Describe a method in Pandas that generates summary statistics for each numeric column (or series) in a data frame. It provides valuable insights into the central tendencies, dispersion, and distribution of the data.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140570333/510f646a-84ea-4468-9e51-e3cd4568e0fd.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140606585/4cb6a074-6ceb-4935-b094-d34b84c3f346.png align="center")

Now, we are plotting the sepal\_width and sepal \_length.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140661967/9ae88df8-715c-400d-aec9-5cca2454bb78.png align="center")

Then dividing it into the x and y working with machine learning algorithms, it's common practice to divide your dataset into two parts: one for the input features (often denoted as X) and another for the target variable or labels (often denoted as y)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140689508/2f68920d-8378-463f-8051-28466b43d2e0.png align="center")

The `train_test_split` method is a fundamental technique in machine learning for dividing a dataset into two subsets: a training set and a testing set. This process is crucial for Model evaluation, Avoiding overfitting, Hyperparameter tuning, and Model selection.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140736777/c2065063-0aff-41ab-b589-d278fb8fd8f0.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140793455/1f6c58f2-63a8-4621-b9f2-82a4e629ef03.png align="center")

Now, for Model selection for the model we are taking up the KNeighborsClassifier (KNN).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140811228/3bbeff68-ab0f-4fc4-a17f-40de8fa57812.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140827813/3a215d85-40f0-43dd-b4d2-e9b875a278ed.png align="center")

In machine learning, the `.fit()` method is used to train a model on a given dataset. Specifically, when you call [`knn.fit`](http://knn.fit)`(X_train, y_train)` For a k-nearest neighbors (K-NN) classifier (or regressor), you are instructing the K-NN model to learn from the provided training data.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140847950/69020646-c5fa-4c62-9f21-3b3d28e6012d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140871387/e2bc86fc-b41a-459f-98ae-e6c56e841e3f.png align="center")

The `knn. predict()` method is used in k-nearest neighbors (K-NN) algorithms to make predictions on new, unseen data points based on the patterns learned during the training phase.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694140889688/c1e9c2d2-1d8a-4cb2-b1a6-ae43ddb753eb.png align="center")

Thank you,

Mohammed Muqafamuddin.
