(DataPoint dp, double label, ByteArrayOutputStream byteOut)
| 787 | } |
| 788 | |
| 789 | @Override |
| 790 | protected void pointToBytes(DataPoint dp, double label, ByteArrayOutputStream byteOut) |
| 791 | { |
| 792 | PrintWriter writer = new PrintWriter(byteOut); |
| 793 | boolean nothingWrittenYet = true; |
| 794 | |
| 795 | //target feature always goes at the front |
| 796 | if(type == DataWriter.DataSetType.CLASSIFICATION) |
| 797 | { |
| 798 | int targetClass = (int) label; |
| 799 | writer.write(classNames[targetClass]); |
| 800 | nothingWrittenYet = false; |
| 801 | } |
| 802 | else if(type == DataWriter.DataSetType.REGRESSION) |
| 803 | { |
| 804 | double targetVal = label; |
| 805 | writer.write(Double.toString(targetVal)); |
| 806 | nothingWrittenYet = false; |
| 807 | } |
| 808 | |
| 809 | Vec v =dp.getNumericalValues(); |
| 810 | int[] c = dp.getCategoricalValues(); |
| 811 | |
| 812 | |
| 813 | //write out numeric features first |
| 814 | for(int j = 0; j < v.length(); j++) |
| 815 | { |
| 816 | if(!nothingWrittenYet) |
| 817 | writer.write(delimiter); |
| 818 | |
| 819 | //bellow handles NaN correctly, rint will just return NaN and then toString prints "NaN" |
| 820 | double val = v.get(j); |
| 821 | if(Math.rint(val) == val)//cast to long before writting to save space |
| 822 | writer.write(Long.toString((long) val)); |
| 823 | else |
| 824 | writer.write(Double.toString(val)); |
| 825 | nothingWrittenYet = false; |
| 826 | } |
| 827 | //then categorical features, useing the safe names we constructed earlier |
| 828 | for(int j = 0; j < c.length; j++) |
| 829 | { |
| 830 | if(!nothingWrittenYet) |
| 831 | writer.write(delimiter); |
| 832 | if(c[j] >= 0) |
| 833 | writer.write(catNamesToUse[j][c[j]]); |
| 834 | //else, its negative - which is missing, so not writing anything out should result in the correct behavior |
| 835 | nothingWrittenYet = false; |
| 836 | } |
| 837 | writer.write("\n"); |
| 838 | writer.flush(); |
| 839 | } |
| 840 | }; |
| 841 | return dw; |
| 842 | } |
nothing calls this directly
no test coverage detected