<jupyter_start><jupyter_text>7. Logistic Regression<jupyter_code>import torch
import torch.nn as nn
import torch.optim as optim

import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>7.1 Prepare Data @ Iris DataData Set Information:This is perhaps the best known database to be found in the pattern recognition literature. Fisher's paper is a classic in the field and is referenced frequently to this day. (See Duda & Hart, for example.) The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is linearly separable from the other 2; the latter are NOT linearly separable from each other. Predicted attribute: class of iris plant. This is an exceedingly simple domain. This data differs from the data presented in Fishers article (identified by <NAME>, spchadwick '@' espeedaz.net ). The 35th sample should be: 4.9,3.1,1.5,0.2,"Iris-setosa" where the error is in the fourth feature. The 38th sample: 4.9,3.6,1.4,0.1,"Iris-setosa" where the errors are in the second and third features.Attribute Information:1. sepal(꽃잎) length in cm 2. sepal(꽃잎) width in cm 3. petal(꽃받침) length in cm 4. petal(꽃받침) width in cm 5. class: -- Iris Setosa (부채붓꽃)-- Iris Versicolour (...) -- Iris Virginica (...)<jupyter_code>iris = datasets.load_iris()

#sepal length in cm, sepal width in cm
X = iris.data[:, :2]

#-- Iris Setosa v.s. -- Iris Versicolour-- Iris Virginica

def f(x) :
    if x < 1 :
        return 0
    else :
        return 1
    
Y = np.array(list(map(f, iris.target)))
plt.scatter(X[:, 0], X[:, 1], c = Y)
plt.colorbar()
plt.show()<jupyter_output><empty_output><jupyter_text>7.2 Define Model<jupyter_code>x, y = torch.from_numpy(X).float(), torch.from_numpy(Y).float()
x, y
linear = nn.Linear(2, 1, bias = True)
sigmoid = nn.Sigmoid()

model = nn.Sequential(linear, sigmoid)
#Binary Cross Entropy Loss
loss = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)<jupyter_output><empty_output><jupyter_text>7.3 Train Model<jupyter_code>for step in range(2000):
    prob = model(x)
    cost = loss(prob, y.view(150, 1))
    
    optimizer.zero_grad()
    cost.backward()
    optimizer.step()
    
    if step % 200 == 0 :
        print("Step : ",step, ", Cost : ", cost.item())<jupyter_output>Step :  0 , Cost :  0.7005308270454407
Step :  200 , Cost :  0.46228280663490295
Step :  400 , Cost :  0.4046521782875061
Step :  600 , Cost :  0.3599453866481781
Step :  800 , Cost :  0.3245868682861328
Step :  1000 , Cost :  0.29608064889907837
Step :  1200 , Cost :  0.2726869583129883
Step :  1400 , Cost :  0.2531803548336029
Step :  1600 , Cost :  0.2366841435432434
Step :  1800 , Cost :  0.22255904972553253<jupyter_text>7.4 Test Model<jupyter_code>fig = plt.figure(figsize = (16, 5))

ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)

z1_plot = ax1.scatter(X[:, 0], X[:, 1], c = Y)
z2_plot = ax2.scatter(X[:, 0], X[:, 1], c = model(x).squeeze().data.numpy() > 0.5)

plt.colorbar(z1_plot,ax=ax1)
plt.colorbar(z2_plot,ax=ax2)

ax1.set_title("REAL")
ax2.set_title("PREDICT")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12763281986796604, 'fraction_numerical': 0.09242376611128576, 'mean_word_length': 4.107544141252006, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '218211', 'license': 'MIT', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1222, 'n_words': 349, 'path': 'Week3/07. Logistic Regression.ipynb', 'repo_name': 'Harry24k/Pytorch-Basic', 'size': '3189'}