데이터베이스 (CSV 파일 형식)에서 많은 기능 (속성)을 읽어야하는 프로젝트에서 작업 중입니다. .Java : 사전에 알려지지 않은 유형의 데이터 읽기
이 파일 외모 (쉽게 읽을 수 있도록 약간의 형식)하는 방법입니다
IsFast; Speed; IsRed; R; G; B
T; 123.4; F; 0.0; 0.0; 1.0
F; 21.3; T; 1.0; 0.0; 0.0
...
당신이 볼 수 있듯이 숫자는 부동 소수점 값을하는 동안, T/F는 부울 값을 나타냅니다. 나중에 속성을 추가하거나 제거 할 수 있습니다.
내 코드에서 각 인스턴스 (행)에 대해이 값을 읽은 다음 다른 코드로 전달하여 데이터를 더 처리해야합니다.
나는 현재이 다루고있어 방법은 다음과 같은 수업을하는 것입니다
public abstract class Feature<T> {
public final String name;
public final T value;
public Feature(String name, T value) {
this.name = name;
this.value = value;
}
public abstract boolean test(T value);
}
public class BoolFeature extends Feature<Boolean> {
public BoolFeature(String name, boolean value) {
super(name, value);
}
@Override
public boolean test(Boolean value) {
return this.value;
}
}
public class DoubleFeature extends Feature<Double> {
public DoubleFeature(String name, double value) {
super(name, value);
}
@Override
public boolean test(Double value) {
return this.value < value;
}
}
내가 다음 CSV 파일을 구문 분석 및 T/F 및 다른 사람을위한 DoubleFeature
들입니다 입력 BoolFeature
개체를 만드는거야 List<Feature>
또는 Feature[]
에 모두 저장 한 다음 해당 컬렉션을 전달합니다. 이 같은 문제를 해결하기 위해 깨끗한 방법이 있나요
public abstract class Metric {
protected List<Feature> features; // this line gives warning #1 from above
public Metric(List<Feature> features) {
this.features = features;
}
public double getSplitQuality(Split split) {
return getImpurity(split.yes) + getImpurity(split.no);
}
public abstract double getImpurity(List<Instance> instances);
}
public class Split {
public final List<Instance> yes;
public final List<Instance> no;
public Split(List<Instance> instances, Feature feature) {
yes = new ArrayList<>();
no = new ArrayList<>();
for (Instance inst : instances)
if (inst.features.get(feature.name).test(feature.value)) // this line gives warning #2 from above
yes.add(inst);
else
no.add(inst);
}
}
public class Instance {
public HashMap<String, Feature> features = new HashMap<>();
public String output = null;
public Instance(String[] featureNames, String[] csvRow) {
/* parse CSV row, creating BoolFeatures and DoubleFeatures
and adding them to the HashMap, for example: */
if (csvRow[0].equals("T") || csvRow[0].equals("F"))
features.put(featureNames[0], new BoolFeature(featureNames[0], csvRow[0].equals("T")));
}
}
:
그러나 이것은 같은 코드에
Feature is a raw type. References to generic type Feature should be parameterized
및
Type safety: The method test(Object) belongs to the raw type Feature. References to generic type Feature should be parameterized
많은 결과?
편집
이전에 지금 # 2 경고했다 대답에 따라, Feature
에 대한 모든 참조에 <?>
을 추가 라인이 컴파일시 에러 제공 후 :
The method test(capture#2-of ?) in the type Feature is not applicable for the arguments (capture#3-of ?)
같은 와일드 카드 형의 사용을 만들 수 있습니까? "그런 다음 다른 코드로 전달한 다음 데이터를 더 처리합니다"라고 말하면 프로세스 다운 스트림은 무엇입니까? 행마다 CSV 구문 분석 결과를 계산합니까? – Comencau
임의의 포리스트 분류 자입니다. 분류 트리를 작성하는 과정에서 모든 기능을 검토하고 인스턴스 (행)를 가장 잘 구분할 수 있어야합니다. 이 분할은'test' 메소드가 필요한 이유입니다. –
@suppresswarnings 주석을 사용하거나 객체 확장 일반 (와일드 카드) >을 사용할 수 있습니다. 나는 당신이 성취하고자하는 것을 완전히 확신하지 못하지만, 경고를 없앨 것입니다. –