Understanding Random Forest probability - probability

Lets say I trained a random forest on classes 0,1,2 as seen below.
Now I predict the classes probabilities with random forest for new points A and B.
Would I get...
For point A a probabilities classes of: 50% - class 0, 25% class 1, 25% class 2?
For point B a probabilities classes of: 90% - class 0, 5% class 1, 5% class 2?
In other words, does the class probability meaning is some kind of normalized distance between classes?
Now, let's say class 0 had only 1 point. Would this change the probabilities above?
Would appreciate if someone can put some light on this.
Thank!

Related

If my model is trained using sigmoid at the final layer and binary_crossentropy, can I shtill out put probability of classes rather than 0/1?

I have trained a CNN model with dense layer at the end using a sigmoid function:
model.add(layers.Dense(1, activation='sigmoid'))
I have also compiled using binary cross entropy:
model.compile(loss='binary_crossentropy',
optimizer = 'Adam',
metrics=[tf.keras.metrics.Precision(),tf.keras.metrics.Recall(),'accuracy'])
The f1 score of the binary images classification comes low and my model predicts one class over the other. So I decided to add a threshold based on the output probability of my sigmoid function at the final layer:
c = load_img('/home/kenan/Desktop/COV19D/validation/covid/ct_scan_19/120.jpg',
color_mode='grayscale',
target_size = (512,512))
c=img_to_array(c)
c= np.expand_dims(c, axis=0)
pred = model.predict_proba(c)
pred
y_classes = ((model.predict(c)> 0.99)+0).ravel()
y_classes
I want to use 'pred' in my code as a probability of the class but it is always either 0 or 1 as shown below:
Out[113]: array([[1.]], dtype=float32)
why doesn't it give the probability of predicting the class between [0,1] instead of 1? is there a way to get the class probability in my case rather than 0 or 1?
No you cant. Sigmoid activation in the final layer will output ONE value in the range of 0 to 1. If you want to obtain class probabilities of the different labels, you'll have to change the final layer activation to softmax.

How do I create a population sample that follows specified demographics?

I have the following class:
class Person
{
GenderEnum Gender;
RaceEnum Race;
double Salary;
...
}
I want to create 1000 instances of this class such that the collection of 1000 Persons follow these 5 demographic statistics:
50% male; 50% female
55% white; 20% black; 15% Hispanic; 5% Asian; 2% Native American; 3% Other;
10% < $10K; 15% $10K-$25K; 35% $25K-$50K; 20% $50K-$100K; 15% $100K-$200K; 5% over $200K
Mean salary for females is 77% of mean salary for males
Mean Salary as a percentage of mean white salary:
white - 100%.
black - 75%.
Hispanic - 83%.
Asian - 115%.
Native American - 94%.
Other - 100%.
The categories above are exactly what I want but the percentages given are just examples. The actual percentages will be inputs to my application and will be based on what district my application is looking at.
How can I accomplish this?
What I've tried:
I can pretty easily create 1000 instances of my Person class and assign the Gender and race to match my demographics. (For my project I'm assuming male/female ratio is independent of race). I can also randomly create a list of salaries based on the specified percentage brackets. Where I run into trouble is figuring out how to assign those salaries to my Person instances in such a way that the mean salaries across gender and mean salaries across race match the specified conditions.
I think you can solve this by assuming that the distribution of income for all categories is the same shape as the one you gave, but scaled by a factor which makes all the values larger or smaller. That is, the income distribution has the same number of bars and the same mass proportion in each bar, but the bars are shifted towards smaller values or towards larger values, and all bars are shifted by the same factor.
If that's reasonable, then this has an easy solution. Note that the mean value of the income distribution over all people is sum(p[i]*c[i], i, 1, #bars), which I'll call M, where p[i] = mass proportion of bar i and c[i] = center of bar i. For each group j, you have the mean sum(s[j]*p[i]*c[i], i, 1, #bars) = s[j]*M where s[j] is the scale factor for group j. Furthermore you know that the overall mean is equal to the sum of the means of the groups, weighting each by the proportion of people in that category, i.e. M = sum(s[j]*M*q[j], j, 1, #groups) where q[j] is the proportion of people in the group. Finally you are given specific values for the mean of each group relative to the mean for white people, i.e. you know (s[j]*M)/(s[k]*M) = s[j]/s[k] = some fraction, where k is the index for the white group. From this much you can solve these equations for s[k] (the scaling factor for the white group) and then s[j] from that.
I've spelled this out for the racial groups only. You can repeat the process for men versus women, starting with the distribution you found for each racial group and finding an additional scaling factor. I would guess that if you did it the other way, gender first and then race, you would get the same results, but although it seems obvious I wouldn't be sure unless I worked out a proof of it.

Need a weighted scoring algorithm to combine scores with different scale

I am working on a scoring system for tickets and each ticket can potentially have up to 4 kinds of different scores. What I would like to do is to combine these four scores in the a final score and prioritize the tickets. I'd also like to assign a weight to each of the 4 score. The details of the 4 scores are listed below:
Score A: 1-5 scale, desired relative weight: 2
Score B: 1-4 scale, desired relative weight: 3
Score C: 1-10 scale, desired relative weight: 2
Score D: 1-5 scale, desired relative weight: 1
Some requirements:
(1) Each ticket may come with arbitrary number of scores, so sometimes we have all 4, sometimes we have no scores(Default final score needed).
(2) If the ticket gets a high score from multiple sources, the final score should be even higher, vice versa.
(3) The score with higher weight plays a bigger role in deciding the final score
(4) The final score should be in 1-4 scale.
I wonder if there are any existing algorithm for solving this kind of issue? Thanks ahead.
Desired input and output example:
(1) Input: {A:N/A, B:4, C:9, D:N/A}
Output: {Final: 4}
Since for both score it's a high score
(2) Input: {A:3, B:N\A, C:8, D:1}
Output: {Final:3}
Although score D is small, it has small weight, so still we get a relative big final score.
(3) Input: {A:N\A, B:N\A, C:N\A, D:N\A}
Output: {Final:2}
Arguable default score.
The overall idea is to rank the tickets according to the four scores.
Define a initial relative weight W for every score.
Convert every initial score S from it's initial scale A into a universal score S' on a universal scale B from minB to maxB.
If a score is missing give it a default value for example
Calculate the final score with your new S
a and b are your weights for the score and the weight.
If you make a large, then only really the biggest score will play a value, if you make b large, then only really the biggest weight will play a value.
Having a and b between [1;2] shouldn't be too extreme. With a or b being 1 you will have a normal weighting system, that doesn't weight bigger scores more.

Clarity in Procedural Texture Algorithms?

In the Big Picture Section of this page here a table is given for comparing different combinations of 3 different functions. Let the function in the left be y = f(x) then what about the functions Average, Difference, Weighted Sum, 4% Threshold ? I need the mathematical equation in terms of y
Everything is explained on that page :
Here are some simple, boring functions which, when repeatedly combined with smaller and smaller versions of themselves, create very interesting patterns. The table below shows you the basic source pattern (left), and combinations of that pattern with smaller versions of itself using various combination methods.
Average (1/n) - This is simply the average of all of the scales being used, 'n' is the total number of scales. So if there are 6 scales, each scale contributes about 16% (1/6th) of the final value.
Difference - This uses the difference between the color values of each scale as the final texture color.
Weighted Sum (1/2^n) - The weighted sum is very similar to the average, except the larger scales have more weight. As 'n' increases, the contribution of that scale is lessened. The smallest scales (highest value of n) have the least effect. This method is the most common and typically the most visually pleasing.
4% Threshold - This is a version of the Weighted Sum where anything below 48% gray is turned black, and anything above 52% gray is turned white.
Let us take the Average and checker function. You are averaging a number of repeating different images, 6 in their example, but 3 in the following example :
So each pixel of the output image is the average value of the pixel values from the other images. You can have as many of these images as you want, and they are always built the same way : the image at level n is made of 4 tiles which are the image at level n-1 scaled to a fourth of its size. Then from all these pictures you apply one of the above functions to get only one.
Is it clearer now ? It is, however, generally hard to give a function f that defines each image. However, the "compounding" functions are defined even though there are n inputs (xs) for 1 output (y = f(x1, x2, ....xn)) in pseudocode and math :
Average (1/n) - For n levels, final_pixel[x][y] = sum for i from 1 to n of image_i[x][y]/n
Difference - For n levels, final_pixel[x][y] = sum for i from 2 to n of to n of image_i[x][y] - image_i-1[x][y] -- Not entirely sure about this one.
Weighted Sum (1/2^n) - For n levels, final_pixel[x][y] = sum for i from 1 to n of image_i[x][y]/(2**n)
4% Threshold - For n levels,
value = sum for i from 1 to n of image_i[x][y]/(2**n)
if value/max_value > .52 then final_pixel[x][y]=white
else if value/max_value < .48 then final_pixel[x][y]=black;
else final_pixel[x][y]=value
Where 2**n is 2 to the power of n.

Probability map of an image

Probability map of an image can be calculated as bellow, may be it help someone
1
We have a probability matrix which has probabilty for each class. Let this be
x=[x1 x2 x3 x4] for 4 classes
2
to get probability map we will have four separate images for each class. let for class x1
x1=x1*255. this will be the pixel value for labeling.
3
Now for each class we will multiply this 255 with each probability value and will set value in the image this one.
4
as a result we will have four gray scale images and these are called probability maps.
You could generate 6 maps for all classes. Or you could select 6 different colors, and use the probability to interpolate a new color, then you will get just one map.

Resources