Ìܼ¡ †
³µÍ× †¤³¤³¤Ç¤Ï¡¢ºÇµÞ¹ß²¼Ë¡¤ò»ÈÍѤ·¤Æ½Å²óµ¢Ê¬ÀϤò½ÐÍè¤ë¤À¤±¥é¥¤¥Ö¥é¥êÅù¤ò»ÈÍѤ»¤º¤Ë¥¹¥¯¥é¥Ã¥Á¤Ç¼ÂÁõ¤·¤Æ¤ß¤ë¡£ ¥â¥Ç¥ë¼° †
ñ²óµ¢Ê¬ÀϤΥâ¥Ç¥ë¼°¤¬¡¡
¡¡¤Ç¤¢¤ë¤Î¤ËÂФ·¤Æ¡¢
ÆÃħÎ̤¬Ê£¿ô¤¢¤ë½Å²óµ¢Ê¬ÀϤξì¹ç¤Î¥â¥Ç¥ë¼°¤Ï¡¡
¡¡¤È¤Ê¤ë¡£¡Ê ¹ÔÎó·×»»ÍÑ¤Ë ¤È¤¹¤ë ¡Ë
°Ê²¼¤Î¤è¤¦¤Ê¥Ç¡¼¥¿¤¬¤¢¤ë»þ¡¢
1¹ÔÌÜ¤Ï ¤È¤Ê¤ê¡¢
°Ê²¼¤Î¹ÔÎó·×»»¤Ç ¤òµá¤á¤ë»ö¤¬¤Ç¤¤ë¡£
¥³¥¹¥È´Ø¿ô †¥³¥¹¥È´Ø¿ô¤Ï°Ê²¼¤ÎÄ̤ꡣ ºÇµÞ¹ß²¼Ë¡ †ºÇµÞ¹ß²¼Ë¡¡Ê¥Ù¥¯¥È¥ë²½¡Ë¤Î¥â¥Ç¥ë¼°¤Ï°Ê²¼¤ÎÄ̤ꡣ Àµµ¬²½ †ÆÃħÎ̤ÎÈϰϤ¬Î󤴤Ȥ˰ۤʤë¾ì¹ç¡¢¥Ç¡¼¥¿¤òÀµµ¬²½¤¹¤ë»ö¤Ë¤è¤Ã¤Æ¡¢Àµ¤·¤¯Ê¬ÀϤò¹Ô¤¦»ö¤¬¤Ç¤¤ë¡£ z-socre Normalization (ɸ½à²½) †Ê¿¶Ñ¤¬0¡¢É¸½àÊк¹¤¬1¤È¤Ê¤ë¤è¤¦¤ËÄ´À°¤¹¤ë¡£ min-max normalization †ºÇÂçÃͤ¬1ºÇ¾®Ãͤ¬0¤È¤Ê¤ë¤è¤¦¤ËÄ´À°¤¹¤ë¡£ ¥µ¥ó¥×¥ë¼ÂÁõ †°Ê²¼¤Î¥Ç¡¼¥¿¤«¤é¡¢Ç¤°Õ¤ÎÉô²°ÌÌÀѤÈÃÛǯ¿ô¤Î»þ¤Ë²ÈĤ¬¤¤¤¯¤é¤Ë¤Ê¤ë¤«¤ò¡¢½Å²óµ¢Ê¬ÀϤò»ÈÍѤ·¤Æ¿äÏÀ¤¹¤ë½èÍý¤ò¼ÂÁõ¤¹¤ë¡£
sample_rent1.py """½Å²óµ¢Ê¬ÀÏ¥µ¥ó¥×¥ë.""" import numpy as np import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def normalize(X, data=None): """Àµµ¬²½(Z-score normalization).""" data = X if data is None else data m = data.shape[0] X_norm = np.zeros((data.shape[0], data.shape[1])) for i in range(data.shape[1]): X_norm[:, i] = (data[:, i] - float(np.mean(X[:, i]))) / float(np.std(X[:, i])) # x0¤òÄɲà X_norm = np.column_stack([np.ones([m,1]), X_norm]) return X_norm def cost(x, y, w): """¥³¥¹¥È´Ø¿ô.""" xw = x.dot(w) return np.dot((xw - y).T, (xw - y)) / (2*m) # ¤â¤·¤¯¤Ï #diff = np.power((x.dot(w) - y), 2) #return diff.sum(axis=0) / (2 * len(y)) def gradient_descent(x, y, w, alpha, iter_num): """ºÇµÞ¹ß²¼Ë¡.""" m = len(y) costs = np.zeros((iter_num, 1)) for i in range(iter_num): w = w - alpha * (1.0/m) * np.transpose(x).dot(x.dot(w) - y) costs[i] = cost(x, y, w) return w, costs if __name__ == "__main__": # -------------------------- # ºÇµÞ¹ß²¼Ë¡¤Ë¤è¤ë½Å²óµ¢Ê¬ÀÏ # -------------------------- # ¥Ç¡¼¥¿Æɤ߹þ¤ß data = np.loadtxt("data/sample_rent1.csv", delimiter=",", skiprows=1) x = data[:, 1:3] y = data[:, 3:4] # ¥Ç¡¼¥¿¤Î¸Ä¿ô m = len(y) # Àµµ¬²½¤·¡¢x0¤òÄɲà X_norm = normalize(x) # ½é´üÃÍ w_int = np.zeros((3, 1)) # ³Ø½¬Î¨ alpha = 0.01 # ³Ø½¬²ó¿ô iter_num = 1000 # ºÇµÞ¹ß²¼Ë¡¤Ë¤è¤ëʬÀÏ¤Î¼Â¹Ô w, costs = gradient_descent(X_norm, y, w_int, alpha, iter_num) # -------------------------- # ºîÀ®¤·¤¿¥â¥Ç¥ë¤ò»ÈÍѤ·¤ÆÊ̤Υǡ¼¥¿¤òͽ¬¤·¤Æ¤ß¤ë # -------------------------- z = np.array([[60, 10], [50, 10], [40, 10]]) result = normalize(x, z).dot(w) for i in range(z.shape[0]): print("¹¤µ: {}Ö, ÃÛǯ¿ô: {}ǯ ¢â {:0.1f}Ëü±ß".format(z[i,0], z[i,1], result[i,0])) # -------------------------- # ¥°¥é¥Õɽ¼¨ # -------------------------- fig = plt.figure(figsize=(10, 5)) # 3D¥°¥é¥Õ ax = fig.add_subplot(1, 2, 1, projection='3d') ax.scatter(data[:, 1], data[:, 2], data[:, 3], color="#ef1234") ax.set_xlabel("Size") ax.set_ylabel("Age") ax.set_zlabel("Rent") # ³Ø½¬Ëè¤Î¥³¥¹¥È ax2 = fig.add_subplot(1, 2, 2) ax2.plot(range(costs.size), costs[:, 0], "r") ax2.set_xlabel("iterations") ax2.set_ylabel("cost") ax2.grid(True) plt.show() ·ë²Ì ¹¤µ: 60Ö, ÃÛǯ¿ô: 10ǯ ¢â 8.1Ëü±ß ¹¤µ: 50Ö, ÃÛǯ¿ô: 10ǯ ¢â 7.2Ëü±ß ¹¤µ: 40Ö, ÃÛǯ¿ô: 10ǯ ¢â 6.3Ëü±ß |