forked from DevelopmentG/Prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (76 loc) · 3.58 KB
/
Program.cs
File metadata and controls
100 lines (76 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class NaiveBayesDiseasePredictor
{
private Dictionary<string, int> diseaseCounts = new Dictionary<string, int>();
private Dictionary<string, Dictionary<string, int>> symptomCounts = new Dictionary<string, Dictionary<string, int>>();
private Dictionary<string, string> diseaseTreatments = new Dictionary<string, string>();
private int totalCases = 0;
private const double LaplaceSmoothing = 1.0;
public void Train(string csvFilePath)
{
var lines = File.ReadAllLines(csvFilePath).Skip(1); // Skip header
foreach (var line in lines)
{
var parts = line.Split(',');
if (parts.Length < 4) continue;
string disease = parts[1].Trim().ToLower(); // Disease Name
string[] symptoms = parts[2].ToLower().Split(';').Select(s => s.Trim()).ToArray(); // Symptoms split by semicolon
string treatment = parts[3].Trim(); // Treatment information
if (!diseaseCounts.ContainsKey(disease))
diseaseCounts[disease] = 0;
diseaseCounts[disease]++;
if (!diseaseTreatments.ContainsKey(disease))
diseaseTreatments[disease] = treatment;
foreach (var symptom in symptoms)
{
if (!symptomCounts.ContainsKey(symptom))
symptomCounts[symptom] = new Dictionary<string, int>();
if (!symptomCounts[symptom].ContainsKey(disease))
symptomCounts[symptom][disease] = 0;
symptomCounts[symptom][disease]++;
}
totalCases++;
}
}
public void Predict(string[] inputSymptoms)
{
Dictionary<string, double> probabilities = new Dictionary<string, double>();
foreach (var disease in diseaseCounts.Keys)
{
double prior = (double)(diseaseCounts[disease] + LaplaceSmoothing) / (totalCases + diseaseCounts.Count * LaplaceSmoothing);
double likelihood = 1.0;
foreach (var symptom in inputSymptoms)
{
int symptomCount = symptomCounts.ContainsKey(symptom) && symptomCounts[symptom].ContainsKey(disease)
? symptomCounts[symptom][disease]
: 0;
double symptomProbability = (symptomCount + LaplaceSmoothing) / (diseaseCounts[disease] + 2 * LaplaceSmoothing);
likelihood *= symptomProbability;
}
probabilities[disease] = prior * likelihood;
}
var sortedPredictions = probabilities.OrderByDescending(p => p.Value).Take(5).ToList(); // Take only top 5
Console.WriteLine("\nTop 5 Predicted Diseases:");
foreach (var prediction in sortedPredictions)
{
string treatment = diseaseTreatments.ContainsKey(prediction.Key) ? diseaseTreatments[prediction.Key] : "No treatment data available";
double percentage = prediction.Value * 100;
Console.WriteLine($"{prediction.Key}: {percentage:F2}% | Treatment: {treatment}");
}
}
}
class Program
{
static void Main()
{
string filePath = "Diseases_Symptoms_Updated.csv"; // Ensure correct CSV path
var predictor = new NaiveBayesDiseasePredictor();
predictor.Train(filePath);
Console.Write("\nEnter symptoms (comma-separated, e.g., palpitations, sweating): ");
string[] inputSymptoms = Console.ReadLine().ToLower().Split(',').Select(s => s.Trim()).ToArray();
predictor.Predict(inputSymptoms);
}
}