📄 attribute.cs
字号:
using System;
using System.Collections;
using System.Text;
namespace NaiveBayes
{
class Attribute : SampleAttribute
{
public Attribute(ArrayList values, double step)
:
base(values)
{
initializeDigitizedValues(step);
initializeDigitizedTypes();
}
public void initializeDigitizedValues(double step)
{
try
{
ArrayList digitValues = new ArrayList();
double maxValue = 0;
double minValue = 1000;
foreach (string value in this.values)
{
digitValues.Add(getDouble(value));
if (getDouble(value) > maxValue)
maxValue = getDouble(value);
if (getDouble(value) < minValue)
minValue = getDouble(value);
}
ArrayList normalizedValues = new ArrayList();
//minimax normalization
foreach (double value in digitValues)
normalizedValues.Add((value - minValue) / (maxValue - minValue));
//discretizing values
this.digitizedValues = normalizedValues.Clone() as ArrayList;
double tt = 1 / step;
for (int j = 0; j < 1 / step; j++)
{
for (int i = 0; i < normalizedValues.Count; i++)
//foreach (double value in normalizedValues)
{
double t1 = Convert.ToDouble(normalizedValues[i]);
double t2 = (j + 1) * step;
if (Convert.ToDouble(normalizedValues[i]) <= (j + 1) * step)
{
normalizedValues[i] = maxValue;
this.digitizedValues[i] = j;
}
}
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
this.type = Type.char_;
initializeDigitizedValues();
}
}
private void initializeDigitizedValues()
{
this.digitizedValues = this.values.Clone() as ArrayList;
for (int j = 0; j < this.values.Count; j++)
{
for (int i = 0; i < this.valueTypes.Count; i++)
{
if (this.values[j].Equals(this.valueTypes[i]))
this.digitizedValues[j] = i;
}
}
}
public void initializeDigitizedTypes()
{
foreach (int value in this.digitizedValues)
{
if (!this.digitizedTypes.Contains(value))
this.digitizedTypes.Add(value);
}
}
public double getDouble(string str)
{
double res = 0;
string[] temp = new string[2];
temp = str.Split('.');
res += (Convert.ToDouble(temp[0]) + 0.1 * Convert.ToDouble(temp[1]));
return res;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -