Saving output of confusionMatrix as a .csv table
Ok, so if you inspect the output of confusionMatrix(xtab, prevalence = 0.25)
, it's a list:
cm <- confusionMatrix(pred, truth)
str(cm)
List of 5
$ positive: chr "abnormal"
$ table : 'table' int [1:2, 1:2] 231 27 32 54
..- attr(*, "dimnames")=List of 2
.. ..$ Prediction: chr [1:2] "abnormal" "normal"
.. ..$ Reference : chr [1:2] "abnormal" "normal"
$ overall : Named num [1:7] 0.828 0.534 0.784 0.867 0.75 ...
..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ...
$ byClass : Named num [1:8] 0.895 0.628 0.878 0.667 0.75 ...
..- attr(*, "names")= chr [1:8] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ...
$ dots : list()
- attr(*, "class")= chr "confusionMatrix"
From here on you select the appropriate objects that you want to create a csv from and make a data.frame that will have a column for each variable. In your case, this will be:
tocsv <- data.frame(cbind(t(cm$overall),t(cm$byClass)))
# You can then use
write.csv(tocsv,file="file.csv")
How to save the output of multiple confusionMatrix to single csv file in r?
The problem with using the as.matrix()
function in this example is you're creating a list of lists. Instead of:
as.matrix(result, what = "classes")
as.matrix(result, what = "overall")
try creating data frames to house your results which you can populate by iterating through your original result
list. The code below should do the trick.
## iterate through all six parts of the confusionMatrix: "positive", "table", "overall", "byClass", "mode", "dots"
for(i in 1:length(names(result[[1]]))){
##create a data frame to house the data for export
data <- data.frame()
## iterate through all results; in the example we have Aizawl" and "Serchhip"
for(j in 1:length(names(result))){
## load the data into a data frame
df <- data.frame(result[[j]][i])
## if data is empty no need to alter or append to data frame so skip to next
if(nrow(df)==0){next}
## add a name column for identifying between result sets; in the example we have Aizawl" and "Serchhip"
df$name <- names(result)[j]
## append the loaded data to the data frame for export
data <- rbind(data, df)
}
## if data is empty no need to export, therefore skip to next
if(nrow(data)==0){next}
## write the data to a csv with the name of the part of the condusionMatrix it contains
## row.names changed to TRUE based on OP's comments
write.csv(data, file = paste0(names(result[[1]])[i],".csv"), row.names = TRUE, na = "")
}
Unless, you'd like you can transform the data frames before using the write.csv()
function. In which case you can use
for(i in 1:length(names(result[[1]]))){
data <- data.frame()
for(j in 1:length(names(result))){
df <- data.frame(result[[j]][i])
if(nrow(df)==0){next}
df$name <- names(result)[j]
data <- rbind(data, df)
}
if(nrow(data)==0){next}
assign(names(result[[1]])[i], data)
}
Python: how to save a confusion matrix
df_confusion.to_csv('your_output_file_name.csv')
df_confusion.to_html('your_output_file_name.html')
check the documentation for full details and parameters:
http://pandas.pydata.org/pandas-docs/version/0.20.3/generated/pandas.DataFrame.to_csv.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html
also, a search would have answered it very fast, please do that in future.
How to read a csv file and plot confusion matrix in python
pd.read_csv
is a function. You call a function in Python by using parenthesis.
You should use pd.read_csv(CSVFILE)
instead of pd.read_csv[CSVFILE]
.
Exporting table as a csv file while preserving correct column numbers
Try using the quote=TRUE
option. Excel should be able to distinguish between ,
used to delimit columns vs. part of the data.
Here is the command with the one additional option.
write.csv(
summary(univariateTable(gender ~ Q(value) + genotype, data=df)),
file="file.csv", quote = TRUE)
Apply confusionMatrix using pipe operator in R
If you do it on the whole data.frame, you get a array of tables separated by stations, hence confusionMatrix() complains:
xtabs( ~ Observed + Forecasted + Station, data =df)
, , Station = Aizawl
Forecasted
Observed 1 3 4 5 6
1 56 2 13 13 0
3 12 0 4 8 0
4 4 0 9 11 0
5 3 0 7 23 0
6 0 0 0 0 0
, , Station = Serchhip
Forecasted
Observed 1 3 4 5 6
1 76 3 18 18 0
3 4 0 2 5 0
4 2 0 4 12 0
5 1 0 9 10 2
6 0 0 0 2 0
So you can try using array_tree()
with margin = 3
to convert it into a list, with which you can use map() to apply the confusion matrix:
df %>%
xtabs( ~ Observed + Forecasted + Station, data =.) %>%
array_tree(.,margin=3) %>%
map(~caret::confusionMatrix(as.table(.x)))
How to log Keras loss output to a file
There is a simple solution to your problem. Every time any of the fit
methods are used - as a result the special callback called History Callback is returned. It has a field history
which is a dictionary of all metrics registered after every epoch. So to get list of loss function values after every epoch you can easly do:
history_callback = model.fit(params...)
loss_history = history_callback.history["loss"]
It's easy to save such list to a file (e.g. by converting it to numpy
array and using savetxt
method).
UPDATE:
Try:
import numpy
numpy_loss_history = numpy.array(loss_history)
numpy.savetxt("loss_history.txt", numpy_loss_history, delimiter=",")
UPDATE 2:
The solution to the problem of recording a loss after every batch is written in Keras Callbacks Documentation in a Create a Callback paragraph.
Related Topics
Delete Rows With Negative Values
Concatenate a Vector of Strings/Character
Find Indices of Duplicated Rows
How to Arrange a Variable List of Plots Using Grid.Arrange
Case Statement Equivalent in R
Create Stacked Barplot Where Each Stack Is Scaled to Sum to 100%
Select the Top N Values by Group
Select the First and Last Row by Group in a Data Frame
To Find Most Frequently Occuring Element in Matrix in R
How to Convert Only Some Positive Numbers to Negative Numbers (Conditional Recoding)
Too Much White Space Between Caption and Figure Produced by Tikzdevice and Ggplot2 in Latex
How to Specify the Size of a Graph in Ggplot2 Independent of Axis Labels
Dplyr Conditional Summarise Function
Ggplot Does Not Work If It Is Inside a For Loop Although It Works Outside of It
How to Generate Permutations or Combinations of Object in R
How to Spread Repeated Measures of Multiple Variables into Wide Format