怎么用python计算高斯定律 20
展开全部
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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 08 16:16:36 2016
@author: SumaiWong
"""
import numpy as np
import pandas as pd
from numpy import dot
from numpy.linalg import inv
iris = pd.read_csv('D:\iris.csv')
dummy = pd.get_dummies(iris['Species']) # 对Species生成哑变量
iris = pd.concat([iris, dummy], axis =1 )
iris = iris.iloc[0:100, :] # 截取前一百行样本
X = iris.ix[:, 0:4]
Y = iris['setosa'].reshape(len(iris), 1) #整理出X矩阵 和 Y矩阵
def GDA(Y, X):
theta1 = Y.mean() #类别1的比例
theta0 = 1-Y.mean() #类别2的比例
mu1 = X[Y==1].mean() #类别1特征的均值向量
mu0 = X[Y==0].mean() #类别2特征的均值向量
X_1 = X[Y==1]
X_0 = X[Y==0]
A = dot(X_1.T, X_1) - len(Y[Y==1])*dot(mu1.reshape(4,1), mu1.reshape(4,1).T)
B = dot(X_0.T, X_0) - len(Y[Y==0])*dot(mu0.reshape(4,1), mu0.reshape(4,1).T)
sigma = (A+B)/len(X) #sigma = X'X-n(X.bar)X.bar'=X'[I-1/n 1 1]X
return theta1, theta0, mu1, mu0, sigma
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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 08 16:16:36 2016
@author: SumaiWong
"""
import numpy as np
import pandas as pd
from numpy import dot
from numpy.linalg import inv
iris = pd.read_csv('D:\iris.csv')
dummy = pd.get_dummies(iris['Species']) # 对Species生成哑变量
iris = pd.concat([iris, dummy], axis =1 )
iris = iris.iloc[0:100, :] # 截取前一百行样本
X = iris.ix[:, 0:4]
Y = iris['setosa'].reshape(len(iris), 1) #整理出X矩阵 和 Y矩阵
def GDA(Y, X):
theta1 = Y.mean() #类别1的比例
theta0 = 1-Y.mean() #类别2的比例
mu1 = X[Y==1].mean() #类别1特征的均值向量
mu0 = X[Y==0].mean() #类别2特征的均值向量
X_1 = X[Y==1]
X_0 = X[Y==0]
A = dot(X_1.T, X_1) - len(Y[Y==1])*dot(mu1.reshape(4,1), mu1.reshape(4,1).T)
B = dot(X_0.T, X_0) - len(Y[Y==0])*dot(mu0.reshape(4,1), mu0.reshape(4,1).T)
sigma = (A+B)/len(X) #sigma = X'X-n(X.bar)X.bar'=X'[I-1/n 1 1]X
return theta1, theta0, mu1, mu0, sigma
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询