逻辑回归模型python
时间: 2023-10-12 07:15:06 浏览: 83
在Python中,可以使用sklearn库来实现逻辑回归模型。下面是一个简单的例子:
首先,导入必要的库和数据集:
```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
data = pd.read_csv('data.csv')
```
接着,将数据集分成训练集和测试集:
```python
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
然后,创建一个逻辑回归模型并拟合训练数据:
```python
model = LogisticRegression()
model.fit(X_train, y_train)
```
最后,使用测试数据评估模型的准确性:
```python
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
```
以上是一个简单的逻辑回归模型的实现示例,你可以根据自己的需求进行修改和扩展。
阅读全文