鏈哄櫒瀛︿範瀹炴垬-Logistics鍥炲綊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏈哄櫒瀛︿範瀹炴垬-Logistics鍥炲綊相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/name' title='name'>name   鏈哄櫒   turn   鑾峰緱   log   erro   缁村害   璇樊   readline   

Logistics鍥炲綊锛氬疄鎴橈紝鏈変袱涓壒寰乆0,X1.100涓牱鏈紝杩涜Logistics鍥炲綊

1.瀵煎叆鏁版嵁

 1 def load_data_set():
 2     """
 3     鍔犺浇鏁版嵁闆?
 4     :return:杩斿洖涓や釜鏁扮粍锛屾櫘閫氭暟缁?
 5         data_arr -- 鍘熷鏁版嵁鐨勭壒寰?
 6         label_arr -- 鍘熷鏁版嵁鐨勬爣绛撅紝涔熷氨鏄瘡鏉℃牱鏈搴旂殑绫诲埆
 7     """
 8     data_arr=[]
 9     label_arr=[]
10     f=open(鈥?/span>TestSet.txt鈥?/span>,鈥?/span>r鈥?/span>)
11     for line in f.readlines():
12         line_arr=line.strip().split()
13         #涓轰簡鏂逛究璁$畻锛屾垜浠皢x0鐨勫€艰涓?.0锛屼篃灏辨槸鍦ㄦ瘡涓€琛岀殑寮€澶存坊鍔犱竴涓?.0锛屼綔涓簒0
14         data_arr.append([1.0,np.float(line_arr[0]),np.float(line_arr[1])])
15         label_arr.append(int(line_arr[2]))
16     return data_arr,label_arr

 

 

2. Logistics鍥炲綊姊害涓婂崌浼樺寲绠楁硶

 1 def sigmoid(x):
 2     return 1.0/(1+np.exp(-x))
 3 
 4 def grad_ascent(data_arr,class_labels):
 5     """
 6     姊害涓婂崌娉曪紝鍏跺疄灏辨槸鍥犱负浣跨敤浜嗘瀬澶т技鐒朵及璁★紝杩欎釜澶у鏈夊繀瑕佸幓鐪嬫帹瀵硷紝鍙湅浠g爜鎰熻涓嶅お澶?
 7     :param data_arr: 浼犲叆鐨勫氨鏄竴涓櫘閫氱殑鏁扮粍锛屽綋鐒朵綘浼犲叆涓€涓簩缁寸殑ndarray涔熻
 8     :param class_labels: class_labels 鏄被鍒爣绛撅紝瀹冩槸涓€涓?1*100 鐨勮鍚戦噺銆?
 9                     涓轰簡渚夸簬鐭╅樀璁$畻锛岄渶瑕佸皢璇ヨ鍚戦噺杞崲涓哄垪鍚戦噺锛屽仛娉曟槸灏嗗師鍚戦噺杞疆锛屽啀灏嗗畠璧嬪€肩粰label_mat
10     :return:
11     """
12     data_mat=np.mat(data_arr)
13     #鍙樻垚鐭╅樀涔嬪悗杩涜杞疆
14     label_mat=np.mat(class_labels).transpose()
15     #鑾峰緱鏁版嵁鐨勬牱鏈噺鍜岀壒寰佺淮搴︽暟
16     m,n=np.shape(data_mat)
17     #瀛︿範鐜?/span>
18     alpha=0.001
19     #鏈€澶ц凯浠f鏁?/span>
20     max_cycles=500
21     # 鐢熸垚涓€涓暱搴﹀拰鐗瑰緛鏁扮浉鍚岀殑鐭╅樀锛屾澶刵涓? -> [[1],[1],[1]]
22     # weights 浠h〃鍥炲綊绯绘暟锛?姝ゅ鐨?ones((n,1)) 鍒涘缓涓€涓暱搴﹀拰鐗瑰緛鏁扮浉鍚岀殑鐭╅樀锛屽叾涓殑鏁板叏閮ㄩ兘鏄?1
23     weights=np.ones((n,1))
24     for k in range(max_cycles):
25         h=sigmoid(data_mat*weights)
26         error=label_mat-h
27         weights=weights+alpha*data_mat.transpose()*error
28     return weights

 

3. 鐢诲嚭鍐崇瓥杈圭晫锛屽嵆鐢诲嚭鏁版嵁闆嗗悎Logistics鍥炲綊鏈€浣虫嫙鍚堢洿鎺ョ殑鍑芥暟

 

 1 def plot_best_fit(weights):
 2     """
 3     鍙鍖?
 4     :param weights:
 5     :return:
 6     """
 7     import matplotlib.pyplot as plt
 8     data_mat, label_mat = load_data_set()
 9     data_arr = np.array(data_mat)
10     n = np.shape(data_mat)[0]
11     x_cord1 = []
12     y_cord1 = []
13     x_cord2 = []
14     y_cord2 = []
15     for i in range(n):
16         if int(label_mat[i]) == 1:
17             x_cord1.append(data_arr[i, 1])
18             y_cord1.append(data_arr[i, 2])
19         else:
20             x_cord2.append(data_arr[i, 1])
21             y_cord2.append(data_arr[i, 2])
22     fig = plt.figure()
23     ax = fig.add_subplot(111)
24     ax.scatter(x_cord1, y_cord1, s=30, color=鈥?/span>k鈥?/span>, marker=鈥?/span>^鈥?/span>)
25     ax.scatter(x_cord2, y_cord2, s=30, color=鈥?/span>red鈥?/span>, marker=鈥?/span>s鈥?/span>)
26     x = np.arange(-3.0, 3.0, 0.1)
27     y = (-weights[0] - weights[1] * x) / weights[2]
28     """
29     y鐨勭敱鏉ワ紝鍗фЫ锛屾槸涓嶆槸娌$湅鎳傦紵
30     棣栧厛鐞嗚涓婃槸杩欎釜鏍峰瓙鐨勩€?
31     dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
32     w0*x0+w1*x1+w2*x2=f(x)
33     x0鏈€寮€濮嬪氨璁剧疆涓?鍙伙紝 x2灏辨槸鎴戜滑鐢诲浘鐨剏鍊硷紝鑰宖(x)琚垜浠(鍚堣宸粰绠楀埌w0,w1,w2韬笂鍘讳簡
34     鎵€浠ワ細 w0+w1*x+w2*y=0 => y = (-w0-w1*x)/w2   
35     """
36     ax.plot(x, y)
37     plt.xlabel(鈥?/span>x1鈥?/span>)
38     plt.ylabel(鈥?/span>y1鈥?/span>)
39     plt.show()

 

 

4. 娴嬭瘯鏁版嵁锛岀敾鍥?/p>

 1 def test():
 2     """
 3     杩欎釜鍑芥暟鍙灏辨槸瀵逛笂闈㈢殑鍑犱釜绠楁硶鐨勬祴璇曪紝杩欐牱灏变笉鐢ㄦ瘡娆¢兘鍦╬ower shell 閲岄潰鎿嶄綔锛屼笉鐒堕夯鐑︽浜?
 4     :return:
 5     """
 6     data_arr, class_labels = load_data_set()
 7     # 娉ㄦ剰锛岃繖閲岀殑grad_ascent杩斿洖鐨勬槸涓€涓?matrix, 鎵€浠ヨ浣跨敤getA鏂规硶鍙樻垚ndarray绫诲瀷
 8     weights = grad_ascent(data_arr, class_labels).getA()
 9     # weights = stoc_grad_ascent0(np.array(data_arr), class_labels)
10     #weights = stoc_grad_ascent1(np.array(data_arr), class_labels)
11     plot_best_fit(weights)
12 
13 if __name__ == 鈥?/span>__main__鈥?/span>:
14     test()

 

 

5. 缁撴灉濡備笅

鎶€鏈浘鐗? src=

 

 鍙﹀锛岃繕鏈?/p>

鐪熷疄璁粌鏁版嵁鍜屾祴璇曟暟鎹?------浠庣枬姘旂梾鐥囬娴嬬梾椹殑姝讳骸鐜?-----
濡備綍棰勬祴鐨勪唬鐮佷篃闄勫姞~
github瀹炵幇鍦板潃锛?a href="https://github.com/CynthiaWendy/Machine-Learning-in-Action-Logistics">https://github.com/CynthiaWendy/Machine-Learning-in-Action-Logistics

 

以上是关于鏈哄櫒瀛︿範瀹炴垬-Logistics鍥炲綊的主要内容,如果未能解决你的问题,请参考以下文章

鏈哄櫒瀛︿範瀹炴垬锛?锛夛細鏀寔鍚戦噺鏈猴紙涓嬶級

鏈哄櫒瀛︿範瀛︿範绱㈠紩

鏈哄櫒瀛︿範锛?3-鍨冨溇閭欢鍒嗙被2

銆怭ython鏈哄櫒瀛︿範銆戝喅绛栨爲鍒嗙被

鏈哄櫒瀛︿範鈥斺€斿簲鐢ㄥ満鏅? 绠楁硶搴旂敤鍦烘櫙

Redis 绉掓潃瀹炴垬