A. GDA和Logistic方法的区别及相应的python代码

GDA方法与Logistic方法的主要区别在于这两个模型的假设不同:GDA方法假设p(x|y)服从多元高斯分布,并且输入特征是连续的;Logistic方法并没有GDA那么强的假设,它既没有要求p(x|y)服从多元高斯分布,也没有要求输入特征是连续的。因此Logistic的适用范围比GDA更加广泛。例如:如果输入特征符合泊松分布,则Logistic得到的结果会比GDA更加准确。如果输入特征满足GDA的要求时,既可以用Logistic方法也可以用GDA,但是在这种情况下GDA得到的结果会比Logistic方法得到的结果准确些。下面给出GDA和Logistic方法的简要说明,最后给出相应的 python代码。
GDA是一种生成学习法,主要利用贝叶斯准则得到后验分布律,然后通过最大后验分布对输入数据进行分类。简单地说,也就是在给定某个特征情况下,拥有此特征的数据属于哪个类的概率大 就属于哪个类。GDA的优势:由于有高斯分布的先验信息,如果确实符合实际数据,则只需要少量的样本就可以得到较好的模型。
Logistic是一种判别想学习法,判别学习法通过建立输入数据与输出信息之间的映射关系学得p(y|x),这个与生成学习法是不同的。在生成学习法中首先要确定p(x|y)和p(y)。Logistic主要是通过sigmoid函数来确定输入数据及是将如何进行分类的。Logistic的优势:具有更高的鲁棒性和对数据的分布不明感(不想GDA那样需要特征服从高斯分布)。
下面是具体的python代码:
一、GDA模型的python代码:

点击(此处)折叠或打开
def GDA(dataIn, classLabel):

m = len(classLabel);

sum_1 = sum(classLabel);

q = sum_1/(float(m));

notLabel = ones((len(classLabel),),dtype=int)-array(classLabel);

row,col = shape(dataIn);

y0x = y1x = mat(zeros(col));

for i in range(m):

y0x += mat(dataIn[i])*notLabel[i];

y1x += mat(dataIn[i])*classLabel[i];

mean_0 = y0x/(m-sum_1);

mean_1 = y1x/sum_1;

correlation = 0;

for i in range(m):

correlation += (mat(dataIn[i]-mean_0)).T*(mat(dataIn[i]-mean_0))*notLabel[i] \

+(mat(dataIn[i]-mean_1)).T*(mat(dataIn[i]-mean_1))*classLabel[i];

correlation = correlation/m;

return q,mean_0,mean_1,correlation;

def calculate_pxy0(x,n=2):

return ((2*math.pi)**(-n/2))*(linalg.det(correlation)**(-0.5))*exp(-0.5*(x-mean_0).T*correlation.I*(x-mean_0));
def calculate_pxy1(n=2):

return ((2*math.pi)**(-n/2))*(linalg.det(correlation)**(-0.5))*exp(-0.5*(x-mean_1).T*correlation.I*(x-mean_1));

def GDAClass(testPoint,dataIn,classLabel):
import math;
x = testPoint;
q,mean_0,mean_1,correlation = GDA(dataIn,classLabel);
n=shape(dataIn)[0];
py0 = 1-q;
py1 = q;
pxy0 = calculate_pxy0(x,n);
pxy1 = calculate_pxy1(x,n);

if pxy0*py0 > pxy1*py1:

return 0;

return 1;
二、Logistic模型的python代码:

点击(此处)折叠或打开
def sigmoid(w,x):

return 1/(1+exp(-w*x))

def logisticRegression(xMat,yMat,maxCycles = 500):

'''

ones((m,n)): 产生m维的向量,且每个值为n

'''

col = shape(xMat)[1];

weight = ones((col,1));

alpha = 0.001;

for j in range(maxCycles):

h = sigmoid(weight,xMat);

err = (yMat-h);

weight += alpha*xMat.transpose*err;

return weight;

B. python 画箭头图 如何用python 画格点上的箭头图,就是有一个xy平面上的20乘

你用的graphics模块?这不是内置的,虽然它是调用内置的Tkinter画图。

option可以是"first","last","both"或"none"。见graphics.py:

def setArrow(self, option):
if not option in ["first","last","both","none"]:
raise GraphicsError(BAD_OPTION)
self._reconfig("arrow", option)

细节要查Tk文档:

6.6. The canvas line object
In general, a line can consist of any number of segments connected end to end, and each segment can be straight or curved. To create a canvas line object on a canvas C, use:
id = C.create_line ( x0, y0, x1, y1, , xn, yn, option, )
The line goes through the series of points
(x0,
y0),
(x1,
y1),

(xn,
yn).
Options include:
arrow The default is for the line to have no arrowheads. Use
arrow=FIRST to get an arrowhead at the(x0,y0)end of the line. Use
arrow=LAST to get an arrowhead at the far end. Use
arrow=BOTH for arrowheads at both ends.

C. 请教一个关于python if的问题 我用的是2.7.3版本 下面是题

把input 换成raw_input接受输入,另外,你为什么要用int把输入的字符串a或b转换成int类型?

#!coding=gbk
A=raw_input("如果您是分时用户,请输入a;如果您是未分时用户,请输入b\n")
if A=="a":
x=int(raw_input("峰时段用电量="))
y=int(raw_input("谷时段用电量="))
xy = x + y
if xy>=0 and xy<=3120:
print"该月应缴电费",0.617*x+0.307*y,"元\n"
elif xy>3120 and xy<=4800:
print"该月应缴电费",0.677*x+0.337*y,"元\n"
else:
print"该月应缴电费",0.977*x+0.487*y,"元\n"
else:
x=int(raw_input("使用电量="))
if x>=0 and x<=3120:
print"该月应缴电费",0.617*x,"元\n"
elif x>3120 and x<=4800):
print"该月应缴电费",0.667*x,"元\n"
else:
print"该月应缴电费",0.917*x,"元\n"

D. python中怎样调用百度搜索的API接口

网络搜索不用API接口,它是get请求,自己拼接就行了。

打开网络搜索,随便搜索一个关键字,看地址栏就有get请求的参数。

E. python怎么判断一组变量互不相等

测试了下,发现m=input();输入‘xinwen’后按回车
m的值是 ‘xinwen\r’,原因就在这里!
而在解析器中则没有这个问题!