Accord.NET: Error - There are no samples for class label 0

66 Views Asked by At

I am working on a machine learning project using Accord.NET library for classification. I have encountered an error message that says, "There are no samples for class label 0. Please make sure that class labels are contiguous and there is at least one training sample for each label."

I have a CSV file data.csv containing CPU usage, memory usage, disk usage, and performance level data. Here is a sample of the data:

CPU Usage,Memory Usage,Disk Usage,Performance Level
50,70,80,0
30,40,60,1
70,90,50,2

I am using this data to train a multiclass support vector machine (SVM) model. However, when I try to train the model using the following line of code:

machine = teacher.Learn(trainInputs, trainLabels);

I receive the mentioned error. I have double-checked my data file, and it appears to have samples for all class labels, starting from 0 and with consecutive integers.

Full code: xaml.cs:

using Accord.MachineLearning;
using Accord.MachineLearning.VectorMachines;
using System;
using System.IO;
using System.Linq;
using System.Windows;
using Accord.IO;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Statistics.Analysis;
using Accord.Statistics.Kernels;

namespace Ai_Training
{
    public class PCPerformanceData
    {
        public double[] Features { get; set; }
        public double PerformanceLevel { get; set; }
    }

    public class PCPerformancePrediction
    {
        public double PredictedPerformanceLevel { get; set; }
    }

    public partial class MainWindow : Window
    {
        private MulticlassSupportVectorMachine<Gaussian> machine;
        private string modelPath;

        private double MaxCpuUsage = 100.0; // Maximum allowed CPU usage
        private double MaxMemoryUsage = 100.0; // Maximum allowed memory usage

        public MainWindow()
        {
            InitializeComponent();
            modelPath = Path.Combine(Environment.CurrentDirectory, "model.txt");
        }

        private void TrainModelButton_Click(object sender, RoutedEventArgs e)
        {
            var dataPath = Path.Combine(Environment.CurrentDirectory, "data.csv");
            var data = ReadData(dataPath);

            var (trainInputs, trainLabels, testInputs, testLabels) = SplitData(data, testFraction: 0.2);

            // Train the model
            TrainModel(trainInputs, trainLabels);

            // Evaluate the model
            var accuracy = EvaluateModel(testInputs, testLabels);

            // Save the model
            SaveModel();

            MessageBox.Show($"Model trained and saved to {modelPath}\nAccuracy: {accuracy}");
        }

        private PCPerformanceData[] ReadData(string dataPath)
        {
            try
            {
                var lines = File.ReadAllLines(dataPath);

                if (lines == null || lines.Length == 0)
                {
                    // Handle the case when the file is empty or contains no lines
                    MessageBox.Show("The data file is empty or contains no lines.");
                    return new PCPerformanceData[0]; // Return an empty array or null value depending on your requirements
                }

                var data = lines.Skip(1).Select(line =>
                {
                    var values = line.Split(',');
                    return new PCPerformanceData
                    {
                        Features = new double[] { double.Parse(values[0]), double.Parse(values[1]), double.Parse(values[2]) },
                        PerformanceLevel = double.Parse(values[3])
                    };
                }).ToArray();

                return data;
            }
            catch (Exception ex)
            {
                // Handle the exception or log the error
                MessageBox.Show($"Error occurred while reading data from file: {ex.Message}");
                return new PCPerformanceData[0]; // Return an empty array or null value depending on your requirements
            }
        }


        private (double[][], int[], double[][], int[]) SplitData(PCPerformanceData[] data, double testFraction)
        {
            var totalDataPoints = data.Length;
            var numTestPoints = (int)(totalDataPoints * testFraction);
            var numTrainPoints = totalDataPoints - numTestPoints;

            var trainInputs = new double[numTrainPoints][];
            var trainLabels = new int[numTrainPoints];
            var testInputs = new double[numTestPoints][];
            var testLabels = new int[numTestPoints];

            var random = new Random();
            var shuffledData = data.OrderBy(_ => random.Next()).ToArray();

            for (var i = 0; i < numTrainPoints; i++)
            {
                trainInputs[i] = shuffledData[i].Features;
                trainLabels[i] = (int)shuffledData[i].PerformanceLevel;
            }

            for (var i = 0; i < numTestPoints; i++)
            {
                testInputs[i] = shuffledData[i + numTrainPoints].Features;
                testLabels[i] = (int)shuffledData[i + numTrainPoints].PerformanceLevel;
            }

            return (trainInputs, trainLabels, testInputs, testLabels);
        }


        private void TrainModel(double[][] trainInputs, int[] trainLabels)
        {
            var teacher = new MulticlassSupportVectorLearning<Gaussian>()
            {
                Learner = (param) => new SequentialMinimalOptimization<Gaussian>()
                {
                    Complexity = 100 // Adjust the complexity parameter as needed
                }
            };

            machine = teacher.Learn(trainInputs, trainLabels);

            // Add this line to assign the trained model to the machine variable
            machine = teacher.Learn(trainInputs, trainLabels);
        }


        private double EvaluateModel(double[][] testInputs, int[] testLabels)
        {
            var predicted = machine.Decide(testInputs);
            var matrix = new GeneralConfusionMatrix(testLabels, predicted);
            var accuracy = matrix.Accuracy;

            return accuracy;
        }

        private void SaveModel()
        {
            Serializer.Save(machine, modelPath);
        }

        private void PredictButton_Click(object sender, RoutedEventArgs e)
        {
            if (!File.Exists(modelPath))
            {
                MessageBox.Show("Model not found. Train the model first.");
                return;
            }

            var cpuUsage = 25.0;
            var memoryUsage = 50.0;

            var inputFeatures = new double[] { cpuUsage, memoryUsage };
            var prediction = PredictPerformanceLevel(inputFeatures);

            MessageBox.Show($"Predicted Performance Level: {prediction.PredictedPerformanceLevel}");

            // Apply the suggested changes
            ApplyChanges(prediction.PredictedPerformanceLevel);
        }

        private PCPerformancePrediction PredictPerformanceLevel(double[] inputFeatures)
        {
            machine = Serializer.Load<MulticlassSupportVectorMachine<Gaussian>>(modelPath);


            var predictedValue = (int)machine.Decide(inputFeatures);

            return new PCPerformancePrediction { PredictedPerformanceLevel = predictedValue };
        }

        private void ApplyChanges(double predictedPerformanceLevel)
        {
            // Apply the suggested changes to the PC here
            // Set the CPU usage to 25%
            // Set the memory usage to 50%
            double actualCpuUsage = 25.0;
            double actualMemoryUsage = 50.0;

            // Validate the actual PC performance after applying the changes
            if (actualCpuUsage > MaxCpuUsage || actualMemoryUsage > MaxMemoryUsage)
            {
                // If the actual performance exceeds the limits, revert the changes
                MessageBox.Show("The actual PC performance level exceeds the maximum allowed limits. Reverting the changes.");
                RevertChanges();
            }
            else
            {
                // Changes applied successfully
                MessageBox.Show("Changes applied successfully.");
            }
        }

        private void RevertChanges()
        {
            // Revert the changes here
            // Set the CPU usage and memory usage back to their original values
            double actualCpuUsage = 25.0;
            double actualMemoryUsage = 99.0;

            MessageBox.Show("Changes reverted. PC performance restored.");
        }
    }
}

xaml:

<Window x:Class="Ai_Training.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PC Performance Prediction" Height="350" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,5">
            <TextBlock Text="CPU Usage:" Margin="0,0,5,0" VerticalAlignment="Center"/>
            <TextBox x:Name="CpuUsageTextBox" Width="50" VerticalAlignment="Center"/>
        </StackPanel>

        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,5">
            <TextBlock Text="Memory Usage:" Margin="0,0,5,0" VerticalAlignment="Center"/>
            <TextBox x:Name="MemoryUsageTextBox" Width="50" VerticalAlignment="Center"/>
        </StackPanel>

        <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,10">
            <TextBlock Text="Disk Usage:" Margin="0,0,5,0" VerticalAlignment="Center"/>
            <TextBox x:Name="DiskUsageTextBox" Width="50" VerticalAlignment="Center"/>
        </StackPanel>

        <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,0">
            <Button Content="Train Model" Margin="5" Click="TrainModelButton_Click"/>
            <Button Content="Predict" Margin="5" Click="PredictButton_Click"/>
        </StackPanel>
    </Grid>
</Window>

Expected outcome: I expect the model training to complete successfully without any errors.

0

There are 0 best solutions below