python逻辑回归工具包
时间: 2023-12-08 13:39:11 浏览: 159
Python中有很多逻辑回归工具包,其中比较常用的是sklearn库。下面是使用sklearn库实现线性逻辑回归和非线性逻辑回归的示例代码:
1. 线性逻辑回归
```python
from sklearn.linear_model import LogisticRegression
# 构造数据集
X = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
y = [0, 0, 0, 1, 1, 1]
# 创建逻辑回归模型
clf = LogisticRegression()
# 训练模型
clf.fit(X, y)
# 预测新数据
print(clf.predict([[0, 0], [7, 8]]))
```
2. 非线性逻辑回归
```python
from sklearn.datasets import make_classification
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
# 构造数据集
X, y = make_classification(n_features=2, n_redundant=0, n_informative=1, random_state=42)
# 创建逻辑回归模型
model = make_pipeline(PolynomialFeatures(3), LogisticRegression())
# 训练模型
model.fit(X, y)
# 预测新数据
print(model.predict([[-0.5, 0.5], [0.5, -0.5]]))
```
阅读全文