1.1目標(biāo)
使用 scikit-learn 培訓(xùn) Logit模型模型。
1.2數(shù)據(jù)集
import numpy as np
X = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])
y = np.array([0, 0, 0, 1, 1, 1])
1.3Fit模型
下面的代碼導(dǎo)入了 scikit-learn 的 Logit模型模型。您可以通過(guò)調(diào)用 fit 函數(shù)將此模型適合于訓(xùn)練數(shù)據(jù)。
from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression()
lr_model.fit(X, y)
輸出:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1, penalty='l2', random_state=None, solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
1.4預(yù)測(cè)
通過(guò)調(diào)用預(yù)測(cè)函數(shù),您可以看到這個(gè)模型所做的預(yù)測(cè)。
y_pred = lr_model.predict(X)
print("Prediction on training set:", y_pred)
輸出:
Prediction on training set: [1 1 1 1 1 1]
1.5計(jì)算準(zhǔn)確度
您可以通過(guò)調(diào)用 score 函數(shù)來(lái)計(jì)算這個(gè)模型的精度。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-565755.html
print("Accuracy on training set:", lr_model.score(X, y))
輸出:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-565755.html
Accuracy on training set: 0.5
到了這里,關(guān)于吳恩達(dá)機(jī)器學(xué)習(xí)2022-Jupyter-用scikitlearn實(shí)現(xiàn)邏輯回歸的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!