I am trying to forecast next number.
The number can be only 1 and -1.
And i have sequential history datas of 1 and -1.
1 -1 1 1 1 -1 -1 1....
I want to implement this with python.
I have tried arima and some other complex codes,
but they were to hard to implement.
I want a simple solution.
How can i implement this with python ?
Without specifying the requirements, not sure what you want.
Here it is a simple one, always the inverse of the last appeared one.
exist = [1, -1, 1, 1, 1, -1, -1, 1]
predict = exist[-1]*-1
Here is another one, try to go close to half population.
exist = [1, -1, 1, 1, 1, -1, -1, 1]
predict = 1 if sum(exist) > 0 else -1
Here is another one, random (from #Iłya Bursov )
predict = random.randrange(-1, 2, 2)
Related
This is hard to explain, I want to make sure a certain item does not exist between two other items. Here's some examples, "-1" is the item to watch:
1, 3, -1, 0, 1, 2 == BAD!
-1, 0, 3, -1, 1, 3 == BAD!
2, -1, -1, 4, 3, 1 == BAD!
1, -1, -1, -1, 2, 0 == BAD!
-1, -1, 0, -1, -1, -1 == BAD!
however...
if it's at the end or the beginning it's okay:
-1, 1, 2, 4, 0, -1 == OKAY!
0, 1, 2, -1, -1, -1 == OKAY!
-1, -1, -1, 2, 2, 1 == OKAY!
context: these are guitar chords and the "-1" is a muted string. I don't want the muted string in the middle of the chord because that's very hard to play
I hope that makes sense. If you know a better way to phrase it let me know and I'll change the title
edit: here's what I have so far because someone asked, but I honestly don't know where to start:
for i := 0; i < len(c); i++ {
//don't include if there's a -1 between two numbers
if c[i] != 0 && c[i] != len(c) {
}
}
Try this. Change the problem statement. The slice of int is OKAY if the numbers that are not -1 are adjacent.
func check(input []int) bool {
lastNonNeg := -1
for i, n := range input {
if n != -1 {
// already encountered non negative and non adjacent (distance gt 1)
if lastNonNeg != -1 && i-lastNonNeg > 1 {
return false
}
// otherwise remember the index
lastNonNeg = i
}
}
return true
}
Input and output:
[1 3 -1 0 1 2] false
[-1 0 3 -1 1 3] false
[2 -1 -1 4 3 1] false
[1 -1 -1 -1 2 0] false
[-1 -1 0 -1 -1 -1] true
[-1 1 2 4 0 -1] true
[0 1 2 -1 -1 -1] true
[-1 -1 -1 2 2 1] true
Attention that input {-1, -1, -1, -1, -1, -1} is OKAY, but all mute chords is not difficult.
https://play.golang.org/p/Fzf8R4Seqan
What if you put all values in a slice?
numbers := strings.Split("1,2,3,4,5", ",")
then you can check where the particular thing is, and if it isn't the beginning, or the end, you might be good.
Say I have an array of N integers set to the value '0', and I want to pick a random element of that array that has the value '0' and put it to value '1'
How do I do this efficiently ?
I came up with 2 solutions but they look quite ineficient
First solution
int array[N] //init to 0s
int n //number of 1s we want to add to the array
int i = 0
while i < n
int a = random(0, N)
if array[a] == 0
array[a] = 1
i++
end if
end while
It would be extremely inefficient for large arrays because of the probability of collision
The second involves a list containing all the 0 positions remaining and we choose a random number between 0 and the number of 0 remaining to lookup the value in the list that correspond to the index in the array.
It's a lot more reliable than the first solution, since the number of operations is bounded, but still has a worst case scenario complexity of N² if we want to fill the array completely
Your second solution is actually a good start. I assume that it involves rebuilding the list of positions after every change, which makes it O(N²) if you want to fill the whole array. However, you don't need to rebuild the list every time. Since you want to fill the array anyway, you can just use a random order and choose the remaining positions accordingly.
As an example, take the following array (size 7 and not initially full of zeroes) : [0, 0, 1, 0, 1, 1, 0]
Once you have built the list of zeros positions, here [0, 1, 3, 6], just shuffle it to get a random ordering. Then fill in the array in the order given by the positions.
For example, if the shuffle gives [3, 1, 6, 0], then you can fill the array like so :
[0, 0, 1, 0, 1, 1, 0] <- initial configuration
[0, 0, 1, 1, 1, 1, 0] <- First, position 3
[0, 1, 1, 1, 1, 1, 0] <- Second, position 1
[0, 1, 1, 1, 1, 1, 1] <- etc.
[1, 1, 1, 1, 1, 1, 1]
If the array is initially filled with zeros, then it's even easier. Your initial list is the list of integers from 0 to N (size of the array). Shuffle it and apply the same process.
If you do not want to fill the whole array, you still need to build the whole list, but you can truncate it after shuffling it (which just means to stop filling the array after some point).
Of course, this solution requires that the array does not change between each step.
You can fill array with n ones and N-n zeros and make random shuffling.
Fisher-Yates shuffle has linear complexity:
for i from N−1 downto 1 do
j ← random integer such that 0 ≤ j ≤ i
exchange a[j] and a[i]
In Ruby, given this array
[0,1,2,3,4,5]
how do i produce
> 0, 1
> 1, 2
> 2, 3
> 3, 4
> 4, 5
[0,1,2,3,4,5].each_cons(2){|a| puts a.join(", ")}
each_cons for sure, but another way:
enum = [0,1,2,3,4,5].to_enum
loop do
puts "#{enum.next}, #{enum.peek}"
end
0, 1
1, 2
2, 3
3, 4
4, 5
See Kernel#to_enum and Kernel#loop. Note the docs for all Kernel instance methods are shown at Object, whereas Kernel module methods are documented at Kernel.
I have a long list (array) in matlab.
-1, -1, -1, -1, 1, 1, -1, -1, 2, 2, 2
I want to replace the -1 s by its nearest positive values.
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2
What is the effective way to do this?
I'm assuming you want to replace the negative values with the nearest non-negative values.
This can be done with interp1 with the 'nearest' and 'extrap' options (thanks to #rayryeng for the latter):
x = [1, -1, -1, -1, 1, 1, -1, -1, 2, 2, 2];
ind = x<0;
xi = interp1(find(~ind),x(~ind),find(ind),'nearest','extrap');
x(ind) = xi;
Assuming A to be the input array, you can use a bsxfun + min based approach -
%// Locations of A with -1 and positive valued locations
p1 = find(A==-1)
p2 = find(A>=0)
%// Find the indices of minimum distance locations for each element
%// with -1 to the closest positive valued elements
[~,min_idx] = min(abs(bsxfun(#minus,p1(:).',p2(:))),[],1) %//'
%// OR [~,min_idx] = min(pdist2(p1(:),p2(:)),[],2)
%// Set -1 valued A's elements with elements located at min_idx
%// in positive valued array
A(p1) = A(p2(min_idx))
Lets say I have an array
Y = [1, 2, 3, 4, 5, 6]
I want to make a new array that replaces every other number with 0, so it creates
y = [1, 0, 3, 0, 5, 0]
How would I go about approaching this and writing code for this in a efficient way?
This should do that:
Y(2:2:end) = 0;
With this line you basically say each element starting from the seconds up to the last, in steps of two, should be zero. This can be done for larger steps too:, Y(N:N:end) = 0 makes every Nth element equal to 0.