将1d numpy ndarray转换为形状(n,2)的一个热编码ndarray

时间:2018-08-18 15:42:19

标签: python-3.x numpy encoding numpy-ndarray

我有一个形状为 public static void readRecord(String searchTerm,String filepath){ boolean found = false; boolean toUpdate = false; String techID=""; String service=""; String firstName=""; String lastName=""; String salary=""; String position=""; String password=""; try { Scanner x = new Scanner(new File(filepath)); x.useDelimiter("[\\|]"); while(x.hasNext()&& !found) { techID = x.next(); service=x.next(); firstName=x.next(); lastName=x.next(); salary=x.next(); position=x.next(); password=x.next(); if(techID.contains(searchTerm)){ found = true; if(!techID.equals(searchTerm)) { toUpdate = true; } } } if(found) { if(toUpdate) { techID = techID.substring(4, techID.length()); } System.out.print("ID: "+techID+"\n"+"Service : "+service+"\n"+"First Name: "+firstName+"\n"+"Last Name : "+lastName+"\n" + "Salary : "+salary +"\n" + "Position : "+position); } else { System.out.print("ID not found"); } x.close(); } catch(Exception e) { e.printStackTrace(); } finally { } } } 的1d numpy ndarray。

Calendar.getInstance().get(Calendar.YEAR) - employmentYear >= n

我想将其转换为形状为(1,2)的ndarray,使其看起来像这样;

nd = array[1,0]

(n,2)中有nd_new = [ [1,0] [1,0] [1,0] ... ... [1,0] ] 行。

2 个答案:

答案 0 :(得分:1)

您可以按以下方式使用np.tile

>>> np.tile(nd, (n, 1))

答案 1 :(得分:0)

我会回答我自己的问题。使用来自keras的一种热门编码工具。

from keras.utils import to_categorical
y_nd_ones = [1] * n
y_nd = to_categorical(y_nd_ones)