From 65a851341b1670c5cef96f45ce282a5715dc5949 Mon Sep 17 00:00:00 2001 From: Joshua Eckroth Date: Wed, 7 Aug 2013 10:24:45 -0400 Subject: [PATCH] Added regression example to readme and new function for regression, classifier-predict-numeric. --- README.md | 58 ++++++++++++++++++++++++++++++++++++++ src/clj_ml/classifiers.clj | 7 +++++ 2 files changed, 65 insertions(+) diff --git a/README.md b/README.md index 5b2c60c..8b14942 100644 --- a/README.md +++ b/README.md @@ -492,6 +492,64 @@ user> clustered-ds ... ``` +## Example: Home price prediction + +http://www.ibm.com/developerworks/library/os-weka1/ + +```clojure +user> (def homes (make-dataset "homes" [:house-size :lot-size :bedrooms + :granite :bathroom :sellingPrice] + [[3529, 9191, 6, 0, 0, 205000] + [3247, 10061, 5, 1, 1, 224900] + [4032, 10150, 5, 0, 1, 197900] + [2397, 14156, 4, 1, 0,189900] + [2200, 9600, 4, 0, 1, 195000] + [3536, 19994, 6, 1, 1,325000] + [2983, 9365, 5, 0, 1, 230000]])) +#'user/homes + +user> (def homes (dataset-set-class homes :sellingPrice)) +#'user/homes + +user> homes +# + +user> (def reg (classifier-train (make-classifier :regression :linear) homes)) +#'user/reg + +user> reg +# +user> + +user> (classifier-predict-numeric reg (make-instance homes [3198, 9669, 5, 1, 1, nil])) +219328.35717359098 +``` + ## Example: Predicting survival on the Titanic https://www.kaggle.com/c/titanic-gettingStarted diff --git a/src/clj_ml/classifiers.clj b/src/clj_ml/classifiers.clj index 16d2387..ecae7b6 100644 --- a/src/clj_ml/classifiers.clj +++ b/src/clj_ml/classifiers.clj @@ -710,6 +710,13 @@ (let [pred (.classifyInstance classifier instance)] (keyword (.value (.classAttribute instance) pred))))) +(defn classifier-predict-numeric + "Predicts the class attribute of an instance using the provided + classifier. Returns the value as a floating-point value (e.g., for + regression)." + ([^Classifier classifier ^Instance instance] + (.classifyInstance classifier instance))) + (defn classifier-label "Classifies and assign a label to a dataset instance. The function returns the newly classified instance. This call is