Python Note6 (Numpy)

Install

After downloaded the Numpy into C:\Python27\Scripts.

pip2.7 install [path_of_numpy_whl]

Basic Knowledges

  • Import all modules within Numpy into the current namespace.
from numpy import *
  • Change the current directory within IDLE development environment.
import os  
os.getcwd()  # obtain the current directory
os.chdir("D:\\test") # change directory  
os.chdir('D:\\test')  # no difference between ' and ''
  • axis
    For 2-D array, axis=0 means processing according to the vertical axis, while axis=1 means the horizontal axis.
    For N-D array, 'axis=i' means Numpy will process the array according to the i-th subscript.

  • sort, sorted & argsort

  • sort
    After sorting process, the original array will be sorted.
    >>> a = [1,2,1,4,3,5] >>> a.sort() >>> a [1, 1, 2, 3, 4, 5]

  • sorted(iterable, cmp, key, reverse)
    After sorting process, the original array will not be influenced.
    >>> a = [1,2,1,4,3,5] >>> sorted(a) [1, 1, 2, 3, 4, 5] >>> a [1, 2, 1, 4, 3, 5]

  • argsort
    Return index of array according to ascending values.

Advanced Knowledges

  • Save a numpy matrix into a txt file.
# np.savetxt(FileName, saveMatdData, format, delimiter, newline)
np.savetxt('s12.txt', data, fmt='%f', delimiter=' ', newline='\n')
  • Save a numpy matrix into a csv file.
# np.savetxt(FileName, saveMatdData, delimiter)
np.savetxt('s13.csv', data, delimiter=',')
  • Load a csv file into a ndarray.
# np.loadtxt(open(csvFileName,'rb'), delimiter, skiprows)
np.loadtxt(open('s13.csv','rb'), delimiter=',', skiprows=0)
  • Load a csv file into a numpy matrix.
np.mat(np.loadtxt(open('s13.csv','rb'), delimiter=',', skiprows=0))

Reference

Python IDLE或shell中切换路径
numpy模块之axis
Python中排序常用到的sort 、sorted和argsort函数

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容