当前位置:   article > 正文

多项式回归模型(Office Prices)_多项式逻辑回归模型

多项式逻辑回归模型

题目:https://www.hackerrank.com/challenges/predicting-office-space-price

 

分析:还是上次的房价预测题目,指明要用多项式回归拟合。在多元多项式拟合时候,目标函数表示如下

 

    

 

     对其目标函数求偏导得到

 

     

 

     很容易写出代码。

 

代码:

  1. #coding:utf-8
  2. import math
  3. class Data:
  4. def __init__(self):
  5. self.x = []
  6. self.y = 0.0
  7. def makeMatrix(row, col, fill = 0.0):
  8. mat = []
  9. for i in range(row):
  10. mat.append([fill] * col)
  11. return mat
  12. def WX(d, w, b):
  13. res = 0.0
  14. for k in range(len(d.x)):
  15. for j in range(b + 1):
  16. res += w[k][j] * math.pow(d.x[k], j)
  17. return res
  18. def Gradient(d, w, f, b, alpha):
  19. for k in range(f):
  20. for j in range(b + 1):
  21. t1, t2 = 0.0, 0.0
  22. for i in range(len(d)):
  23. t1 += (WX(d[i], w, b) - d[i].y) * math.pow(d[i].x[k], j)
  24. w[k][j] -= alpha * t1
  25. def getValues(d, w, b):
  26. res = 0.0
  27. for i in range(len(d)):
  28. tmp = WX(d[i], w, b)
  29. res += 0.5 * (d[i].y - tmp) * (d[i].y - tmp)
  30. return res
  31. def Iterator(d, w, f, b):
  32. alpha = 0.003
  33. delta = 0.5
  34. oldVal = getValues(d, w, b)
  35. Gradient(d, w, f, b, alpha)
  36. newVal = getValues(d, w, b)
  37. while abs(oldVal - newVal) > delta:
  38. oldVal = newVal
  39. Gradient(d, w, f, b, alpha)
  40. newVal = getValues(d, w, b)
  41. def main():
  42. while True:
  43. try:
  44. F, N = map(int, raw_input().split())
  45. d = []
  46. b = 5
  47. w = makeMatrix(F, b + 1)
  48. for i in range(0, N):
  49. t = Data()
  50. t.x = map(float, raw_input().split())
  51. t.y = t.x.pop()
  52. d.append(t)
  53. Iterator(d, w, F, b)
  54. N = int(raw_input())
  55. for i in range(0, N):
  56. t = Data()
  57. t.x = map(float, raw_input().split())
  58. print '%.2f'% WX(t, w, b)
  59. except EOFError:
  60. break
  61. if __name__ == '__main__':
  62. main()

 

不过,上述代码得到的结果偏差比较大,需要重新考虑。除了上述方式外,还有一种特征组合方法效果不错。

 

代码:

  1. #include <iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <vector>
  7. #define Vector vector
  8. using namespace std;
  9. struct Data
  10. {
  11. Vector<double> x;
  12. double y;
  13. };
  14. double WX(const Data& d, const Vector<double>& w)
  15. {
  16. double ans = 0;
  17. for(int i = 0; i < w.size(); i++)
  18. ans += w[i] * d.x[i];
  19. return ans;
  20. }
  21. void Gradient(const Vector<Data>& d, Vector<double> &w, double alpha)
  22. {
  23. for(int i = 0; i < w.size(); i++)
  24. {
  25. double tmp = 0;
  26. for(int j = 0; j < d.size(); j++)
  27. tmp += alpha * d[j].x[i] * (WX(d[j], w) - d[j].y);
  28. w[i] -= tmp;
  29. }
  30. }
  31. double getValues(const Vector<Data>& d, Vector<double> w)
  32. {
  33. double res = 0;
  34. for(int i = 0; i < d.size(); i++)
  35. {
  36. double tmp = WX(d[i], w);
  37. res += fabs(d[i].y - tmp);
  38. }
  39. return res;
  40. }
  41. void Iterator(const Vector<Data>& d, Vector<double> &w)
  42. {
  43. double alpha = 0.3 / d.size();
  44. double delta = 0.5;
  45. double oldVal = getValues(d, w);
  46. Gradient(d, w, alpha);
  47. double newVal = getValues(d, w);
  48. while(fabs(oldVal - newVal) > delta)
  49. {
  50. oldVal = newVal;
  51. Gradient(d, w, alpha);
  52. newVal = getValues(d, w);
  53. }
  54. }
  55. Vector<double> getFeatures(Vector<double> x)
  56. {
  57. Vector<double> res;
  58. int n = x.size();
  59. for(int i = 0; i < n; i++)
  60. for(int j = i; j < n; j++)
  61. for(int k = j; k < n; k++)
  62. res.push_back(x[i] * x[j] * x[k]);
  63. return res;
  64. }
  65. int main()
  66. {
  67. int F, N;
  68. Vector<double> w;
  69. Vector<Data> d;
  70. while(scanf("%d %d", &F, &N) != EOF)
  71. {
  72. d.clear();
  73. w.clear();
  74. int features = 0;
  75. for(int i = 0; i < N; i++)
  76. {
  77. Data t;
  78. double _x, _y;
  79. t.x.push_back(1);
  80. for(int j = 1; j <= F; j++)
  81. {
  82. scanf("%lf", &_x);
  83. t.x.push_back(_x);
  84. }
  85. t.x = getFeatures(t.x);
  86. features = t.x.size();
  87. scanf("%lf", &_y);
  88. t.y = _y;
  89. d.push_back(t);
  90. }
  91. for(int i = 0; i < features; i++)
  92. w.push_back(0);
  93. Iterator(d, w);
  94. d.clear();
  95. scanf("%d", &N);
  96. for(int i = 0; i < N; i++)
  97. {
  98. Data t;
  99. double _x;
  100. t.x.push_back(1);
  101. for(int j = 1; j <= F; j++)
  102. {
  103. scanf("%lf", &_x);
  104. t.x.push_back(_x);
  105. }
  106. t.x = getFeatures(t.x);
  107. printf("%.2lf\n", WX(t, w));
  108. }
  109. }
  110. return 0;
  111. }


 

另外利用Python的机器学习开源库sklearn很方便处理。具体可以参考如下链接。

 

题解:http://blog.guozengxin.cn/2015/01/08/hackerrank-predicting-office-space-price/

sklearn官网:http://scikit-learn.org/stable/

sklearn源代码:https://github.com/scikit-learn/scikit-learn/

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/526089
推荐阅读
相关标签
  

闽ICP备14008679号