0% found this document useful (0 votes)
23 views1 page

Labaihw

Uploaded by

aboudarghampeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Labaihw

Uploaded by

aboudarghampeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

from sklearn.

datasets import load_breast_cancer


from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

breast_cancer = load_breast_cancer()
X = breast_cancer.data
y = breast_cancer.target

print("Features:", breast_cancer.feature_names)
print("Target:", breast_cancer.target_names)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42)
k = 5
knn = KNeighborsClassifier(n_neighbors=k)

# Training the model


knn.fit(X_train, y_train)

# Make predictions on the test set


y_pred = knn.predict(X_test)

# Evaluate the accuracy of the model


accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy * 100, "%")

plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, marker= 'o', edgecolor


='k',s=100,label='training data')
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, marker= 'x', edgecolor
='k',s=100,label='testing data')
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.title("Breast Cancer Dataset (Feature 1 vs Feature 2)")
plt.show()

You might also like