我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用numpy.recfromcsv()。
def test_dtype_with_converters_and_usecols(self): dstr = "1,5,-1,1:1\n2,8,-1,1:n\n3,3,-2,m:n\n" dmap = {'1:1':0, '1:n':1, 'm:1':2, 'm:n':3} dtyp = [('e1','i4'),('e2','i4'),('e3','i2'),('n', 'i1')] conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]} test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',', names=None, converters=conv) control = np.rec.array([[1,5,-1,0], [2,8,-1,1], [3,3,-2,3]], dtype=dtyp) assert_equal(test, control) dtyp = [('e1','i4'),('e2','i4'),('n', 'i1')] test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',', usecols=(0,1,3), names=None, converters=conv) control = np.rec.array([[1,5,0], [2,8,1], [3,3,3]], dtype=dtyp) assert_equal(test, control)
def test_recfromcsv(self): # data = TextIO('A,B\n0,1\n2,3') kwargs = dict(missing_values="N/A", names=True, case_sensitive=True) test = np.recfromcsv(data, dtype=None, **kwargs) control = np.array([(0, 1), (2, 3)], dtype=[('A', np.int), ('B', np.int)]) self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control) # data = TextIO('A,B\n0,1\n2,N/A') test = np.recfromcsv(data, dtype=None, usemask=True, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], dtype=[('A', np.int), ('B', np.int)]) assert_equal(test, control) assert_equal(test.mask, control.mask) assert_equal(test.A, [0, 2]) # data = TextIO('A,B\n0,1\n2,3') test = np.recfromcsv(data, missing_values='N/A',) control = np.array([(0, 1), (2, 3)], dtype=[('a', np.int), ('b', np.int)]) self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control) # data = TextIO('A,B\n0,1\n2,3') dtype = [('a', np.int), ('b', np.float)] test = np.recfromcsv(data, missing_values='N/A', dtype=dtype) control = np.array([(0, 1), (2, 3)], dtype=dtype) self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control)
def load_csv_cached(filename='../apps/naive_c_stats.csv', cache={}): """ Get C statistics numpy record list, or return None if the file does not exist. """ if filename in cache: return cache[filename] if not os.path.exists(filename): ans = None else: ans = numpy.recfromcsv(filename) cache[filename] = ans return ans
def setUp(self): self.data = { "DC_PEC": ''' import numpy as np from urllib.request import urlretrieve; urlretrieve('https://s3.amazonaws.com/assets.datacamp.com/production/course_998/datasets/titanic_sub.csv', 'titanic.csv') ''', "DC_CODE": ''' file = 'titanic.csv' # Import file using np.genfromtxt: data data = np.genfromtxt(file , delimiter = ",", names = True , dtype = None) # Print out datatype of data print(type(data)) # Import file using np.recfromcsv: d d = np.recfromcsv(file) # Print out first three entries of d print(d[:3]) ''', "DC_SOLUTION": ''' file = 'titanic.csv' # Import file using np.genfromtxt: data data = np.genfromtxt(file , delimiter = ",", names = True , dtype = None) # Print out datatype of data print(type(data)) # Import file using np.recfromcsv: d d = np.recfromcsv(file) # Print out first three entries of d print(d[:3]) ''' }
def test_Pass(self): self.data["DC_SCT"] = ''' # Test: Predefined code predef_msg = "You don't have to change any of the predefined code." test_object("file", undefined_msg = predef_msg, incorrect_msg = predef_msg) # [6-21-2016: UPDATED WITH FIX] # Test: call to np.genfromtxt() and 'data' variable test_object("data", do_eval = False) test_function( "numpy.genfromtxt", not_called_msg = "Make sure you call `np.genfromtxt()`.", incorrect_msg = "Did you pass the correct arguments to `np.genfromtxt()`?") # [6-21-2016: NEEDS FIX] # Test: Predefined code test_function("type", do_eval = False, not_called_msg = "error in type", incorrect_msg = "error in type") test_function("print", not_called_msg = predef_msg, incorrect_msg = predef_msg) # [6-21-2016: UPDATED WITH FIX] # Test: call to np.recfromcsv() and 'd' variable test_object("d", do_eval = False) test_function( "numpy.recfromcsv", not_called_msg = "Make sure you call `np.recfromcsv()`.", incorrect_msg = "Did you pass the correct arguments to `np.recfromcsv()`?") # Test: Predefined code test_function("print", index = 2, incorrect_msg = "error is in print2") success_msg("Good job!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def _load(self,filename): kwargs = dict(delimiter=',') if filename is None: filename = os.path.join(self.DATADIR,"extras/extra_dwarfs.csv") self.filename = filename raw = np.recfromcsv(filename,**kwargs) self.data.resize(len(raw)) self.data['name'] = raw['name'] self.data['ra'] = raw['ra'] self.data['dec'] = raw['dec'] self.data['glon'],self.data['glat'] = cel2gal(raw['ra'],raw['dec'])
def _load(self,filename): kwargs = dict(delimiter=',') if filename is None: filename = os.path.join(self.DATADIR,"extras/extra_clusters.csv") self.filename = filename raw = np.recfromcsv(filename,**kwargs) self.data.resize(len(raw)) self.data['name'] = raw['name'] self.data['ra'] = raw['ra'] self.data['dec'] = raw['dec'] self.data['glon'],self.data['glat'] = cel2gal(raw['ra'],raw['dec'])
def test_recfromcsv(self): with temppath(suffix='.txt') as path: path = Path(path) with path.open('w') as f: f.write(u'A,B\n0,1\n2,3') kwargs = dict(missing_values="N/A", names=True, case_sensitive=True) test = np.recfromcsv(path, dtype=None, **kwargs) control = np.array([(0, 1), (2, 3)], dtype=[('A', np.int), ('B', np.int)]) self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control)
def load_data_file(name, skip_header=None) -> np.recarray: """Load a data file. Returns ------- data : :class:`numpy.recarray` data values """ fname = os.path.join(os.path.dirname(__file__), 'data', name) return np.recfromcsv( fname, skip_header=skip_header, case_sensitive=True).view(np.recarray)