Skip to content

Commit e00a371

Browse files
author
Chetanfs
committed
implemented non-linear lin reg and lin reg with transformations.
1 parent d16eb96 commit e00a371

13 files changed

+327
-81
lines changed

HW1 - PLA Implementation/.idea/workspace.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HW2 - Linear Regression Verification.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ mean(unlist(lapply(1:1000, function(i) {
2828
lm2 <- lm(data=test, c2 ~ x + y)
2929
summary(lm2)$sigma
3030
})))
31+
32+
33+
## Prob 9/10 Test
34+
35+
weigths <- matrix(unlist(lapply(1:100, function(i) {
36+
test2 <- data.table(x=runif(100, -1, 1), y=runif(100, -1, 1))
37+
test2[,x2:=x^2]
38+
test2[,y2:=y^2]
39+
test2[,xy:=x*y]
40+
test2[,cls:=ifelse(x2+y2-0.6 >=0, 1, -1)]
41+
coef(lm(cls ~ x + y + x2 + y2 + xy, data=test2))
42+
}), recursive=F), ncol=6, nrow=100, byrow=T)
43+
44+
apply(weigths, 2, mean)

The Learning Problem HW2.docx

137 KB
Binary file not shown.

hw2Programs/.idea/workspace.xml

Lines changed: 179 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
174 Bytes
Binary file not shown.
2.34 KB
Binary file not shown.
3.92 KB
Binary file not shown.
103 Bytes
Binary file not shown.

hw2Programs/src/LinRegImplementation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ public static void main(String[] args) {
2929
double[][] testData = genRandomPoints(Ntest, 2);
3030
double[][] testClass = getPointClasses(testData, realLine);
3131

32+
// Insert output noise
33+
for (int i = 0; i < N/10; i++) {
34+
int randIndex = (int)(Math.random()*N);
35+
if (realClasses[randIndex][0] == 0)
36+
realClasses[randIndex][0] = 1;
37+
else
38+
realClasses[randIndex][0] = 0;
39+
}
40+
3241
// Estimate parameters, note: .inverse() calculates pseudoinverse if inverse isn't possible
3342
Matrix inputs = Matrix.constructWithCopy(randPoints);
3443
Matrix output = Matrix.constructWithCopy(realClasses);
3544
Matrix weights = inputs.inverse().times(output);
3645

46+
3747
// How to estimate parameters without .inverse() pseudoinverse shortcut
3848
// Matrix inputsT = inputs.transpose();
3949
// Matrix weights = inputsT.times(inputs).inverse().times(inputsT).times(output);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Jama.Matrix;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Created by cheta_000 on 5/22/2015.
7+
*/
8+
public class LinRegNonLinear {
9+
10+
public static void main(String[] args) {
11+
int N = 1000;
12+
int iters = 1000;
13+
ArrayList<Double> props = new ArrayList<>();
14+
for (int j = 0; j < iters; j++) {
15+
// Generate training data set
16+
double[][] train = LinRegImplementation.genRandomPoints(N, 2);
17+
double[][] trainCls = LinRegTransformed.assignClassWithNoise(train, N);
18+
19+
// Fit parameters
20+
Matrix inputs = Matrix.constructWithCopy(train);
21+
Matrix outputs = Matrix.constructWithCopy(trainCls);
22+
Matrix weights = inputs.inverse().times(outputs);
23+
24+
// Assess probability
25+
props.add(LinRegImplementation.evaluate(inputs, trainCls, weights));
26+
}
27+
System.out.println(props.stream().mapToDouble(Double::doubleValue).average().getAsDouble());
28+
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)