Skip to content

Commit a433c0e

Browse files
committed
modified version in least squares linear regression tutorial
modified variable names in KNeighbors classifer and regressor
1 parent ca31bc0 commit a433c0e

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

Notebooks/Least Squares Linear Regression.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@
9999
"output_type": "stream",
100100
"text": [
101101
"Installing packages:\n",
102-
"\t.package(url: \"https://github.com/param087/swiftML\", from: \"0.0.2\")\n",
102+
"\t.package(url: \"https://github.com/param087/swiftML\", from: \"0.0.3\")\n",
103103
"\t\tswiftML\n",
104104
"With SwiftPM flags: []\n",
105105
"Working in: /tmp/tmp29sd7bnt/swift-install\n",
106106
"Fetching https://github.com/param087/swiftML\n",
107107
"Completed resolution in 2.95s\n",
108108
"Cloning https://github.com/param087/swiftML\n",
109-
"Resolving https://github.com/param087/swiftML at 0.0.2\n",
109+
"Resolving https://github.com/param087/swiftML at 0.0.3\n",
110110
"Compile Swift Module 'swiftML' (16 sources)\n",
111111
"/tmp/tmp29sd7bnt/swift-install/package/.build/checkouts/swiftML-1801b701/Sources/swiftML/KNeighborsClassifier.swift:106:13: warning: variable 'indices' was written to, but never read\n",
112112
" var indices: Tensor<Int32>\n",
@@ -127,7 +127,7 @@
127127
}
128128
],
129129
"source": [
130-
"%install '.package(url: \"https://github.com/param087/swiftML\", from: \"0.0.2\")' swiftML"
130+
"%install '.package(url: \"https://github.com/param087/swiftML\", from: \"0.0.3\")' swiftML"
131131
]
132132
},
133133
{

Sources/swiftML/BernoulliNB.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public class BernoulliNB {
4747
// Find unique classes in target values.
4848
(self.classes, self.indices) = Raw.unique(labels.flattened())
4949

50-
precondition(self.classes.shape[0] == 2, "Labels must have only two classes.")
51-
5250
// Initialize the classLogPrior and featureLogProb based on feature count and sample count.
5351
var separated = [[Tensor<Float>]]()
5452
self.classLogPrior = Tensor<Float>(zeros: [self.classes.shape[0]])

Sources/swiftML/KNeighborsClassifier.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public class KNeighborsClassifier {
100100
/// - Returns: Predicted classification.
101101
internal func predictSingleSample(_ test: Tensor<Float>) -> Tensor<Int32> {
102102
var distances = Tensor<Float>(zeros: [self.data.shape[0]])
103-
var maxLabel = Tensor<Int32>(zeros: [self.neighborCount])
104-
var maxDistances: Tensor<Float>
105-
var maxIndex: Tensor<Int32>
103+
var minDistanceLabels = Tensor<Int32>(zeros: [self.neighborCount])
104+
var minDistances: Tensor<Float>
105+
var minDistanceIndex: Tensor<Int32>
106106
var classes: Tensor<Int32>
107107
var indices: Tensor<Int32>
108108

@@ -112,27 +112,27 @@ public class KNeighborsClassifier {
112112
}
113113

114114
// Find the top neighbor with minimum distance.
115-
(maxDistances, maxIndex) =
115+
(minDistances, minDistanceIndex) =
116116
Raw.topKV2(distances, k: Tensor<Int32>(Int32(data.shape[0])), sorted: true)
117-
maxDistances = Raw.reverse(maxDistances, dims: Tensor<Bool>([true]))
118-
maxDistances = maxDistances
117+
minDistances = Raw.reverse(minDistances, dims: Tensor<Bool>([true]))
118+
minDistances = minDistances
119119
.slice(lowerBounds: Tensor<Int32>([0]),
120120
sizes: Tensor<Int32>([Int32(self.neighborCount)]))
121121

122-
maxIndex = Raw.reverse(maxIndex, dims: Tensor<Bool>([true]))
123-
maxIndex = maxIndex
122+
minDistanceIndex = Raw.reverse(minDistanceIndex, dims: Tensor<Bool>([true]))
123+
minDistanceIndex = minDistanceIndex
124124
.slice(lowerBounds: Tensor<Int32>([0]),
125125
sizes: Tensor<Int32>([Int32(self.neighborCount)]))
126126

127127
for i in 0..<self.neighborCount {
128-
maxLabel[i] = self.labels[Int(maxIndex[i].scalarized())]
128+
minDistanceLabels[i] = self.labels[Int(minDistanceIndex[i].scalarized())]
129129
}
130130

131131
// Weights the neighbors based on their weighing method.
132132
let labelsAndWeightsTensor = computeWeights(
133-
distances: maxDistances, labels: Tensor<Float>(maxLabel))
133+
distances: minDistances, labels: Tensor<Float>(minDistanceLabels))
134134

135-
(classes, indices) = Raw.unique(Tensor<Int32>(maxLabel))
135+
(classes, indices) = Raw.unique(Tensor<Int32>(minDistanceLabels))
136136

137137
var kClasses = Tensor<Int32>(zeros: [classes.shape[0]])
138138
var kWeights = Tensor<Float>(zeros: [classes.shape[0]])

Sources/swiftML/KNeighborsRegressor.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,34 @@ public class KNeighborsRegressor {
9797
/// - Returns: Predicted target value.
9898
internal func predictSingleSample(_ test: Tensor<Float>) -> Tensor<Float> {
9999
var distances = Tensor<Float>(zeros: [self.data.shape[0]])
100-
var maxLabel = Tensor<Float>(zeros: [self.neighborCount])
101-
var maxDistances: Tensor<Float>
102-
var maxIndex: Tensor<Int32>
100+
var minDistanceLabels = Tensor<Float>(zeros: [self.neighborCount])
101+
var minDistances: Tensor<Float>
102+
var minDistanceIndex: Tensor<Int32>
103103

104104
// Calculate the distance between test and all data points.
105105
for i in 0..<self.data.shape[0] {
106106
distances[i] = minkowskiDistance(self.data[i], test, p: self.p)
107107
}
108108

109109
// Find the top neighbors with minimum distance.
110-
(maxDistances, maxIndex) =
110+
(minDistances, minDistanceIndex) =
111111
Raw.topKV2(distances, k: Tensor<Int32>(Int32(data.shape[0])), sorted: true)
112-
maxDistances = Raw.reverse(maxDistances, dims: Tensor<Bool>([true]))
113-
maxDistances = maxDistances
112+
minDistances = Raw.reverse(minDistances, dims: Tensor<Bool>([true]))
113+
minDistances = minDistances
114114
.slice(lowerBounds: Tensor<Int32>([0]),
115115
sizes: Tensor<Int32>([Int32(self.neighborCount)]))
116116

117-
maxIndex = Raw.reverse(maxIndex, dims: Tensor<Bool>([true]))
118-
maxIndex = maxIndex
117+
minDistanceIndex = Raw.reverse(minDistanceIndex, dims: Tensor<Bool>([true]))
118+
minDistanceIndex = minDistanceIndex
119119
.slice(lowerBounds: Tensor<Int32>([0]),
120120
sizes: Tensor<Int32>([Int32(self.neighborCount)]))
121121

122122
for i in 0..<self.neighborCount {
123-
maxLabel[i] = self.labels[Int(maxIndex[i].scalarized())]
123+
minDistanceLabels[i] = self.labels[Int(minDistanceIndex[i].scalarized())]
124124
}
125125

126126
// Average weight based on neighbors weights.
127-
let avgWeight = computeWeights(distances: maxDistances, labels: maxLabel)
127+
let avgWeight = computeWeights(distances: minDistances, labels: minDistanceLabels)
128128
return avgWeight
129129
}
130130

0 commit comments

Comments
 (0)