예제 #1
0
void GLinearRegressor_linear_test(GRand& prng)
{
	// Train
	GMatrix features1(0, 3);
	GMatrix labels1(0, 1);
	for(size_t i = 0; i < 1000; i++)
	{
		double* pVec = features1.newRow();
		pVec[0] = prng.uniform();
		pVec[1] = prng.uniform(); // irrelevant attribute
		pVec[2] = prng.uniform();
		labels1.newRow()[0] = 0.3 * pVec[0] + 2.0 * pVec[2] + 5.0;
	}
	GLinearRegressor lr(prng);
	lr.train(features1, labels1);

	// Check some values
	if(lr.beta()->rows() != 1 || lr.beta()->cols() != 3)
		throw Ex("failed");
	if(std::abs(lr.beta()->row(0)[0] - 0.3) > 1e-6)
		throw Ex("failed");
	if(std::abs(lr.beta()->row(0)[1] - 0.0) > 1e-6)
		throw Ex("failed");
	if(std::abs(lr.beta()->row(0)[2] - 2.0) > 1e-6)
		throw Ex("failed");
	if(std::abs(lr.epsilon()[0] - 5.0) > 1e-6)
		throw Ex("failed");

	// Test
	GMatrix features2(0, 3);
	GMatrix labels2(0, 1);
	for(size_t i = 0; i < 1000; i++)
	{
		double* pVec = features2.newRow();
		pVec[0] = prng.uniform();
		pVec[1] = prng.uniform(); // irrelevant attribute
		pVec[2] = prng.uniform();
		labels2.newRow()[0] = 0.3 * pVec[0] + 2.0 * pVec[2] + 5.0;
	}
	double results;
	lr.accuracy(features2, labels2, &results);
	if(results > 0.0005)
		throw Ex("failed");
}
예제 #2
0
파일: GLinear.cpp 프로젝트: b2020b/waffles
void GLinearRegressor_linear_test(GRand& prng)
{
	// Train
	GMatrix features1(0, 3);
	GMatrix labels1(0, 1);
	for(size_t i = 0; i < 1000; i++)
	{
		GVec& vec = features1.newRow();
		vec[0] = prng.uniform();
		vec[1] = prng.uniform(); // irrelevant attribute
		vec[2] = prng.uniform();
		labels1.newRow()[0] = 0.3 * vec[0] + 2.0 * vec[2] + 5.0;
	}
	GLinearRegressor lr;
	lr.train(features1, labels1);

	// Check some values
	if(lr.beta()->rows() != 1 || lr.beta()->cols() != 3)
		throw Ex("failed");
	if(std::abs(lr.beta()->row(0)[0] - 0.3) > 1e-6)
		throw Ex("failed");
	if(std::abs(lr.beta()->row(0)[1] - 0.0) > 1e-6)
		throw Ex("failed");
	if(std::abs(lr.beta()->row(0)[2] - 2.0) > 1e-6)
		throw Ex("failed");
	if(std::abs(lr.epsilon()[0] - 5.0) > 1e-6)
		throw Ex("failed");

	// Test
	GMatrix features2(0, 3);
	GMatrix labels2(0, 1);
	for(size_t i = 0; i < 1000; i++)
	{
		GVec& vec = features2.newRow();
		vec[0] = prng.uniform();
		vec[1] = prng.uniform(); // irrelevant attribute
		vec[2] = prng.uniform();
		labels2.newRow()[0] = 0.3 * vec[0] + 2.0 * vec[2] + 5.0;
	}
	double rmse = sqrt(lr.sumSquaredError(features2, labels2) / features2.rows());
	if(rmse > 0.0224)
		throw Ex("failed");
}