Numpy重塑/尺寸变化

时间:2018-02-12 23:52:00

标签: python numpy

我想更改一个数组,例如

@RequestMapping(value = "/add/korisnik", method = RequestMethod.POST)
public String addKorisnik(@RequestParam String ime, @RequestParam String prezime, @RequestParam String username, @RequestParam String password, @RequestParam String orcid, @RequestParam String role) throws JAXBException, FileNotFoundException{

    Korisnik.Roles roles = new Korisnik.Roles();
    roles.setRole(role);
    Korisnik k = new Korisnik();
    k.setIme(ime);
    k.setPrezime(prezime);
    k.setUsername(username);
    k.setPassword(password);
    k.setOrcid(orcid);
    k.setRoles(roles);

    System.out.println(k.toString());

    // create JAXB context and instantiate marshaller
    JAXBContext context = JAXBContext.newInstance(Korisnik.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    StringWriter sw = new StringWriter();
    m.marshal(k, sw);

    // Write to File
    File f = new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml");
    if (f.exists()) { 
        return "Username already taken.";
    }
    else {
        m.marshal(k, new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml"));
    }

    // acquire the content
    InputStream docStream = ObjavaNaucnihRadovaApplication.class.getClassLoader().getResourceAsStream(
                "data/korisnici/" + k.getUsername() + ".xml");

    // create the client
    DatabaseClient client = DatabaseClientFactory.newClient(MarkLogicConfig.host,
                MarkLogicConfig.port, MarkLogicConfig.admin,
                MarkLogicConfig.password, MarkLogicConfig.authType);

    // create a manager for XML documents
    XMLDocumentManager docMgr = client.newXMLDocumentManager();

    // create a handle on the content
    InputStreamHandle handle = new InputStreamHandle(docStream);

    // write the document content
    docMgr.write("http://localhost:8011/korisnici/" + k.getUsername()+".xml", handle);  

    //release the client
    client.release();

    return "OK";
}

array([[  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.]])

我以为我会使用重塑进行管理,但在这种情况下,所有订单语句都不起作用。目前,我这样做

array([[[7.,   3.],
        [7.,   3.],
        [7.,   3.],
        [7.,   3.]],

       [[14.,   1.],
        [14.,   1.],
        [14.,   1.],
        [14.,   1.]],

       [[9.,   17.],
        [9.,    17.],
        [9.,   17.],
        [9.,   17.]]])

我想知道是否有更好的方法来做到这一点

1 个答案:

答案 0 :(得分:3)

reshape然后swapaxes

import numpy as np

a = np.array(
      [[  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.]])

a.reshape((a.shape[0], -1, 2)).swapaxes(0, 1)