• 12

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to save a list as numpy array in python?

I need to know if it is possible to save a python list as a numPy array.

If you look here, it might tell you what you need to know.


Basically, you can create an array from a sequence.

import numpy as np
a = np.array( [2,3,4] )

Or from a sequence of sequences.

import numpy as np
a = np.array( [[2,3,4], [3,4,5]] )
  • 162
Reply Report
    • I would have put that syntax a little differently. How about import numpy as np then a = np.array ( [[2,3,4],[3,4,5]] ) ?

I suppose, you mean converting a list into a numpy array? Then,

import numpy as np

# b is some list, then ...    
a = np.array(b).reshape(lengthDim0, lengthDim1);

gives you a as an array of list b in the shape given in reshape.

  • 4
Reply Report

Here is a more complete example:

import csv
import numpy as np

with open('filename','rb') as csvfile:
     cdl = list( csv.reader(csvfile,delimiter='\t'))
     print "Number of records = " + str(len(cdl))

#then later

npcdl = np.array(cdl)

Hope this helps!!

  • 0
Reply Report

Trending Tags