• python金融分析项目


    1.进入ipython:

    C:UsersAdministrator>ipython
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AM
    D64)]
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

    2.常用命令:

    In [1]: a = [1,2,3,4,5]
    
    In [2]: a.append(2) #可以用TAB键自动补全
    
    In [3]: a
    Out[3]: [1, 2, 3, 4, 5, 2]
    
    In [4]: a?  #加一个?查看a的详细信息
    Type:        list
    String form: [1, 2, 3, 4, 5, 2]
    Length:      6
    Docstring:
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
    
    In [5]: def func(a,b):
       ...:     """docstring test"""
       ...:     return a+b
       ...:
    
    In [6]:  func??  #加两个?查看函数的详细信息
    Signature: func(a, b)
    Source:
    def func(a,b):
        """docstring test"""
        return a+b
    File:      c:usersadministrator<ipython-input-5-64258f09b59c>
    Type:      function

     In [8]: a.a*?
     a.append

     In [7]: a
     Out[7]: [1, 2, 3, 4, 5, 2]

     In [9]: a.*?  #查看列表a的常用方法

     a.__add__
     a.__class__
     a.__contains__
     a.__delattr__
     a.__delitem__
     a.__dir__
     a.__doc__
     a.__eq__
     a.__format__
     a.__ge__
     a.__getattribute__
     a.__getitem__
     a.__gt__
     a.__hash__
     a.__iadd__
     a.__imul__
     a.__init__
     a.__init_subclass__
     a.__iter__
     a.__le__
     a.__len__
     a.__lt__
     a.__mul__
     a.__ne__
     a.__new__
     a.__reduce__
     a.__reduce_ex__
     a.__repr__
     a.__reversed__
     a.__rmul__
     a.__setattr__
     a.__setitem__
     a.__sizeof__
     a.__str__
     a.__subclasshook__
     a.append
     a.clear
     a.copy
     a.count
     a.extend
     a.index
     a.insert
     a.pop
     a.remove
     a.reverse
     a.sort

     命令:

    In [11]: a.__*__?
    a.__add__
    a.__class__
    a.__contains__
    a.__delattr__
    a.__delitem__
    a.__dir__
    a.__doc__
    a.__eq__
    a.__format__
    a.__ge__
    a.__getattribute__
    a.__getitem__
    a.__gt__
    a.__hash__
    a.__iadd__
    a.__imul__
    a.__init__
    a.__init_subclass__
    a.__iter__
    a.__le__
    a.__len__
    a.__lt__
    a.__mul__
    a.__ne__

    In [12]: def func(a,b):
    ...: """docstring test"""
    ...: return a/b
    ...: a = 10
    ...: b = 2
    ...: c = func(a,b)
    ...: print(c)
    ...:
    5.0

    可以拖动文件夹至ipython界面进入指定目录

    In [24]: cd D:projects
    D:projects

    In [25]: %run test.py
    5.0  #输出

    In [28]: !ipconfig  #加感叹号运行命令

    Windows IP 配置


    无线局域网适配器 无线网络连接 8:

    连接特定的 DNS 后缀 . . . . . . . :
    本地链接 IPv6 地址. . . . . . . . : fe80::f076:cf21:6241:d7f%25
    IPv4 地址 . . . . . . . . . . . . : 192.168.191.1
    子网掩码 . . . . . . . . . . . . : 255.255.255.0
    默认网关. . . . . . . . . . . . . :

    python求函数运行时间一般要用到装饰器,但是ipython的方法是:多次运行函数并求平均(%timeit func(1,2))

    In [29]: %timeit func(1,2)
    134 ns ± 4.55 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each

    更改函数信息:

    >>> def func(a,b):
    ... return a/b

    func(10,0)

    python3报错信息

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 2, in func
    ZeroDivisionError: division by zero

    In [30]: #%paste

    In [30]: def func(a,b):
    ...: """docstring test"""
    ...: return a/b
    ...: a = 10
    ...: b = 0
    ...: c = func(a,b)
    ...: print(c)
    ...:
    ---------------------------------------------------------------------------
    ZeroDivisionError Traceback (most recent call last)
    <ipython-input-30-301f9b654341> in <module>()
    4 a = 10
    5 b = 0
    ----> 6 c = func(a,b)
    7 print(c)

    <ipython-input-30-301f9b654341> in func(a, b)
    1 def func(a,b):
    2 """docstring test"""
    ----> 3 return a/b  #用箭头指向错误的行
    4 a = 10
    5 b = 0

    ZeroDivisionError: division by zero

    抛出异常

    In [31]: %pdb on
    Automatic pdb calling has been turned ON #启动调试器

    In [32]: %paste #粘贴要复制的内容
    def func(a,b):
    """docstring test"""
    return a/b
    a = 10
    b = 0
    c = func(a,b)
    print(c)

    ## -- End pasted text --
    ---------------------------------------------------------------------------
    ZeroDivisionError Traceback (most recent call last)
    <ipython-input-32-6cfa0b0f11a2> in <module>()
    4 a = 10
    5 b = 0
    ----> 6 c = func(a,b)
    7 print(c)

    <ipython-input-32-6cfa0b0f11a2> in func(a, b)
    1 def func(a,b):
    2 """docstring test"""
    ----> 3 return a/b
    4 a = 10
    5 b = 0

    ZeroDivisionError: division by zero
    > <ipython-input-32-6cfa0b0f11a2>(3)func()
    1 def func(a,b):
    2 """docstring test"""
    ----> 3 return a/b #设置了断点
    4 a = 10
    5 b = 0

    ipdb> p a
    10
    ipdb> p b
    0
    ipdb> q#退出调试

    In [33]:

    a.用上下键可以查看历史命令

    In [33]: a = 1

    In [34]: b =2

    In [35]: c = a+b

    In [36]: a+b
    Out[36]: 3

    In [37]: _ #查看上一条命令的结果
    Out[37]: 3

    In [39]: a*b
    Out[39]: 2

    In [40]: a+b
    Out[40]: 3

    In [41]: __ #查看上两条命令的结果
    Out[41]: 2

    可以查看上第三条,第四条就不行了

    In [42]: ___
    Out[42]: 2

    In [43]: ____
    ---------------------------------------------------------------------------
    NameError Traceback (most recent call last)
    <ipython-input-43-a09794d69507> in <module>()
    ----> 1 ____

    NameError: name '____' is not defined
    > <ipython-input-43-a09794d69507>(1)<module>()
    ----> 1 ____

    3.jupyter notebook

    pip3 install jupyter 

    新建:

    pycharm新建jupyter notebook

     

    4.numpy基本使用

    4.1

    In [44]: import numpy as np

    In [45]: np.array([1,2,3,4,5])
    Out[45]: array([1, 2, 3, 4, 5])

    In [46]: for i in _: #for循环上一个数组
    ...: print(i)
    ...:
    1
    2
    3
    4
    5

    In [47]: a = list(range(100))

    In [48]: a
    Out[48]:

    [0,1,2,3,...,99,100]

    查看a占用的内存:

    In [50]: import sys

    In [51]: sys.getsizeof(a)
    Out[51]: 1008 字节

    In [52]: b = np.array(range(100))

    In [53]: b
    Out[53]:
    array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
    34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
    51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
    68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
    85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

    In [54]: sys.getsizeof(b)
    Out[54]: 496

    In [56]: import random as rd

    In [57]: prize = [random.uniform(10.0,20.0) for i in range(20)]

    In [58]: prize
    Out[58]:
    [10.004054627786882,
    18.096649929234854,
    14.43396704748472,
    12.20024053360273,
    17.176704067418257,
    13.007875001992808,
    19.97226551920407,
    15.510685953676614,
    11.584123618476683,
    11.069274429538424,
    10.266891497698419,
    13.762984583010937,
    17.668216765172286,
    10.01516244592815,
    11.443377100041701,
    13.034463476325001,
    13.680557780172048,
    19.745559061062647,
    14.12108494581293,
    15.738287202323185]

    In [59]: prize = [round(random.uniform(10.0,20.0),2) for i in range(20)]#取两位小数

    In [60]: prize
    Out[60]:
    [16.44,
    17.95,
    16.07,
    19.17,
    18.02,
    13.22,
    13.17,
    14.59,
    14.29,
    19.66,
    11.19,
    14.29,
    12.59,
    17.82,
    18.38,
    10.65,
    14.67,
    17.92,
    11.78,
    15.96]

    In [12]: import random

    In [13]: import random as rd

    In [6]: num = [random.randint(1,10) for i in range(20)]

    In [7]: num
    Out[7]: [7, 9, 8, 4, 7, 8, 9, 6, 6, 4, 8, 10, 1, 6, 4, 10, 5, 8, 1, 5]

    In [14]: prize = [random.uniform(10.0,20.0) for i in range(20)]

    In [16]: prize = [round(random.uniform(10.0,20.0),2) for i in range(20)]

    In [20]: sum = 0

    In [21]: for i,j in zip(prize,num):
    ...: sum+=i*j
    ...:

    In [22]: sum
    Out[22]: 1899.42

    def func(a,b):
    sum = 0
    for i,j in zip(a,b):
    sum+=i*j
    return sum

    In [25]: %timeit func(prize,num) #运行时间
    2.28 µs ± 81.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops eac

    In [25]: %timeit func(prize,num)
    2.28 µs ± 81.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

    #计算商品总价,方法1

    In [26]: prize_np = np.array(prize)

    In [27]: num_np = np.array(num)

    In [28]: np.dot(prize_np,num_np)算出商品总价#向量的点乘
    Out[28]: 1899.4200000000001

    In [29]: %timeit np.dot(prize_np,num_np)#查看运行的时间
    1.3 µs ± 13.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

     #计算商品总价,方法2

    In [30]: prize_np * num_np
    Out[30]:
    array([ 91.36, 171. , 11.19, 111.84, 37.42, 133.29, 86.96,
    31.72, 37.72, 33.39, 142.88, 41.16, 123.44, 141.84,
    46.08, 113.22, 163.8 , 100.92, 130.27, 149.92])

    In [31]: _.sum()#求和
    Out[31]: 1899.4200000000001


    In [24]: prize_np.dtype#数据类型
    Out[24]: dtype('float64')#64位长度,占8字节

    In [27]: a = np.arange(1000)

    In [28]: a.size
    Out[28]: 1000

    In [29]: a.shape
    Out[29]: (1000,)

    In [30]: a = np.arange(1000)

    In [31]: z = np.array([[1,2,3],[4,5,6]])

    In [32]: z
    Out[32]:
    array([[1, 2, 3],
    [4, 5, 6]])

    In [33]: z.size #大小
    Out[33]: 6

    In [34]: z.shape #形状
    Out[34]: (2, 3)#矩阵两行三列

    In [35]: z.ndim#维度
    Out[35]: 2  #二维数组

    In [36]: a.ndim
    Out[36]: 1  #一维数组

    In [37]: z.T #行变成列,列变成行 (转制)
    Out[37]:
    array([[1, 4],
          [2, 5],
          [3, 6]])

    In [38]: z.dtype
    Out[38]: dtype('int32')

    In [39]: z
    Out[39]:
    array([[1, 2, 3],
    [4, 5, 6]])

    In [40]: z[0][0]=3.2

    In [41]: z
    Out[41]:
    array([[3, 2, 3],
    [4, 5, 6]])

    In [42]: z
    Out[42]:
    array([[3, 2, 3],
    [4, 5, 6]])

    In [43]: z.dtype = 'float32'

    In [44]: z
    Out[44]:
    array([[ 4.20389539e-45, 2.80259693e-45, 4.20389539e-45],
    [ 5.60519386e-45, 7.00649232e-45, 8.40779079e-45]], dtype=float32)

    In [45]: z.dtype = 'int32'

    In [46]: z
    Out[46]:
    array([[3, 2, 3],
    [4, 5, 6]])

    In [47]: z.astype('float32')
    Out[47]:
    array([[ 3., 2., 3.],
    [ 4., 5., 6.]], dtype=float32)

    In [48]: z
    Out[48]:
    array([[3, 2, 3],
    [4, 5, 6]])

    In [49]: z = z.astype('float32')

    In [50]: z
    Out[50]:
    array([[ 3., 2., 3.], #.代表是*.0
          [ 4., 5., 6.]], dtype=float32)

    In [56]: z = z.astype('float32')

    In [57]: z
    Out[57]:
    array([[ 1., 2., 3.],
    [ 4., 5., 6.]], dtype=float32)

    In [58]: z[0][0]=3.2

    In [59]: z
    Out[59]:
    array([[ 3.20000005, 2. , 3. ],
    [ 4. , 5. , 6. ]], dtype=float32)

  • 相关阅读:
    Element filtername is not allowed here-web.xml version="3.0"-intellij idea
    探究JavaScript闭包
    telnet的安装和使用
    Oracle数据库常用的sql语句
    centos6上安装jenkins
    idea的maven项目不知道为啥下载不下来jar包,看本地仓库只是下载了一下xml文件,没有jar包问题
    Oracle数据库使用mybatis的时候,实体类日期为Date类型,mybatis里面定义的是Date类型,插入的时候,时分秒全部是12:00:00问题
    maven打包某个分支的包
    maven打包到私服,打的是war包,好郁闷
    多线程初学习
  • 原文地址:https://www.cnblogs.com/bingabcd/p/7405161.html
Copyright © 2020-2023  润新知