利用sklearn掌握对数几率回归算法
时间: 2023-07-04 21:04:20 浏览: 110
对数几率回归
5星 · 资源好评率100%
对数几率回归(Logistic Regression)是一种用于建立分类模型的线性模型。它可以用于二分类和多分类问题。本文将介绍如何利用sklearn库来实现对数几率回归算法。
首先,我们需要导入sklearn的LogisticRegression模型:
```python
from sklearn.linear_model import LogisticRegression
```
接下来,我们需要准备训练集和测试集数据。这里我们使用sklearn库自带的鸢尾花数据集作为例子:
```python
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
```
然后,我们将数据集划分为训练集和测试集:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
随后,我们创建一个LogisticRegression对象,并使用fit方法拟合训练数据:
```python
clf = LogisticRegression(random_state=0).fit(X_train, y_train)
```
最后,我们可以使用predict方法来预测测试集数据的分类结果,并使用score方法计算模型在测试集上的准确率:
```python
y_pred = clf.predict(X_test)
score = clf.score(X_test, y_test)
print('Accuracy:', score)
```
完整代码如下:
```python
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a LogisticRegression object and fit the training data
clf = LogisticRegression(random_state=0).fit(X_train, y_train)
# Predict the class labels for the test set
y_pred = clf.predict(X_test)
# Calculate the model accuracy on the test set
score = clf.score(X_test, y_test)
# Print the model accuracy
print('Accuracy:', score)
```
输出结果为:
```
Accuracy: 1.0
```
这说明我们的模型在测试集上的分类准确率达到了100%。
阅读全文