新手小白刚刚开始接触机器学习,写了一个线性回归的代码,但是跑出来的结果一直不对,黔驴技穷了,求大佬指点
class LinearRegression: def read_data(self): data = np.genfromtxt("data.csv", delimiter=',') x_num = data.shape[1] - 1 data_num = data.shape[0] bais = np.ones(data_num).reshape((data_num, 1)) x = data[:, 0 : x_num].reshape((data_num, x_num)) x = np.concatenate((bais, x), axis=1) y = data[:, -1].reshape((data_num, 1)) return data, x, y def generate_parameter(self, x_num): parameter = np.zeros((x_num, 1)) return parameter def compute_cost(self, x, y, parameter): tmp = np.dot(x, parameter) - y return 0.5 * float(np.dot(np.transpose(tmp), tmp)) def gradient_descent(self, x, y, parameter, lr): tmp = np.dot(x, parameter) - y gradient = np.dot(np.transpose(x), tmp) parameter_updated = parameter - lr * gradient return parameter_updated def train_model(self, x, y, parameter, lr, iteration): cost_list = [] para_tmp = parameter for i in range(iteration): cost_list.append(self.compute_cost(x, y, para_tmp)) para_tmp = self.gradient_descent(x, y, para_tmp, lr) return para_tmp, cost_list
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)