How to Give Inputs to a Machine Learning Model

One of the mistakes data science newbies make while working on a machine learning project is that they train a machine learning model, check its accuracy score, and then complete the project. They do not test the performance of the model by giving inputs to the model based on the features they used to train it. So if you don’t know how to give inputs to a machine learning model, this article is for you. In this article, I’ll walk you through how to give inputs to a machine learning model using Python.

Here’s How to Give Inputs to a Machine Learning Model

Every data science professional should know about giving inputs to a machine learning model. It helps in calculating the predicted value based on the input values. Most data science beginners skip this step while creating a machine learning project.

To give inputs to a machine learning model, you have to create a NumPy array, where you have to input the values of the features you used to train your machine learning model. Then we can use that array in the model.predict() method, and at the end, it will give the predicted value as an output based on the inputs.

Now let’s learn about giving inputs to a machine learning model with an example using Python. Here I will first train a classification model on the famous Iris dataset, and then, in the end, I will input the values of the features (sepal_length, sepal_width, petal_length, petal_width) to the model to see the predicted output based on the inputs:

import pandas as pd
import numpy as np
iris = pd.read_csv("IRIS.csv")
# splitting the dataset
x = iris.drop("species", axis=1)
y = iris["species"]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.2,
random_state=0)
# training the model
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(x_train, y_train)
# giving inputs to the machine learning model
# features = [[sepal_length, sepal_width, petal_length, petal_width]]
features = np.array([[5, 2.9, 1, 0.2]])
# using inputs to predict the output
prediction = knn.predict(features)
print("Prediction: {}".format(prediction))
Prediction: ['Iris-setosa']

So this is how you can give inputs to a model to calculate the predicted value based on the input values.

Summary

I hope you now have understood about giving inputs to a machine learning model based on the features you used to train the model. Every data science professional should know about giving inputs to a machine learning model. It helps in calculating the predicted value based on the input values. I hope you liked this article on giving inputs to a machine learning model using Python. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1207

Leave a Reply