precision

Applying Decimal in Python

Applying Decimal in Python Question: I am trying to solve the following question: Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies? Old attempt with reference …

Total answers: 2

Python vs C++ Precision

Python vs C++ Precision Question: I am trying to reproduce a C++ high precision calculation in full python, but I got a slight difference and I do not understand why. Python: from decimal import * getcontext().prec = 18 r = 0 + (((Decimal(0.95)-Decimal(1.0))**2)+(Decimal(0.00403)-Decimal(0.00063))**2).sqrt() # r = Decimal(‘0.0501154666744709107’) C++: #include <iostream> #include <math.h> int main() { …

Total answers: 1

Python large numbers (float and integer)

Python large numbers (float and integer) Question: I am trying to understand the following calculation results. Out[1], Out[2] and Out[3] seem to be related to the limit on precision of floats, and Out[4] may be due to the fact that there is no limit on digits of int. Correct? I wonder if someone can explain …

Total answers: 1

Pytorch getting RuntimeError: Found dtype Double but expected Float

Pytorch getting RuntimeError: Found dtype Double but expected Float Question: I am trying to implement a neural net in PyTorch but it doesn’t seem to work. The problem seems to be in the training loop. I’ve spend several hours into this but can’t get it right. Please help, thanks. I haven’t added the data preprocessing …

Total answers: 2

Different slices give different inequalities for same elements

Different slices give different inequalities for same elements Question: import numpy as np a = np.array([.4], dtype=’float32′) b = np.array([.4, .6]) print(a > b) print(a > b[0], a > b[1]) print(a[0] > b[0], a[0] > b[1]) [ True False] [False] [False] True False What’s the deal? Yes, b.dtype == ‘float64’, but so are its slices …

Total answers: 1

Why is np.dot imprecise? (n-dim arrays)

Why is np.dot imprecise? (n-dim arrays) Question: Suppose we take np.dot of two ‘float32’ 2D arrays: res = np.dot(a, b) # see CASE 1 print(list(res[0])) # list shows more digits [-0.90448684, -1.1708503, 0.907136, 3.5594249, 1.1374011, -1.3826287] Numbers. Except, they can change: CASE 1: slice a np.random.seed(1) a = np.random.randn(9, 6).astype(‘float32’) b = np.random.randn(6, 6).astype(‘float32’) for …

Total answers: 1

How to make a delay at ~100-200us

How to make a delay at ~100-200us Question: I would like to try some hardware testing with python. I have to send commands to the hardware where there is a specification for consecutive bytes transmission time interval (~100-200us). However, I found the sleep() method in time module unstable when the delay time is too small. …

Total answers: 3

How do I use parameter epsabs in scipy.integrate.quad in Python?

How do I use parameter epsabs in scipy.integrate.quad in Python? Question: I am trying to compute the integrals more precise by specifying the parameter epsabs for scipy.integrate.quad, say we are integrating the function sin(x) / x^2 from 1e-16 to 1.0 from scipy.integrate import quad import numpy integrand = lambda x: numpy.sin(x) / x ** 2 …

Total answers: 1

ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='<U3')

ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='<U3') Question: I recieve this error while trying to obtain the recall score. X_test = test_pos_vec + test_neg_vec Y_test = [“pos”] * len(test_pos_vec) + [“neg”] * len(test_neg_vec) recall_average = recall_score(Y_test, y_predict, average=”binary”) print(recall_average) This will give me: C:Usersanca_elena.moisaAppDataLocalProgramsPythonPython36libsite-packagessklearnmetricsclassification.py:1030: FutureWarning: elementwise comparison failed; returning scalar instead, but …

Total answers: 4

Rand Index function (clustering performance evaluation)

Rand Index function (clustering performance evaluation) Question: As far as I know, there is no package available for Rand Index in python while for Adjusted Rand Index you have the option of using sklearn.metrics.adjusted_rand_score(labels_true, labels_pred). I wrote the code for Rand Score and I am going to share it with others as the answer to …

Total answers: 3