(1)使用KNN算法对泰坦尼克号数据集进行分类 (2)使用测试集测试训练好的模型,并打印准确率
时间: 2023-12-14 11:39:47 浏览: 112
好的,我来为您介绍如何使用KNN算法对泰坦尼克号数据集进行分类。
首先,我们需要导入所需的库和数据集。这里我使用的是Python的scikit-learn库自带的泰坦尼克号数据集。代码如下:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
titanic = load_titanic()
X = titanic.data
y = titanic.target
```
然后,我们对数据集进行训练集和测试集的划分。这里我将数据集划分为70%的训练集和30%的测试集。代码如下:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
```
接下来,我们创建一个KNN分类器,并使用训练集对其进行训练。这里我选择的K值为5。代码如下:
```python
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
```
最后,我们使用测试集对训练好的模型进行测试,并打印出准确率。代码如下:
```python
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("准确率为:{:.2f}%".format(accuracy*100))
```
完整代码如下:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
titanic = load_titanic()
X = titanic.data
y = titanic.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("准确率为:{:.2f}%".format(accuracy*100))
```
希望能对您有所帮助!
阅读全文