logo Bg
group

Post

Your First Machine Learning Project In Python Step-By-Step

Your First Machine Learning Project In Python Step-By-Step

Great! Here is a step-by-step guide for your first machine learning project in Python:

 

Step 1: Install Required Libraries

Before we start, we need to install the required libraries. You can use the following command to install the libraries:

 

pip install numpy pandas matplotlib seaborn scikit-learn

 

Step 2: Import Required Libraries and Load the Data

Now, let's import the required libraries and load the data

 

For this project, we will use the famous Iris dataset, which is included in scikit-learn.

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris

iris = load_iris()
data = pd.DataFrame(data= np.c_[iris['data'], iris['target']], columns= iris['feature_names'] + ['target'])
 

Step 3: Explore the Data

It's always a good idea to explore the data before building a machine learning model. Let's visualize the data using various plots and charts.

 

sns.pairplot(data, hue='target')
plt.show()

 

Step 4: Prepare the Data

Before we can train our machine learning model, we need to prepare the data. This includes splitting the data into training and testing sets and scaling the features.

 

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
 

Step 5: Train and Test the Model

Now, we can train and test our machine learning model. For this project, we will use the k-nearest neighbors (KNN) algorithm.

 

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)

y_pred = knn.predict(X_test)
 

Step 6: Evaluate the Model

Finally, we can evaluate the performance of our machine learning model using various performance metrics such as accuracy, precision, recall, and F1 score.

 

from sklearn.metrics import accuracy_score, classification_report

print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
 

That's it! You have successfully completed your first machine learning project in Python. This is just the beginning, and there's a lot more to learn and explore in the world of machine learning.