Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a CSV file with data in the format:
Row 1:
[-0.74120803 0.6338942 ],[-1.01583889 0.20901699],[-1.02969154 0.14459244],[ 0.10362657 0.31347394],[ 1.69977092 -0.13384537],[ 1.39789431 -0.52155783],[ 0.02928792 0.24156825],[-1.03616494 0.33943 ],[ 0.84921822 0.47879992],[ 0.279905 0.96184517],[ 0.43602597 -0.27275052],[ 1.4766132 -0.48128695],[ 0.96219625 -0.44950686],[ 0.24356381 -0.0253022 ],[ 0.09346193 0.07808998],[ 0.26571546 -0.1678716 ],[ 0.03055046 1.05913456],[ 1.94137487e+00 -1.57339675e-03],[ 0.22311559 0.98762516],[ 2.00176133 0.13017485],......
Note that the data is of two rows: the first row contains both x and y coordinates and 2nd row contains their flag status.
Row 2
0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1,1,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,1,1,0,0,0,1,1,1,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,1,1,.......
I want to store the data in 3 lists: x, y, and flag.
Thank you for helping with this part.
row1 = '[-0.74120803 0.6338942 ],[-1.01583889 0.20901699],[-1.02969154 0.14459244],[ 0.10362657 0.31347394],[ 1.69977092 -0.13384537],[ 1.39789431 -0.52155783],[ 0.02928792 0.24156825],[-1.03616494 0.33943 ],[ 0.84921822 0.47879992],[ 0.279905 0.96184517],[ 0.43602597 -0.27275052],[ 1.4766132 -0.48128695],[ 0.96219625 -0.44950686],[ 0.24356381 -0.0253022 ],[ 0.09346193 0.07808998],[ 0.26571546 -0.1678716 ],[ 0.03055046 1.05913456],[ 1.94137487e+00 -1.57339675e-03],[ 0.22311559 0.98762516],[ 2.00176133 0.13017485]'
row2 = '0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1'
l = []
for xy, flag in zip(row1.split(','), row2.split(',')):
x, y = xy.strip('[] ').split(' ')
l.append((float(x), float(y), int(flag)))
print l
If you prefere 3 separate lists:
row1 = '[-0.74120803 0.6338942 ],[-1.01583889 0.20901699],[-1.02969154 0.14459244],[ 0.10362657 0.31347394],[ 1.69977092 -0.13384537],[ 1.39789431 -0.52155783],[ 0.02928792 0.24156825],[-1.03616494 0.33943 ],[ 0.84921822 0.47879992],[ 0.279905 0.96184517],[ 0.43602597 -0.27275052],[ 1.4766132 -0.48128695],[ 0.96219625 -0.44950686],[ 0.24356381 -0.0253022 ],[ 0.09346193 0.07808998],[ 0.26571546 -0.1678716 ],[ 0.03055046 1.05913456],[ 1.94137487e+00 -1.57339675e-03],[ 0.22311559 0.98762516],[ 2.00176133 0.13017485]'
row2 = '0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1'
listX, listY = [], []
for xy in row1.split(','):
x, y = xy.strip('[] ').split(' ')
listX.append(float(x))
listY.append(float(y))
listFlag = [int(flag) for flag in row2.split(',')]
print listX, listY, listFlag
Two one-liners will do:
flags = [int(x) for x in row2.split(',')]
x, y = zip(*((float(value) for value in entry[1:-1].split()) for entry in row1.split(',')))
Now:
print(flags[:5])
print(list(x[:5]))
print(list(y[:5]))
Output:
[0, 0, 0, 1, 1]
[-0.74120803, -1.01583889, -1.02969154, 0.10362657, 1.69977092]
[0.6338942, 0.20901699, 0.14459244, 0.31347394, -0.13384537]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
my_list = [['Chris',33,'JAN'],['Katia',40,'JAN'],['Petunia',54,'JAN'],['Clee',26,'JAN'],['katt',73,'JAN'],['battt',83,'JAN'],['FRIES',59,'FEB'],['GGEEZ',89,'FEB'],['SHEEESH',25,'MAR']]
threshold = [[217, 'JAN'], [104, 'FEB'], [18, 'MAR']]
output: [['Chris','Katia','Petunia','Clee','katt'],['FRIES','GGEEZ'],['SHEEESH']]
I want to make a new list with the first element in the nested array (the names) until the sum of the second elements in the nested array passes the 217 for JAN, 104 for FEB and 18 for MARCH.
I dont know how to do it since both of the lists are are indented and I find that hard to work with, But it should check it in a loop if my_list[2] == threshold[1] and sum the my_list[1]s until it is greater or equal to threshold[0] than it should go and check if the and check if my_list[2] == threshold[1] (but this time we skip the remaining januaries and check if the february is equal to the mylist and so on, its hard to articulate
Try:
my_list = [['Chris',33,'JAN'],['Katia',40,'JAN'],['Petunia',54,'JAN'],['Clee',26,'JAN'],['katt',73,'JAN'],['battt',83,'JAN'],['FRIES',59,'FEB'],['GGEEZ',89,'FEB'],['SHEEESH',25,'MAR']]
threshold = [[217, 'JAN'], [104, 'FEB'], [18, 'MAR']]
results = []
for max_num, month in threshold:
accumulator = []
count = 0
for s, num, month_ in my_list:
if month == month_ and count < max_num:
accumulator.append(s)
results.append(accumulator)
print(results)
output:
[['Chris', 'Katia', 'Petunia', 'Clee', 'katt', 'battt'], ['FRIES', 'GGEEZ'], ['SHEEESH']]
output = []
for a,b in threshold:
sum = 0
curr = []
for x,y,z in my_list:
if z == b and sum < a:
sum += y
curr.append(x)
output.append(curr)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
The following code does not update the array V. The array is first created before the loop and is supposed to be changed within the loop, but it does not. Could someone explain why?
PS: I'm a beginner in Python.
import numpy as np
def priceCall(S0=4,u=2,d=0.5,T=1,r=0.25,K=5):
n = range(T+1);
Tmn = np.subtract(T,n);
S = S0*np.power(u,Tmn)*np.power(d,n);
p = (1+r-d)/(u-d);
V = np.maximum(np.subtract(S,K),[0]*len(S));
for j in range(1,T+1):
for i in range(1,len(V)-j):
V[i] = 1/(1+r)*(p*V[i]+(1-p)*V[i+1]);
print(V)
return V
Because your second forloop is not even executing. Let's assume you want to call priceCall with no parameters, in that case it means that the first range will be range(1, 2) which essentially means [1], and j=1 for the first iteration of the loop, meaning that the second range will be range(1, 1) which essentially means [] because the end index in a range will always be excluded.
Here is how I tested it:
import numpy as np
def priceCall(S0=4,u=2,d=0.5,T=1,r=0.25,K=5):
n = range(T+1);
Tmn = np.subtract(T,n);
S = S0*np.power(u,Tmn)*np.power(d,n);
p = (1+r-d)/(u-d);
V = np.maximum(np.subtract(S,K),[0]*len(S));
print("INITIAL")
print(V)
print("R1")
print(range(1, T+1))
for j in range(1, T+1):
print("R2")
print(range(1, len(V)-j))
for i in range(1, len(V)-j):
new_val = 1/(1+r)*(p*V[i]+(1-p)*V[i+1])
print(new_val)
V[i] = new_val
print("AFTER")
print(V)
return V
priceCall()
And here is the output:
(py3) > $ python sover.py
INITIAL
[3. 0.]
R1
range(1, 2)
R2
range(1, 1)
AFTER
[3. 0.]
Demonstration that the vector does indeed change:
import numpy as np
def priceCall(S0=4,u=2,d=0.5,T=1,r=0.25,K=5):
n = range(T+1);
Tmn = np.subtract(T,n);
S = S0*np.power(u,Tmn)*np.power(d,n);
V = np.maximum(np.subtract(S,K),[0]*len(S));
print("INITIAL")
print(V)
V[0] = 9999
print("AFTER")
print(V)
priceCall()
with the output
(py3) > $ python sover.py
INITIAL
[3. 0.]
AFTER
[9999. 0.]
You have an off by one error in your nested for loop. len(V) is equal to 1 when you enter the first loop.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
x = 5
y = 8
I want to combine those variables and define an another variable, like:
z = 58
It depends on what you mean by "combine".
You can concatenate numbers as strings:
z = int(str(x) + str(y))
But you can also compute x * 10 + y:
z = x * 10 + y
This will give different results if y > 9, e.g. for x = 5 and y = 10, the first version will give 510, while the second version will give 60.
Convert them to strings and concatenate them, then convert them back to an integer:
z = int(str(x) + str(y))
If you're using Python 3.6 or later, this can be done quite concisely with format strings:
>>> x = 5
>>> y = 8
>>> z = int(f'{x}{y}')
>>> z
58
>>>
A more general solution would be something like:
>>> def join_ints(*args):
... return int(''.join(map(str, args)))
...
>>> join_ints(5, 6, 8, 3)
5683
>>>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I split a 3D numpy array into fixed size 3D sub-arrays, do some manipulation on the sub-arrays, and finally put them back in the same order to create the original big 3D volume?
e.g. big volume is nxnxm
so, I would like to split it to sub-vlumes of k x k x k, and do some manipulation on each sub volume and put them together again to create nxnxm
A simple solution would be to process your array with nested for-loops:
A = np.random.rand(5, 4)
print "A:", A
step = 2
newHeight = np.ceil(float(A.shape[0]) / step)
newWidth = np.ceil(float(A.shape[1]) / step)
B = np.zeros((newHeight, newWidth))
C = np.zeros(A.shape)
for i in range(B.shape[0]):
for j in range(B.shape[1]):
B[i, j] = np.mean(A[i*step:(i+1)*step, j*step:(j+1)*step])
C[i*step:(i+1)*step, j*step:(j+1)*step] = B[i, j]
print "B:", B
print "C:", C
Output:
A: [[ 0.86754517 0.65107995 0.01074822 0.18394825]
[ 0.03184878 0.07052286 0.44014168 0.84913463]
[ 0.2982024 0.94988568 0.33208104 0.28697172]
[ 0.36721371 0.9352932 0.22780242 0.13650031]
[ 0.84073176 0.33792535 0.53240018 0.54008341]]
B: [[ 0.40524919 0.37099319]
[ 0.63764875 0.24583887]
[ 0.58932856 0.53624179]]
C: [[ 0.40524919 0.40524919 0.37099319 0.37099319]
[ 0.40524919 0.40524919 0.37099319 0.37099319]
[ 0.63764875 0.63764875 0.24583887 0.24583887]
[ 0.63764875 0.63764875 0.24583887 0.24583887]
[ 0.58932856 0.58932856 0.53624179 0.53624179]]
A is the large input array
B is the small output array
C is the large output array
step is the size of each block, 20 in your case
newHeight and newWidth is the computed size of B: dividing the size of A by the window size step and rounding up
i*step:(i+1)*step and j*step:(j+1)*step are the vertical and horizontal ranges for each block in A and C, respectively.
I'm using a small array of 5x4 as well as two dimensions only for simplicity and readability of the example results. It should be not to hard to extend this approach to three dimensions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to reproduce this [50 x 50] matrix generated with Python as:
n = 50
a = np.linspace(-5, 5, n).reshape(-1,1)
b = a
np.sum(a**2, 1).reshape(-1, 1) + np.sum(b**2, 1)
using R. The problem is that the result is some sort of matrix, which cannot be reproduced through:
n = 50
a = seq(-5, 5, length.out = n)
b = a
a^2 + b^2
which generates a vector.
I am not familiar with the object names in Python, but I see that np.sum(a**2, 1).reshape(-1, 1) produces what looks like a [50 x 1] column vector:
array([[ 2.50000000e+01],
[ 2.30008330e+01],
...
[ 2.10849646e+01],
[ 2.30008330e+01],
[ 2.50000000e+01]])
while np.sum(b**2, 1):
array([ 2.50000000e+01, 2.30008330e+01, 2.10849646e+01,
1.92523948e+01, 1.75031237e+01, 1.58371512e+01,
...
1.27551020e+01, 1.42544773e+01, 1.58371512e+01,
1.75031237e+01, 1.92523948e+01, 2.10849646e+01,
2.30008330e+01, 2.50000000e+01])
looks like the transposed of that same vector. So we have an operation of the form [50 x 1] * [1 x 50] = [50 x 50].
What is the generic name of this operation? And how can I reproduce it in R?
You are looking for ?outer I believe. As per the help file, it returns:
The outer product of the arrays X and Y ... the array A with dimension
c(dim(X), dim(Y))
So, for your specific example, try:
outer(a^2,b^2,FUN=`+`)
# [,1] [,2] [,3]
#[1,] 50.00000 48.00083 46.08496 ...to col 50
#[2,] 48.00083 46.00167 44.08580 ...to col 50
#[3,] 46.08496 44.08580 42.16993 ...to col 50
# ...to row 50