机器学习 – 正规方程法(Unity版本)

// @author TomYuan
using UnityEngine;
using System.Collections;
using LinearAlgebra;
using UnityEngine.UI;

public class Main : MonoBehaviour {

	void Start () {
		// For Example
		// Predict The House Value
		// Training Data Set
		/**
		 *  RoomNumber    Feet(2)    Coordinates   Cost
		 *      3          2000           1        250,000
		 *      2          800            2        300,000
		 *      2          850            1        150,000
		 *      2          550            1         78,000
		 *      4          2000           3        150,000
		 */
		/**
		 *  Cal RoomNumber(3)   Feets(2000)  Coordinates(2)
		 */
		// Min = (X(T)*X)(-1)*X(T)*y
		// f(x) = a + bx1 + cx2 + dx3
		Matrix X = Matrix.Create (5, 4, 
			           new double[] {
							1, 3, 2000, 1,
							1, 2, 800, 2,
							1, 2, 850, 1,
							1, 2, 550, 1,
							1, 4, 2000, 3
						});
		Matrix y = Matrix.Create (5, 1, 
			            new double[] {
							250000,
							300000,
							150000,
							78000,
							150000
						});
		Matrix result = ((X.Transpose () * X).Inverse ()) * X.Transpose () * y;
		// Cal Result
		Matrix example = Matrix.Create(1, 4, new double[] {
			1, 3, 2000, 2
		});
		// double cost = result.Elements[0, 0] + result.Elements[1, 0] * 3 + result[2, 0] * 2000 + result[3, 0] * 2;
		// Debug.Log ("RoomNumber(3)   Feets(2000)  Coordinates(2)'s Cost:" + Mathf.Ceil((float)cost));
		Matrix cost_matrix = example * result;
		Debug.Log ("RoomNumber(3)   Feets(2000)  Coordinates(2)'s Cost:" + Mathf.Ceil((float)cost_matrix.Elements[0, 0]));
		GameObject.Find ("Text").GetComponent ().text = "RoomNumber(3)   Feets(2000)  Coordinates(2)'s Cost:" + Mathf.Ceil ((float)cost_matrix.Elements [0, 0]);
			
	}
        ......
}

欢迎留言

avatar
  Subscribe  
Notify of