Python: Removing a range of numbers from array list - python

Im having issues removing elements from a range a through b from an array list. The solutions ive searched online seem to only work for individual elements, adjacent elements and or elements that are whole numbers. Im dealing with float numbers.
self.genx = np.arange(0, 5, 0.1)
temp_select = self.genx[1:3] #I want to remove numbers from 1 - 3 from genx
print(temp_select)
self.genx = list(set(self.genx)-set(temp_select))
print(self.genx)
plt.plot(self.genx,self.geny)
However I get the following in the console and this is because im subtracting floats rather than whole numbers so it literally subtracts rather than removing which is what it would do if dealing with whole numbers:
genx: [ 0.0 , 0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.8 , 0.9 , 1.0, 1.1 , 1.2 , 1.3 , 1.4 , 1.5 , 1.6 , 1.7 , 1.8 , 1.9 , 2.0, , 2.1 , 2.2 , 2.3 , 2.4 , 2.5 , 2.6 , 2.7 , 2.8 , 2.9
, 3.0 , 3.1 , 3.2 , 3.3 , 3.4 , 3.5 , 3.6 , 3.7 , 3.8 , 3.9 , 4.0 , 4.1 , 4.2 , 4.3 , 4.4
, 4.5 , 4.6 , 4.7 , 4.8 , 4.9]
temp_select: [ 0.1 0.2]
genx(after subtracted): [0.0, 0.5, 2.0, 3.0, 4.0, 1.5, 1.0, 1.1000000000000001, 0.70000000000000007, 0.90000000000000002, 2.7000000000000002, 0.30000000000000004, 2.9000000000000004, 1.9000000000000001, 3.3000000000000003, 0.40000000000000002, 4.7000000000000002, 3.4000000000000004, 2.2000000000000002, 2.8000000000000003, 1.4000000000000001, 0.60000000000000009, 3.6000000000000001, 1.3, 1.2000000000000002, 4.2999999999999998, 4.2000000000000002, 4.9000000000000004, 3.9000000000000004, 3.8000000000000003, 2.3000000000000003, 4.8000000000000007, 3.2000000000000002, 1.7000000000000002, 2.5, 3.5, 1.8, 4.1000000000000005, 2.4000000000000004, 4.4000000000000004, 1.6000000000000001, 0.80000000000000004, 2.6000000000000001, 4.6000000000000005, 2.1000000000000001, 3.1000000000000001, 3.7000000000000002, 4.5]

I didn't test this but you should be able to do something like the following:
self.genx = [ item for item in self.genx if not range_min < item < range_max ]

self.genx = [ item for item in self.genx if not range_min <= item <= range_max ]
Is this what you want??

Related

Subtracting a List from a Dictionary

I am trying to subtract a list of values from each key in a dictionary. Each key in the dictionary contains 20 y-values for a predicted line. I want to find the difference between these y-values and a different set of given values.
ydata contains 20 points. ycalc has a length of 100 to which keys are assigned for, from L1-L99. Each Key contains 20 points as well. I want to subtract each key from ydata. This is what I have tried, the main issue is that my method return a list of 20 values, when I expect a list of 100 values where each value is a list of 20 points.
ydata = [ 1.2 1.8 1.7 3.0 3.5 3.2 4.5 4.8 5.3 6.2 5.7 6.8 7.0 7.8 8.5 8.6 9.1 11.5 10.3 10.8]
ycalc = 'L0': array([-0.8, -0.6, -0.4, -0.2, 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2,
1.4, 1.6, 1.8, 2. , 2.2, 2.4, 2.6, 2.8, 3. ]), 'L1': array([-0.57777778, -0.37777778, -0.17777778, 0.02222222, 0.22222222,
0.42222222, 0.62222222, 0.82222222, 1.02222222, 1.22222222,
1.42222222, 1.62222222, 1.82222222, 2.02222222, 2.22222222,
2.42222222, 2.62222222, 2.82222222, 3.02222222, 3.22222222]), 'L2': array([-0.35555556, -0.15555556, 0.04444444, 0.24444444, 0.44444444,
0.64444444, 0.84444444, 1.04444444, 1.24444444, 1.44444444,
1.64444444, 1.84444444, 2.04444444, 2.24444444, 2.44444444,
2.64444444, 2.84444444, 3.04444444, 3.24444444, 3.44444444]), 'L3': array([-0.13333333, 0.06666667, 0.26666667, 0.46666667, 0.66666667,
0.86666667, 1.06666667, 1.26666667, 1.46666667, 1.66666667,
1.86666667, 2.06666667, 2.26666667, 2.46666667, 2.66666667,
2.86666667, 3.06666667, 3.26666667, 3.46666667, 3.66666667]), 'L4': array([0.08888889, 0.28888889, 0.48888889, 0.68888889, 0.88888889,
1.08888889, 1.28888889, 1.48888889, 1.68888889, 1.88888889,
2.08888889, 2.28888889, 2.48888889, 2.68888889, 2.88888889,
3.08888889, 3.28888889, 3.48888889, 3.68888889, 3.88888889]), etc.
for i in ycalc:
ydiff = - i + array(ydata)
print(ydiff)
returns [-0.2 0. -0.5 0.4 0.5 -0.2 0.7 0.6 0.7 1.2 0.3 1. 0.8 1.2
1.5 1.2 1.3 3.3 1.7 1.8]
but I want something like this:
([-0.2 0. -0.5 0.4 0.5 -0.2 0.7 0.6 0.7 1.2 0.3 1. 0.8 1.2 1.5 1.2 1.3 3.3 1.7 1.8]), ([-0.3 0.1 -0.6 0.4 0.5 -0.2 0.2 0.6 0.8 1.2 0.5 1. 0.8 1.2 1.5 1.2 1.3 3.3 1.7 1.8]), etc.

Change value of only 1 cell based on criteria DataFrame

Based on a condition, I want to change the value of the first row on a certain column, so far this is what I have
despesas['recibos'] =''
for a in recibos['recibos']:
if len(despesas.loc[(despesas['despesas']==a) & (despesas['recibos']==''), 'recibos'])>0:
despesas.loc[(despesas['despesas']==a) & (despesas['recibos']==''),
'recibos'].iloc[0] =a
So I want to change only the first value of the column recibos by the value on a where (despesas['despesas']==a) & (despesas['recibos']=='')
Edit 1
Example:
despesas['despesas'] = [11.95, 2.5, 1.2 , 0.6 , 2.66, 2.66, 3. , 47.5 , 16.95,17.56]
recibos['recibos'] = [11.95, 1.2 , 1.2 , 0.2 , 2.66, 2.66, 3. , 47.5 , 16.95, 17.56]
And the result should be:
[[11.95, 11.95], [2.5, null] , [1.2, 1.2] , [0.6, null] , [2.66, 2.66], [2.66, 2.66], [3., 3] , [47.5, 45.5 ], [16.95, 16.95], [17.56, 17.56]]
It could be works:
mapper = recibos['recibos'].map(despesas['despesas'].value_counts()).fillna(0)
despesas['recibos'] = recibos['recibos'].where(recibos.groupby('recibos')
.cumcount()
.lt(mapper),'null')
print(despesas)
despesas recibos
0 11.95 11.95
1 2.50 1.2
2 1.20 null
3 0.60 null
4 2.66 2.66
5 2.66 2.66
6 3.00 3
7 47.50 47.5
8 16.95 16.95
9 17.56 17.56
I found the solution that I was looking for
from itertools import count, filterfalse
despesas['recibos'] =''
for index, a in despesas.iterrows():
if len(recibos.loc[recibos['recibos']==a['despesas']])>0:
despesas.iloc[index,1]=True
recibos.drop(recibos.loc[recibos['recibos']==a['despesas']][:1].index, inplace=True)

Why tf.linspace() will return a long decimal number?

Today, I tried to equally split a number range into a number list with TensorFlow's linspace function, I found it return a very annoying result:
import tensorflow as tf
print(tf.linspace(-3.0, 3.0, 11))
The output:
tf.Tensor(
[-3. -2.4 -1.8 -1.1999999 -0.5999999 0.
0.60000014 1.2000003 1.8000002 2.4 3. ], shape=(11,), dtype=float32)
But if I use the same function in Numpy, it will show a more reasonable result:
import numpy as np
print(np.linspace(-3.0, 3.0, 11))
The output:
[-3. -2.4 -1.8 -1.2 -0.6 0. 0.6 1.2 1.8 2.4 3. ]
Why TensorFlow will return a number that has a long decimal, like -0.5999999 not just -0.6?

storing matrices computed out of two for loops (appending a list did not work)

i am implementing the Jacobi iterative method
The problem is i can not store the calculated matrix after each iteration, i tried to append into an empty list but it keeps overwriting the previous elements in that list and i end up with a single matrix repeated K times.
I need to subtract and operate on those matrices for convergence criteria
# Iterate Jacobi until convergence
U = np.array([[8.9,8.9,8.9,8.9,8.9],[8.4,0,0,0,9.2],[7.2,0,0,0,9.4],[6.1,6.8,7.7,8.7,6.1]])
UI=U
UF=U
UFK=[]
k=0
while k<3:
k=k+1 # update the iteration counter
for i in range (1,Nx-1):
for j in range (1,Ny-1):
UF[j,i] = (UI[j+1,i]+UI[j,i+1]+UI[j-1,i]+UI[j,i-1])*0.25 #the matrix i want to store after each iteration
UFK.append(UF) #
print (UF) # when i print UF i get the correct matrix at each iteration displayed
[[ 8.9 8.9 8.9 8.9 8.9 ]
[ 8.4 4.325 3.30625 5.3515625 9.2 ]
[ 7.2 4.58125 3.896875 6.83710938 9.4 ]
[ 6.1 6.8 7.7 8.7 6.1 ]]
[[ 8.9 8.9 8.9 8.9 8.9 ]
[ 8.4 6.296875 6.11132812 7.76210937 9.2 ]
[ 7.2 6.0484375 6.67421875 8.13408203 9.4 ]
[ 6.1 6.8 7.7 8.7 6.1 ]]
[[ 8.9 8.9 8.9 8.9 8.9 ]
[ 8.4 7.36494141 7.67531738 8.47734985 9.2 ]
[ 7.2 7.00979004 7.62979736 8.5517868 9.4 ]
[ 6.1 6.8 7.7 8.7 6.1 ]]
print(UFK) # when i display the appended UFK it is just repeating a single matrix 3 times
[array([[ 8.9 , 8.9 , 8.9 , 8.9 , 8.9 ],
[ 8.4 , 7.36494141, 7.67531738, 8.47734985, 9.2 ],
[ 7.2 , 7.00979004, 7.62979736, 8.5517868 , 9.4 ],
[ 6.1 , 6.8 , 7.7 , 8.7 , 6.1 ]]),
array([[ 8.9 , 8.9 , 8.9 , 8.9 , 8.9 ],
[ 8.4 , 7.36494141, 7.67531738, 8.47734985, 9.2 ],
[ 7.2 , 7.00979004, 7.62979736, 8.5517868 , 9.4 ],
[ 6.1 , 6.8 , 7.7 , 8.7 , 6.1 ]]),
array([[ 8.9 , 8.9 , 8.9 , 8.9 , 8.9 ],
[ 8.4 , 7.36494141, 7.67531738, 8.47734985, 9.2 ],
[ 7.2 , 7.00979004, 7.62979736, 8.5517868 , 9.4 ],
[ 6.1 , 6.8 , 7.7 , 8.7 , 6.1 ]])]
UI=U # why? UI is not a copy of U, it IS U
# UF=U # another why? Changes of UF will change UI and U as well
UFK=[] # appending to a list is great
k=0
while k<3:
k=k+1 # update the iteration counter
UF = np.zeros_like(U) # a fresh copy for iteration
for i in range (1,Nx-1):
for j in range (1,Ny-1):
UF[j,i] = (UI[j+1,i]+UI[j,i+1]+UI[j-1,i]+UI[j,i-1])*0.25
UFK.append(UF) #
print (UF)
print(UFK)
UFK should now be a list of the k UF arrays.
Since you are overwriting all elements of UF it doesn't matter how it it is initialed, just so long as it does not step on other arrays, including UF from previous iterations.
But on further thought, maybe changing UI is part of the plan. If so, why obscure the fact with the UF and UI variables? In this case you can collect the intermediate iterations with a U.copy() - that is, save a copy of U to the list, rather than the U itself.
for i... :
for j....:
U[j,i] = (U[j+1,i]+U[j,i+1]+U[j-1,i]+U[j,i-1])*0.25
UFK.append(U.copy())
print (U)
A list contains pointers to objects. If I write
alist = [U, U, U]
U[0,0] = 10000
that 10000 will appear in all 3 elements of the list - because they are the same thing.
In your code you case UF to the list, and then modify it at each iteration. The result is that your list just contains k pointers to the same array.
You have to set the dimension of UFK before you append it or you always replicate the same matrix several times. The following code can generate the output correctly:
UFK = np.array([]).reshape(0,5)
k = 0
while k < 3:
k += 1
for i in range(1, Nx-1):
for j in range(1, Ny-1):
UF[j, i] = (UI[j+1, i] + UI[j, i+1] + UI[j-1, i] + UI[j, i-1]) * 0.25
UFK = np.append(UFK, UF, axis=0)
Another way to append the array is UFK = np.vstack((UFK, UF)) which will give you the same result.

How can I select single item from one list and doing operation on all items of second list using Python

For example if I have one list having data , and whose item should be selected one by one
a = [0.11 , 0.22 , 0.13, 6.7, 2.5, 2.8]
and the other one for which all items should be selected
b = [1.2 1.4, 2.6, 2.3, 5.7 9.9]
if I select 0.11 from a and do opertation like addition with all the items of b and then save the result in new array or list , how is that br possible with python? ...
I am sorry for the question as I am trying to learn python on my own, kindly tell me how is this thing possible.
Thank you in advance.
You need a nested loop. You can do it in a list comprehension to produce a list of lists:
[[item_a + item_b for item_b in b] for item_a in a]
If you want the end result to be a list of lists it could go like this:
c = [[x + y for x in b] for y in a]
If you want the end result to be a single list with next sublists appended to each other you could write as such:
c=[]
for (y in a):
c += ([y + x for x in b])
Another option is to convert your list into numpy array and then exploit the broadcasting property of numpy arrays:
import numpy as np
npA = np.array(a)
npB = np.array(b)
npA[:, None] + npB
array([[ 1.31, 1.51, 2.71, 2.41, 5.81, 10.01],
[ 1.42, 1.62, 2.82, 2.52, 5.92, 10.12],
[ 1.33, 1.53, 2.73, 2.43, 5.83, 10.03],
[ 7.9 , 8.1 , 9.3 , 9. , 12.4 , 16.6 ],
[ 3.7 , 3.9 , 5.1 , 4.8 , 8.2 , 12.4 ],
[ 4. , 4.2 , 5.4 , 5.1 , 8.5 , 12.7 ]])
You can also do element wise multiplication simply with:
npA[:, None] * npB
which returns:
array([[ 0.132, 0.154, 0.286, 0.253, 0.627, 1.089],
[ 0.264, 0.308, 0.572, 0.506, 1.254, 2.178],
[ 0.156, 0.182, 0.338, 0.299, 0.741, 1.287],
[ 8.04 , 9.38 , 17.42 , 15.41 , 38.19 , 66.33 ],
[ 3. , 3.5 , 6.5 , 5.75 , 14.25 , 24.75 ],
[ 3.36 , 3.92 , 7.28 , 6.44 , 15.96 , 27.72 ]])

Categories

Resources