How can I use SVM regression to predict power production
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi!
I have an SVM program for predicting a day ahead power production of a solar power plant. My inputs are solar irradiance, mean temperature, Pressure and the previous day power generated. i used to 2 years data for the site (2014 & 2015) to train the model. However, even while training the model, the errors appear to be very high using both linear and kernel functions. another problem I have is that when I plot the graph, all the actual values turn zero. Attached are the screenshots of the rmse and how the graph looks. What can I do?
My code is
% Initailization
clear all;clc;
% Normalization
data= csvread('traindaily 14-15.csv');
% Input -> x, Output -> y
x=data(:,2:end-1);
y=data(:,end);
% Number of data points
N= length(data);
alpha=zeros(N,1);
% Tolerence value
norm1=10e2; tol=0.2;
% Maximum number of iterations
itr=0; itrmax=10e2;
eps=0.1;
%% linear kernel
for i = 1:N
for j = 1:N
kernel = (x(i))' .* x(i);
end
end
%% algorithm
while (norm1>tol && itr < itrmax)
alpha_ = alpha;
alpha = alpha;
for i = 1: N
alpha(i)=alpha(i) + y(i) -eps*sign(alpha(i))*kernel;
if alpha_(i)*alpha(i)<0
alpha(i)=0;
end
end
norm1=norm(alpha_-alpha);
itr=itr+1;
end
fprintf('Total number of iteration = %d',itr)
%% weight and bias
% Weights
w=sum(alpha.*x)
% Bias
b=mean(y-(w*x')' -eps*ones(N,1))
%% predicted values
% Predicted values
for j=1:N
fx1(j,:)=alpha(j)*kernel;
end
fx=sum(fx1)';
disp('[Actual Values Predicted Values]')
disp([y(1:10) ,fx1(1:10)])
% Mean Square error
rmse =sqrt(norm(y-fx1)^2/N)
%% visualization
% Plotting
figure
scatter(data(:,1),y)
hold on
scatter(data(:,1),fx1,'*')
hold off
xlabel({'X_1'});
ylabel({'X_2'});
legend1 = legend('Actual Values','Predicted Values');
0 Kommentare
Antworten (1)
Jaimin
am 9 Jan. 2025
Hi @Aisha Sa'ad
The provided SVM implementation has issues that may contribute to high errors and plotting problems. The kernel calculation seems incorrect; it is expected to involve both data points for a linear kernel. Additionally, the update for the alpha values is not properly implemented, as it typically involves the gradient of the loss function. It is advisable to use MATLAB's built-in “fitcsvm” function, which efficiently handles these complexities and optimizes the training process.
Kindly refer to a following code snippet for understanding.
% Example SVM Regression Script for Solar Power Prediction
numDays = 100;
rng(0); % For reproducibility
% Generate random features: [Solar Irradiance, Mean Temperature, Pressure, Previous Day Power]
solarIrradiance = rand(numDays, 1) * 1000; % in W/m^2
meanTemperature = rand(numDays, 1) * 35; % in °C
pressure = rand(numDays, 1) * 10 + 1000; % in hPa
previousDayPower = rand(numDays, 1) * 100; % in kWh
% Generate target variable: Power Output
powerOutput = 0.5 * solarIrradiance + 0.3 * meanTemperature + ...
0.1 * pressure + 0.6 * previousDayPower + randn(numDays, 1) * 10;
% Combine features into a matrix
X = [solarIrradiance, meanTemperature, pressure, previousDayPower];
y = powerOutput;
% Split data into training and testing sets
trainRatio = 0.8;
numTrain = floor(trainRatio * numDays);
X_train = X(1:numTrain, :);
y_train = y(1:numTrain);
X_test = X(numTrain+1:end, :);
y_test = y(numTrain+1:end);
% Train SVM regression model
svmModel = fitrsvm(X_train, y_train, 'KernelFunction', 'linear');
% Predict on test data
predictedValues = predict(svmModel, X_test);
% Calculate RMSE for test data
rmse = sqrt(mean((y_test - predictedValues).^2));
fprintf('Test RMSE: %.2f\n', rmse);
figure;
scatter(1:numTrain, y_train, 'b', 'DisplayName', 'Training Data');
hold on;
scatter(numTrain+1:numDays, y_test, 'g', 'DisplayName', 'Test Data');
scatter(numTrain+1:numDays, predictedValues, 'r*', 'DisplayName', 'Predicted Values');
hold off;
xlabel('Day');
ylabel('Power Output (kWh)');
legend('show');
title('Actual vs Predicted Power Output');
For more information kindly refer to the following MathWorks documentation.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!