Numpy学习笔记练习代码 ——

Posted 风在人舟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Numpy学习笔记练习代码 ——相关的知识,希望对你有一定的参考价值。

import numpy as np

A = np.array([(1,First,0.5,1+2j),(2,Second,1.5,1+3j),(3,Third,0.8,1-2j)],dtype=(i2,a6,f4,c8))

A
Out[3]: 
array([(1, bFirst,  0.5       ,  1.+2.j),
       (2, bSecond,  1.5       ,  1.+3.j),
       (3, bThird,  0.80000001,  1.-2.j)],
      dtype=[(f0, <i2), (f1, S6), (f2, <f4), (f3, <c8)])

A[1]
Out[4]: (2, bSecond,  1.5,  1.+3.j)

A[f2]
Out[5]: array([ 0.5       ,  1.5       ,  0.80000001], dtype=float32)

A[f1]
Out[6]: 
array([bFirst, bSecond, bThird],
      dtype=|S6)

A = np.array([(1,First,0.5,1+2j),(2,Second,1.5,1+3j),(3,Third,0.8,1-2j)],dtype=[(id,i2),(position,a6),(value,f4),(complex,c8)])

A
Out[8]: 
array([(1, bFirst,  0.5       ,  1.+2.j),
       (2, bSecond,  1.5       ,  1.+3.j),
       (3, bThird,  0.80000001,  1.-2.j)],
      dtype=[(id, <i2), (position, S6), (value, <f4), (complex, <c8)])

A = np.array([(1,First,0.5,1+2j),(2,Second,1.5,1+3j),(3,Third,0.8,1-2j)])

A
Out[10]: 
array([[1, First, 0.5, (1+2j)],
       [2, Second, 1.5, (1+3j)],
       [3, Third, 0.8, (1-2j)]],
      dtype=<U11)

A.dtype.names = (id,position,value,complex)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-4f156499990d> in <module>()
----> 1 A.dtype.names = (id,position,value,complex)

ValueError: there are no fields defined

A = np.array([(1,First,0.5,1+2j),(2,Second,1.5,1+3j),(3,Third,0.8,1-2j)],dtype=[(id,i2),(position,a6),(value,f4),(complex,c8)])

A
Out[13]: 
array([(1, bFirst,  0.5       ,  1.+2.j),
       (2, bSecond,  1.5       ,  1.+3.j),
       (3, bThird,  0.80000001,  1.-2.j)],
      dtype=[(id, <i2), (position, S6), (value, <f4), (complex, <c8)])

A.dtype.names = (id,order,value,complex)

A[order]
Out[15]: 
array([bFirst, bSecond, bThird],
      dtype=|S6)

data = np.arange(1,17).reshape(4,4)

data
Out[17]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

np.save(saved_data,data)

loaded_data = np.load(saved_data.npy)

loaded_data
Out[20]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

data = np.genfromtxt(E:\0.0\pythondemo\demo1\tdemo.csv,delimiter=,,names=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-dacc8747616f> in <module>()
----> 1 data = np.genfromtxt(E:\0.0\pythondemo\demo1\tdemo.csv,delimiter=,,names=True)

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows)
   1549                 fhd = iter(np.lib._datasource.open(fname, rbU))
   1550             else:
-> 1551                 fhd = iter(np.lib._datasource.open(fname, rb))
   1552             own_fhd = True
   1553         else:

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in open(path, mode, destpath)
    149 
    150     ds = DataSource(destpath)
--> 151     return ds.open(path, mode)
    152 
    153 

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in open(self, path, mode)
    492 
    493         # NOTE: _findfile will fail on a new file opened for writing.
--> 494         found = self._findfile(path)
    495         if found:
    496             _fname, ext = self._splitzipext(found)

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in _findfile(self, path)
    335 
    336         for name in filelist:
--> 337             if self.exists(name):
    338                 if self._isurl(name):
    339                     name = self._cache(name)

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_datasource.py in exists(self, path)
    440 
    441         # Test local path
--> 442         if os.path.exists(path):
    443             return True
    444 

D:\ProgramData\Anaconda3\lib\genericpath.py in exists(path)
     17     """Test whether a path exists.  Returns False for broken symbolic links"""
     18     try:
---> 19         os.stat(path)
     20     except OSError:
     21         return False

ValueError: stat: embedded null character in path

data = np.genfromtxt(rE:\0.0\pythondemo\demo1\tdemo.csv,delimiter=,,names=True)
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-22-bbff58f6826a> in <module>()
----> 1 data = np.genfromtxt(rE:\0.0\pythondemo\demo1\tdemo.csv,delimiter=,,names=True)

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows)
   1605     if names is True:
   1606         names = validate_names([_bytes_to_name(_.strip())
-> 1607                                 for _ in first_values])
   1608         first_line = b‘‘
   1609     elif _is_string_like(names):

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py in <listcomp>(.0)
   1605     if names is True:
   1606         names = validate_names([_bytes_to_name(_.strip())
-> 1607                                 for _ in first_values])
   1608         first_line = b‘‘
   1609     elif _is_string_like(names):

D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\_iotools.py in _bytes_to_name(s)
     23 
     24     def _bytes_to_name(s):
---> 25         return s.decode(ascii)
     26 else:
     27     _bytes_to_complex = complex

UnicodeDecodeError: ascii codec cant decode byte 0xd0 in position 0: ordinal not in range(128)

data = np.genfromtxt(rE:\0.0\pythondemo\demo1\tdemo.csv,delimiter=,,names=True)

data
Out[24]: 
array([( 1.,  123.,  1.4,  23.), ( 2.,  110.,  0.5,  18.),
       ( 3.,  164.,  2.1,  19.)],
      dtype=[(id, <f8), (value1, <f8), (value2, <f8), (value3, <f8)])

# 内容为空的项填充为nan值

data[id]
Out[26]: array([ 1.,  2.,  3.])

data[1]
Out[27]: ( 2.,  110.,  0.5,  18.)

 

以上是关于Numpy学习笔记练习代码 ——的主要内容,如果未能解决你的问题,请参考以下文章

Numpy学习笔记

学习笔记:python3,代码片段(2017)

[Python]numpy数据分析练习[1~20]]

C语言进阶学习笔记二指针的进阶(重点必看+代码演示+练习)

Python数据分析numpy入门-------numpy100题练习

Python数据分析numpy入门-------numpy100题练习