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.
Binary file not shown.
Binary file not shown.
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);

hw2Programs/src/LinRegNonLinear.java

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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import Jama.Matrix;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.stream.IntStream;
6+
7+
/**
8+
* Created by cheta_000 on 5/22/2015.
9+
*/
10+
public class LinRegTransformed {
11+
12+
public static void main(String[] args) {
13+
int N =1000;
14+
int iters = 1000;
15+
double[][] weightArr = new double[6][iters];
16+
ArrayList<Double> normalReg = new ArrayList<>();
17+
for (int j = 0; j < iters; j++) {
18+
// Generate training/test set and assign classes
19+
double[][] training = LinRegImplementation.genRandomPoints(N, 2);
20+
double[][] realClass = assignClassWithNoise(training, N);
21+
double[][] test = LinRegImplementation.genRandomPoints(N, 2);
22+
double[][] testClass = assignClassWithNoise(test, N);
23+
24+
// Expand training/test vector into nonlinear transformed feature vector
25+
double[][] expandedTraining = nonlinearTransform(training, N);
26+
double[][] expandedTest = nonlinearTransform(test, N);
27+
28+
// Fit parameters
29+
Matrix inputs = Matrix.constructWithCopy(expandedTraining);
30+
Matrix outputs = Matrix.constructWithCopy(realClass);
31+
Matrix weights = inputs.inverse().times(outputs);
32+
for (int i = 0; i < 6; i++) {
33+
weightArr[i][j] = weights.getArray()[i][0];
34+
}
35+
}
36+
double[] avgWeights = Arrays.stream(weightArr).mapToDouble(w -> Arrays.stream(w).average().getAsDouble()).toArray();
37+
printArr(avgWeights);
38+
}
39+
40+
public static double[][] assignClassWithNoise(double[][] training, int N) {
41+
double[][] realClass = new double[N][1];
42+
for (int i = 0; i < N; i++) {
43+
realClass[i][0] = Math.pow(training[i][1], 2) + Math.pow(training[i][2], 2) - 0.6;
44+
realClass[i][0] = realClass[i][0] >= 0 ? 1 : -1;
45+
46+
if (i % 10 == 0) { // Inducing noise in 10% of cases
47+
realClass[i][0] = realClass[i][0]*-1;
48+
}
49+
}
50+
return realClass;
51+
}
52+
53+
public static double[][] nonlinearTransform(double[][] data, int N) {
54+
double[][] expandedTraining = new double[N][6];
55+
for(int i = 0; i < N; i++) {
56+
expandedTraining[i][0] = data[i][0];
57+
expandedTraining[i][1] = data[i][1];
58+
expandedTraining[i][2] = data[i][2];
59+
expandedTraining[i][3] = data[i][1]*data[i][2];
60+
expandedTraining[i][4] = Math.pow(data[i][1], 2);
61+
expandedTraining[i][5] = Math.pow(data[i][2], 2);
62+
}
63+
return expandedTraining;
64+
}
65+
66+
public static void printArr(double[][] myData, int numRows, int numCols)
67+
{
68+
for(int i = 0; i < numRows; i++)
69+
{
70+
for(int j = 0; j < numCols; j++)
71+
{
72+
System.out.print(myData[i][j] + "\t");
73+
}
74+
System.out.println();
75+
}
76+
}
77+
78+
public static void printArr(double[] myData)
79+
{
80+
for(int j = 0; j < myData.length; j++)
81+
{
82+
System.out.print(myData[j] + "\t");
83+
}
84+
System.out.println();
85+
}
86+
87+
}

hw2Programs/src/test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public static void main(String[] args) {
99
double test [][] = {{1, 2}, {3, 4}};
1010
Arrays.stream(test).forEach(p -> System.out.println(p[0] + "" + p[1]));
1111
// test[0]={5, 6} confirmed hat this doesn't work
12+
13+
System.out.println(Math.pow(3, 4));
14+
15+
System.out.println(test[1]);
1216
}
1317

1418
}

~WRL1635.tmp

493 KB
Binary file not shown.

0 commit comments

Comments
 (0)