Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using Studia.Models;
- namespace Studia
- {
- public static class Program
- {
- public static bool Logging { get; set; }
- public static void Log(string message)
- {
- if(Logging)
- File.AppendAllLines("log.txt", new List<string>() { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": " + message });
- }
- public static List<InputData> ParseData()
- {
- var lines = File.ReadAllLines(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\data.dat");
- return lines.Select(line => line.Split(','))
- .Select(inputParameters => new InputData()
- {
- Preg = double.Parse(inputParameters[0], CultureInfo.InvariantCulture),
- Plas = double.Parse(inputParameters[1], CultureInfo.InvariantCulture),
- Pres = double.Parse(inputParameters[2], CultureInfo.InvariantCulture),
- Skin = double.Parse(inputParameters[3], CultureInfo.InvariantCulture),
- Insu = double.Parse(inputParameters[4], CultureInfo.InvariantCulture),
- Mass = double.Parse(inputParameters[5], CultureInfo.InvariantCulture),
- Pedi = double.Parse(inputParameters[6], CultureInfo.InvariantCulture),
- Age = double.Parse(inputParameters[7], CultureInfo.InvariantCulture),
- Result = (inputParameters[8].ToLower().Trim() == "tested_positive")
- })
- .ToList();
- }
- public static double EuklidesDistance(InputData a, InputData b) => Math.Pow((a.GetWeight - b.GetWeight), 2);
- public static double ManhatanDistance(InputData a, InputData b) => Math.Abs(a.GetWeight - b.GetWeight);
- public static List<InputData> GetKnn(this List<InputData> data, InputData measuredData, int k, Func<InputData, InputData, double> calculateDistanceMethod)
- {
- data.ForEach(measure =>
- {
- measure.Distance = calculateDistanceMethod(measure, measuredData);
- Log($"For measure: {measure.ToString()} distance is {measure.Distance}");
- });
- var orderedData = data.OrderBy(measure => measure.Distance).ToList();
- k = orderedData.Count - k > 0 ? k : 0;
- var neighbours = orderedData.Take(5).ToList();
- Log($"SELECTED {k} neighbors:");
- foreach (var neighbor in neighbours)
- Log($"For neighbor: {neighbor} distance is {neighbor.Distance}");
- return neighbours;
- }
- public static InputData ReadPatientsData()
- {
- var request = new InputData();
- typeof(InputData).GetProperties()
- .Where(prop => prop.GetCustomAttributes(typeof(RangeAttribute)).Any())
- .ToList()
- .ForEach(prop =>
- {
- var description = prop.GetCustomAttribute<DescriptionAttribute>();
- Console.WriteLine(description.Description);
- var range = prop.GetCustomAttribute<RangeAttribute>();
- Console.WriteLine($"Range between <{(double)range.Minimum};{(double)range.Maximum}>");
- Console.WriteLine("Enter value: ");
- double value;
- while (!double.TryParse(Console.ReadLine(), out value) || value < (double)range.Minimum || value > (double)range.Maximum)
- {
- Console.WriteLine("Wrong value, try again");
- }
- prop.SetValue(request, value);
- Log($"Patients data, entered property value {value} for {prop.Name} in range <{(double)range.Minimum};{(double)range.Maximum}>" );
- });
- return request;
- }
- static void Main(string[] args)
- {
- Logging = args.Any() && args[0] == "log";
- Console.WriteLine("From National Institute of Diabetes and Digestive and Kidney Diseases. Several constraints were placed on the selection of these instances from a larger database. In particular, all patients here are females at least 21 years old of Pima Indian heritage. ");
- Console.WriteLine("The class label represents if the person has not diabetes (tested_negative) or the person has diabetes (tested_positive). ");
- var data = ParseData();
- Console.Write("Enter k (kNN) parameter: ");
- var k = int.Parse(Console.ReadLine());
- var patientsData = ReadPatientsData();
- // TestCase k = 5 100%
- // var measure = new InputData()
- // {
- // Preg = 3,
- // Plas = 150,
- // Pres = 76,
- // Skin = 0,
- // Insu = 0,
- // Mass = 21,
- // Pedi = 0.207,
- // Age = 37
- // };
- Console.WriteLine("Choose distance algorithm");
- Console.WriteLine("1.Manhatan");
- Console.WriteLine("2.Euklides");
- Func<InputData, InputData, double> distanceMethod = null;
- if (Console.ReadLine() == "1")
- distanceMethod = ManhatanDistance;
- else
- distanceMethod = EuklidesDistance;
- var neighbors = data.GetKnn(patientsData, k, distanceMethod);
- Log($"For measurements: {patientsData}");
- var positive = neighbors.Count(x => x.Result);
- var negative = neighbors.Count(x => !x.Result);
- if(positive > negative)
- Console.WriteLine("tested_positive");
- else if (positive == negative)
- Console.WriteLine("50%");
- else
- Console.WriteLine("tested_negative");
- Console.WriteLine($"Positive: {positive}, Negative: {negative}");
- Log($"Positive: {positive}, Negative: {negative}");
- Console.ReadLine();
- }
- }
- }
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- namespace Studia.Models
- {
- public class InputData
- {
- [Description("Number of times pregnant ")]
- [Range(0.0, 17.0)]
- public double Preg { get; set; }
- [Description("Plasma glucose concentration a 2 hours in an oral glucose tolerance test ")]
- [Range(0.0, 199.0)]
- public double Plas { get; set; }
- [Description("Diastolic blood pressure (mm Hg) ")]
- [Range(0.0, 122.0)]
- public double Pres { get; set; }
- [Description("Triceps skin fold thickness (mm) ")]
- [Range(0.0, 99.0)]
- public double Skin { get; set; }
- [Description("2-Hour serum insulin (mu U/ml) ")]
- [Range(0.0, 846.0)]
- public double Insu { get; set; }
- [Description("Body mass index (weight in kg/(height in m)^2) ")]
- [Range(0.0, 67.1)]
- public double Mass { get; set; }
- [Description("Diabetes pedigree function")]
- [Range(0.078, 2.42)]
- public double Pedi { get; set; }
- [Description("Age (years)")]
- [Range(21.0, 81.0)]
- public double Age { get; set; }
- public bool Result { get; set; }
- public override string ToString()
- {
- return $"Preg: {Preg}; Plas: {Plas}; Pres: {Pres}; Skin: {Skin}; Insu: {Insu}; Mass:{Mass}; Pedi:{Pedi}; Age:{Age}";
- }
- public double GetWeight => (Preg + Plas + Pres + Skin + Insu + Mass + Pedi + Age);
- public double Distance { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement