英文:
How to convert String to numerical value in Java
问题
我正在进行神经网络项目的工作,在数据集中我只有字符串值,但我知道神经网络只接受数字值,所以我该如何将字符串转换为类似于以下的双精度数值:
“red” = 0.4582932
“green” = 0.512932
“black” = 0.542123
英文:
I'm working on my Neural Network project, and in data set I have only string values, but I know that NN accepts only numerical values so how can I convert string to double like this :
“red” = 0.4582932
“green” = 0.512932
“black” = 0.542123
答案1
得分: 1
如果值是动态的,那么可以使用@ElliotFrisch在问题评论中提供的解决方案,该解决方案涉及使用分别为String和Double类型的Map。
如果值是预定的且不会更改,那么枚举可能是您的解决方案。
enum Color {
RED(0.4582932),
GREEN(0.512932),
BLACK(0.542123);
private final double value;
private Color(double value) {
this.value = value;
}
}
然后,您可以创建一个枚举值的集合。在此使用ImmutableSet。
private static final Set<Color> COLORS = ImmutableSet.copyOf(EnumSet.allOf(Color.class));
英文:
If the values are dynamic then use the solution that @ElliotFrisch provided in the question comments regarding the use of a Map of type String and Double respectively.
If the values are predetermined and do not change then an enumeration might be the solution for you.
enum Color {
RED(0.4582932),
GREEN(0.512932),
BLACK(0.542123);
private final double value;
private Color(double value) {
this.value = value;
}
}
You could then create a Set of the enum values. ImmutableSet here.
private static final Set<Color> COLORS = ImmutableSet.copyOf(EnumSet.allOf(Color.class));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论