Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions NN2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
clear all
clc
close all
% Generate synthetic data for male and female
numPoints = 100; % Number of points per class

% Generate random data for males (e.g., height, weight)
maleHeight = randn(numPoints, 1) * 5 + 175; % Mean height around 175 cm
maleWeight = randn(numPoints, 1) * 10 + 75; % Mean weight around 75 kg
maleData = [maleHeight, maleWeight];

% Generate random data for females (e.g., height, weight)
femaleHeight = randn(numPoints, 1) * 5 + 165; % Mean height around 165 cm
femaleWeight = randn(numPoints, 1) * 10 + 65; % Mean weight around 65 kg
femaleData = [femaleHeight, femaleWeight];

% Combine data into input and target matrices
inputs = [maleData; femaleData]'; % Transpose to get features in columns
targets = [zeros(1, numPoints), ones(1, numPoints)]; % 0 for male, 1 for female

% Create and configure the feedforward neural network
hiddenLayerSize = 10; % Number of neurons in the hidden layer
net = feedforwardnet(hiddenLayerSize);

% Train the network
net = train(net, inputs, targets);

% Test the network
outputs = net(inputs);
predictions = outputs > 0.5; % Convert probabilities to binary predictions

% Calculate accuracy
accuracy = sum(predictions == targets) / length(targets) * 100;

% Display accuracy
fprintf('Accuracy: %.2f%%\n', accuracy);

% Visualize the results
figure;
hold on;
scatter(maleData(:,1), maleData(:,2), 'b', 'filled'); % Male points
scatter(femaleData(:,1), femaleData(:,2), 'r', 'filled'); % Female points

% Create a grid for visualizing the decision boundary
[x1Grid, x2Grid] = meshgrid(140:1:200, 40:1:100); % Grid for height and weight
gridInputs = [x1Grid(:)'; x2Grid(:)'];
gridOutputs = net(gridInputs);
decisionBoundary = reshape(gridOutputs, size(x1Grid));

% Plot the decision boundary
contour(x1Grid, x2Grid, decisionBoundary, [0.5 0.5], 'k', 'LineWidth', 2);
title('Feedforward Neural Network for Male vs. Female Classification');
xlabel('Height (cm)');
ylabel('Weight (kg)');
legend('Male', 'Female', 'Decision Boundary');
hold off;