## 2017年2月15日 Linear Discriminant Analysis 原

airxiechao

Linear Discriminant analysis seeks a line that best separates two classes, coefficients of the line will be given by

Variable is classified as class 1 if

``````from __future__ import division
import numpy as np
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from pylab import scatter, show, legend, xlabel, ylabel

#prepare data
X = data[['BUSAGE','DAYSDELQ']].as_matrix()
y = data['LABEL'].as_matrix()

# visualize data
def visualize(X, y):
y1, y2 = np.unique(y)
pos = np.where(y == y1)
neg = np.where(y == y2)
scatter(X[pos, 0], X[pos, 1], marker='o', c='b')
scatter(X[neg, 0], X[neg, 1], marker='x', c='r')
show()

visualize(X, y)``````

``````class myLinearDiscriminatAnalysis:
def fit(self, X, y):
self.y1, self.y2 = np.unique(y)
X1 = X[np.where(y==self.y1)]
X2 = X[np.where(y==self.y2)]
n1 = X1.shape[0]
n2 = X2.shape[0]
self.p1 = 1.0 * n1 / X.shape[0]
self.p2 = 1.0 * n2 / X.shape[0]
self.u1 = np.mean(X1, axis=0)
self.u2 = np.mean(X2, axis=0)
C1 = np.cov(X1, rowvar=False)
C2 = np.cov(X2, rowvar=False)
C = 1.0 / (n1+n2)* (n1*C1+n2*C2)
self.beta = np.dot(np.linalg.inv(C), self.u1-self.u2)

def predict(self, X):
thr = np.log(self.p1 / self.p2)
d = np.dot(X, self.beta)-np.dot((self.u1+self.u2)/2.0, self.beta)
p = np.array([''] * X.shape[0])
p[d>thr] = self.y1
p[d<=thr] = self.y2
return p

def score(self, X, y):
p = self.predict(X)
return np.count_nonzero((y == p) == True) / y.shape[0]

lda = myLinearDiscriminatAnalysis()
lda.fit(X, y)
print 'score:', lda.score(X,y)
#score: 0.85

clf = LinearDiscriminantAnalysis()
clf.fit(X, y)
print 'scikit score:', clf.score(X,y)
#scikit score: 0.88``````

### airxiechao

MNIST数据集 预处理（降维） https://www.kaggle.com/arthurtok/interactive-intro-to-dimensionality-reduction 解决"Curse ofDimensionality"问题 。if we are able toproject our data f......

ciyiquan5963
2017/11/08
0
0

necther
2018/04/13
0
0

jnulzl
2015/11/17
0
0

softmax 函数 又称为 normalized exponential function:is a generalization of the logistic function that “squashes” a K-dimensional vector zof arbitrary real values to a K-dimens......

oldpan
2017/12/05
0
0
2017年上半年软考报名时间汇总（持续更新）

liuyiok
2017/02/13
0
0

xiangyunyan
23分钟前
6
0

【服务端】在输出的方法块中，加入要输出的字段（qcm_batch_id） QCMUserType.cs: public struct QCM_Custom_Create_Batch_Out_Tag { public BASCoreType.Cmn_Out_T......

_Somuns
23分钟前
5
0
Aliyun Serverless VSCode Extension v1.12.0 发布

Aliyun Serverless VSCode Extension 是阿里云 Serverless 产品 函数计算 Function Compute 的 VSCode 插件，该插件结合了函数计算 Fun 工具以及函数计算 SDK ，是一款 VSCode 图形化开发调试...

24分钟前
6
0

27分钟前
8
0

28分钟前
8
0