Writing the iOS application

This section provides the details of the Swift code that uses the model in the .mlmodel format and does the housing price prediction:

//  ViewController.swift
import UIKit
import CoreML
class ViewController: UIViewController {
let model = HousePricer()

This line is to initialize the model class that we have imported into the project. The following lines define outlets/variables to the text fields to interact with them:

    @IBOutlet weak var crim: UITextField!
@IBOutlet weak var zn: UITextField!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var b: UITextField!
@IBOutlet weak var ptratio: UITextField!
@IBOutlet weak var medv: UITextField!
@IBOutlet weak var lstat: UITextField!
@IBOutlet weak var rad: UITextField!
@IBOutlet weak var tax: UITextField!
@IBOutlet weak var dis: UITextField!
@IBOutlet weak var age: UITextField!
@IBOutlet weak var rm: UITextField!
@IBOutlet weak var nox: UITextField!
@IBOutlet weak var chas: UITextField!
@IBOutlet weak var indus: UITextField!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad();
updated(rad);
}
@IBAction func updated(_ sender: Any) {
guard let modeloutput = try? model.prediction(CRIM: Double(crim.text!)!, ZN: Double(zn.text!)!, INDUS: Double(indus.text!)!, CHAS: Double(chas.text!)!, NOX: Double(nox.text!)!, RM: Double(rm.text!)!, AGE: Double(age.text!)!, DIS: Double(dis.text!)!, RAD: Double(rad.text!)!, TAX: Double(tax.text!)!, PTRATIO: Double(ptratio.text!)!, B: Double(b.text!)!, LSTAT: Double(lstat.text!)!) else {
fatalError("unexpected runtime error")
}
price.text = "$" + String(format: "%.2f",modeloutput.price);
}
}

The preceding function is added as an onchange listener to all the preceding text fields. In this, we are using the model object we have created previously and predicting the price for the given values in the text fields.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset