机器学习-感知机C++代码
#include
<iostream
>
#include
<fstream
>
#include
<vector
>
#include
<sstream
>
using namespace std
;
int lr
= 1;
void data_read(istream
& in,vector
<vector
<int
>> &datas
)
{
string line
;
while (getline(in,line
))
{
istringstream
sline(line
);
vector
<int
> data
;
int x
;
while (sline
>> x
)
{
data
.push_back(x
);
}
datas
.push_back(data
);
}
}
bool
check(int
* w
,int
&b
,vector
<vector
<int
>> datas
)
{
int
j(datas
.size());
int
i(0);
while (i
<j
)
{
vector
<int
> data(datas
[i
]);
if ((-data
[2] * (w
[0] * data
[0] + w
[1] * data
[1]+b
)) >=0)
{
b
= b
+ data
[2];
w
[0] = w
[0] + data
[2]*data
[0];
w
[1] = w
[1] + data
[2]*data
[1];
return false;
}
i
++;
}
return true;
}
int
main()
{
ifstream
fs("./data.txt");
vector
<vector
<int
>> samples
;
data_read(fs
, samples
);
int w
[2] = {0,0};
int b
= 0;
while(!check(w
, b
, samples
));
cout
<< w
[0] << " " << w
[1] << " " << b
<< endl
;
return 1;
}
转载请注明原文地址: https://lol.8miu.com/read-37283.html