breast cancer classification in python

To classify breast cancer using Python, we can use the Scikit-learn library, which provides a variety of machine learning algorithms.

  1. Load the Dataset
main.py
from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
X = data['data']
y = data['target']
113 chars
6 lines
  1. Split the Dataset
main.py
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)
144 chars
4 lines
  1. Train the Model

We can use the Support Vector Machine (SVM) algorithm to train our breast cancer classifier.

main.py
from sklearn.svm import SVC

model = SVC(kernel='linear')
model.fit(X_train, y_train)
86 chars
5 lines
  1. Evaluate the Model
main.py
from sklearn.metrics import accuracy_score

y_pred_train = model.predict(X_train)
train_accuracy = accuracy_score(y_train, y_pred_train)

y_pred_test = model.predict(X_test)
test_accuracy = accuracy_score(y_test, y_pred_test)

print('Train accuracy:', train_accuracy)
print('Test accuracy:', test_accuracy)
307 chars
11 lines

This will give us the accuracy of our model for both the training and test sets. We can also use other metrics such as precision, recall, and F1-score to evaluate our model.

Note: This is just one example of how to classify breast cancer using Python. There are many other algorithms and techniques that can be used depending on the requirements of the problem.

gistlibby LogSnag