線性回歸第一個機器學習算法 - 單變量線性回歸
"""
使用sklearn實現線性回歸
"""
import numpy as np
from sklearn.linear_model import LinearRegression
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))
#encoding=utf-8
"""
線性回歸實現梯度下降的批處理(batch_gradient_descent )
"""
import numpy as np
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X]
#print(X_b)
learning_rate = 0.1
#通常在做機器學習的時候,一般不會等到他收斂,因為太浪費時間,所以會設置一個收斂次數
n_iterations = 1000
m = 100
#1.初始化theta, w0...wn
theta = np.random.randn(2, 1)
count = 0
#4. 不會設置閾值,之間設置超參數,迭代次數,迭代次數到了,我們就認為收斂了
for iteration in range(n_iterations):
count += 1
#2. 接著求梯度gradient
gradients = 1.0/m * X_b.T.dot(X_b.dot(theta)-y)
#3. 應用公式調整theta值, theta_t + 1 = theta_t - grad * learning_rate
theta = theta - learning_rate * gradients
print(count)
print(theta)
import numpy as np
import time
a = np.array([1,2,3,4])
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
c = np.dot(a,b)
toc = time.time()
print("Vectorized version:" + str(1000*(toc-tic)) + 'ms')
c = 0
tic = time.time()
for i in range(1000000):
c += a[i]*b[i]
toc = time.time()
print("for loop:" + str(1000*(toc-tic)) + 'ms')
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。