I get values coming from sensors. Thes are the boundaries:
I only have to use Boolean variables and if else statements. Others loops I have tried but in Real time Data coming from sensors it don't work like that.
So the problem is:
I get values from stream
3.9
3.9
3.9
3.5
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.5
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
3.9
Above stream comes many times.
I need to do b++ when I get this streams once.
var count1 = 0
var count2 = 0
var countValue1 = true
var countValue2 = false
if (x1 == 3.9) {
if (countValue1) {
count1++
count2 = 0
if (count1 == 22) {
count1 = 0
b++
// if(x1 < 3.5 )
countValue1 = false
countValue2 = true
}
} else if (countValue2) {
count2++
count1 = 0
if (count2 == 22) {
count2 = 0
b++
countValue1 = true
countValue2 = false
}
}
}
In First Event it works fine.
The second times I run the app my b value increase by 4 or 3 sometimes. I don't know why this happen.
Related
I am having a bit of trouble with the extraction operator. It is reading in continuous lines of code.
The function is:
friend std::istream &operator>>(std::istream &in, Points2D &some_points) {
//If sequence exists, deallocate it.
if(some_points.sequence_ != NULL){
delete some_points.sequence_;
some_points.sequence_ = nullptr;
}
in >> some_points.size_;
some_points.sequence_ = new std::array<Object, 2> [some_points.size()];
for(size_t i = 0; i < some_points.size(); i++){
in >> some_points.sequence_[i][0] >> some_points.sequence_[i][1];
}
std::cout << std::endl;
return in;
};
It is supposed to add the values, The autograder for this assignment is not working properly because the input is not correct.
Your program is being tested for the following input:
4 1.5 2.5 6.6 9.2 4.5 3.2 5.4
3 200.0 6.0 450.2 8.8 9.6 3.4
Your program produced the following output:
Enter a sequence of points (double)
(1.5, 2.5) (6.6, 9.2) (4.5, 3.2) (5.4, 3) //THIS IS WHERE IT IS HAPPENING, ALLOCATES 5.4 AND 3
//TOGETHER INSTEAD OF STOPPING
Enter a sequence of points (double)
(0, 6) (450.2, 8.8) (9.6, 3.4) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0)
(0, 0) (0, 0) (0, 0) ... (0, 0) (0, 0) (0, 0)
This line is stated to have 4 pairs, it should abort the program because 5.4 does not have any other value to assign to it.
4 1.5 2.5 6.6 9.2 4.5 3.2 5.4 // (1.5,2.5) (6.6,9.2) (4.5,3.2)
This line of 3 pairs is fine as every one of them has a pair
3 200.0 6.0 450.2 8.8 9.6 3.4 // (200.0,6.0) (450.2,8.8) (9.6,3.4)
My question is how do I make it so my program aborts if it doesn't have a second value instead of reading it from the next line?
I'm trying to run an TfLite Model on a x86_64 system. It seems that all is working fine. But when I try to get the input or output tensor with typed_input_tensor(0) then I get a null pointer.
My model is a simple HelloWorldNN:
import tensorflow as tf
import numpy as np
from tensorflow import keras
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
model.fit(xs, ys, epochs=10)
print(model.predict([10.0]))
model.summary()
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("linear.tflite","wb").write(tflite_model)
For the C++ part I cloned the tensorflow git and checked out the commit d855adfc5a0195788bf5f92c3c7352e638aa1109. This is the commit which is neccessary to using Coral hardware which I plan to use. I build the tensorflow-lite.a and linked it to my application.
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile("linear.tflite");
if (tflite::InterpreterBuilder(*model, resolver)(&interpreter) != kTfLiteOk) {
std::cerr << "Failed to build interpreter." << std::endl;
}
if (interpreter->AllocateTensors() != kTfLiteOk) {
std::cerr << "Failed to allocate tensors." << std::endl;
}
std::cout << "Number of tensors" << interpreter->tensors_size() <<" Num of Inputs "<<
tflite::PrintInterpreterState(interpreter.get());
float* input = interpreter->typed_input_tensor<float>(0);
interpreter->Invoke();
float* output = interpreter->typed_output_tensor<float>(0);
If I run the code then both input and output pointers are null pointers. The output of interpreter.get() is the follow:
Number of tensors8 Num of Inputs 18446732345621392436
Interpreter has 8 tensors and 3 nodes
Inputs: 4
Outputs: 5
Tensor 0 dense/BiasAdd_int8 kTfLiteInt8 kTfLiteArenaRw 1 bytes ( 0.0 MB) 1 1
Tensor 1 dense/MatMul_bias kTfLiteInt32 kTfLiteMmapRo 4 bytes ( 0.0 MB) 1
Tensor 2 dense/kernel/transpose kTfLiteInt8 kTfLiteMmapRo 1 bytes ( 0.0 MB) 1 1
Tensor 3 dense_input_int8 kTfLiteInt8 kTfLiteArenaRw 1 bytes ( 0.0 MB) 1 1
Tensor 4 dense_input kTfLiteFloat32 kTfLiteArenaRw 4 bytes ( 0.0 MB) 1 1
Tensor 5 dense/BiasAdd kTfLiteFloat32 kTfLiteArenaRw 4 bytes ( 0.0 MB) 1 1
Tensor 6 (null) kTfLiteNoType kTfLiteMemNone 0 bytes ( 0.0 MB) (null)
Tensor 7 (null) kTfLiteNoType kTfLiteMemNone 0 bytes ( 0.0 MB) (null)
Node 0 Operator Builtin Code 114 QUANTIZE
Inputs: 4
Outputs: 3
Node 1 Operator Builtin Code 9 FULLY_CONNECTED
Inputs: 3 2 1
Outputs: 0
Node 2 Operator Builtin Code 6 DEQUANTIZE
Inputs: 0
Outputs: 5`
I've no idea where is my mistake. It worked with tensorflow 1.15. But 1.15 I can't use anymore with Coral hardware. I would be grateful for any help
Ok, I found my problem. I hadn't updated the include files. The files were still from 1.15. :-)
For example,
I have some data like this:
column = pd.Series([1,2,3,np.nan,4,np.nan,7])
print column
Executing the command, the result looks like:
0 1.0
1 2.0
2 3.0
3 NaN
4 4.0
5 NaN
6 7.0
Now I want to know what is the first value before each NaN value, such as 3.0 which is before the first NaN. And 4.0 is the result before the second NaN value. Is there any built-in function in pandas which can complete this or shall I write a for loop to do this?
Solution working with non consecutive NaNs.
You can use boolean indexing with mask created by isnull, shift and fillna:
print (column[column.isnull().shift(-1).fillna(False)])
2 3.0
4 4.0
dtype: float64
print (column.isnull())
0 False
1 False
2 False
3 True
4 False
5 True
6 False
dtype: bool
print (column.isnull().shift(-1))
0 False
1 False
2 True
3 False
4 True
5 False
6 NaN
dtype: object
print (column.isnull().shift(-1).fillna(False))
0 False
1 False
2 True
3 False
4 True
5 False
6 False
dtype: bool
With consecutive NaNs need multiple by inverted c by mul:
column = pd.Series([np.nan,2,3,np.nan,np.nan,np.nan,7,np.nan, np.nan, 5,np.nan])
c = column.isnull()
mask = c.shift(-1).fillna(False).mul(~c)
print (mask)
0 False
1 False
2 True
3 False
4 False
5 False
6 True
7 False
8 False
9 True
10 False
dtype: bool
print (column[mask])
2 3.0
6 7.0
9 5.0
dtype: float64
Same idea as #jezrael... numpyfied.
column[np.append(np.isnan(column.values)[1:], False)]
2 3.0
4 4.0
dtype: float64
Complete with pd.Series reconstruction
m = np.append(np.isnan(column.values)[1:], False)
pd.Series(column.values[m], column.index[m])
2 3.0
4 4.0
dtype: float64
Not nearly as quick but intuitive. Group by the the cumsum of isnull and take the last value. Of this result, get rid of last row.
column.groupby(column.isnull().cumsum()).last().iloc[:-1]
0 3.0
1 4.0
dtype: float64
What I need to do: I have a vector of lines right now, v[0] is the first line and so on. I would like to read the first number from each line as the challenge and the second number from each line as the judge and then apply the conditions in the code. I want to use a stringstream to read extract the numbers from the lines.
What my code is doing right now: It is reading only the first number from each line. So the first number of the first line is the challenge and the first number of the second line is the judge and the first number of the third line is the challenge.
std::vector<string> v;
string line;
int i;
double challenge;
int judge;
while (getline(cin, line)) {
if (line.empty()) {
break;
}
v.push_back(line);
}
for (i = 0; i < v.size();i++ ) {
cin >> v[i];
std::stringstream ss(v[i]);
ss << v[i];
ss >> challenge >> judge;
if (challenge < 1 || challenge > 5) {
cout << "bad_difficulty" << endl; //must add the condition or empty
v.erase(v.begin() + i);
}
if (judge != 5 || judge != 7 ) {
cout << "bad_judges" << endl; //must add the condition or empty
v.erase(v.begin() + i);
}
cout << v[i] << endl;
}
return 0;
}
For example:
Input:
5.1 7 5.4 3.0 9.6 2.9 2.8 2.0 5.4
-3.8 7 2.9 1.1 5.7 7.2 4.8 8.5 3.9
2.2 5 9.4 4.7 7.3 1.9 5.7 6.0 7.1
2.4 6 9.2 5.2 1.0 2.9 4.9 7.4 7.9
2.1 7 7.9 4.9 0.0 7.2 9.1 7.8 6.7 4.3
3.8 5
2.0
4.0 7 2.4 1.9 3.2 8.3 14.8 0.1 9.7
2.5 7 8.4 -8.0 5.0 6.0 8.0 1.3 3.3
1.6 -1 9.5 2.5 5.8 7.9 5.5 1.6 7.9
Output should be:
bad_difficulty
bad_difficulty
2.2 5 9.4 4.7 7.3 1.9 5.7 6.0 7.1
bad_judges
2.1 7 7.9 4.9 0.0 7.2 9.1 7.8 6.7 4.3
3.8 5
bad_judges
4.0 7 2.4 1.9 3.2 8.3 14.8 0.1 9.7
2.5 7 8.4 -8.0 5.0 6.0 8.0 1.3 3.3
bad_judges
Current Output:
bad_difficulty
bad_judges
2.2 5 9.4 4.7 7.3 1.9 5.7 6.0 7.1
bad_judges
2.1 7 7.9 4.9 0.0 7.2 9.1 7.8 6.7 4.3
bad_judges
2.0
bad_judges
2.5 7 8.4 -8.0 5.0 6.0 8.0 1.3 3.3
bad_judges
1.6 -1 9.5 2.5 5.8 7.9 5.5 1.6 7.9
Let's go walking through that delete loop.
i = 0
v[0] contains line 1.
Line 1 is 5.1 7
challenge > 5, remove 0. The vector shifts up by 1 v[0] now contains line 2
Judge == 7 do not remove 0
increment i
i = 1
v[1] contains line 3. Line 2 has been skipped
Line 3 is 2.2 5
challenge < 5, do not remove 1.
Judge is not 5 or 7. remove 1. The vector shifts up by 1 v[1] now contains line 4
increment i
i = 2
v[2] contains line 5. Line 4 has been skipped
Line 5 is 2.1 7
challenge < 5, do not remove 2.
Judge is 7. do not remove 2.
increment i
i = 3
v[3] contains line 6.
Line 6 is 3.8 5
challenge < 5, do not remove 3.
Judge is 5. do not remove 3.
increment i
i = 3
v[3] contains line 7.
Line 7 is 2.0
challenge < 5, do not remove 3.
Judge is UNDEFINED! PANIC! PANIC! Crom only knows what happens.
increment i
Anyway, the basic pattern here should be clear. When you remove an element from a vector, all subsequent elements are shifted up. Solution: When you remove an element, do not increment i. An elsestatement will take care of this for you.
Next because there are two separate if statements there is the possibility that both conditions will be true and v[i] will be removed twice. There are a bunch of ways around this. Manthan Tilva's solution with continue is simple and effective, but this can be handled more obviously with an else if or by rolling both tests into the same if.
Third, using values that were not read from the stream are undefined. and should not be used. Discard the line without looking any further. if (ss >> challenge >> judge) will help here.
SAS code:
DATA aaa;
INPUT x1 x2 group ##;
CARDS;
3.9 210 1 4.8 270 2 4.4 250 3
4.2 190 1 4.7 180 2 3.7 305 3
3.7 240 1 5.4 230 2 2.9 240 3
4.0 170 1 4.5 245 2 4.5 330 3
4.4 220 1 4.6 270 2 3.3 230 3
5.2 230 1 4.4 220 2 4.5 195 3
2.7 160 1 5.9 290 2 3.8 275 3
2.4 260 1 5.5 220 2 3.7 310 3
3.6 240 1 4.3 290 2
5.5 180 1 5.1 310 2
2.9 200 1
3.3 300 1
;
PROC ANOVA ;
CLASS group;
MODEL x1 x2=group;
MANOVA H=group/PRINTH PRINTE SUMMARY;
RUN;
quit;
SAS output:
Characteristic Roots and Vectors of: E Inverse * H, where
H = Anova SSCP Matrix for group
E = Error SSCP Matrix
Characteristic Characteristic Vector V'EV=1
Root Percent x1 x2
0.64162782 75.19 0.23674984 0.00222702
0.21172068 24.81 -0.11171221 0.00402658
I used R to compute the eigenvalues and eigenvectors of E Inverse * H:
E=matrix(c(14.652666667,-53.58333333,-53.58333333,47426.041667),
nrow=2, ncol=2,byrow = TRUE)
E
H=matrix(c(7.926,122.48333333,122.48333333,13753.958333),
nrow=2, ncol=2,byrow = TRUE)
H
C=solve(E)%*%H
C
eigen(E)
The eigenvalues are same, but the eigenvectors are different with SAS characteristic vectors.
Can you tell me why or algorithms? Thank you!