From 4fd289f38a4158c919ab5e196e20c1884083ff9b Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 07:19:53 +1000 Subject: [PATCH 1/8] Create Image_Classification_Example_MNIST_CNN.py Image Classification Example --- .../Image_Classification_Example_MNIST_CNN.py | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 computer_vision/Image_Classification_Example_MNIST_CNN.py diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/Image_Classification_Example_MNIST_CNN.py new file mode 100644 index 000000000000..b80707a11978 --- /dev/null +++ b/computer_vision/Image_Classification_Example_MNIST_CNN.py @@ -0,0 +1,141 @@ +""" +This Script contains the default EMNIST code for comparison. + +Execution Details are available here: +https://www.kaggle.com/code/dipuk0506/mnist-cnn + +The code is modified from: + nextjournal.com/gkoehler/pytorch-mnist + +@author: Dipu Kabir +""" + +import torch, torchvision +import torch.nn as nn +import torch.nn.functional as F +import torch.optim as optim + +n_epochs = 8 +batch_size_train = 64 +batch_size_test = 1000 +learning_rate = 0.01 +momentum = 0.5 +log_interval = 100 + + +torch.backends.cudnn.enabled = False + + +train_loader = torch.utils.data.DataLoader( + torchvision.datasets.MNIST('/files/', train=True, download=True, + transform=torchvision.transforms.Compose([ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize( + (0.1307,), (0.3081,)) + ])), + batch_size=batch_size_train, shuffle=True) + +test_loader = torch.utils.data.DataLoader( + torchvision.datasets.MNIST('/files/', train=False, download=True, + transform=torchvision.transforms.Compose([ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize( + (0.1307,), (0.3081,)) + ])), + batch_size=batch_size_test, shuffle=True) + +examples = enumerate(test_loader) +batch_idx, (example_data, example_targets) = next(examples) + +print(example_data.shape) + +import matplotlib.pyplot as plt + +fig = plt.figure() +for i in range(6): + plt.subplot(2,3,i+1) + plt.tight_layout() + plt.imshow(example_data[i][0], cmap='gray', interpolation='none') + plt.title("Ground Truth: {}".format(example_targets[i])) + plt.xticks([]) + plt.yticks([]) +fig + + +class Net(nn.Module): + def __init__(self): + super(Net, self).__init__() + self.conv1 = nn.Conv2d(1, 10, kernel_size=5) + self.conv2 = nn.Conv2d(10, 20, kernel_size=5) + self.conv2_drop = nn.Dropout2d() + self.fc1 = nn.Linear(320, 50) + self.fc2 = nn.Linear(50, 10) + + def forward(self, x): + x = F.relu(F.max_pool2d(self.conv1(x), 2)) + x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = x.view(-1, 320) + x = F.relu(self.fc1(x)) + x = F.dropout(x, training=self.training) + x = self.fc2(x) + return F.log_softmax(x) + + + +network = Net() +optimizer = optim.SGD(network.parameters(), lr=learning_rate, + momentum=momentum) + + +train_losses = [] +train_counter = [] +test_losses = [] +test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] + + +def train(epoch): + network.train() + for batch_idx, (data, target) in enumerate(train_loader): + optimizer.zero_grad() + output = network(data) + loss = F.nll_loss(output, target) + loss.backward() + optimizer.step() + if batch_idx % log_interval == 0: + print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( + epoch, batch_idx * len(data), len(train_loader.dataset), + 100. * batch_idx / len(train_loader), loss.item())) + train_losses.append(loss.item()) + train_counter.append( + (batch_idx*64) + ((epoch-1)*len(train_loader.dataset))) + +def test(): + network.eval() + test_loss = 0 + correct = 0 + with torch.no_grad(): + for data, target in test_loader: + output = network(data) + test_loss += F.nll_loss(output, target, size_average=False).item() + pred = output.data.max(1, keepdim=True)[1] + correct += pred.eq(target.data.view_as(pred)).sum() + test_loss /= len(test_loader.dataset) + test_losses.append(test_loss) + print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( + test_loss, correct, len(test_loader.dataset), + 100. * correct / len(test_loader.dataset))) + + +test() +for epoch in range(1, n_epochs + 1): + train(epoch) + test() +#%% + +fig = plt.figure() +plt.plot(train_counter, train_losses, color='blue') +plt.scatter(test_counter, test_losses, color='red') +plt.legend(['Train Loss', 'Test Loss'], loc='upper right') +plt.xlabel('number of training examples seen') +plt.ylabel('negative log likelihood loss') +fig From 29671f5efd92fd35abc684565415566125ef291e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:28:16 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../Image_Classification_Example_MNIST_CNN.py | 157 ++++++++++-------- 1 file changed, 91 insertions(+), 66 deletions(-) diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/Image_Classification_Example_MNIST_CNN.py index b80707a11978..d9e34f7cf28e 100644 --- a/computer_vision/Image_Classification_Example_MNIST_CNN.py +++ b/computer_vision/Image_Classification_Example_MNIST_CNN.py @@ -1,7 +1,7 @@ """ This Script contains the default EMNIST code for comparison. -Execution Details are available here: +Execution Details are available here: https://www.kaggle.com/code/dipuk0506/mnist-cnn The code is modified from: @@ -27,22 +27,36 @@ train_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST('/files/', train=True, download=True, - transform=torchvision.transforms.Compose([ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize( - (0.1307,), (0.3081,)) - ])), - batch_size=batch_size_train, shuffle=True) + torchvision.datasets.MNIST( + "/files/", + train=True, + download=True, + transform=torchvision.transforms.Compose( + [ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize((0.1307,), (0.3081,)), + ] + ), + ), + batch_size=batch_size_train, + shuffle=True, +) test_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST('/files/', train=False, download=True, - transform=torchvision.transforms.Compose([ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize( - (0.1307,), (0.3081,)) - ])), - batch_size=batch_size_test, shuffle=True) + torchvision.datasets.MNIST( + "/files/", + train=False, + download=True, + transform=torchvision.transforms.Compose( + [ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize((0.1307,), (0.3081,)), + ] + ), + ), + batch_size=batch_size_test, + shuffle=True, +) examples = enumerate(test_loader) batch_idx, (example_data, example_targets) = next(examples) @@ -53,12 +67,12 @@ fig = plt.figure() for i in range(6): - plt.subplot(2,3,i+1) - plt.tight_layout() - plt.imshow(example_data[i][0], cmap='gray', interpolation='none') - plt.title("Ground Truth: {}".format(example_targets[i])) - plt.xticks([]) - plt.yticks([]) + plt.subplot(2, 3, i + 1) + plt.tight_layout() + plt.imshow(example_data[i][0], cmap="gray", interpolation="none") + plt.title("Ground Truth: {}".format(example_targets[i])) + plt.xticks([]) + plt.yticks([]) fig @@ -79,63 +93,74 @@ def forward(self, x): x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x) - - - + + network = Net() -optimizer = optim.SGD(network.parameters(), lr=learning_rate, - momentum=momentum) +optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) train_losses = [] train_counter = [] test_losses = [] -test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] +test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)] def train(epoch): - network.train() - for batch_idx, (data, target) in enumerate(train_loader): - optimizer.zero_grad() - output = network(data) - loss = F.nll_loss(output, target) - loss.backward() - optimizer.step() - if batch_idx % log_interval == 0: - print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( - epoch, batch_idx * len(data), len(train_loader.dataset), - 100. * batch_idx / len(train_loader), loss.item())) - train_losses.append(loss.item()) - train_counter.append( - (batch_idx*64) + ((epoch-1)*len(train_loader.dataset))) - + network.train() + for batch_idx, (data, target) in enumerate(train_loader): + optimizer.zero_grad() + output = network(data) + loss = F.nll_loss(output, target) + loss.backward() + optimizer.step() + if batch_idx % log_interval == 0: + print( + "Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format( + epoch, + batch_idx * len(data), + len(train_loader.dataset), + 100.0 * batch_idx / len(train_loader), + loss.item(), + ) + ) + train_losses.append(loss.item()) + train_counter.append( + (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)) + ) + + def test(): - network.eval() - test_loss = 0 - correct = 0 - with torch.no_grad(): - for data, target in test_loader: - output = network(data) - test_loss += F.nll_loss(output, target, size_average=False).item() - pred = output.data.max(1, keepdim=True)[1] - correct += pred.eq(target.data.view_as(pred)).sum() - test_loss /= len(test_loader.dataset) - test_losses.append(test_loss) - print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( - test_loss, correct, len(test_loader.dataset), - 100. * correct / len(test_loader.dataset))) - - + network.eval() + test_loss = 0 + correct = 0 + with torch.no_grad(): + for data, target in test_loader: + output = network(data) + test_loss += F.nll_loss(output, target, size_average=False).item() + pred = output.data.max(1, keepdim=True)[1] + correct += pred.eq(target.data.view_as(pred)).sum() + test_loss /= len(test_loader.dataset) + test_losses.append(test_loss) + print( + "\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n".format( + test_loss, + correct, + len(test_loader.dataset), + 100.0 * correct / len(test_loader.dataset), + ) + ) + + test() for epoch in range(1, n_epochs + 1): - train(epoch) - test() -#%% + train(epoch) + test() +# %% fig = plt.figure() -plt.plot(train_counter, train_losses, color='blue') -plt.scatter(test_counter, test_losses, color='red') -plt.legend(['Train Loss', 'Test Loss'], loc='upper right') -plt.xlabel('number of training examples seen') -plt.ylabel('negative log likelihood loss') +plt.plot(train_counter, train_losses, color="blue") +plt.scatter(test_counter, test_losses, color="red") +plt.legend(["Train Loss", "Test Loss"], loc="upper right") +plt.xlabel("number of training examples seen") +plt.ylabel("negative log likelihood loss") fig From 4e3484a3cf8b34a4166f96d4f3448785dff6e50d Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 14:18:40 +1000 Subject: [PATCH 3/8] Update and rename Image_Classification_Example_MNIST_CNN.py to image_classification_example_mnist_cnn.py --- ...image_classification_example_mnist_cnn.py} | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) rename computer_vision/{Image_Classification_Example_MNIST_CNN.py => image_classification_example_mnist_cnn.py} (88%) diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/image_classification_example_mnist_cnn.py similarity index 88% rename from computer_vision/Image_Classification_Example_MNIST_CNN.py rename to computer_vision/image_classification_example_mnist_cnn.py index d9e34f7cf28e..5aa8761ca588 100644 --- a/computer_vision/Image_Classification_Example_MNIST_CNN.py +++ b/computer_vision/image_classification_example_mnist_cnn.py @@ -4,15 +4,16 @@ Execution Details are available here: https://www.kaggle.com/code/dipuk0506/mnist-cnn -The code is modified from: +The code is improved from: nextjournal.com/gkoehler/pytorch-mnist @author: Dipu Kabir """ -import torch, torchvision +import torch +import torchvision import torch.nn as nn -import torch.nn.functional as F +import torch.nn.functional as functional import torch.optim as optim n_epochs = 8 @@ -86,13 +87,13 @@ def __init__(self): self.fc2 = nn.Linear(50, 10) def forward(self, x): - x = F.relu(F.max_pool2d(self.conv1(x), 2)) - x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = functional.relu(functional.max_pool2d(self.conv1(x), 2)) + x = functional.relu(functional.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) - x = F.relu(self.fc1(x)) - x = F.dropout(x, training=self.training) + x = functional.relu(self.fc1(x)) + x = functional.dropout(x, training=self.training) x = self.fc2(x) - return F.log_softmax(x) + return functional.log_softmax(x) network = Net() @@ -110,7 +111,7 @@ def train(epoch): for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() output = network(data) - loss = F.nll_loss(output, target) + loss = functional.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % log_interval == 0: @@ -136,7 +137,7 @@ def test(): with torch.no_grad(): for data, target in test_loader: output = network(data) - test_loss += F.nll_loss(output, target, size_average=False).item() + test_loss += functional.nll_loss(output, target, size_average=False).item() pred = output.data.max(1, keepdim=True)[1] correct += pred.eq(target.data.view_as(pred)).sum() test_loss /= len(test_loader.dataset) From 7b70f64d5adf1e5717a17af167856b92f2287913 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 15:57:55 +1000 Subject: [PATCH 4/8] Update image_classification_example_mnist_cnn.py --- computer_vision/image_classification_example_mnist_cnn.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/computer_vision/image_classification_example_mnist_cnn.py b/computer_vision/image_classification_example_mnist_cnn.py index 5aa8761ca588..85fee01d45fe 100644 --- a/computer_vision/image_classification_example_mnist_cnn.py +++ b/computer_vision/image_classification_example_mnist_cnn.py @@ -10,6 +10,8 @@ @author: Dipu Kabir """ +! pip install torch + import torch import torchvision import torch.nn as nn From a4b73300d8d76045b708ae653bfca7dd07047a6e Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 16:07:14 +1000 Subject: [PATCH 5/8] Delete computer_vision/image_classification_example_mnist_cnn.py --- .../image_classification_example_mnist_cnn.py | 169 ------------------ 1 file changed, 169 deletions(-) delete mode 100644 computer_vision/image_classification_example_mnist_cnn.py diff --git a/computer_vision/image_classification_example_mnist_cnn.py b/computer_vision/image_classification_example_mnist_cnn.py deleted file mode 100644 index 85fee01d45fe..000000000000 --- a/computer_vision/image_classification_example_mnist_cnn.py +++ /dev/null @@ -1,169 +0,0 @@ -""" -This Script contains the default EMNIST code for comparison. - -Execution Details are available here: -https://www.kaggle.com/code/dipuk0506/mnist-cnn - -The code is improved from: - nextjournal.com/gkoehler/pytorch-mnist - -@author: Dipu Kabir -""" - -! pip install torch - -import torch -import torchvision -import torch.nn as nn -import torch.nn.functional as functional -import torch.optim as optim - -n_epochs = 8 -batch_size_train = 64 -batch_size_test = 1000 -learning_rate = 0.01 -momentum = 0.5 -log_interval = 100 - - -torch.backends.cudnn.enabled = False - - -train_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST( - "/files/", - train=True, - download=True, - transform=torchvision.transforms.Compose( - [ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize((0.1307,), (0.3081,)), - ] - ), - ), - batch_size=batch_size_train, - shuffle=True, -) - -test_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST( - "/files/", - train=False, - download=True, - transform=torchvision.transforms.Compose( - [ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize((0.1307,), (0.3081,)), - ] - ), - ), - batch_size=batch_size_test, - shuffle=True, -) - -examples = enumerate(test_loader) -batch_idx, (example_data, example_targets) = next(examples) - -print(example_data.shape) - -import matplotlib.pyplot as plt - -fig = plt.figure() -for i in range(6): - plt.subplot(2, 3, i + 1) - plt.tight_layout() - plt.imshow(example_data[i][0], cmap="gray", interpolation="none") - plt.title("Ground Truth: {}".format(example_targets[i])) - plt.xticks([]) - plt.yticks([]) -fig - - -class Net(nn.Module): - def __init__(self): - super(Net, self).__init__() - self.conv1 = nn.Conv2d(1, 10, kernel_size=5) - self.conv2 = nn.Conv2d(10, 20, kernel_size=5) - self.conv2_drop = nn.Dropout2d() - self.fc1 = nn.Linear(320, 50) - self.fc2 = nn.Linear(50, 10) - - def forward(self, x): - x = functional.relu(functional.max_pool2d(self.conv1(x), 2)) - x = functional.relu(functional.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) - x = x.view(-1, 320) - x = functional.relu(self.fc1(x)) - x = functional.dropout(x, training=self.training) - x = self.fc2(x) - return functional.log_softmax(x) - - -network = Net() -optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) - - -train_losses = [] -train_counter = [] -test_losses = [] -test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)] - - -def train(epoch): - network.train() - for batch_idx, (data, target) in enumerate(train_loader): - optimizer.zero_grad() - output = network(data) - loss = functional.nll_loss(output, target) - loss.backward() - optimizer.step() - if batch_idx % log_interval == 0: - print( - "Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format( - epoch, - batch_idx * len(data), - len(train_loader.dataset), - 100.0 * batch_idx / len(train_loader), - loss.item(), - ) - ) - train_losses.append(loss.item()) - train_counter.append( - (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)) - ) - - -def test(): - network.eval() - test_loss = 0 - correct = 0 - with torch.no_grad(): - for data, target in test_loader: - output = network(data) - test_loss += functional.nll_loss(output, target, size_average=False).item() - pred = output.data.max(1, keepdim=True)[1] - correct += pred.eq(target.data.view_as(pred)).sum() - test_loss /= len(test_loader.dataset) - test_losses.append(test_loss) - print( - "\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n".format( - test_loss, - correct, - len(test_loader.dataset), - 100.0 * correct / len(test_loader.dataset), - ) - ) - - -test() -for epoch in range(1, n_epochs + 1): - train(epoch) - test() -# %% - -fig = plt.figure() -plt.plot(train_counter, train_losses, color="blue") -plt.scatter(test_counter, test_losses, color="red") -plt.legend(["Train Loss", "Test Loss"], loc="upper right") -plt.xlabel("number of training examples seen") -plt.ylabel("negative log likelihood loss") -fig From 158c1feaf3908b4cf80c44032d145374d6d8641d Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 22:23:49 +1000 Subject: [PATCH 6/8] Create probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 maths/probability_of_n_heads_in_m_tossing.py diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py new file mode 100644 index 000000000000..e8a39fc2d647 --- /dev/null +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -0,0 +1,43 @@ +import numpy + +def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: + """ + Calculate the factorial of specified number (n!). + + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True + >>> Probability_of_n_heads_in_m_tossing(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> Probability_of_n_heads_in_m_tossing(-1,5) + Traceback (most recent call last): + ... + ValueError: The function is not defined for negative values + >>> Probability_of_n_heads_in_m_tossing(3,2) + Traceback (most recent call last): + ... + ValueError: Head count should be smaller than toss count + + >>> Probability_of_n_heads_in_m_tossing(1,1) + 0.5 + >>> Probability_of_n_heads_in_m_tossing(0,2) + 0.25 + >>> Probability_of_n_heads_in_m_tossing(2,3) + 0.375 + """ + if head_count != int(head_count) or toss_count != int(toss_count): + raise ValueError("The function only accepts integer values") + if head_count < 0 or toss_count < 0: + raise ValueError("The function only accepts positive values") + if head_count > toss_count: + raise ValueError("Head count should be smaller than toss count") + + value = numpy.ones(1) + + for iter1 in range(toss_count): + value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0) + value = value/2 + + return value[head_count] From d8b68ba00027f6124fffbf1c9270fcaa6a63935f Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 22:51:17 +1000 Subject: [PATCH 7/8] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index e8a39fc2d647..ac48cba605f1 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,3 +1,27 @@ + + """ +Algorithm Explanation: +If you toss 0 time -> 0 head +Distribution [1] -> Meaning: 1 in the 0-index + +If you toss 1 time -> 0 or 1 head +Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes + +If you toss 2 times -> 0 to 2 heads +Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} + +If you toss 3 times -> 0 to 3 heads +Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} + +Therefore, +Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + +I used that method in my paper +Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions +Journal: ACM Computing Surveys (CSUR) +""" + + import numpy def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: From c49619c98124052c931d5c724a7bdca844a33ba4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:54:04 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/probability_of_n_heads_in_m_tossing.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index ac48cba605f1..c15acd0cc8f6 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -8,16 +8,16 @@ Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes If you toss 2 times -> 0 to 2 heads -Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} +Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} If you toss 3 times -> 0 to 3 heads -Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} +Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} Therefore, Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 -I used that method in my paper -Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions +I used that method in my paper +Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions Journal: ACM Computing Surveys (CSUR) """ @@ -43,7 +43,7 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int Traceback (most recent call last): ... ValueError: Head count should be smaller than toss count - + >>> Probability_of_n_heads_in_m_tossing(1,1) 0.5 >>> Probability_of_n_heads_in_m_tossing(0,2) @@ -57,11 +57,11 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int raise ValueError("The function only accepts positive values") if head_count > toss_count: raise ValueError("Head count should be smaller than toss count") - + value = numpy.ones(1) - + for iter1 in range(toss_count): value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0) value = value/2 - + return value[head_count]