python数据分析学习笔记一

来源:互联网 发布:relief算法 python 编辑:程序博客网 时间:2024/04/25 17:38

第一章 Python程序库入门

Numpy 数值数组和函数

Scipy 科学计算库

Matplotlib 基于numpy绘图库

Ipython 交互是计算

 

示例代码如下:

# -*- coding: utf-8 -*-# @Time    : 2016/12/6 19:38# @Author  : Retacn# @Site    : 计算两个向量的和# @File    : numpyTest.py# @Software: PyCharm__author__ = "retacn"__copyright__ = "property of mankind."__license__ = "CN"__version__ = "0.0.1"__maintainer__ = "retacn"__email__ = "zhenhuayue@sina.com"__status__ = "Development"import sysfrom datetime import datetimeimport numpy as np# 使用numpy计算两个向量的和def numpysum(n):    a = np.arange(n) ** 2    b = np.arange(n) ** 3    c = a + b    return c# 使用python原生方法def pythonsum(n):    a = list(range(n))    b = list(range(n))    c = []    print(len(a))    for i in range(len(a)):        a[i] = i ** 2        b[i] = i ** 3        c.append(a[i] + b[i])    return csize = int(sys.argv[1])start = datetime.now()c = pythonsum(size)delta = datetime.now() - startprint('最后两个元素的和', c[-2:])print('使用pthon原生计算的时间:', delta.microseconds)start = datetime.now()c = numpysum(size)delta = datetime.now() - startprint('最后两个元素的和', c[-2:])print('使用numpy计算的时间:', delta.microseconds)

 

Pycharm在运行程序时添加参数

在run菜单中的edit confignurations中的script parameters中添加

运行结果如下:

最后两个元素的和 [995007996, 998001000]

使用pthon原生计算的时间: 1000

最后两个元素的和 [995007996 998001000]

使用numpy计算的时间: 56004

 

将ipython 用作shell

#Pylab参数,将自动导入scipy  numpy matplotlib

 

C:\Users\Administrator>ipython -pylab

d:\python27\lib\site-packages\IPython\terminal\ipapp.py:291:UserWarning: `-pylab` flag has been deprecated.

   Use `--matplotlib <backend>` and import pylab manually.

 warnings.warn("`-pylab` flag has been deprecated.\n"

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 272016, 15:19:22) [MSC v.1500 32 bit (Intel)]

Type "copyright","credits" or "license" for more information.

 

IPython 5.1.0 -- An enhanced InteractivePython.

?        -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help     -> Python's own help system.

object?  -> Details about 'object', use 'object??' for extra details.

Using matplotlib backend: TkAgg

 

#退出ipython

In [1]: quit()

 

#保存会话

In [1]: %logstart

Activating auto-logging. Current sessionstate plus future input saved.

Filename       : ipython_log.py

Mode           : rotate

Output logging : False

Raw input log  : False

Timestamping   : False

State          : active

 

In [2]: print('hello world')

hello world

 

#关闭会话

In [3]: %logoff

Switching logging OFF

 

#得到当前日期

In [5]: !date

当前日期: 2016/12/06 周二

输入新日期: (年月日)

 

#显示历史上用过的命令

In [10]: %hist

%logstart

print('hello world')

%logoff

ipython_log.py

!date

thedata=!date

thedata

a=2+2

a

%hist

 

#显示历史上用过的命令,并作过滤

In [11]: %hist -g a=2

   8:a=2+2

  11:%hist -g a=2

 

 

学习手册页

使用帮助功能

In [13]: help ar

 arange      arcsin       arctan2      argmin       argwhere     array2string array_repr   arrow

arccos      arcsinh      arctanh      argpartition around       array_equal  array_split

 arccosh     arctan       argmax       argsort      array        array_equiv  array_str

 

In [14]: help(arange)

Help on built-in function arange in modulenumpy.core.multiarray:

 

arange(...)

   arange([start,] stop[, step,], dtype=None)

 

   Return evenly spaced values within a given interval.

 

   Values are generated within the half-open interval ``[start, stop)``

   (in other words, the interval including `start` but excluding `stop`).

   For integer arguments the function is equivalent to the Python built-in

   `range <http://docs.python.org/lib/built-in-funcs.html>`_function,

   but returns an ndarray rather than a list.

 

   When using a non-integer step, such as 0.1, the results will often not

   be consistent.  It is better touse ``linspace`` for these cases.

 

   Parameters

   ----------

   start : number, optional

       Start of interval.  The intervalincludes this value.  The default

       start value is 0.

   stop : number

       End of interval.  The intervaldoes not include this value, except

       in some cases where `step` is not an integer and floating point

       round-off affects the length of `out`.

   step : number, optional

       Spacing between values.  For anyoutput `out`, this is the distance

       between two adjacent values, ``out[i+1] - out[i]``.  The default

       step size is 1.  If `step` isspecified, `start` must also be given.

   dtype : dtype

       The type of the output array.  If`dtype` is not given, infer the data

       type from the other input arguments.

 

   Returns

   -------

   arange : ndarray

       Array of evenly spaced values.

 

       For floating point arguments, the length of the result is

       ``ceil((stop - start)/step)``. Because of floating point overflow,

       this rule may result in the last element of `out` being greater

       than `stop`.

 

   See Also

   --------

   linspace : Evenly spaced numbers with careful handling of endpoints.

   ogrid: Arrays of evenly spaced numbers in N-dimensions.

   mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.

 

   Examples

   --------

   >>> np.arange(3)

   array([0, 1, 2])

   >>> np.arange(3.0)

   array([ 0.,  1.,  2.])

   >>> np.arange(3,7)

   array([3, 4, 5, 6])

   >>> np.arange(3,7,2)

array([3, 5])

 

 

#使用问号进行查询:

In [15]: arange?

Docstring:

arange([start,]stop[, step,], dtype=None)

 

Return evenlyspaced values within a given interval.

 

Values aregenerated within the half-open interval ``[start, stop)``

(in other words,the interval including `start` but excluding `stop`).

For integerarguments the function is equivalent to the Python built-in

`range<http://docs.python.org/lib/built-in-funcs.html>`_ function,

but returns anndarray rather than a list.

 

When using anon-integer step, such as 0.1, the results will often not

beconsistent.  It is better to use``linspace`` for these cases.

 

Parameters

----------

start : number,optional

    Start of interval.  The interval includes this value.  The default

    start value is 0.

stop : number

    End of interval.  The interval does not include this value,except

    in some cases where `step` is not aninteger and floating point

    round-off affects the length of `out`.

step : number,optional

    Spacing between values.  For any output `out`, this is the distance

    between two adjacent values, ``out[i+1] -out[i]``.  The default

    step size is 1.  If `step` is specified, `start` must also begiven.

dtype : dtype

    The type of the output array.  If `dtype` is not given, infer the data

    type from the other input arguments.

 

Returns

-------

arange : ndarray

    Array of evenly spaced values.

 

    For floating point arguments, the length ofthe result is

    ``ceil((stop - start)/step)``.  Because of floating point overflow,

    this rule may result in the last element of`out` being greater

    than `stop`.

 

See Also

--------

linspace :Evenly spaced numbers with careful handling of endpoints.

ogrid: Arrays ofevenly spaced numbers in N-dimensions.

mgrid:Grid-shaped arrays of evenly spaced numbers in N-dimensions.

 

Examples

--------

>>>np.arange(3)

array([0, 1, 2])

>>>np.arange(3.0)

array([ 0.,  1., 2.])

>>>np.arange(3,7)

array([3, 4, 5,6])

>>>np.arange(3,7,2)

array([3, 5])

Type:      builtin_function_or_method

#使用tab自动补全.前提是安装readline

In [16]: randn

            rand            random          ranf

            randint         random_integersrange

            randn           random_sample   rank

0 0
原创粉丝点击