@@ -22,7 +22,7 @@ import ModelSupport
22
22
import TensorFlow
23
23
24
24
public struct BostonHousing {
25
- public let trainPercentage : Float = 0.8
25
+ public let trainPercentage : Float = 0.8
26
26
public let numRecords : Int
27
27
public let numColumns : Int
28
28
public let numTrainRecords : Int
@@ -33,35 +33,41 @@ public struct BostonHousing {
33
33
public let yTest : Tensor < Float >
34
34
35
35
static func downloadBostonHousingIfNotPresent( ) -> String {
36
- let remoteURL = URL ( string: " https://archive.ics.uci.edu/ml/machine-learning-databases/housing/ " ) !
37
- let localURL = DatasetUtilities . defaultDirectory. appendingPathComponent ( " BostonHousing " , isDirectory: true )
36
+ let remoteURL = URL (
37
+ string: " https://storage.googleapis.com/s4tf-hosted-binaries/datasets/BostonHousing/ " ) !
38
+ let localURL = DatasetUtilities . defaultDirectory. appendingPathComponent (
39
+ " BostonHousing " , isDirectory: true )
38
40
39
41
let downloadPath = localURL. path
40
42
let directoryExists = FileManager . default. fileExists ( atPath: downloadPath)
41
43
let contentsOfDir = try ? FileManager . default. contentsOfDirectory ( atPath: downloadPath)
42
44
let directoryEmpty = ( contentsOfDir == nil ) || ( contentsOfDir!. isEmpty)
43
-
45
+
44
46
if !directoryExists || directoryEmpty {
45
47
let _ = DatasetUtilities . downloadResource (
46
48
filename: " housing " , fileExtension: " data " ,
47
49
remoteRoot: remoteURL, localStorageDirectory: localURL,
48
50
extract: false )
49
51
}
50
52
51
- return try ! String ( contentsOf: localURL. appendingPathComponent ( " housing.data " ) , encoding: String . Encoding. utf8)
53
+ return try ! String (
54
+ contentsOf: localURL. appendingPathComponent ( " housing.data " ) ,
55
+ encoding: String . Encoding. utf8)
52
56
}
53
-
57
+
54
58
public init ( ) {
55
59
let data = BostonHousing . downloadBostonHousingIfNotPresent ( )
56
60
57
61
// Convert Space Separated CSV with no Header
58
- let dataRecords : [ [ Float ] ] = data. split ( separator: " \n " ) . map { String ( $0) . split ( separator: " " ) . compactMap { Float ( String ( $0) ) } }
62
+ let dataRecords : [ [ Float ] ] = data. split ( separator: " \n " ) . map {
63
+ String ( $0) . split ( separator: " " ) . compactMap { Float ( String ( $0) ) }
64
+ }
59
65
60
66
let numRecords = dataRecords. count
61
67
let numColumns = dataRecords [ 0 ] . count
62
68
63
- let dataFeatures = dataRecords. map { Array ( $0 [ 0 ..< numColumns- 1 ] ) }
64
- let dataLabels = dataRecords. map { Array ( $0 [ ( numColumns- 1 ) ... ] ) }
69
+ let dataFeatures = dataRecords. map { Array ( $0 [ 0 ..< numColumns - 1 ] ) }
70
+ let dataLabels = dataRecords. map { Array ( $0 [ ( numColumns - 1 ) ... ] ) }
65
71
66
72
self . numRecords = numRecords
67
73
self . numColumns = numColumns
@@ -73,15 +79,17 @@ public struct BostonHousing {
73
79
let yTrain = Array ( Array ( dataLabels [ 0 ..< numTrainRecords] ) . joined ( ) )
74
80
let yTest = Array ( Array ( dataLabels [ numTrainRecords... ] ) . joined ( ) )
75
81
76
- let xTrainDeNorm = Tensor < Float > ( xTrain) . reshaped ( to: TensorShape ( [ numTrainRecords, numColumns- 1 ] ) )
77
- let xTestDeNorm = Tensor < Float > ( xTest) . reshaped ( to: TensorShape ( [ numTestRecords, numColumns- 1 ] ) )
82
+ let xTrainDeNorm = Tensor < Float > ( xTrain) . reshaped (
83
+ to: TensorShape ( [ numTrainRecords, numColumns - 1 ] ) )
84
+ let xTestDeNorm = Tensor < Float > ( xTest) . reshaped (
85
+ to: TensorShape ( [ numTestRecords, numColumns - 1 ] ) )
78
86
79
87
// Normalize
80
88
let mean = xTrainDeNorm. mean ( alongAxes: 0 )
81
89
let std = xTrainDeNorm. standardDeviation ( alongAxes: 0 )
82
90
83
- self . xTrain = ( xTrainDeNorm - mean) / std
84
- self . xTest = ( xTestDeNorm - mean) / std
91
+ self . xTrain = ( xTrainDeNorm - mean) / std
92
+ self . xTest = ( xTestDeNorm - mean) / std
85
93
self . yTrain = Tensor < Float > ( yTrain) . reshaped ( to: TensorShape ( [ numTrainRecords, 1 ] ) )
86
94
self . yTest = Tensor < Float > ( yTest) . reshaped ( to: TensorShape ( [ numTestRecords, 1 ] ) )
87
95
}
0 commit comments