Simple python program to learn AI and ML for students
Here's a simple Python program to help you get started with basic concepts in artificial intelligence (AI) and machine learning (ML). This program demonstrates how to build a basic machine learning model using the popular scikit-learn library.
We'll go through a classic ML task: training a model to classify the famous Iris dataset.
### Step-by-Step Guide
1. **Install Required Libraries**:
First, ensure you have the necessary libraries installed. You can install them using pip:
```sh
pip install numpy pandas scikit-learn matplotlib
```
2. **Load and Explore the Data**:
The Iris dataset is included in scikit-learn, so we'll load it directly from the library.
3. **Preprocess the Data**:
Prepare the data for training by splitting it into training and testing sets.
4. **Train a Model**:
We'll use a simple classifier, such as the k-nearest neighbors (KNN) algorithm.
5. **Evaluate the Model**:
Check the model's performance using the testing set.
6. **Visualize the Results**:
Use matplotlib to plot some results.
### Full Python Program
```python
# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Create a DataFrame for better visualization
df = pd.DataFrame(X, columns=iris.feature_names)
df['target'] = y
df['target_names'] = df['target'].apply(lambda x: iris.target_names[x])
# Explore the data
print(df.head())
# Split the data 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)
# Initialize and train the KNN classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
# Make predictions
y_pred = knn.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")
# Confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
# Plot the confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=iris.target_names, yticklabels=iris.target_names)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
```
### Explanation
1. **Import Libraries**:
- We import the necessary libraries including `numpy`, `pandas`, `scikit-learn`, and `matplotlib`.
2. **Load the Data**:
- The Iris dataset is loaded using `load_iris()` from `scikit-learn`.
3. **Explore the Data**:
- A DataFrame is created for better visualization, and the first few rows are printed.
4. **Preprocess the Data**:
- The data is split into training and testing sets using `train_test_split()`.
5. **Train the Model**:
- A KNN classifier is initialized and trained on the training data.
6. **Evaluate the Model**:
- Predictions are made on the test data, and the accuracy is calculated.
- A confusion matrix is generated to visualize the performance of the classifier.
7. **Visualize the Results**:
- The confusion matrix is plotted using `seaborn` and `matplotlib`.
### Running the Program
Copy and paste the code into a Python script or a Jupyter Notebook. Run the script to see the output, which includes the accuracy of the model and a confusion matrix plot.
This program gives you a basic introduction to loading data, preprocessing it, training a model, making predictions, evaluating the model, and visualizing the results. From here, you can explore more complex models, different datasets, and advanced techniques in AI and ML.
No Comments have been Posted.