last day (15 days later) » 

9:18 PM
-2
Q: How to: square root squares for cosine similarity within an array ~java~

Johnny DeppMy issue is that I have am creating a book recommendation system and when I try to square root the squares to determine similarity. I do not believe it is square rooting all the contents of each array. The user is prompted with the twenty books and then inputs answers ranging from '1-5' based o...

 
I can't make sense of your sqrtSquares() method, which doesn't seem to do anything like what you describe. You constantly store negative numbers in the zero variable, do nothing with it, then try to take the root of a negative number... Also, why is there a loop when you unconditionally return at the end of the first iteration?
 
I thought to put a loop to iterate through the array, that is passed.
 
That's right. Your loop only calculates the square root of the first element in A and then returns. To add to that, like @azurefrog said, it attempts to calculate the root of a negative number if the first element itself is -1.
 
Can you describe the functionality of the function sqrtSquares(double []A) and also the input double []A?
 
the functionality is to take each element within array A and square root it. The double A input was to pass the array within the similarityscore function
 
9:18 PM
My point is that that is not the functionality. Regardless of what you intended, your loop doesn't loop, it always returns on the first iteration.
 
@JohnnyDepp the return type of the root function should be double[] because you are also intending to return an array of roots. Secondly, you need to loop over A, check if A[i] is a negative number. If yes, then replace A[i] by zero else replace A[i] by its square root. Then return A outside the for loop.
Nope. You always loop over an array or a list or a collection of something. Math.sqrt(A[i]) is just a single number. You cannot loop over it. You need to run your for loop like this: for(int i=0; i<A.length; i++) { // check for the conditions and calculate roots here }.
Good job with the edit but there are a few issues still. You don't need a separate variable called replacement, you can simply assign the value like this: A[i]=0; or A[i] = Math.sqrt(A[i]);. And then just return A outside the loop. Right now you keep overwriting the variable answer and you will face the same issue as before.
 
@RachayitaGiri appreciate you taking the time to help out. I edited method but should I return the entire array?
 
From what I understand, that is what you need, don't you? An array of square roots of elements in A?
 
that does make sense. My professor just wrote it without it I assume.
 
Great! So now you will also need to change the data types of p1 and p2. They should also be double[].
 
9:18 PM
@RachayitaGiri I truly appreciate your thorough help. Thank you!
 
@RachayitaGiri Since the output of sqrtSquares() is used for multiplication, I believe it is supposed to return a double and the function sqrtSquares() is trying to find the square root of all the numbers of the array combined
 
Then there is a logical issue. Do you want the sum of all the square roots? Or do you want the max/min of those roots, or a mean or median?
 
yes. So then I would not return an array?
@RachayitaGiri I would like to sum all of the square roots
 
Nope. Then you don't return that, just keep adding them as you go. Let me edit your code directly.
 
that makes sense. Thank you so much
 
9:18 PM
Is the code running fine now? I am not sure. Because you again have two unconditional return statements in the similarity function.
 
@JohnnyDepp, nope, I guess since you have tagged cosine similarity, you want square root of sum of square of all the elements en.wikipedia.org/wiki/Cosine_similarity
 
@RachayitaGiri the code is now just returning the first number of each array. When I run it any rating that was -1 is now replaced to zero. Therefore instead of the output being NaN it now 0.0
@RachayitaGiri yes that is correct. I wanted to go through all the elements of the array
 
That is because you are repeating your mistake. You are returning inside the for loop. So you process the first element and return. Remove that return statement from there.
 

last day (15 days later) »