ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

2026京东笔试真题【货位好坏线性判别】多语言题解

2026京东笔试真题【货位好坏线性判别】多语言题解 货位好坏线性判别(Py/Js/Go)题解京东 2026年8月29号 笔试真题 第二题题目内容仓储质检组要用一条固定规则的线性判别给待检货位打上好坏标签。值班员必须按当晚工艺单写死的学习率、轮数和更新顺序训练再对抽检清单输出预测不能改成别的分类器。每条训练记录是一组货位特征vvv和一个标签y∈{0,1}y \in \{0,1\}y∈{0,1}其中0表示不合格1表示合格。训练时先把标签映成t∈{−1,1}t \in \{-1,1\}t∈{−1,1}y0y0y0映成t−1t-1t−1y1y1y1映成t1t1t1特征要补上偏置vˉ[1, v]\bar{v}[1,\,v]vˉ[1,v]。权向量hhh从全0开始学习率固定为1.0一共训练10轮。每一轮按训练集原始顺序扫一遍预测符号t^sign(h⊤vˉ)\hat{t}\mathrm{sign}(h^\top \bar{v})t^sign(h⊤vˉ)sign\mathrm{sign}sign规定h⊤vˉ≥0h^\top \bar{v} \ge 0h⊤vˉ≥0时t^1\hat{t}1t^1否则t^−1\hat{t}-1t^−1若t^≠t\hat{t} \neq tt^t更新h←h1.0⋅t⋅vˉh \leftarrow h 1.0 \cdot t \cdot \bar{v}h←h1.0⋅t⋅vˉ测试时用同一套sign\mathrm{sign}sign得到t^\hat{t}t^再映回标签1映成1-1映成0。输入描述标准输入是一段 JSON含两个键train和test。train训练列表每个元素为[特征列表, 标签y]标签只取0或1test测试列表每个元素为一组特征输出描述输出一行 JSON内容为预测标签列表每个值是0或1顺序与test一致。样例1输入{train: [[[0], 0], [[1], 0], [[4], 1], [[5], 1]], test: [[0], [1], [2], [3], [4], [5]]}输出[0, 0, 1, 1, 1, 1]说明一维货位读数较小的两条标成不合格较大的两条标成合格。按固定规则训练10轮后分界落在1与2之间因此测试六个点的预测是前两个0、后四个1。样例2输入{train: [[[0, 0], 0], [[2, 0], 1]], test: [[0, 0], [2, 0], [1, 0]]}输出[0, 1, 1]说明只有两条训练样本。权向量从零开始遇错就加一刀测点两个原样本保持原标签中间点被判成合格。思路按照题意进行模拟即可初始化权重假设特征有d维额外添加一个偏置项。训练10轮每个样本做四件事标签转换计算预测值判断预测值预测错误更新权重训练结束后进行测试。对于test中的每个特征同样计算h · [1, features]pythonimportsysimportjson datajson.load(sys.stdin)traindata[train]testdata[test]dlen(train[0][0])# h[0] 为偏置h[1:] 为特征权重h[0.0]*(d1)# 训练 10 轮for_inrange(10):forfeatures,yintrain:# 0 - -11 - 1t-1ify0else1# h · v_bar其中 v_bar [1, v]scoreh[0]forjinrange(d):scoreh[j1]*features[j]# score 0 - 1否则 -1pred1ifscore0else-1# 预测错误才更新ifpred!t:h[0]tforjinrange(d):h[j1]t*features[j]# 测试ans[]forfeaturesintest:scoreh[0]forjinrange(d):scoreh[j1]*features[j]pred1ifscore0else-1# 1 - 1-1 - 0ans.append(1ifpred1else0)print(json.dumps(ans,separators(,,:)))javascriptconstreadlinerequire(readline);constrlreadline.createInterface({input:process.stdin,output:process.stdout});constlines[];rl.on(line,line{lines.push(line);});rl.on(close,(){constdataJSON.parse(lines.join(\n));consttraindata.train;consttestdata.test;constdtrain[0][0].length;// h[0] 为偏置h[1:] 为特征权重consthnewArray(d1).fill(0.0);// 训练 10 轮for(letround0;round10;round){for(const[features,y]oftrain){// 0 - -11 - 1constty0?-1:1;// h · v_bar其中 v_bar [1, v]letscoreh[0];for(letj0;jd;j){scoreh[j1]*features[j];}// score 0 - 1否则 -1constpredscore0?1:-1;// 预测错误才更新if(pred!t){h[0]t;for(letj0;jd;j){h[j1]t*features[j];}}}}// 测试constans[];for(constfeaturesoftest){letscoreh[0];for(letj0;jd;j){scoreh[j1]*features[j];}constpredscore0?1:-1;// 1 - 1-1 - 0ans.push(pred1?1:0);}console.log(JSON.stringify(ans));});Gopackagemainimport(bufioencoding/jsonfmtos)typeDatastruct{Train[][]interface{}json:trainTest[][]float64json:test}funcmain(){in:bufio.NewReader(os.Stdin)out:bufio.NewWriter(os.Stdout)deferout.Flush()vardata Data decoder:json.NewDecoder(in)iferr:decoder.Decode(data);err!nil{return}train:data.Train test:data.Test// train 中的 features 默认会被解析为 []interface{}firstFeatures:train[0][0].([]interface{})d:len(firstFeatures)// h[0] 为偏置h[1:] 为特征权重h:make([]float64,d1)// 训练 10 轮forround:0;round10;round{for_,sample:rangetrain{features:sample[0].([]interface{})y:int(sample[1].(float64))// 0 - -11 - 1t:-1ify1{t1}// h · v_bar其中 v_bar [1, v]score:h[0]forj:0;jd;j{scoreh[j1]*features[j].(float64)}// score 0 - 1否则 -1pred:-1ifscore0{pred1}// 预测错误才更新ifpred!t{h[0]float64(t)forj:0;jd;j{h[j1]float64(t)*features[j].(float64)}}}}// 测试ans:make([]int,0,len(test))for_,features:rangetest{score:h[0]forj:0;jd;j{scoreh[j1]*features[j]}pred:-1ifscore0{pred1}// 1 - 1-1 - 0ifpred1{ansappend(ans,1)}else{ansappend(ans,0)}}result,_:json.Marshal(ans)fmt.Fprintln(out,string(result))}
返回列表