While statement on Octave - while-loop

I have a problem with this while on Octave:
while(k<=1.0)
imshow(cv*k+id*(1-k));
disp(k);
k=k+0.04;
pause(0.1);
endwhile
And this is the output of the values of k:
0
0.040000
0.080000
0.12000
0.16000
0.20000
0.24000
0.28000
0.32000
0.36000
0.40000
0.44000
0.48000
0.52000
0.56000
0.60000
0.64000
0.68000
0.72000
0.76000
0.80000
0.84000
0.88000
0.92000
0.96000
Why does k never reach 1 even if I entered <= in the while condition?

Related

Getting errors trying to change the values of a column with conditions

Here's my data:
id medianHouseValue housingMedianAge totalBedrooms totalRooms \
0 23 113.903 31.0 543.0 2438.0
1 24 99.701 56.0 337.0 1692.0
2 26 107.500 41.0 123.0 535.0
3 27 93.803 53.0 244.0 1132.0
4 28 105.504 52.0 423.0 1899.0 households population medianIncome
0 481.0 1016.0 1.7250
1 328.0 856.0 2.1806
2 121.0 317.0 2.4038
3 241.0 607.0 2.4597
4 400.0 1104.0 1.8080
Here's what I'm trying to do:
change any values in the medianIncome column that are 0.4999 or lower to 0.4999 and change any values that are 15.0001 and higher to 15.0001.
I've tried this:
housing.loc[housing[‘medianIncome'] > 15.0001, 'medianIncome'] = 15.0001
housing.loc[housing[‘medianIncome'] < 0.4999, 'medianIncome'] = 0.4999
And get this error:
AttributeError: 'list' object has no attribute 'loc'
So then I tried this:'
housing['medianIncome'] = np.where(housing['medianIncome'] >= 15.0001, housing['medianIncome'])
housing['medianIncome'] = np.where(housing['medianIncome'] <= 0.4999, housing['medianIncome'])
And get this error:
TypeError: list indices must be integers or slices, not str
I've looked up both errors but can't seem to find a solution that will accommodate. There's a lot more rows, it's just not letting me co[y/paste them all here and I can't recall how to upload the data set.

pandas: filter rows of DataFrame with other condition dataframe

I have a pandas dataframe like this
id f1 f2 f3 f4
1 8.327 9.905 8.133 0.785
2 3.549 0.452 7.798 5.797
3 0.011 0.238 1.291 7.593
4 0.325 0.792 4.643 4.3
5 7.093 7.312 3.641 9.88
6 2.88 7.834 5.727 6.984
7 5.554 1.649 4.018 0.623
8 2.501 2.941 9.323 0.565
9 1.032 6.961 3.905 8.116
10 9.68 7.922 7.015 7.542
11 8.096 4.344 1.153 5.244
I would like to filter data by other condition dataframe. I want to find out records that satisfy all the following conditions.
variable interval
1 f1 (0,4)
2 f2 [1,3]
3 f3 (5,+np.inf)
4 f4 [0,10]
I know I can achieve this with the following code.
df.query('f1>0 and f1<4 and f2>=1 and f2<=3 and f3>5 and f4>=0 and f4<=10')
# or
df.loc[df.f1.between(0,4,inclusive='neither')&df.f2.between(1,3)&df.f3.between(5,np.inf)&df.f4.between(0,10)]
The downside is that I need to modify the code if the conditions change. Is there a pythonic way to handle this issue?
You can dynamically construct a query using:
left_op = lambda y: '>=' if y.closed_left else '>'
right_op = lambda y: '<=' if y.closed_right else '<'
construct_query = lambda x, y: f"({x}{left_op(y)}{y.left} and {x}{right_op(y)}{y.right})"
qry = " and ".join(
df2.apply(lambda x: construct_query(x.variable, x.interval),
axis = 1).tolist()
)
where df2 is your second dataframe with variable and interval columns.
For your example data qry looks like:
'(f1>0 and f1<4) and (f2>=1 and f2<=3) and (f3>=5 and f3<inf) and (f4>=0 and f4<=10)'
Now if you do df.query(qry) it will give:
id f1 f2 f3 f4
7 8 2.501 2.941 9.323 0.565

Avoiding loops in python/pandas

I can do python/pandas to basic stuff, but I still struggle with the "no loops necessary" world of pandas. I tend to fall back to converting to lists and doing loops like in VBA and then just bring those list back to dfs. I know there is a simpler way, but I can't figure it out.
I simple example is just a very basic strategy of creating a signal of -1 if a series is above 70 and keep it -1 until the series breaks below 30 when the signal changes to 1 and keep this until a value above 70 again and so on.
I can do this via simple list looping, but I know this is far from "Pythonic"! Can anyone help "translating" this to some nicer code without loops?
#rsi_list is just a list from a df column of numbers. Simple example:
rsi={'rsi':[35, 45, 75, 56, 34, 29, 26, 34, 67. 78]}
rsi=pd.DataFrame(rsi)
rsi_list=rsi['rsi'].tolist()
signal_list=[]
hasShort=0
hasLong=0
for i in range(len(rsi_list)-1):
if rsi_list[i] >= 70 or hasShort==1:
signal_list.append(-1)
if rsi_list[i+1] >= 30:
hasShort=1
else:
hasShort=0
elif rsi_list[i] <= 30 or hasLong==1:
signal_list.append(1)
if rsi_list[i+1] <= 70:
hasLong=1
else:
hasLong=0
else:
signal_list.append(0)
#last part just for the list to be the same lenght of the original df as I put it back as a column
if rsi_list[-1]>=70:
signal_list.append(-1)
else:
signal_list.append(1)
First clip the values to 30 in lower and 70 in upper bound, use where to change to nan all the values that are not 30 or 70. replace by 1 and -1 and propagate these values with ffill. fillna with 0 the values before the first 30 or 70.
rsi['rsi_cut'] = (
rsi['rsi'].clip(lower=30,upper=70)
.where(lambda x: x.isin([30,70]))
.replace({30:1, 70:-1})
.ffill()
.fillna(0)
)
print(rsi)
rsi rsi_cut
0 35 0.0
1 45 0.0
2 75 -1.0
3 56 -1.0
4 34 -1.0
5 29 1.0
6 26 1.0
7 34 1.0
8 67 1.0
9 78 -1.0
Edit: maybe a bit easier, use ge (greater or equal) and le (less or equal) and do a subtraction, then replace the 0s with the ffill method
print((rsi['rsi'].le(30).astype(int) - rsi['rsi'].gt(70))
.replace(to_replace=0, method='ffill'))

AMPL Non-Linear least Square

Could anyone help me to find the error in this AMPL's code for a simple least-square error base on the function:
F(X)=1/1+e^-x
param N>=1;# N Number of simulations
param M>=1;# Number of inputs
param simulations {1..N};
param training{1..N,1..M};
var W{1..10};
minimize obj: sum{i in simulations , j in 1..4} (1/(1+exp-(W[9]/(1+exp(-
W[j]/(1+exp(-training[i][j]))))+ W[10]/(1+exp(-W[2*j]/(1+exp(-training[i][j]))))))-training[i][5])^2;
'###### DATA
param N:=6;
param M:=4;
param training:
1 2 3 4 5 :=
1 0.209 0.555 0.644 0.355 0.0
2 0.707 0.450 0.587 0.305 1.0
3 0.579 0.521 0.745 0.394 1.0
4 0.574 0.883 0.211 0.550 1.0
5 0.797 0.055 0.430 0.937 1.0
6 0.782 0.865 0.114 0.317 1.0 ;
Thank you!
A couple of things:
is that quote mark before ###### DATA meant to be there?
You have specified that training has dimension N x M, and your data specifies that N=6, M=4, but you then define training as 6 x 5 and your objective function also refers to column 5.
If that doesn't answer your question, you might want to give more information about what error messages you're getting.

iterate over line gnupllot

all,
I have a file that contains "time" in the first column and then bunch of data points in the following columns, and I want to print all of them to the same file and show how each object moves differently in time, but i am not sure how to iterative over such a file, I have search for a long time but to no luck.
Here is an example of some data:
0 0.001 0.006
1 0.001 0.090
2 0.005 0.099
3 0.008 0.999
4 0.009 0.100
5 0.010 0.100
Expect in my file i have 100 + lines after the time column. This is what i have so far in my gnuplot loop:
do for [i=2:99] {
plot 'data.out' using 1:i w l lt 7 lw 1 }
Any help is appreciated, thanks all.
in case you want to have everything in "one plot", you could interchange the order of the for loop and the plot command:
plot for [i=2:99] 'data.out' using 1:i w l lt 7 lw 1
In order to determine the number of columns automatically, one might use the stat command as in:
fName = 'data.out'
stat fName nooutput
N = STATS_columns #number of columns found in file
plot for [i=2:N] fName u 1:i w l lt 7 lw 1