X has 11 features, but LogisticRegression is expecting 2 features as input.
时间: 2023-12-14 13:03:31 浏览: 173
If you have 11 features in your dataset but LogisticRegression is expecting only 2 features as input, you might need to perform feature selection or dimensionality reduction techniques to reduce the number of features before feeding them into the LogisticRegression model. Here's an example of using Principal Component Analysis (PCA) for dimensionality reduction:
```python
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load data
X, y = load_data()
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Perform PCA for dimensionality reduction
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)
# Create and train the logistic regression model
logreg = LogisticRegression()
logreg.fit(X_train_pca, y_train)
# Make predictions on the test set
y_pred = logreg.predict(X_test_pca)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
In this example, we first use PCA to reduce the dimensionality of the input features from 11 to 2. Then, we create a LogisticRegression model and train it using the transformed training data. Finally, we make predictions on the transformed test data and calculate the accuracy.
Note that PCA is just one example of dimensionality reduction technique. Depending on your specific problem, you may consider other techniques such as feature selection or other dimensionality reduction methods like Linear Discriminant Analysis (LDA) or t-distributed Stochastic Neighbor Embedding (t-SNE).
阅读全文