想体验一把No.1的快乐吗?话不多说直接上代码。
代码:
import os
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
import numpy as np
def get_dataset(path):
dataset, labels = [], []
filenames = os.listdir(path)
for filename in filenames:
labels.append(filename[0])
filepath = os.path.join(path, filename)
dataset.append(np.fromfile(filepath, dtype=np.uint8))
return dataset, labels
if __name__ == '__main__':
X_train, y_train = get_dataset("train")
X_test, y_test = get_dataset("test")
# 数据标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test) # 使用同一个scaler的transform,避免误差
y_train = list(y_train)
model = SVC()
model.fit(X_train, y_train)
y_pred_test = model.predict(X_test)
# 保存预测结果到result.csv
results = pd.DataFrame({'label': y_test, 'num': y_pred_test})
results.to_csv('result.csv', index=False)
Tips:
相信大家也发现了,是Wrong Answer。诧异这种情况也能排名,所以发出来供大家娱乐一下,稍微修改一下就是能过的代码。仅供娱乐,仅供娱乐,仅供娱乐。