Create Baseline Model An initial MLP Classifier was created using the Scikit-learn library to consider for a baseline
时间: 2024-04-30 16:20:35 浏览: 118
The first step in creating a baseline model is to import the necessary libraries. In this case, we will be using the Scikit-learn library for creating a Multilayer Perceptron (MLP) Classifier:
```python
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
```
Next, we need to load the data that we will be using to train and test the model. We can use the sklearn.datasets module to load a built-in dataset, such as the iris dataset:
```python
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
```
We then split the data into training and testing sets using the train_test_split function from the model_selection module:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
Now we can create our MLP Classifier and fit it to the training data:
```python
clf = MLPClassifier(hidden_layer_sizes=(5, 2), max_iter=1000)
clf.fit(X_train, y_train)
```
In this example, we have specified a MLP Classifier with two hidden layers of sizes 5 and 2, and a maximum number of iterations of 1000.
Finally, we can use the trained model to make predictions on the test data and evaluate its accuracy:
```python
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
This will output the accuracy of the model on the test data. This basic MLP Classifier can serve as a baseline for future model development and improvement.
阅读全文