generated from LufsX/Challenge-SVM
This commit is contained in:
+12
-19
@@ -75,28 +75,21 @@ def predict_svm(inX, weights):
|
||||
|
||||
# 从文件中加载数据函数
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
data, labels = [], []
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f.readlines():
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
|
||||
Args:
|
||||
- directory (str): 数据文件路径。
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标 签数据。
|
||||
"""
|
||||
data = []
|
||||
labels = []
|
||||
with open(filepath, 'r') as file:
|
||||
lines = file.readlines()
|
||||
for line in lines:
|
||||
line_data = line.strip().split('\t')
|
||||
features = np.array([float(x) for x in line_data[:-1]])
|
||||
label = int(line_data[-1])
|
||||
data.append(features)
|
||||
labels.append(label)
|
||||
target_variable = labels
|
||||
|
||||
return np.array(data), np.array(labels)
|
||||
return data, target_variable
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user