本文共 4075 字,大约阅读时间需要 13 分钟。
#利用Sklearn来确定theta值做简单的二维线性回归的预测
import numpy as npfrom sklearn.linear_model import LinearRegression#模拟生成100行1列的数据x = 2*np.random.rand(100,1)y = 4 + 3*x + np.random.randn(100,1)#实例化lin_reg = LinearRegression()#fit方法就是训练模型的方法lin_reg.fit(x,y)# intercept指的是相当于截距, coef是参数print(lin_reg.intercept_,lin_reg.coef_)x_new = np.array([[0],[2]])print(lin_reg.predict(x_new))
# 批梯度下降法来进行参数的调试,利用的是将损失函数降到最小来确定theta值import numpy as np#模拟100行1列的x数据,X = 2 * np.random.rand(100, 1)y = 4 + 3 * X + np.random.randn(100, 1)#这一步是生成100*2的矩阵,主要是为了后面的计算:y = theta0*x0 + theta1*x1 (x0=1)X_b = np.c_[np.ones((100, 1)), X]# 表达式中的阿尔法值,即学习率learning_rate = 0.1# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数n_iterations = 100000# 表示训练集的总数m = 100# 1.初始化theta值,theta0和theta1theta = np.random.randn(2, 1)# 4. 不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了for iteration in range(n_iterations): # 2. 接着求梯度gradient gradients = 1/m * X_b.T.dot(X_b.dot(theta)-y) # 3. 应用公式调整theta值, theta_t + 1 = theta_t - grad * learning_rate # 同步更新 theta = theta - learning_rate * gradientsprint(theta)
import numpy as npX1 = 2 * np.random.rand(100, 1)X2 = 4 * np.random.rand(100, 1)X3 = 6 * np.random.rand(100, 1)y = 4 + 3 * X1 + 4 * X2 + 5 * X3 + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X1, X2, X3]print(X_b)learning_rate = 0.1# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数n_iterations = 1000#训练集的总数m = 100# 1.初始化theta0,theta1,theta2,theta3theta = np.random.randn(4, 1)count = 0# 4. 不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了for iteration in range(n_iterations): count += 1 # 2. 接着求梯度gradient gradients = 1/m * X_b.T.dot(X_b.dot(theta)-y) # 3. 应用公式调整theta值, theta_t + 1 = theta_t - grad * learning_rate theta = theta - learning_rate * gradientsprint(count)print(theta)
import numpy as npimport randomX = 2 * np.random.rand(100, 1)y = 4 + 3 * X + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X]n_epochs = 100a0 = 0.1#指的是a0的衰减率,decay_rate = 1m = 1num = [i for i in range(100)]def learning_schedule(epoch_num): return (1.0 / (decay_rate * epoch_num + 1)) * a0theta = np.random.randn(2, 1)# epoch 是轮次的意思,意思是用m个样本做一轮迭代for epoch in range(n_epochs): # 生成100个不重复的随机数 rand = random.sample(num, 100) for i in range(m): random_index = rand[i] #通过每遍历一次(也就是每下降一步),计算一次theta值,然后输出最终的较为准确的值 xi = X_b[random_index:random_index+1] # print(xi) yi = y[random_index:random_index+1] # print(yi) gradients = xi.T.dot(xi.dot(theta)-yi) learning_rate = learning_schedule(epoch + i) theta = theta - learning_rate * gradientsprint(theta)
import numpy as npX1 = 2 * np.random.rand(100, 1)X2 = 4 * np.random.rand(100, 1)X3 = 6 * np.random.rand(100, 1)y = 4 + 3 * X1 + 4 * X2 + 5 * X3 + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X1, X2, X3]print(X_b)learning_rate = 0.1# 通常在做机器学习的时候,一般不会等到他收敛,因为太浪费时间,所以会设置一个收敛次数n_iterations = 1000#训练集的总数m = 100# 1.初始化theta0,theta1,theta2,theta3theta = np.random.randn(4, 1)# 4. 不会设置阈值,之间设置超参数,迭代次数,迭代次数到了,我们就认为收敛了for iteration in range(n_iterations): # 2. 接着求梯度gradient gradients = 1/m * X_b.T.dot(X_b.dot(theta)-y) # 3. 应用公式调整theta值, theta_t + 1 = theta_t - grad * learning_rate theta = theta - learning_rate * gradientsprint(theta)
import numpy as npimport randomX = 2 * np.random.rand(100, 1)y = 4 + 3 * X + np.random.randn(100, 1)X_b = np.c_[np.ones((100, 1)), X]n_epochs = 500t0, t1 = 5, 50m = 100num = [i for i in range(100)]def learning_schedule(t): return float(t0) / (t + t1)theta = np.random.randn(2, 1)batch_num = 5batch_size = int(m / 5)# epoch 是轮次的意思,意思是用(i+1)*batch_size-i*batch_size个样本做一轮迭代,一共迭代n_epochs代for epoch in range(n_epochs): for i in range(batch_num): start = i*batch_size end = (i+1)*batch_size xi = X_b[start:end] yi = y[start:end] gradients = 1.0/batch_size * xi.T.dot(xi.dot(theta)-yi) learning_rate = learning_schedule(epoch*m + i) theta = theta - learning_rate * gradientsprint(theta)
转载地址:http://umbwi.baihongyu.com/