Why there are negative values coming up when I create a random array - "np.random.rand" - numpy

Why there is negative numbers when I create the below array?
a12 = np.random.randn(3, 5)
a12
Output:-
array([[-1.43586215, 1.16316375, 0.01023306, -0.98150865, 0.46210347],
[ 0.1990597 , -0.60021688, 0.06980208, -0.3853136 , 0.11351735],
[ 0.66213067, 1.58601682, -1.2378155 , 2.13303337, -1.9520878 ]])

np.random.randn() draws a sample from the Standard Normal Distribution i.e N(0,1). Passing in the dimensions returns an array of the given shape i.e np.random.randn(3,5) will return an array with shape (3,5) with all elements drawn from the standard normal distribution. Hence, we can get negative numbers, and infact all numbers in R.

Related

Change cell color if the two values of this cell have opposite signs in Pretty tables in Julia

How to change cell color if the two values of this cell have opposite signs in Pretty tables in Julia below is my code and the table is attached.
names = string.(-1/1:1/4:1/1)
pretty_table(AStrings , header = ([-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]), row_names= names)
After digging through the docs:
using PrettyTables
# making some demo data
data = collect(zip(rand([-1.0,1.0],5,5),rand([-1.0,1.0],5,5)))
names = [-1, -1/2, 0, 1/2, 1]
# this is the Highlighter which makes text red when signs differ.
# signs differ if their product is negative.
hl = Highlighter((d,i,j)->d[i,j][1]*d[i,j][2] < 0, crayon"red")
Then the Highlighter is used as follows:
pretty_table(data ; header = names, row_names= names, highlighters=hl)
Well, colors don't go through in text, so put an image of result.
This answer is beyond what was asked by the OP, but hopefully would be informative. In addition to Dan Getz's answer, one can apply more than one rule for highlighting the values. For example, if you want to make pairs with positive value green besides the first rule, you can pass a tuple of Highlighter to the highlighters keyword argument.
I will use Dan's example to show you the results:
julia> hl = (
Highlighter((d,i,j)->d[i,j][1]*d[i,j][2]<0, crayon"red"),
Highlighter((d,i,j)->d[i,j][1]>0 && d[i,j][2]>0, crayon"green")
)
The result of pretty_table(data; header=names, row_names=names, highlighters=hl) would be:

How to manually calculate the median in VBA?

If (temp = 8) Then
med = 0
Else
med = Application.Median(TP.Columns(j))
End If
Instead of using the in built function Application.Median, how do I calculate the median?
Instead of using in built functions you can create an array to store all your values and then get the median value by arranging the values and getting the one in array length/2.
If your array length / 2 is a decimal value you will have to get the values of rounding up and rounding down the array length/2, adding them and the dividing them by 2.

48bit RGB single pixel value

3 RGB values are represented with a single one value in some image processing applications.
For example: The single value for RGB(2758, 5541, 4055) is 4542.64
There are some questions related on how to obtain single pixel values from 8bit RGB images but none works with 48bit RGB images. How can I obtain that value?
If I do (2758 + 5541 + 4055) / 3 the result is 4118 which is near but not the same.
It appears that you are trying to determine the grayscale formula used to arrive at that given value. I suggest that you read Seven grayscale conversion algorithms by Tanner Helland.
Based on your example of:
The single value for RGB(2758, 5541, 4055) is 4542.64
It appears that value is computed using the formula:
Gray = (Red * 0.3 + Green * 0.59 + Blue * 0.11)

Numpy maximum(arrays)--how to determine the array each max value came from

I have numpy arrays representing July temperature for each year since 1950.
I can use the numpy.maximum(temp1950,temp1951,temp1952,..temp2014)
to determine the maximum July temperature at each cell.
I need the maximum for each cell..the numpy.maximum() works for only 2 arrays
How do I determine the year that each max value came from?
Also the numpy.maximum(array1,array2) works comparing only two arrays.
Thanks to Praveen, the following works fine:
array1 = numpy.array( ([1,2],[3,4]) )
array2 = numpy.array( ([3,4],[1,2]) )
array3 = numpy.array( ([9,1],[1,9]) )
all_arrays = numpy.dstack((array1,array2,array3))
#maxvalues = numpy.maximum(all_arrays)#will not work
all_arrays.max(axis=2) #this returns the max from each cell location
max_indexes = numpy.argmax(all_arrays,axis=2)#this returns correct indexes
The answer is argmax, except that you need to do this along the required axis. If you have 65 years' worth of temperatures, it doesn't make sense to keep them in separate arrays.
Instead, put them all into a single 2D dimensional array using something like np.vstack and then take the argmax over rows.
alltemps = np.vstack((temp1950, temp1951, ..., temp2014))
maxindexes = np.argmax(alltemps, axis=0)
If your temperature arrays are already 2D for some reason, then you can use np.dstack to stack in depth instead. Then you'll have to take argmax over axis=2.
For the specific example in your question, you're looking for something like:
t = np.dstack((array1, array2)) # Note the double parantheses. You need to pass
# a tuple to the function
maxindexes = np.argmax(t, axis=2)
PS: If you are getting the data out of a file, I suggest putting them in a single array to start with. It gets hard to handle 65 variable names.
You need to use Numpy's argmax
It would give you the index of the largest element in the array, which you can map to the year.

How to Resize using Lanczos

I can easily calculate the values for sinc(x) curve used in Lanczos, and I have read the previous explanations about Lanczos resize, but being new to this area I do not understand how to actually apply them.
To resample with lanczos imagine you
overlay the output and input over
eachother, with points signifying
where the pixel locations are. For
each output pixel location you take a
box +- 3 output pixels from that
point. For every input pixel that lies
in that box, calculate the value of
the lanczos function at that location
with the distance from the output
location in output pixel coordinates
as the parameter. You then need to
normalize the calculated values by
scaling them so that they add up to 1.
After that multiply each input pixel
value with the corresponding scaling
value and add the results together to
get the value of the output pixel.
For example, what does "overlay the input and output" actually mean in programming terms?
In the equation given
lanczos(x) = {
0 if abs(x) > 3,
1 if x == 0,
else sin(x*pi)/x
}
what is x?
As a simple example, suppose I have an input image with 14 values (i.e. in addresses In0-In13):
20 25 30 35 40 45 50 45 40 35 30 25 20 15
and I want to scale this up by 2, i.e. to an image with 28 values (i.e. in addresses Out0-Out27).
Clearly, the value in address Out13 is going to be similar to the value in address In7, but which values do I actually multiply to calculate the correct value for Out13?
What is x in the algorithm?
If the values in your input data is at t coordinates [0 1 2 3 ...], then your output (which is scaled up by 2) has t coordinates at [0 .5 1 1.5 2 2.5 3 ...]. So to get the first output value, you center your filter at 0 and multiply by all of the input values. Then to get the second output, you center your filter at 1/2 and multiply by all of the input values. Etc ...