Count unique values across multiple columns - excel-2010

I have data in this format
A B C D
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
...
4 4 4 4
I want to count number of unique values in each row and print it
output:
A B C D unique-count
1 1 1 1 4
1 1 1 2 3
1 1 1 3 3
1 1 1 4 3
...
4 4 4 4 4

Related

Left Join also working in direct query in Power BI

This is only sample case, my original table is more complex.
Table A
SchoolId
ClubId
ChildID
TeacherId
AttendanceDate
IsPresent
A
1
1
1
22-MAY-2022
1
A
1
2
1
22-MAY-2022
0
A
1
3
1
22-MAY-2022
1
B
2
11
2
22-MAY-2022
1
B
2
22
2
22-MAY-2022
0
B
2
33
2
22-MAY-2022
0
Table B
ChildID
TeacherId
CreateOn
IsPresent
ReasonId
2
1
22-MAY-2022
0
1
2
1
23-MAY-2022
0
2
22
2
22-MAY-2022
0
2
33
2
22-MAY-2022
0
3
Table C
ReasonId
ReasonMaster
1
Health
2
Social
3
Unknown
I want the left join result like this :
SchoolId
ClubId
ChildID
TeacherId
AttendanceDate
IsPresent
ReasonId
ReasonMaster
A
1
1
1
22-MAY-2022
1
A
1
2
1
22-MAY-2022
0
2
Social
A
1
3
1
22-MAY-2022
1
B
2
11
2
22-MAY-2022
1
B
2
22
2
22-MAY-2022
0
2
Social
B
2
33
2
22-MAY-2022
0
3
Unknown
Here are my cases:
I only want to retrieve the latest data from table B based on create on column on the table B to the table A. Because there is a duplicate input by users. for instance in the table B child Id= 2 & Teacher Id=1.
I only need to retrieve the the data if the status in the table A, column IsPresent=0
There is an additional data from table C which reason master.
I try this Query, but CTE function not working in Power BI.
;with MaxCreate as (Select ChildID,TeacherID,Max(CreateOn) as MaxCreateOn
from TableB
group by ChildID,TeacherID)
,LatestCreate as (select TableB.ChildID, TableB.TeacherId, TableB.CreateOn, TableB.IsPresent, TableB.ReasonId
from TableB
inner join MaxCreate
on TableB.ChildID = MaxCreate.ChildID
and TableB.TeacherId = MaxCreate.TeacherId
and TableB.CreateOn = MaxCreate.MaxCreateOn)
Select
TableA.SchoolId
,TableA.ClubId
,TableA.ChildID
,TableA.TeacherId
,TableA.AttendanceDate
,TableA.IsPresent
,LatestCreate.ReasonId
,TableC.ReasonMaster
From TableA
Left join LatestCreate
on TableA.ChildID = LatestCreate.ChildID
and TableA.TeacherID = LatestCreate.TeacherID
left join TableC
on LatestCreate.ReasonId = TableC.ReasonId

Count the number of contiguous subarrays with maximum element as x

Given an array of numbers, print the number of subarrays with maximum element as x. For example :
Input :
arr = [1, 2, 3, 3, 1]
x = [3,2,1,4]
output : 11,2,2,0
Subarrays for x = 1:
1
1
Subarrays for x = 2:
2
1 2
Subarrays for x = 3:
1 2 3
1 2 3 3
1 2 3 3 1
2 3
2 3 3
2 3 3 1
3
3 3
3 3 1
3
3 1
There are no subarray with maximum element as 4. So for x = 4 we have to print 0.
My first attempt was to generate all subarrays and count that.The time complexity of this approach is very bad(O(n^3))

what kind of encoding is this and how to decode it

I asked my friend and he said that it's AST. But I'm not sure and don't know how to decode it.
SCE = {} SCE[0x3B06D907] = function() return SCE[0x16D35994]({SCE[0x2CCB9B3C](), SCE[0x24AA4743](), SCE[0x1E716305](), SCE[0x2A7C5767](), SCE[0x165A6AC3](), SCE[0x1D0ACBC5](), SCE[0x3B79C46E](), SCE[0x1D4DE704](), SCE[0x27680DA1](), SCE[0x1C317CFE](), SCE[0x316934F5](), SCE[0x18C41DEC](), SCE[0x2A7D468A](), SCE[0x3A8183EA](), SCE[0x28E77657](), SCE[0x3159E5B8](), SCE[0x1ACECA1C]() , SCE[0x285218A3](), SCE[0x341688EA](), SCE[0x26DCC896](), SCE[0x1A80B2E1](), SCE[0x17D18EE4](), SCE[0x29C39207](), SCE[0x27BA5263](), SCE[0x166769AE](), SCE[0x1A81C222](), SCE[0x3549905A]()}) end SCE[0x3813D8D1] = '0 3 2 0 3 9 0 3 3 0 4 0 0 3 6 0 3 2 0 3 2 0 3 3 0 3 3 0 4 0 0 3 8 0 3 2 0 3 3 0 4 0 0 3 2 0 3 3 0 3 2 0 3 3 0 3 7 0 3 7 0 3 3 0 3 4 0 3 5 0 3 2 0 3 3 0 3 5 0 3 2 0 ' SCE[0x38B2F6A7] = '3 2 0 3 8 0 3 2 0 3 2 0 3 3 0 3 3 0 4 0 0 4 0 0 3 2 0 3 2 0 3 9 0 3 2 0 3 3 0 3 6 0 3 2 0 3 2 0 3 2 0 3 2 0 3 2 0 3 3 0 3 3 0 3 7 0 3 3 0 3 2 0 3 2 0 3 6 0 3 2 0 3 ' SCE[0x2D2D5130] = function(Ox26464833) if SCE[0x38B2F6A7] == SCE[0x23AC3D05] or SCE[0x38B2F6A7] == SCE[0x3B509C73] or SCE[0x142600A7](SCE[0x38B2F6A7]) <= SCE[0x2F7A2C68] then return else Ox26464833 = SCE[0x2EC981D9] end return SCE[0x38B2F6A7] end SCE[0x3111B14D] = '2 0 3 9 0 3 3 0 4 0 0 3 8 0 3 2 0 3 2 0 3 9 0 3 2 0 3 3 0 3 2 0 3 3 0 3 7 0 3 2 0 3 3 0 3 4 0 3 5 0 3 2 0 3 3 0 3 6 0 3 2 0 3 3 0 3 5 0 3 3 0 3 4 0 3 5 0 3 2 0 3 2 ' SCE[0x32D39C31] = function(Ox26464833) if SCE[0x3111B14D] == SCE[0x23AC3D05] or SCE[0x3111B14D] == SCE[0x3B509C73] or SCE[0x142600A7](SCE[0x3111B14D]) <= SCE[0x2F7A2C68] then return else Ox26464833 = SCE[0x14EF2C5C] end return SCE[0x3111B14D] end SCE[0x290A5F73] = '0 3 6 0 3 3 0 4 0 0 4 0 0 3 2 0 3 2 0 3 7 0 3 2 0 3 3 0 3 6 0 3 2 0 3 2 0 3 5 0 3 2 0 3 2 0 3 9 0 3 3 0 4 0 0 3 6 0 3 3 0 4 0 0 3 8 0 3 3 0 4 0 0 4 0 0 3 2 0 3 2 0 ' SCE[0x1AD49A98] = function(Ox26464833) if SCE[0x290A5F73] == SCE[0x23AC3D05] or SCE[0x290A5F73] == SCE[0x3B509C73] or SCE[0x142600A7](SCE[0x290A5F73]) <= SCE[0x2F7A2C68] then return

Transform string variable into 0-1 columns

As a very begginer in SPSS I would ask you for help with some transformation from table A into table B. I have to recode values of "brand" variable into columns and make 0-1 variables.
#table A#
nr brand
1 GREEN CARE PROFESSIONAL
1 GREEN CARE PROFESSIONAL
1 GREEN CARE PROFESSIONAL
2 HENKEL
3 HENKEL
3 HENKEL
3 HENKEL
3 VIZIR
4 BIEDRONKA
4 BOBINI
4 BOBINI
4 BOBINI
4 BOBINI
4 BOBINI
4 HENKEL
5 VIZIR
6 HENKEL
#table B#
nr GREEN HENKEL VIZIR BIEDR BOBINI
1 1 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
2 0 1 0 0 0
3 0 1 0 0 0
3 0 1 0 0 0
3 0 1 0 0 0
3 0 0 1 0 0
4 0 0 0 1 0
4 0 0 0 0 1
4 0 0 0 0 1
4 0 0 0 0 1
4 0 0 0 0 1
4 0 0 0 0 1
4 0 1 0 0 0
5 0 0 1 0 0
6 0 1 0 0 0
I can do it in this particular case in this simple way:
compute HENKEL=0.
...
do if BRAND='GREEN_CARE' .
compute GREEN_CARE=1.
else if ....
but the loop has to be usable with another variable and different number of values ect. I was trying to make it all day and gave up.
Do you have any idea to make it in a easy way?
Thanks!
The following syntax does the job on the sample data you provided.
First, let's recreate the sample data to demonstrate on:
Data list list/nr (f1) brand (a30).
begin data
1 "GREEN CARE PROFESSIONAL"
1 "GREEN CARE PROFESSIONAL"
1 "GREEN CARE PROFESSIONAL"
2 "HENKEL"
3 "HENKEL"
3 "HENKEL"
3 "HENKEL"
3 "VIZIR"
4 "BIEDRONKA"
4 "BOBINI"
4 "BOBINI"
4 "BOBINI"
4 "BOBINI"
4 "BOBINI"
4 "HENKEL"
5 "VIZIR"
6 "HENKEL"
end data.
dataset name originalDataset.
Now for the restructure.
sort cases by nr brand.
* creating an index to enumerate cases for each combination of `nr` and `brand`.
* This is necessary for the `casestovars` command to work later.
compute ind=1.
if $casenum>1 and lag(nr)=nr and lag(brand)=brand ind=lag(ind)+1.
exe.
* variable names can't have spaces in them, so changing the category names accordingly.
compute brand=replace(rtrim(brand)," ","_").
sort cases by nr ind brand.
compute exist=1.
casestovars /id=nr ind /index= brand/autofix=no.

OneVsRestClassifier(svm.SVC()).predict() gives continous values

I am trying to use y_scores=OneVsRestClassifier(svm.SVC()).predict() on datasets
like iris and titanic .The trouble is that I am getting y_scores as continous values.like for iris dataset I am getting :
[[ -3.70047231 -0.74209097 2.29720159]
[ -1.93190155 0.69106231 -2.24974856]
.....
I am using the OneVsRestClassifier for other classifier models like knn,randomforest,naive bayes and they are giving appropriate results in the form of
[[ 0 1 0]
[ 1 0 1]...
etc on the iris dataset .Please help.
Well this is simply not true.
>>> from sklearn.multiclass import OneVsRestClassifier
>>> from sklearn.svm import SVC
>>> from sklearn.datasets import load_iris
>>> iris = load_iris()
>>> clf = OneVsRestClassifier(SVC())
>>> clf.fit(iris['data'], iris['target'])
OneVsRestClassifier(estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
kernel='rbf', max_iter=-1, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False),
n_jobs=1)
>>> print clf.predict(iris['data'])
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2]
maybe you called decision_function instead (which would match your output dimension, as predict is supposed to return a vector, not a matrix). Then, SVM returns signed distances to each hyperplane, which is its decision function from mathematical perspective.

Resources