-
Notifications
You must be signed in to change notification settings - Fork 0
/
project3A_dwyer
80 lines (62 loc) · 1.67 KB
/
project3A_dwyer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 22:19:40 2017
@author: marydwyer
"""
# Mary Dwyer
# Machine Learning Project 2B
# October 25, 2017
# Linear Classification
import numpy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import scipy.stats as scipy
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
def sigmoid(x):
return 1/(1+np.exp(-x))
# Given
Mu1 = np.array([1,1])
Mu2 = np.array([-1,-1])
sigma = np.identity(2)
## Generate Data
N = 1000
N1 = numpy.random.binomial(N, 0.3)
N2 = N - N1
data1 = numpy.random.multivariate_normal(Mu1,sigma,N1) #(meanvector, covariance, number of data sets)
data2 = numpy.random.multivariate_normal(Mu2,sigma,N2)
mean1 = np.mean(data1, axis =0)
mean2 = np.mean(data2, axis =0)
cov1 = np.cov(np.transpose(data1))
cov2 = np.cov(np.transpose(data2))
cov = (N1*cov1 + N2*cov2)/N
#### Gaussian Generative Model ####
## Apply formulas
# Equation 4.66
W = np.matmul(np.linalg.inv(cov),(mean1-mean2))
W = np.transpose(W)
# Equation 4.67
a = (-0.5) * np.transpose(mean1)
b = np.matmul(np.linalg.inv(cov),mean1)
c = (-0.5) * np.transpose(mean2)
d = np.matmul(np.linalg.inv(cov),mean2)
W0 = np.matmul(a,b) + np.matmul(c,d) + numpy.log(N1/N2)
# Equation 4.65
x = numpy.linspace(-5,5,500) #shape (1x500)
y = []
for i in range (500):
y.append((-(W[0])*x[i] - W0)/(W[1]))
plt.ylim((-5,5))
plt.xlim((-5,5))
plt.scatter(data1[:,0],data1[:,1])
plt.scatter(data2[:,0],data2[:,1])
plt.plot(x,y)
sum = 0
for i in range (len(data1)):
sig = W[0]*data2[i,0] + W[1]*data2[i,1] + W0
result = sigmoid(sig)
if(result<0.5):
sum = sum + 1
print(sum/N1)