rewrite your code above to use Pipeline
时间: 2024-04-10 12:33:18 浏览: 19
Sure! Here's the rewritten code using `Pipeline` from scikit-learn:
```python
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
# Define the features and target variable
features = ['INDUS', 'CRIM']
target = 'target'
# Create the pipeline
pipeline = Pipeline([
('scaler', StandardScaler()),
('regressor', LinearRegression())
])
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(bos[features], bos[target], test_size=0.2, random_state=0)
# Fit the pipeline on the training data
pipeline.fit(X_train, y_train)
# Evaluate the model on the training and testing data
train_accuracy = pipeline.score(X_train, y_train)
test_accuracy = pipeline.score(X_test, y_test)
print("Training accuracy:", train_accuracy)
print("Testing accuracy:", test_accuracy)
```
In this code, we create a `Pipeline` object consisting of two steps: a `StandardScaler` for feature scaling and a `LinearRegression` model. The `ColumnTransformer` is not required here since we only have numerical features.
We then split the data into training and testing sets using `train_test_split`. Next, we fit the pipeline on the training data and evaluate the model's accuracy on both the training and testing data using the `score` method. Finally, we print the training and testing accuracies.