I have the following loop.
x_array = []
for x in np.arange(0.01, 0.1, 0.01 ):
x_array.append(x)
Why are some of the elements in x_array in so many decimals?
[0.01,
0.02,
0.03,
0.04,
0.05,
0.060000000000000005,
0.06999999999999999,
0.08,
0.09]
If you want your list of numbers without "additional" digits in the
fractional part, try the following code:
x_array = np.arange(0.01, 0.1, 0.01).round(2).tolist()
As you see, you don't even need any explicit loop.
The result is just what you want, i.e.:
[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09]
Another choice is:
x_array = (np.arange(1, 10) / 100).tolist()
Related
I have this exercise and the goal is to solve it with complexity less than O(n^2).
You have an array with length N filled with event probabilities. Create another array in which for each element i calculate the probability of all event to happen until the position i.
I have coded this O(n^2) solution. Any ideas how to improve it?
probabilityTable = [0.1, 0.54, 0.34, 0.11, 0.55, 0.75, 0.01, 0.06, 0.96]
finalTable = list()
for i in range(len(probabilityTable)):
finalTable.append(1)
for j in range(i):
finalTable[i] *= probabilityTable[j]
for item in finalTable:
print(item)
probabilityTable = [0.1, 0.54, 0.34, 0.11, 0.55, 0.75, 0.01, 0.06, 0.96]
finalTable = probabilityTable.copy()
for i in range(1, len(probabilityTable)):
finalTable[i] = finalTable[i] * finalTable[i - 1]
for item in finalTable:
print(item)
new_probs = [probabilityTable[0]]
for prob in probabilityTable[1:]:
new_probs.append(new_probs[-1] + prob)
I looping over on dataframe df1 to look for maximum order and then I want to take discount_first to assign to max order.
For one dataset everything goes OK
new_rate_1 = []
for value in df1["maximum_order"]:
new_val = df[df["New_Order_Lines"]==value]["discount_first"]
new_val = new_val.tolist()[0]
new_rate_1.append(new_val)
new_rate_1
[-1.3,
-1.3,
0.35,
0.8,
0.75,
0.55,
0.8,
0.85,
0.4,
0.75,
0.85,
0.85,
0.55,
0.45,
0.8,
0.65,
0.55,
0.85,
0.35,
0.85,
0.9,
0.5,
0.55,
-0.6,
0.85,
0.75,
0.35,
0.15,
0.55,
0.7,
0.8,
0.85,
0.75,
0.65,
0.75,
0.75,
0.35,
0.85,
0.4,
...
....
]
for other data set i start getting error ?
IndexError: list index out of range
If I dont index the list within the look I dont get error and output looks like this
[[0.8],
[0.8],
[0.55],
[0.55],
[0.55],
[0.85],
[0.55],
[0.85],
[0.85],
[0.65],
[0.65],
[0.75],
[0.7]
.....
any suggestion/advice how can I get rid of behaviour?
Thanks in advance
How about using this
# new_val = new_val.tolist()[0]
new_val = new_val.values.flatten()[0]
Why looping at all when you can do it without a loop?
you can use isin()+tolist() method:
new_rate_1 =df.loc[df["New_Order_Lines"].isin(df1["maximum_order"]),"discount_first"].tolist()
Noob question, but I can't seem to figure out why this is throwing an error: IndexError: index 4 is out of bounds for axis 2 with size 4
import numpy as np
numP = 4;
P = np.zeros((3,3,numP))
P[:,:,1] = np.array([[0.50, 0.25, 0.25],
[0.20, 0.55, 0.25],
[0.20, 0.30, 0.50]])
P[:,:,2] = np.array([[0.70, 0.20, 0.10],
[0.05, 0.75, 0.20],
[0.10, 0.20, 0.70]])
P[:,:,3] = np.array([[0.45, 0.35, 0.20],
[0.20, 0.65, 0.15],
[0.00, 0.30, 0.70]])
P[:,:,4] = np.array([[0.60, 0.20, 0.20],
[0.20, 0.60, 0.20],
[0.05, 0.05, 0.90]])
Python is 0-indexed (as in list[0] refers to the first element in the list, list[1] refers to the second element... etc)
so the last assignment should be P[:,:,3]
I would like to know if there is an equivalent for pandas.Series.unique() when the series contains non-hashable elements (in my case, lists).
For instance, with
>> ds
XTR
s0b0_VARC-0.200 [0.05, 0.05]
s0b0_VARC-0.100 [0.05, 0.05]
s0b0_VARC0.000 [0.05, 0.05]
s0b0_VARC0.100 [0.05, 0.05]
s0b1_VARC-0.200 [0.05, 0.05]
s0b1_VARC0.000 [0.05, 0.05]
s0b1_VARC0.100 [0.05, 0.05]
s0b2_VARC-0.200 [0.05, 0.05]
s0b2_VARC-0.100 [0.06, 0.025]
s0b2_VARC0.000 [0.05, 0.05]
s0b2_VARC0.100 [0.05, 0.05]
I would like to get
>> ds.unique()
2
Thanks #Quang Hoang
Inspired from this SO answer, I wrote the following function (not sure how robust it is though):
def count_unique_values(series):
try:
tuples = [tuple(x) for x in series.values]
series = pd.Series(tuples)
nb = len(series.unique())
print(nb)
except TypeError:
nb = len(series.unique())
return nb
This question already has answers here:
How do I use a decimal step value for range()?
(34 answers)
Closed 9 years ago.
How can I make an for loop in python with steps of 0.01?
I tried this but it doesn't work:
for X0 in range (-0.02, 0.02, 0.01):
for Y0 in range (-0.06, 0.09, 0.01):
it says
TypeError: range() integer end argument expected, got float.
[x * 0.01 for x in xrange(10)]
will produce
[0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09]
The python range only takes integers as the error message indicates. If you want to use float steps, you can either use numpy's arange or just divide a integer range:
>>> import numpy as np
>>> print np.arange(-0.02, 0.02, 0.01)
array([-0.02, -0.01, 0. , 0.01])
in your example:
for X0 in np.arange(-0.02, 0.02, 0.01):
for Y0 in np.arange(-0.06, 0.09, 0.01):
or:
>>> print [a/0.01 - 0.02 for a in range(4)]
[-0.02, -0.01, 0.0, 0.009999999999999998]
If you don't want to use a library:
def float_range(a,b,c):
while a < b:
yield a
a += c
for X0 in float_range (-0.02, 0.02, 0.01):
for Y0 in float_range (-0.06, 0.09, 0.01):
print X0, Y0