Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

python machine learning sample program

Exploring Machine Learning with the Iris Dataset Welcome to our journey into the world of machine learning with Python. Today, we'll explore a simple yet powerful example using the Iris dataset, a classic dataset in the machine learning community. The Iris dataset consists of measurements of various characteristics of Iris flowers, such as sepal length, sepal width, petal length, and petal width. Our goal is to build a machine learning model that can classify the species of Iris flowers based on these measurements. First, we load the Iris dataset using the scikit-learn library, a popular machine learning library in Python. With just a few lines of code, we have access to this rich dataset. Next, we split the dataset into training and testing sets. This step ensures that we have separate data for training our model and evaluating its performance. To ensure that our model performs well, we standardize the features using a technique called feature scaling. This step helps to normalize the data and improve the performance of our machine learning algorithm Now comes the exciting part. We initialize a Logistic Regression model, a simple yet effective algorithm for classification tasks. With scikit-learn, building and training a machine learning model is as simple as a single line of code. After training our model, we evaluate its performance on the testing set. By comparing the model's predictions to the actual labels, we can calculate the accuracy of our model. And there we have it! Our machine learning model has successfully learned to classify Iris flowers with an impressive accuracy. This example demonstrates the power of Python and scikit-learn in making complex machine learning tasks accessible to everyone.


```python

# Import necessary libraries

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score


# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target


# Split the dataset into training and testing sets

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


# Standardize the features

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)


# Initialize the Logistic Regression model

model = LogisticRegression()


# Train the model

model.fit(X_train_scaled, y_train)


# Make predictions on the test set

y_pred = model.predict(X_test_scaled)


# Calculate the accuracy of the model

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

```


In this program:

1. We import necessary libraries including scikit-learn modules for dataset loading, data preprocessing, model building, and performance evaluation.
2. We load the Iris dataset using `load_iris()` function from scikit-learn.
3. We split the dataset into training and testing sets using `train_test_split()` function.
4. We standardize the features using `StandardScaler()` to ensure that all features have the same scale.
5. We initialize a Logistic Regression model using `LogisticRegression()` constructor.
6. We train the model using the training data with `fit()` method.
7. We make predictions on the test set using `predict()` method.
8. We calculate the accuracy of the model using `accuracy_score()` function.

This program demonstrates a basic machine learning workflow for classification tasks using scikit-learn. You can further extend this program by exploring different machine learning algorithms, tuning hyperparameters, and evaluating model performance using cross-validation techniques.

caa May 16 2024 71 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?