<jupyter_start><jupyter_text>Logistic Regression - scikit-learn-datasets  Imports<jupyter_code>from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import torch
import numpy as np
import torch.nn as nn
from torch.nn import functional as F<jupyter_output><empty_output><jupyter_text>Data Preparation<jupyter_code>breast_cancer = datasets.load_breast_cancer()
#print(breast_cancer.DESCR)
X = breast_cancer.data
y = breast_cancer.target
types = breast_cancer.target_names
types<jupyter_output><empty_output><jupyter_text>> **Splitting the data** - into train and test<jupyter_code>X_train, X_test, y_train, y_test= train_test_split(X, y, random_state=42, test_size=.2)<jupyter_output><empty_output><jupyter_text>> **Scaling the data** - using the `StandardScaler`<jupyter_code>scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>> **Converting data** - Convering the data into ``pytorch`` tensors.<jupyter_code>X_train_tensors =torch.from_numpy(X_train.astype('float32'))
X_test_tensors =torch.from_numpy(X_test.astype('float32'))

y_train_tensors =torch.from_numpy(y_train.astype('float32'))
y_test_tensors =torch.from_numpy(y_test.astype('float32'))
X_train_tensors.shape, y_train_tensors.shape, X_test_tensors.shape, y_test_tensors.shape<jupyter_output><empty_output><jupyter_text>Changing the y_shapes<jupyter_code>y_test_tensors = y_test_tensors.view(y_test_tensors.shape[0], 1)
y_train_tensors = y_train_tensors.view(y_train_tensors.shape[0], 1)
y_test_tensors[:2], y_train_tensors[:5]<jupyter_output><empty_output><jupyter_text>Model Creation<jupyter_code>input_shape = X_test_tensors.shape[-1]
input_shape 
class LogisticRegression(nn.Module):
    def __init__(self):
        super().__init__()
        self.linn = nn.Linear(input_shape, 1)
        
    def forward(self, X):
        return torch.sigmoid(self.linn(X)) ## good for binary classifiction problems
    
    
model = LogisticRegression()
model<jupyter_output><empty_output><jupyter_text>Loss and Optimizer> For the `loss` we are going to use `Binary Cross Entropy Loss Function (BCELoss)` and `SGD()` optimizer<jupyter_code>loss_function = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)<jupyter_output><empty_output><jupyter_text>Trainning the Model<jupyter_code>EPOCHS = 200

for epoch in range(EPOCHS):
    # forward pass 
    y_pred = model(X_train_tensors)
    # loss
    loss = loss_function(y_pred, y_train_tensors) ## Pass the y_pred first
    # backward pass
    loss.backward()
    ## update weights
    optimizer.step()
    ## zero grad
    optimizer.zero_grad()
    if epoch % 20 == 0:
        print(f"Epochs: {epoch+1}/{EPOCHS}, loss: {loss.item():.4f}")<jupyter_output>Epochs: 1/200, loss: 0.7163
Epochs: 21/200, loss: 0.4684
Epochs: 41/200, loss: 0.3641
Epochs: 61/200, loss: 0.3081
Epochs: 81/200, loss: 0.2729
Epochs: 101/200, loss: 0.2485
Epochs: 121/200, loss: 0.2303
Epochs: 141/200, loss: 0.2162
Epochs: 161/200, loss: 0.2048
Epochs: 181/200, loss: 0.1953<jupyter_text>Evaluating the Model<jupyter_code>total, correct = 0, 0

with torch.no_grad():
    y_pred = torch.round(model(X_test_tensors))
    
    for i, j in zip(y_pred, y_test_tensors):
        if i == j:
            correct+=1
        total+=1
    print(f"Accuracy: {correct/total:.2f}")<jupyter_output>Accuracy: 0.98<jupyter_text>Making prediction<jupyter_code>torch.round(model(X_test_tensors[:2])), y_test_tensors[:2]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12172701949860724, 'fraction_numerical': 0.04011142061281337, 'mean_word_length': 6.068897637795276, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '439844', 'license': 'MIT', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1376, 'n_words': 241, 'path': '01_PyTorch_Basics/07_Logistic_Regression/Logistic_Regression.ipynb', 'repo_name': 'CrispenGari/pytorch-python', 'size': '3590'}