Preprocessing, classification, Confusion Matrix

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Lisa4ka9
User
Beiträge: 1
Registriert: Sonntag 4. Dezember 2022, 02:15

Guten Tag zusammen,

Ich bin Anfänger im Programmieren. Habe eine Frage zu Erstellung einer Confusion Matrix, einem mat daten set.

Hier ist mein code. Unten befindet sich der code für die Confusion Matrix, irgendwie kommt da nur eine Leere Matrix raus ohne zahlen von dem date set.

Wie kann ich den code verändern damit der Daten set in einer Confusion matrix angezeigt wird?

Ich danke im Voraus!

import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
from scipy.io import loadmat

# Load the Indian Pines dataset
data = loadmat('Indian_pines.mat')['indian_pines_corrected']
gt = loadmat('Indian_pines_gt.mat')['indian_pines_gt']

# Reshape the data to have a shape of (21025, 200)
data_reshaped = data.reshape(-1, data.shape[-1])

# Normalize the data using MinMaxScaler
scaler = MinMaxScaler()
normalized_data = scaler.fit_transform(data_reshaped)

# Apply PCA to reduce dimensionality
n_components = 30
pca = PCA(n_components=n_components)
pca_data = pca.fit_transform(normalized_data).reshape(data.shape[0], data.shape[1], n_components)

# Extract the labeled pixels and corresponding labels
labeled_pixels = np.where(gt > 0)
labels = gt[labeled_pixels]

# Create feature vectors for the labeled pixels
features = []
for i in range(len(labeled_pixels[0])):
x, y = labeled_pixels[0], labeled_pixels[1]
feature_vector = pca_data[x, y, :]
features.append(feature_vector)
features = np.array(features)

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.3, random_state=42)

# Train a random forest classifier
n_estimators = 100
max_depth = 20
clf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
clf.fit(X_train, y_train)

# Predict on the test set and evaluate accuracy
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)

import matplotlib.pyplot as plt

# Visualize the ground truth labels
plt.figure(figsize=(8, 8))
plt.imshow(gt)
plt.title('Ground Truth Labels')
plt.colorbar()
plt.show()

# Visualize the predicted labels
pred_labels = np.zeros_like(gt)
for i in range(len(labeled_pixels[0])):
x, y = labeled_pixels[0], labeled_pixels[1]
feature_vector = pca_data[x, y, :]
pred_label = clf.predict([feature_vector])[0]
pred_labels[x, y] = pred_label
plt.figure(figsize=(8, 8))
plt.imshow(pred_labels)
plt.title('Predicted Labels')
plt.colorbar()
plt.show()

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.io import loadmat

# Load the Indian Pines ground truth data
data = loadmat('Indian_pines.mat')['indian_pines_corrected']
gt = loadmat('Indian_pines_gt.mat')['indian_pines_gt']

# Load the predicted labels from a classifier
pred_labels = np.zeros_like(gt)

# Define class labels and colormap
class_labels = np.arange(17)
cmap = sns.color_palette('viridis', as_cmap=True)

# Create confusion matrix by counting class occurrences
conf_mat = np.zeros((17, 17))
for i in range(17):
for j in range(17):
conf_mat[i, j] = np.sum(np.logical_and(gt == i+1, pred_labels == j))

# Plot the heatmap
ax = sns.heatmap(conf_mat, cmap=cmap, xticklabels=class_labels, yticklabels=class_labels, annot=True, fmt='.0f')

# Set the axis labels and title
ax.set_xlabel('Predicted class')
ax.set_ylabel('True class')
plt.title('Confusion Matrix')

# Show the plot
plt.show()
Antworten