学习Numpy

1.什么是numpy

NumPy系统是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix))。

包括:

1、一个强大的N维数组对象Array;

2、比较成熟的(广播)函数库;

3、用于整合C/C++和Fortran代码的工具包;

4、实用的线性代数、傅里叶变换和随机数生成函数。

numpy和稀疏矩阵运算包scipy配合使用更加方便。

2.搭建numpy环境

在安装python的环境下,用pip管理工具安装(没有安装pip应先安装pip):

安装pip:sudo apt-get install pip

安装numpy:sudo pip install numpy

安装scipy:sudo pip install scipy

安装matplotlib:sudo pip install matplotlib

3.如何学习

进入python安装包目录

查看安装的numpy包下的__ini__.py文件

"""
NumPy
=====

Provides
  1. An array object of arbitrary homogeneous items
  2. Fast mathematical operations over arrays
  3. Linear Algebra, Fourier Transforms, Random Number Generation

How to use the documentation
----------------------------
Documentation is available in two forms: docstrings provided
with the code, and a loose standing reference guide, available from
`the NumPy homepage <http://www.scipy.org>`_.

We recommend exploring the docstrings using
`IPython <http://ipython.scipy.org>`_, an advanced Python shell with
TAB-completion and introspection capabilities.  See below for further
instructions.

The docstring examples assume that `numpy` has been imported as `np`::

  >>> import numpy as np

Code snippets are indicated by three greater-than signs::

  >>> x = 42
  >>> x = x + 1

Use the built-in ``help`` function to view a function‘s docstring::

  >>> help(np.sort)
  ... # doctest: +SKIP

For some objects, ``np.info(obj)`` may provide additional help.  This is
particularly true if you see the line "Help on ufunc object:" at the top
of the help() page.  Ufuncs are implemented in C, not Python, for speed.
The native Python help() does not know how to view their help, but our
np.info() function does.

To search for documents containing a keyword, do::

  >>> np.lookfor(‘keyword‘)
  ... # doctest: +SKIP

General-purpose documents like a glossary and help on the basic concepts
of numpy are available under the ``doc`` sub-module::

  >>> from numpy import doc
  >>> help(doc)
  ... # doctest: +SKIP

Available subpackages
---------------------
doc
    Topical documentation on broadcasting, indexing, etc.
lib
    Basic functions used by several sub-packages.
random
    Core Random Tools
linalg
    Core Linear Algebra Tools
fft
    Core FFT routines
polynomial
    Polynomial tools
testing
    NumPy testing tools
f2py
    Fortran to Python Interface Generator.
distutils
    Enhancements to distutils with support for
    Fortran compilers support and more.

Utilities
---------
test
    Run numpy unittests
show_config
    Show numpy build configuration
dual
    Overwrite certain functions with high-performance Scipy tools
matlib
    Make everything matrices.
__version__
    NumPy version string

Viewing documentation using IPython
-----------------------------------
Start IPython with the NumPy profile (``ipython -p numpy``), which will
import `numpy` under the alias `np`.  Then, use the ``cpaste`` command to
paste examples into the shell.  To see which functions are available in
`numpy`, type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
down the list.  To view the docstring for a function, use
``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
the source code).

Copies vs. in-place operation
-----------------------------
Most of the functions in `numpy` return a copy of the array argument
(e.g., `np.sort`).  In-place versions of these functions are often
available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
Exceptions to this rule are documented.

"""
from __future__ import division, absolute_import, print_function

import sys
import warnings

from ._globals import ModuleDeprecationWarning, VisibleDeprecationWarning
from ._globals import _NoValue

# We first need to detect if we‘re being called as part of the numpy setup
# procedure itself in a reliable manner.
try:
    __NUMPY_SETUP__
except NameError:
    __NUMPY_SETUP__ = False

if __NUMPY_SETUP__:
    sys.stderr.write(‘Running from numpy source directory.\n‘)
else:
    try:
        from numpy.__config__ import show as show_config
    except ImportError:
        msg = """Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there."""
        raise ImportError(msg)

    from .version import git_revision as __git_revision__
    from .version import version as __version__

    from ._import_tools import PackageLoader

    def pkgload(*packages, **options):
        loader = PackageLoader(infunc=True)
        return loader(*packages, **options)

    from . import add_newdocs
    __all__ = [‘add_newdocs‘,
               ‘ModuleDeprecationWarning‘,
               ‘VisibleDeprecationWarning‘]

    pkgload.__doc__ = PackageLoader.__call__.__doc__

    # We don‘t actually use this ourselves anymore, but I‘m not 100% sure that
    # no-one else in the world is using it (though I hope not)
    from .testing import Tester
    test = testing.nosetester._numpy_tester().test
    bench = testing.nosetester._numpy_tester().bench

    # Allow distributors to run custom init code
    from . import _distributor_init

    from . import core
    from .core import *
    from . import compat
    from . import lib
    from .lib import *
    from . import linalg
    from . import fft
    from . import polynomial
    from . import random
    from . import ctypeslib
    from . import ma
    from . import matrixlib as _mat
    from .matrixlib import *
    from .compat import long

    # Make these accessible from numpy name-space
    # but not imported in from numpy import *
    if sys.version_info[0] >= 3:
        from builtins import bool, int, float, complex, object, str
        unicode = str
    else:
        from __builtin__ import bool, int, float, complex, object, unicode, str

    from .core import round, abs, max, min

    __all__.extend([‘__version__‘, ‘pkgload‘, ‘PackageLoader‘,
               ‘show_config‘])
    __all__.extend(core.__all__)
    __all__.extend(_mat.__all__)
    __all__.extend(lib.__all__)
    __all__.extend([‘linalg‘, ‘fft‘, ‘random‘, ‘ctypeslib‘, ‘ma‘])

    # Filter annoying Cython warnings that serve no good purpose.
    warnings.filterwarnings("ignore", message="numpy.dtype size changed")
    warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
    warnings.filterwarnings("ignore", message="numpy.ndarray size changed")

    # oldnumeric and numarray were removed in 1.9. In case some packages import
    # but do not use them, we define them here for backward compatibility.
    oldnumeric = ‘removed‘
    numarray = ‘removed‘

此处告诉我们numpy提供什么功能支持,如何使用文档,如何使用numpy内置的帮助功能,可用的子包等等信息。

现在就开始学习!

numpy开发文档:https://docs.scipy.org/doc/numpy/reference/

时间: 2024-11-05 18:33:22

学习Numpy的相关文章

Python学习-Numpy数据处理

引文 标准的python中用list保存数值,可以当数组使用.但由于列表的元素是任意对象,因此列表中保存的是对象的指针.对于数值运算来说,这种结构显然会浪费内存和CPU计算时间. 此外,python还提供了array模块,但由于其不支持多维数组,因此也不适合数值计算. So,Numpy正好弥补了这些不足,Numpy提供了两个基本的对象:ndarray和ufunc.ndarray是存储单一数据类型的多维数组,而ufunc是能够对数组进行处理的函数. 导入方法 import numpy as np

学习Numpy基础操作

# coding:utf-8 import numpy as np from numpy.linalg import * def day1(): ''' ndarray :return: ''' lst = [[1, 2, 3], [4, 5, 6]] print(type(lst)) np_lst = np.array(lst) print(type(np_lst)) np_lst = np.array(lst, dtype=np.float) # bool # int,int8,int16,

【机器学习】NumPy、Panda相关数据结构学习

NumPy学习 NumPy重要的一个特点:是一个N维数组对象.提供了shape(指明行数.列数)和dtype(数据类型) 初始化 1 data1 = [3,5] 2 3 arr1 = np.array(data1) 4 arr1 5 输出:array([3, 5]) 注意,如果有切片来自于该数据,改变切片,也会改变原来的数据. Panda 主要由Series和DataFrame组成. Series 1,Series包括了index和values,其中values数据包括各种NumPy数据类型.与

Numpy 学习之路(1)——数组的创建

数组是Numpy操作的主要对象,也是python数据分析的主要对象,本系列文章是本人在学习Numpy中的笔记. 文章中以下都基于以下方式的numpy导入: import numpy as np from numpy import * 1.普通数组的创建——np.arange(), np.array(), (1) arange()建立是顺序数组,函数原型:arange([start,]stop[,step],dtype=None) 其中start参数如果省略,则表示从0开始,默认的dtype为fl

numpy简单入门学习

为了快速的学习numpy,只要参阅了官网的快速入门教程进行学习,官网的网址:https://docs.scipy.org/doc/numpy-dev/user/quickstart.html.虽然和matlab的操作大同小异,但是还是需要很多明确的python的概念,比如序列,列表以及元组的概念,当然这也是python里面需要注意最多的基本的数据类型.现将学习的基本过程叙述如下: numpy是通过python语言实现的用于科学研究中的计算.可以方便的进行代数计算.傅里叶计算等.这是Numpy学习

NumPy基础入门学习

对于习惯使用了MATLAB的用户而言,学习NumPy这个python工具包付出的成本应该是不大的. NumPy的主要的object是多维数组,是一个有相同类型的数字等构成的一张表格,可以通过元组进行索引.本篇主要列出NumPy中最常用的一些操作. 1,ndarray 类型的一些属性 >>> from numpy import * >>> a=array([[1,2,3],[4,5,6]]) >>> a array([[1, 2, 3], [4, 5,

NumPy学习笔记 一

NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分析>第四版(华东师范大学数学系).<概率论与数理统计>(陈希孺,中科大出版).<概率论与数理统计>第二版(茆诗松.程依明等编).<组合最优化:理论与方法>(现代数学译丛23).笔记一主要记录NumPy&SciPy及相关软件的环境准备部分. NumPy的官方网站

NumPy学习笔记 三 股票价格

NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分析>第四版(华东师范大学数学系).<概率论与数理统计>(陈希孺,中科大出版).<概率论与数理统计>第二版(茆诗松.程依明等编).<组合最优化:理论与方法>(现代数学译丛23).笔记三主要操作股票价格数据. 股票价格数据通常包括开盘价.最高价.最低价和收盘价.

请不要重复犯我在学习Python和Linux系统上的错误

本人已经在运维行业工作了将近十年,我最早接触Linux是在大二的样子,那时候只追求易懂,所以就选择了Ubuntu作为学习.使用的对象,它简单.易用.好操作.界面绚丽,对于想接触Linux的新手来说是非常不错的.后来因为个人的知识有限,玩不转Linux的种种配置.各种插件以及软件缺失,加之没有持之以恒的坚持下去,使用了一段时间后感觉Bug多.没游戏.办公写文档也不方便,很多软件需要现学,最终希望用大学时光学习Linux的愿望夭折了. 后来一段时间里,自己接触了Python语言,Python语言让我