How to use del in tuples - tuples

x = 1.0, [2,4,8], 16
4 can be popped out from x[1] by using pop
x[1].pop(1)
but is there anyway this output can be achieved using del?
(1.0, [2, 8], 16)

Related

List negative indexing of 0; x[-0]

Can anyone explain how works Negative index of 0?
x = [2, 3, 5, 6]
print(x[-0])
output: 2
What you enter as the index gets evaluated. For example :
x = [2, 3, 5, 6]
print(x[2 - 1])
It prints 3 because it is evaluated as print(x[1]). Here you used -0 so it is the same as 0.

I'm getting the following error when I run my code - TypeError: must be real number, not list

This is the problem :
You will be given a list of lists, each sublist will be of length 2 i.e. [[x,y],[p,q],[l,m]..[r,s]] consider its like a martrix of n rows and two columns
a. the first column Y will contain interger values
b. the second column 𝑌𝑠𝑐𝑜𝑟𝑒 will be having float values
Your task is to find the value of 𝑓(𝑌,𝑌𝑠𝑐𝑜𝑟𝑒)=−1∗1𝑛Σ𝑓𝑜𝑟𝑒𝑎𝑐ℎ𝑌,𝑌𝑠𝑐𝑜𝑟𝑒𝑝𝑎𝑖𝑟(𝑌𝑙𝑜𝑔10(𝑌𝑠𝑐𝑜𝑟𝑒)+(1−𝑌)𝑙𝑜𝑔10(1−𝑌𝑠𝑐𝑜𝑟𝑒)) here n is the number of rows in the matrix
Ex:
[[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
output:
0.4243099
−18⋅((1⋅𝑙𝑜𝑔10(0.4)+0⋅𝑙𝑜𝑔10(0.6))+(0⋅𝑙𝑜𝑔10(0.5)+1⋅𝑙𝑜𝑔10(0.5))+...+(1⋅𝑙𝑜𝑔10(0.8)+0⋅𝑙𝑜𝑔10(0.2)))
My code -
def compute_log_loss(A):
Y = len(A)
Ys = len(A[0])
l = 0
for i in range(Y):
for j in range(Ys):
l += A[i]*math.log10(A[j]) + (1-A[i])*math.log10(1-A[j])
loss=(-1*l)/Y
print(loss)
A = [[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
compute_log_loss(A)```
Im getting the following error :
TypeError Traceback (most recent call last)
<ipython-input-97-bd3b5244f95b> in <module>
17 print(loss)
18 A = [[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
---> 19 compute_log_loss(A)
<ipython-input-97-bd3b5244f95b> in compute_log_loss(A)
13 for i in range(Y):
14 for j in range(Ys):
---> 15 l += A[i]*math.log10(A[j]) + (1-A[i])*math.log10(1-A[j])
16 loss=(-1*l)/Y
17 print(loss)
TypeError: must be real number, not list
A[i] is a list not an single element.
A = [[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9], [1, 0.8]]
A[0] =[1, 0.4]
A[0][0] =1
A[0][1] =0.4

How do I create a grid in python with different values?

I'm trying to write a 4X4 grid in python where the last two rows contain the same numbers as the first two rows.
The end result should be exactly this:
0 1 2 3
4 5 6 7
0 1 2 3
4 5 6 7
The goal is to make a game where the above grid is traversable. I've tried list comprehensions and concatenating two lists and it's not producing the right answers.
Concatenating two lists should work. The code for concatenating two lists
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
l3 = [l1 , l2];
l4 = l3+l3
print l4
should yield [[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]

Different results between python map and numpy vectorize

My understanding is that (one use of) numpy's vectorize allows me to send an array to a function that normally only takes scalars, instead of using the built in map function (in combination with a lambda function or the like). However, under the following scenario I am getting different results when I use map vs numpy.vectorize and I can't seem to figure out why.
import numpy as np
def basis2(dim, k, x):
y = np.array([-0.2, -0.13, -0.06, 0, 0.02, 0.06, 0.15, 0.3, 0.8,
1.6, 3.1, 6.1, 10.1, 15.1, 23.1, 30.1, 35.0, 40.0, 45.0, 50.0, 55.0])
if x < y[k] or x > y[k + dim + 1]:
return 0
elif dim != 0:
ret = ((x - y[k]) / (y[k + dim] - y[k])) * basis2(dim - 1, k, x) + (
(y[k + dim + 1] - x) / (y[k + dim + 1] - y[k + 1])) * basis2(dim - 1, k + 1, x)
return ret
else:
return 1.0
w = np.array([20.0, 23.1, 30.0])
func = lambda x: basis2(3, 14, x)
vec = map(func, w)
func2 = np.vectorize(basis2)
vec2 = func2(3, 14, w)
print vec # = [0, 0.0, 0.23335417007039491]
print vec2 # = [0 0 0]
As the docstring says:
The data type of the output of vectorized is determined by calling
the function with the first element of the input. This can be avoided
by specifying the otypes argument.
you need to add a otypes argument:
func2 = np.vectorize(basis2, otypes="d")
or change return 0 to return 0.0 in basis2().

Making a list of evenly spaced numbers in a certain range in python

What is a pythonic way of making list of arbitrary length containing evenly spaced numbers (not just whole integers) between given bounds? For instance:
my_func(0,5,10) # ( lower_bound , upper_bound , length )
# [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 ]
Note the Range() function only deals with integers. And this:
def my_func(low,up,leng):
list = []
step = (up - low) / float(leng)
for i in range(leng):
list.append(low)
low = low + step
return list
seems too complicated. Any ideas?
Given numpy, you could use linspace:
Including the right endpoint (5):
In [46]: import numpy as np
In [47]: np.linspace(0,5,10)
Out[47]:
array([ 0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
Excluding the right endpoint:
In [48]: np.linspace(0,5,10,endpoint=False)
Out[48]: array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
You can use the following approach:
[lower + x*(upper-lower)/length for x in range(length)]
lower and/or upper must be assigned as floats for this approach to work.
Similar to unutbu's answer, you can use numpy's arange function, which is analog to Python's intrinsic function range. Notice that the end point is not included, as in range:
>>> import numpy as np
>>> a = np.arange(0,5, 0.5)
>>> a
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
>>> a = np.arange(0,5, 0.5) # returns a numpy array
>>> a
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
>>> a.tolist() # if you prefer it as a list
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
f = 0.5
a = 0
b = 9
d = [x * f for x in range(a, b)]
would be a way to do it.
Numpy's r_ convenience function can also create evenly spaced lists with syntax np.r_[start:stop:steps]. If steps is a real number (ending on j), then the end point is included, equivalent to np.linspace(start, stop, step, endpoint=1), otherwise not.
>>> np.r_[-1:1:6j, [0]*3, 5, 6]
array([-1. , -0.6, -0.2, 0.2, 0.6, 1.])
You can also directly concatente other arrays and also scalars:
>>> np.r_[-1:1:6j, [0]*3, 5, 6]
array([-1. , -0.6, -0.2, 0.2, 0.6, 1. , 0. , 0. , 0. , 5. , 6. ])
You can use the folowing code:
def float_range(initVal, itemCount, step):
for x in xrange(itemCount):
yield initVal
initVal += step
[x for x in float_range(1, 3, 0.1)]
Similar to Howard's answer but a bit more efficient:
def my_func(low, up, leng):
step = ((up-low) * 1.0 / leng)
return [low+i*step for i in xrange(leng)]