I ran the following code for a binary classification task w/ an SVM in both R (first…
- User
I ran the following code for a binary classification task w/ an SVM in both R (first sample) and Python (second example).
Given randomly generated data (X) and response (Y), this code performs leave group out cross validation 1000 times. Each entry of Y is therefore the mean of the prediction across CV iterations.
Computing area under the curve should give ~0.5, since X and Y are completely random. However, this is not what we see. Area under the curve is frequently significantly higher than 0.5. The number of rows of X is very small, which can obviously cause problems.
Any idea what could be happening here? I know that I can either increase the number of rows of X or decrease the number of columns to mediate the problem, but I am looking for other issues.
Y=as.factor(rep(c(1,2), times=14)) X=matrix(runif(length(Y)*100), nrow=length(Y))
library(e1071) library(pROC)
colnames(X)=1:ncol(X) iter=1000 ansMat=matrix(NA,length(Y),iter) for(i in seq(iter)){
#get traintrain=sample(seq(length(Y)),0.5*length(Y)) if(min(table(Y[train]))==0) next #test from train test=seq(length(Y))[-train] #train model XX=X[train,] YY=Y[train] mod=svm(XX,YY,probability=FALSE) XXX=X[test,] predVec=predict(mod,XXX) RFans=attr(predVec,'decision.values') ansMat[test,i]=as.numeric(predVec)}
ans=rowMeans(ansMat,na.rm=TRUE)
r=roc(Y,ans)$auc print(r)
Similarly, when I implement the same thing in Python I get similar results.
Y = np.array([1, 2]14) X = np.random.uniform(size=[len(Y), 100]) n_iter = 1000 ansMat = np.full((len(Y), n_iter), np.nan) for i in range(n_iter): # Get train/test index train = np.random.choice(range(len(Y)), size=int(0.5len(Y)), replace=False, p=None) if len(np.unique(Y)) == 1: continue test = np.array([i for i in range(len(Y)) if i not in train]) # train model mod = SVC(probability=False) mod.fit(X=X[train, :], y=Y[train]) # predict and collect answer ansMat[test, i] = mod.predict(X[test, :]) ans = np.nanmean(ansMat, axis=1) fpr, tpr, thresholds = roc_curve(Y, ans, pos_label=1) print(auc(fpr, tpr))`
- Assistant
- User
I think the issue is, that acutally, the AUCs needs to be calculated for each iteration and then averaged instead of averaging the classification labels obtained in each iteration and then calculating the AUC
- Assistant
- User
But why does the above mentioned strategy not work?
- Assistant
The strategy of averaging predictions across iterations and then calculating the AUC does not work as intended for several reasons:
- User
But why is the AUC so high (around 0.9) with the initial calculation
- Assistant
- User
Can you again explain, why averaging with this small sample size would always lead to too high AUCs
- Assistant
- User
if I run the above code with only 4 samples, I get an accuracy score of 1.0 and the following averged classifications in 1000 iterations: 1.547904 1.273016 1.505988 1.461756 Why are objects with the repsonse 1 more likely classified as 2 with the above algorithm?
- Assistant
- User
the above mentioned patterns always occur, also with larger sample sizes. But it is not that pronounced
- Assistant
- User
you are suggesting a random underlying process. However these patters always occur and are thus likely not random. Do you have other ideas
- Assistant
- User
I think it has something to do with the sample size. With the result that an object labelled 1 is a little more likely to be assigned 2 given the data splitting method
- Assistant
- User
Still that doesnt explain the high AUC in the mentioned code
- Assistant
- User
can you give me a methodically corrected version in R
- Assistant
Certainly! To correct the methodology, you need to calculate the AUC for each iteration using decision function values or probability estimates, not class labels. However, the function from the package in R does not provide probability estimates by default. To get probability estimates, you need to set when fitting the model.
- User
why is the AUC always 1 if I use only 4 samples?
- Assistant
- User
Why are objects with the response 2 more likely (i.e., more often) classified as 1 in 1000 iterations?
- Assistant
- User
I ran the following code for a binary classification task w/ an SVM in both R (first sample) and Python (second example).
Given randomly generated data (X) and response (Y), this code performs leave group out cross validation 1000 times. Each entry of Y is therefore the mean of the prediction across CV iterations.
Computing area under the curve should give ~0.5, since X and Y are completely random. However, this is not what we see. Area under the curve is frequently significantly higher than 0.5. The number of rows of X is very small, which can obviously cause problems.
Any idea what could be happening here? I know that I can either increase the number of rows of X or decrease the number of columns to mediate the problem, but I am looking for other issues.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.