日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多项式回归模型(Office Prices)

發布時間:2024/4/11 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多项式回归模型(Office Prices) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

?

分析:還是上次的房價預測題目,指明要用多項式回歸擬合。在多元多項式擬合時候,目標函數表示如下

?

????

?

?????對其目標函數求偏導得到

?

?????

?

???? 很容易寫出代碼。

?

代碼:

#coding:utf-8import mathclass Data:def __init__(self):self.x = []self.y = 0.0def makeMatrix(row, col, fill = 0.0):mat = []for i in range(row):mat.append([fill] * col)return matdef WX(d, w, b):res = 0.0for k in range(len(d.x)):for j in range(b + 1):res += w[k][j] * math.pow(d.x[k], j)return resdef Gradient(d, w, f, b, alpha):for k in range(f):for j in range(b + 1):t1, t2 = 0.0, 0.0for i in range(len(d)):t1 += (WX(d[i], w, b) - d[i].y) * math.pow(d[i].x[k], j)w[k][j] -= alpha * t1def getValues(d, w, b):res = 0.0for i in range(len(d)):tmp = WX(d[i], w, b)res += 0.5 * (d[i].y - tmp) * (d[i].y - tmp)return resdef Iterator(d, w, f, b):alpha = 0.003delta = 0.5oldVal = getValues(d, w, b)Gradient(d, w, f, b, alpha)newVal = getValues(d, w, b)while abs(oldVal - newVal) > delta:oldVal = newValGradient(d, w, f, b, alpha)newVal = getValues(d, w, b)def main():while True:try:F, N = map(int, raw_input().split())d = []b = 5w = makeMatrix(F, b + 1)for i in range(0, N):t = Data()t.x = map(float, raw_input().split())t.y = t.x.pop()d.append(t)Iterator(d, w, F, b)N = int(raw_input())for i in range(0, N):t = Data()t.x = map(float, raw_input().split())print '%.2f'% WX(t, w, b)except EOFError:breakif __name__ == '__main__':main()

?

不過,上述代碼得到的結果偏差比較大,需要重新考慮。除了上述方式外,還有一種特征組合方法效果不錯。

?

代碼:

#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 += fabs(d[i].y - tmp);}return res; }void Iterator(const Vector<Data>& d, Vector<double> &w) {double alpha = 0.3 / d.size();double delta = 0.5;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);} }Vector<double> getFeatures(Vector<double> x) {Vector<double> res;int n = x.size();for(int i = 0; i < n; i++)for(int j = i; j < n; j++)for(int k = j; k < n; k++)res.push_back(x[i] * x[j] * x[k]);return res; }int main() {int F, N;Vector<double> w;Vector<Data> d;while(scanf("%d %d", &F, &N) != EOF){d.clear();w.clear();int features = 0;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);}t.x = getFeatures(t.x);features = t.x.size();scanf("%lf", &_y);t.y = _y;d.push_back(t);}for(int i = 0; i < features; 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);}t.x = getFeatures(t.x);printf("%.2lf\n", WX(t, w));}}return 0; }


?

另外利用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/

?

總結

以上是生活随笔為你收集整理的多项式回归模型(Office Prices)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。