defidentity_basis(x): ret = np.expand_dims(x, axis=1) return ret
defmultinomial_basis(x, feature_num=10): x = np.expand_dims(x, axis=1) # shape(N, 1) ret = [x] # 分别乘幂 for i inrange(2,feature_num+1): ret.append(x**i) #将乘幂的结果连起来 ret=np.concatenate(ret,axis=1) print(ret.shape) return ret
defgaussian_basis(x, feature_num=10): '''高斯基函数''' #========== #todo '''请实现高斯基函数''' #========== # 高斯基函数 centers = np.linspace(0,25,feature_num) s = centers[1]-centers[0] phi = np.expand_dims(x,axis=1) x = np.concatenate([phi]*feature_num,axis=1) t =( x - centers)/s ret = np.exp(- 0.5 * t ** 2) return ret
# def gradient(phi,y,initial_theta, eta=0.001, n_iters=10000): # w = initial_theta # for i in range(n_iters): # w = w - eta * dj(w,phi,y) # return w # initial_theta = np.zeros(phi.shape[1]) # w = gradient(phi,y_train,initial_theta)
deff(x): phi0 = np.expand_dims(np.ones_like(x), axis=1) phi1 = basis_func(x) phi = np.concatenate([phi0, phi1], axis=1) y = np.dot(phi, w) return y