pays much attention to a method called Linear Discriminant Analysis, which
has many interesting properties.
In order to test the algorithm's efficiency, she collects many datasets.
What's more, each data is divided into two parts: training data and test
data. She gets the parameters of the model on training data and test the
model on test data. To her surprise, she finds each dataset's test error curve is just a parabolic curve. A parabolic curve corresponds to a quadratic function. In mathematics, a quadratic function is a polynomial function of the form f(x) = ax2 + bx + c. The quadratic will degrade to linear function if a = 0.

It's very easy to calculate the minimal error if there is only one test error curve. However, there are several datasets, which means Josephina will obtain many parabolic curves. Josephina wants to get the tuned parameters that make the best performance on all datasets. So she should take all error curves into account, i.e., she has to deal with many quadric functions and make a new error definition to represent the total error. Now, she focuses on the following new function's minimum which related to multiple quadric functions. The new function F(x) is defined as follows: F(x) = max(Si(x)), i = 1...n. The domain of x is [0, 1000]. Si(x) is a quadric function. Josephina wonders the minimum of F(x). Unfortunately, it's too hard for her to solve this problem. As a super programmer, can you help her?
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e4 + 5;
const double eps = 1e-10;
const int INF = 1 << 30;
int a[maxn],b[maxn],c[maxn];
int n;
double cal(double x)
{
double my = -INF;//函数值可能为负,不能初始化为0
for (int i = 0; i < n; i ++) {
my = max(my,a[i] * 1.0 * x * x + b[i] * 1.0 * x + c[i] * 1.0);
}
return my;
}
void solve()
{
double l = 0.0,r = 1000.0;
double lmid = 0,rmid = 0;
double ans1 = 0,ans2 = 0;
while (r - l >= eps) {
lmid = l + (r - l) / 3;
rmid = r - (r - l) / 3;
ans1 = cal(lmid);
ans2 = cal(rmid);
if(ans1 < ans2) r = rmid;
else l = lmid;
}
printf("%.4lf\n",cal((l + r)/ 2));
}
int main()
{
int T;
cin >> T;
while (T --) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
cin >> n;
for (int i = 0; i < n; i ++) {
scanf("%d%d%d",&a[i],&b[i],&c[i]);
}
solve();
}
return 0;
}
Josephina是一位热衷于机器学习的女孩,她特别关注线性判别分析方法,并试图通过多个数据集来评估该方法的有效性。面对复杂的二次误差曲线,她希望找到一组参数,使所有数据集上的表现最佳。本文介绍了一种解决此问题的方法,通过三分法来确定最小误差。
424

被折叠的 条评论
为什么被折叠?



