בינה מאלכותית RB14-06 : זיהוי סוג של זן צמח האירוס ע"י בינה מאלכותית

A.I   Predict Iris flower species (Setosa, Versicolor, Virginica).

Iris is a flowering plant genus of 310 accepted species[1] with showy flowers. As well as being the scientific name, iris is also widely used as a common name for all Iris species, as well as some belonging to other closely related genera. A common name for some species is flags, while the plants of the subgenus Scorpiris are widely known as junos, particularly in horticulture. It is a popular garden flower

 

 


 

Setosa


 

Versicolor


Virginica


 :  Variable table

 Attribute Information:
   1. sepal length in cm
   2. sepal width in cm
   3. petal length in cm
   4. petal width in cm
   5. class:
      — Iris Setosa
      — Iris Versicolour
      — Iris Virginica
for example  :
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
..
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
Summary Statistics:
         Min  Max   Mean    SD   Class Correlation
   sepal length: 4.3  7.9   5.84  0.83    0.7826
    sepal width: 2.0  4.4   3.05  0.43   -0.4194
   petal length: 1.0  6.9   3.76  1.76    0.9490  (high!)
    petal width: 0.1  2.5   1.20  0.76    0.9565  (high!)
Class Distribution: 33.3% for each of 3 classes.


https://archive.ics.uci.edu/dataset/53/iris

What do the instances in this dataset represent?

Each instance is a plant

Additional Information

This is one of the earliest datasets used in the literature on classification methods and widely used in statistics and machine learning. The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is linearly separable from the other 2; the latter are not linearly separable from each other.

 

download the file iris-flower-p

 

 

=== Model Saved ===
Model file: d:\temp\iris_ann_model.h5

Iris Flower Classification with an Artificial Neural Network (ANN)

This program demonstrates how to build a simple Artificial Neural Network (ANN) using TensorFlow and Keras to classify iris flowers based on their features.
The dataset is stored in a CSV file (d:\temp\iris-flower-p.csv) and contains sepal length, sepal width, petal length, petal width, and the class (flower type).


Step 1: Importing the Required Libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow import keras
from tensorflow.keras import layers
  • pandas: to load and work with the CSV file as a table.

  • numpy: to handle numbers and arrays.

  • matplotlib.pyplot: to create graphs.

  • train_test_split: to split the dataset into training and validation sets.

  • LabelEncoder: to convert text labels (flower names) into numbers.

  • StandardScaler: to scale features for better ANN training.

  • keras and layers: to build the ANN itself.


Step 2: Loading the Dataset

file_path = r"d:\temp\iris-flower-p.csv"
df = pd.read_csv(file_path, skiprows=1, header=None)
df.columns = ["sepal length", "sepal width", "petal length", "petal width", "class"]
  • skiprows=1: skips the first row in case it has extra information.

  • header=None: prevents pandas from treating the first row as column names.

  • The columns are then renamed manually for clarity.


Step 3: Dataset Summary

print("Number of records:", len(df))
print("Number of features:", df.shape[1]-1)
print(df["class"].value_counts())
  • len(df): number of rows (records).

  • df.shape[1]-1: number of features, excluding the class column.

  • value_counts(): shows how many examples exist for each flower type.


Step 4: Correlation Matrix

corr = df.drop("class", axis=1).corr()
print(corr)
  • Drops the class column (because it’s text).

  • .corr(): calculates how numeric features are related. For example, petal length and petal width are strongly correlated.


Step 5: Preparing Features and Labels

X = df.drop("class", axis=1).values
y = df["class"].values
  • X: the numeric data (inputs for the model).

  • y: the flower type (target output).


Step 6: Encoding the Labels

encoder = LabelEncoder()
y_encoded = encoder.fit_transform(y)
  • Converts class names into numbers: Setosa → 0, Versicolor → 1, Virginica → 2.


Step 7: Scaling the Features

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
  • Scales all features so they are on a similar range (mean 0, standard deviation 1).

  • This helps the neural network learn faster and more reliably.


Step 8: Flattening the Features

X_flat = X_scaled.reshape(X_scaled.shape[0], -1)
  • Ensures the input data is in the correct shape: one row per flower, one column per feature.

  • Neural networks expect flat (one-dimensional) inputs, not multi-dimensional tables.


Step 9: Splitting into Training and Validation Sets

X_train, X_val, y_train, y_val = train_test_split(
X_flat, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
)
  • test_size=0.2: 20% of the dataset is reserved for validation.

  • random_state=42: ensures the split is always the same when rerunning the code.

  • stratify=y_encoded: makes sure each set has the same class proportions.


Step 10: Building the ANN Model

num_classes = len(np.unique(y_encoded))

model = keras.Sequential([
layers.Input(shape=(X_flat.shape[1],)),
layers.Dense(16, activation="relu"),
layers.Dense(8, activation="relu"),
layers.Dense(num_classes, activation="softmax")
])

  • Input layer: accepts the four features.

  • Dense(16, activation="relu"): first hidden layer with 16 neurons.

  • Dense(8, activation="relu"): second hidden layer with 8 neurons.

  • Dense(num_classes, activation="softmax"): output layer with one neuron per class, using softmax to output probabilities.


Step 11: Compiling the Model

model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
  • optimizer="adam": adjusts weights during training.

  • loss="sparse_categorical_crossentropy": correct loss for multi-class problems with integer labels.

  • metrics=["accuracy"]: tracks accuracy during training.


Step 12: Training the Model

history = model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
epochs=90,
batch_size=8,
verbose=1
)
  • epochs=90: the network sees the entire dataset 90 times.

  • batch_size=8: processes 8 samples at a time before updating weights.

  • validation_data: evaluates performance on unseen validation data.

  • verbose=1: prints training progress.


Step 13: Saving the Model

model_file = r"d:\temp\iris_ann_model.h5"
print("\n=== Model Saved ===")
print(f"Model file: {model_file}\n")
model.save(model_file)
  • Saves the trained model to a file called iris_ann_model.h5.

  • The model can later be reloaded to make predictions without retraining.


Step 14: Visualizing Training History

plt.figure(figsize=(10,4))

plt.subplot(1,2,1)
plt.plot(history.history["loss"], label="Train Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Loss over Epochs")
plt.legend()

plt.subplot(1,2,2)
plt.plot(history.history["accuracy"], label="Train Accuracy")
plt.plot(history.history["val_accuracy"], label="Validation Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("Accuracy over Epochs")
plt.legend()

plt.show()

  • Creates two plots:

    • Loss vs Epochs: shows how the error decreases.

    • Accuracy vs Epochs: shows how prediction accuracy improves.

  • These plots help confirm whether the model is learning effectively.


Key Parameters Recap

  • skiprows=1: skips the first row in the CSV file.

  • train_test_split(test_size=0.2, random_state=42, stratify=y): controls dataset splitting.

  • Dense(units, activation): defines each layer of the ANN.

  • compile(optimizer, loss, metrics): defines how the model learns.

  • fit(epochs, batch_size, validation_data): controls the training process.


This program demonstrates the full workflow of building an ANN:
loading the data, preparing it, building a network, training it, saving it, and finally visualizing the training progress.

Prediction  :

Perfect. Let’s create a prediction script that:

  1. Loads the saved model (iris_ann_model.h5).

  2. Defines a few test flower measurements:

    • [5.1, 3.5, 1.4, 0.2]

    • [6.4, 3.2, 4.5, 1.5]

    • [5.8, 2.7, 5.1, 1.9]

  3. Preprocesses them the same way as during training (StandardScaler).

  4. Predicts class probabilities (percentages).

  5. Prints the predicted class name and confidence %.

 

 

 

 

כתיבת תגובה