logistics regression python
时间: 2023-09-03 11:12:31 浏览: 121
Sure, I can help you with that! Logistic regression is a popular algorithm for binary classification problems. In Python, you can use the scikit-learn library to implement logistic regression.
Here's an example code snippet for logistic regression in Python:
```python
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Assuming you have your data and labels ready
X = ... # Input features
y = ... # Binary labels
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a logistic regression model
model = LogisticRegression()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Calculate accuracy score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
This code imports the necessary modules, splits the data into training and testing sets using `train_test_split`, creates a logistic regression model using `LogisticRegression()`, trains the model using `fit`, makes predictions on the test set using `predict`, and calculates the accuracy score using `accuracy_score`.
Make sure to replace `X` and `y` with your actual data and labels. Additionally, you may need to preprocess your data before applying logistic regression.
I hope this helps you get started with logistic regression in Python! Let me know if you have any further questions.
阅读全文