private void Button7_OnClick(object sender, RoutedEventArgs e)
{
//1.将文件内数据转换为矩阵
var mb = Matrix<double>.Build;
int a = 0;
FileStream fs1 = new FileStream("C:\\Users\\YS\\Desktop\\data\\data.txt", FileMode.Open,FileAccess.Read);
StreamReader read1 = new StreamReader(fs1,Encoding.Default);
string strline;
while ((strline = read1.ReadLine()) != null)
{
a++;//得到行数
}
read1.Close();
fs1.Close();
double[,] x = new double[a, 3];//建立对应行数的数组
FileStream fs2 = new FileStream("C:\\Users\\YS\\Desktop\\data\\data.txt", FileMode.Open, FileAccess.Read);
StreamReader read2 = new StreamReader(fs2, Encoding.Default);
int b = 0;
while ((strline = read2.ReadLine()) != null)
{
string[] stra = strline.Split('\t');
x[b, 0] = Convert.ToDouble(stra[0]);
x[b, 1] = Convert.ToDouble(stra[1]);
x[b, 2] = Convert.ToDouble(stra[2]);
b++;
}
var matrix1 = mb.DenseOfArray(x);//数据转为矩阵
read2.Close();
fs2.Close();
//2.特征与结果分开
double[,] mx = new double[a,3];
for (int i = 0; i < a; i++)
{
mx[i, 0] = 1.0;
}
for (int i = 0; i < a; i++)
{
for (int j = 1; j < 3; j++)
{
mx[i, j] = matrix1[i, j-1];
}
}
double[,] ml = new double[a,1];
for (int i = 0; i < a; i++)
{
ml[i, 0] = matrix1[i, 2];
}
var m_feature = mb.DenseOfArray(mx);//加上偏置项的特征矩阵
var m_label = mb.DenseOfArray(ml);//结果矩阵
int c = m_feature.ColumnCount;
var m_w = mb.Dense(c,1,1.0);//初始化权重,每一行特征的权重是一样的
for (int iterations = 0; iterations < 100/*迭代次数*/; iterations++)
{