一、參數估計方法介紹
參數估計方法是指在統計學中用來確定一組未知參數的特定過程。它是指從樣本數據中計算參數的方法,如均值、方差、標準偏差等。參數估計方法在統計學中有着廣泛的應用,如數據分析、預測、推斷等。
二、最小二乘法
最小二乘法是參數估計方法中的一種常見方法,它是指尋找一條曲線,使得該曲線上的點到樣本數據點的距離平方和最小。在回歸分析中,最小二乘法用於擬合一條直線,使其能夠最好地描述數據集的相關性。
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4, 5])
y = np.array([1.5, 3.5, 5.5, 7.5, 9.5])
fit = np.polyfit(x, y, 1)
fit_fn = np.poly1d(fit)
plt.plot(x, y, 'ro', x, fit_fn(x), '--k')
plt.title('Example of Least Squares Regression')
plt.show()
三、最大似然估計
最大似然估計是參數估計中最常用的一種方法之一,它是指在給定某些觀測數據的條件下,尋找一個能夠最大化這些數據出現的概率的參數值。最大似然估計在機器學習中以及眾多其他領域有着廣泛的應用。
import numpy as np
def log_likelihood(theta, x, y):
m = len(y)
y_pred = x.dot(theta)
error = y - y_pred
likelihood = (1 / np.sqrt(2 * np.pi * m)) * np.exp(-(error ** 2) / (2 * m))
log_likelihood = np.sum(np.log(likelihood))
return log_likelihood
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]
eta = 0.01
n_iterations = 1000
m = 100
theta = np.random.randn(2, 1)
for iteration in range(n_iterations):
gradients = 2 / m * X_b.T.dot(X_b.dot(theta) - y)
theta = theta - eta * gradients
max_log_likelihood = -np.inf
best_theta = None
for epoch in range(1000):
random_theta = np.random.randn(2, 1)
log_likelihood_value = log_likelihood(random_theta, X_b, y)
if log_likelihood_value > max_log_likelihood:
max_log_likelihood = log_likelihood_value
best_theta = random_theta
四、貝葉斯調參
貝葉斯調參是一種結合貝葉斯理論和數值優化的參數估計方法,它能夠利用先驗概率信息來尋找最優的超參數。貝葉斯調參在機器學習中的應用較為廣泛,能夠有效地提高模型的性能。
from hyperopt import hp, fmin, tpe, Trials
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
iris = load_iris()
X = iris.data
y = iris.target
space = {
'C': hp.loguniform('C', -10, 10),
'penalty': hp.choice('penalty', ['l1', 'l2']),
'fit_intercept': hp.choice('fit_intercept', [True, False])
}
def hyperparameter_tuning(space):
model = LogisticRegression(C=space['C'], penalty=space['penalty'], fit_intercept=space['fit_intercept'])
accuracy = cross_val_score(model, X, y=y, cv=5).mean()
return {'loss': -accuracy, 'status': 'ok'}
trials = Trials()
best = fmin(fn=hyperparameter_tuning, space=space, algo=tpe.suggest, max_evals=100, trials=trials)
print(best)
五、正則化參數估計
正則化參數估計是指在優化過程中加入懲罰項以防止過度擬合的參數估計方法。通過對模型中的參數進行正則化,可以減小參數的絕對值,從而達到控制模型複雜度的目的。
from sklearn.linear_model import Lasso lasso = Lasso(alpha=0.1) lasso.fit(X, y) print(lasso.coef_)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/160099.html
微信掃一掃
支付寶掃一掃