从今天开始要多做一些关于机器学习方面的竞赛题目,题目来源主要是Hackerrank和Kaggle。链接如下
Hackerrank:https://www.hackerrank.com/
Kaggle:https://www.kaggle.com/
在Hackerrank中提交源代码,这就使得很多库都需要自己写,限制比较多。而Kaggle只需要提交数据,所以随便怎么搞都行。现在来讲第一道题,房价预测,这是Andrew Ng课程里的比较经典的例子。题目描述如下
题目:https://www.hackerrank.com/challenges/predicting-house-prices
分析:比较简单,用梯度下降法即可。
代码:
#include <iostream> #include <string.h> #include <fstream> #include <stdio.h> #include <math.h> #include <vector> #define Vector vector using namespace std; struct Data { Vector<double> x; double y; }; double WX(const Data& d, const Vector<double>& w) { double ans = 0; for(int i = 0; i < w.size(); i++) ans += w[i] * d.x[i]; return ans; } void Gradient(const Vector<Data>& d, Vector<double> &w, double alpha) { for(int i = 0; i < w.size(); i++) { double tmp = 0; for(int j = 0; j < d.size(); j++) tmp += alpha * d[j].x[i] * (WX(d[j], w) - d[j].y); w[i] -= tmp; } } double getValues(const Vector<Data>& d, Vector<double> w) { double res = 0; for(int i = 0; i < d.size(); i++) { double tmp = WX(d[i], w); res += (d[i].y - tmp) * (d[i].y - tmp); //常用平方来衡量误差 } return res; } void Iterator(const Vector<Data>& d, Vector<double> &w) { double alpha = 0.005; double delta = 0.000001; double oldVal = getValues(d, w); Gradient(d, w, alpha); double newVal = getValues(d, w); while(fabs(oldVal - newVal) > delta) { oldVal = newVal; Gradient(d, w, alpha); newVal = getValues(d, w); } } int main() { //freopen("data", "r", stdin); int F, N; Vector<double> w; Vector<Data> d; while(scanf("%d %d", &F, &N) != EOF) { d.clear(); w.clear(); for(int i = 0; i < N; i++) { Data t; double _x, _y; t.x.push_back(1); for(int j = 1; j <= F; j++) { scanf("%lf", &_x); t.x.push_back(_x); } scanf("%lf", &_y); t.y = _y; d.push_back(t); } for(int i = 0; i <= F; i++) w.push_back(0); Iterator(d, w); d.clear(); scanf("%d", &N); for(int i = 0; i < N; i++) { Data t; double _x; t.x.push_back(1); for(int j = 1; j <= F; j++) { scanf("%lf", &_x); t.x.push_back(_x); } printf("%.2lf\n", WX(t, w)); } } return 0; }
时间: 2024-10-23 04:35:33