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』,原因就在這里!
而在解析器中則沒有這個問題!