1
# Loading the dataset
iris_df = pd.read_csv('/content/drive/MyDrive/A2. Online Courses/X_Projects/Iris Dataset/Iris data.csv')

# Renaming the column headers
iris_df.columns=['sepal length', 'sepal width', 'petal length', 'petal width', 'class']

# Converting the string values to categorical columns

# 1. Creating dummy classes
iris_class = pd.get_dummies(iris_df['class'])

# 2. Dropping the original columns
iris_df.drop('class', axis = 1, inplace = True)

# 3. Concatenating the new categorical columns
iris_df = pd.concat([iris_df, iris_class], axis = 1)

iris_df.columns

# Assigning the X and y

X = iris_df.iloc[:, 0:3]
y = iris_df.iloc[:, 4:]

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation

# Number of features: 4       # Number of classes: 3

# Creating model
model = Sequential()
model.add(Dense(units = 8, activation = 'relu'))
model.add(Dense(units = 6, activation = 'relu'))
model.add(Dense(units = 3, activation='softmax'))

# Compile: For Multi-class Classification problem, using 'categorical crossentropy'
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Fitting the model
model.fit(x = X_train, y = y_train, epochs=600, validation_data=(X_test, y_test), verbose=2)

model_loss = pd.DataFrame(model.history.history)
model_loss[['loss', 'val_loss']].plot()
model_loss[['accuracy', 'val_accuracy']].plot()

predictions = model.predict(X_test)

predictions

array([[9.91973519e-01, 4.85600685e-05, 7.97787122e-03],
       [3.39794671e-04, 7.64947593e-01, 2.34712690e-01],
       [9.31529386e-04, 1.20215327e-01, 8.78853202e-01],
       [2.09086225e-03, 5.55364132e-01, 4.42544997e-01],
       [9.96031940e-01, 5.75195099e-06, 3.96230957e-03],
       [1.07533892e-03, 8.67206931e-01, 1.31717727e-01],
       [6.63461164e-04, 7.90892065e-01, 2.08444476e-01],
       [4.38666949e-03, 1.25589490e-01, 8.70023847e-01],
       [8.25164432e-04, 6.42811000e-01, 3.56363893e-01],
       [9.89306748e-01, 1.14999981e-04, 1.05783129e-02],
       [7.61364761e-04, 8.27928066e-01, 1.71310574e-01],
       [2.32721795e-03, 3.50931257e-01, 6.46741450e-01],
       [9.94626820e-01, 1.44262531e-05, 5.35868248e-03],
       [1.18378259e-03, 1.01181127e-01, 8.97635043e-01],
       [4.44082718e-04, 8.34371805e-01, 1.65184125e-01],
       [3.74634028e-03, 6.96787164e-02, 9.26574945e-01],
       [2.76076910e-03, 3.56818497e-01, 6.40420794e-01],
       [3.20809218e-03, 2.26153702e-01, 7.70638227e-01],
       [2.49455828e-04, 8.05073321e-01, 1.94677263e-01],
       [9.86723840e-01, 2.19085487e-04, 1.30570335e-02],
       [1.73235254e-04, 8.39850068e-01, 1.59976721e-01],
       [9.87945318e-01, 1.64468336e-04, 1.18902512e-02],
       [1.18783256e-03, 7.35450029e-01, 2.63362080e-01],
       [9.89641011e-01, 1.04567785e-04, 1.02544641e-02],
       [9.89132226e-01, 1.20702636e-04, 1.07470136e-02],
       [9.88790512e-01, 1.32409317e-04, 1.10771339e-02],
       [2.30662851e-03, 1.29420295e-01, 8.68273139e-01],
       [9.79509437e-04, 6.96065247e-01, 3.02955240e-01],
       [1.01504195e-03, 1.30220637e-01, 8.68764341e-01],
       [2.33234209e-03, 2.27807447e-01, 7.69860208e-01]], dtype=float32)

我正在使用 IRIS 数据集:https ://archive.ics.uci.edu/ml/datasets/Iris使用神经网络解决多类分类问题。上面是它的代码。

问题:为什么我从数据集中得到一个多标签输出,即使在使用“softmax”之后,我应该得到一个单标签多类分类

4

0 回答 0