python基础-4

一.Numpy/Scipy

 1 #coding=utf-8
 2 import numpy
 3 import scipy
 4
 5 x = numpy.ones((3, 4))
 6 print x
 7 """
 8 [[ 1.  1.  1.  1.]
 9  [ 1.  1.  1.  1.]
10  [ 1.  1.  1.  1.]]
11 """
12
13 y = numpy.array([[1, 2], [3, 4]])
14 print y
15 """
16 [[1 2]
17  [3 4]]
18  """
19
20 print numpy.linalg.det(y) #-2.0
21
22 print numpy.arange(1, 5, 0.5) #[ 1.   1.5  2.   2.5  3.   3.5  4.   4.5]
23
24 a = numpy.array([[5, 5, 5], [5, 5, 5]])
25 b = numpy.array([[2, 2, 2], [2, 2, 2]])
26 print a * b
27 """
28 [[10 10 10]
29  [10 10 10]]
30 """
31
32 print a.sum() #30
33 print a.sum(axis=0) #[10 10 10]
34 print a.sum(axis=1) #[15 15]
35
36 a = numpy.array([1, 3, 5])
37 b = numpy.array([2, 4, 6])
38 c = numpy.array([7, 8, 9])
39 print numpy.where(a > 2, b, c) #[7 4 6] Numpy.where函数是三元表达式x if condition else y的矢量化版本a > 2 [False, True, True]
 1 #coding=utf-8
 2 import numpy
 3 import scipy
 4
 5 def fun(x, y):
 6     return (x + 1) * (y + 1)
 7
 8 a = numpy.fromfunction(fun, (9, 9))
 9 print a
10 """
11 [[  1.   2.   3.   4.   5.   6.   7.   8.   9.]
12  [  2.   4.   6.   8.  10.  12.  14.  16.  18.]
13  [  3.   6.   9.  12.  15.  18.  21.  24.  27.]
14  [  4.   8.  12.  16.  20.  24.  28.  32.  36.]
15  [  5.  10.  15.  20.  25.  30.  35.  40.  45.]
16  [  6.  12.  18.  24.  30.  36.  42.  48.  54.]
17  [  7.  14.  21.  28.  35.  42.  49.  56.  63.]
18  [  8.  16.  24.  32.  40.  48.  56.  64.  72.]
19  [  9.  18.  27.  36.  45.  54.  63.  72.  81.]]
20  """
21
22 a = numpy.array([[1, 2, 3]])
23 b = numpy.array([[3, 4, 5]])
24 print numpy.add(a, b) #[[4 6 8]]
25 print numpy.multiply(a, b) #[[ 3  8 15]]

二.Pandas

 1 #coding=utf-8
 2 from pandas import Series
 3 import pandas as pd
 4 a = Series([3, 5, 7], index=[‘a‘, ‘b‘, ‘c‘])
 5 print a[‘a‘] #3
 6
 7 data = {‘a‘:1, ‘b‘:2, ‘c‘:3}
 8 sindex = [‘a‘, ‘b‘, ‘d‘]
 9 Ser = Series(data, index=sindex)
10 print Ser
11 """
12 a     1
13 b     2
14 d   NaN
15 dtype: float64
16 """
17 print Series.isnull(Ser)
18 """
19 a    False
20 b    False
21 d     True
22 dtype: bool
23 """
24 print a
25 """
26 a    3
27 b    5
28 c    7
29 dtype: int64
30 """
31
32 b = {‘a‘:2, ‘b‘:3, ‘d‘:5}
33 print Series(a) + Series(b)
34 """
35 a     5
36 b     8
37 c   NaN
38 d   NaN
39 dtype: float64
40 """
41 data = {‘Name‘:[‘a‘, ‘b‘, ‘c‘], ‘Num‘:[1, 2, 3]}
42 a = pd.DataFrame(data)
43 """
44   Name  Num
45 0    a    1
46 1    b    2
47 2    c    3
48 """
49 print a[‘Name‘]
50 print a.Name
51 """
52 0    a
53 1    b
54 2    c
55 Name: Name, dtype: object
56 """
57 print a[0:2]
58 print a[a.index < 2]
59 """
60   Name  Num
61 0    a    1
62 1    b    2
63 """
64 print a.ix[1]
65 """
66 Name    b
67 Num     2
68 """
69 del a[‘Name‘]
70 print a
71 """
72    Num
73 0    1
74 1    2
75 2    3
76 """

三.Matplotlib

 1 import pandas as pd
 2 from matplotlib.finance import quotes_historical_yahoo
 3 from datetime import date
 4 today = date.today()
 5 start = (today.year - 1, today.month, today.day)
 6 quote = quotes_historical_yahoo(‘AXP‘, start, today)
 7 fields = [‘date‘, ‘open‘, ‘close‘, ‘high‘, ‘low‘, ‘volume‘]
 8 df = pd.DataFrame(quote, index=range(1, len(quote) + 1), columns=fields)
 9 print df.head(10)
10 """
11       date       open      close       high        low   volume
12 1   735663  79.412407  79.097240  79.924559  78.851015  6530200
13 2   735666  78.939660  79.294224  79.609392  78.742676  5846100
14 3   735667  78.526003  77.915364  78.565393  77.678990  7525000
15 4   735668  78.200986  78.250226  78.545699  77.915364  4546200
16 5   735669  78.890411  80.328364  80.761722  78.821469  9386600
17 6   735670  80.269272  79.382861  80.417009  78.742676  6919400
18 7   735673  79.658632  80.269272  80.417009  79.530598  5295100
19 8   735674  79.973799  79.835914  79.983650  79.333620  4258200
20 9   735675  79.363166  80.623836  81.076889  79.057843  6449400
21 10  735676  80.604134  80.308669  80.722325  79.717731  4677000
22 """
23 list1 = []
24 for i in range(0, len(quote)):
25     x = date.fromordinal(int(quote[i][0]))
26     y = date.strftime(x, ‘%y-%m-%d‘)
27     list1.append(y)
28 df = pd.DataFrame(quote, index=list1, columns=fields)
29 df = df.drop([‘date‘], axis=1)
30 print df
31 """
32                open      close       high        low    volume
33 15-03-06  79.412407  79.097240  79.924559  78.851015   6530200
34 15-03-09  78.939660  79.294224  79.609392  78.742676   5846100
35 15-03-10  78.526003  77.915364  78.565393  77.678990   7525000
36 15-03-11  78.200986  78.250226  78.545699  77.915364   4546200
37 15-03-12  78.890411  80.328364  80.761722  78.821469   9386600
38 15-03-13  80.269272  79.382861  80.417009  78.742676   6919400
39 15-03-16  79.658632  80.269272  80.417009  79.530598   5295100
40 15-03-17  79.973799  79.835914  79.983650  79.333620   4258200
41 15-04-01  77.128302  77.997907  78.383301  76.930659   6163500
42 15-04-02  77.997907  78.758811  78.827984  77.642158   5695200
43 ........
44 16-03-04  58.439999  58.290001  58.650002  57.810001   5407400
45
46 [252 rows x 5 columns]
47 """
时间: 2024-08-06 07:59:03

python基础-4的相关文章

linux+jmeter+python基础+抓包拦截

LINUX 一 配置jdk 环境 *需要获取root权限,或者切换为root用户 1.windows下载好,去 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 官方网站下载jdk(linux相应版本) 2.在usr目录下创建java路径文件夹 [root bin]cd /usr mkdir java 3.将jdk-8u60-linux-x64.tar.gz放到刚才创建的文件夹下

Python基础教程(第九章 魔法方法、属性和迭代器)

本文内容全部出自<Python基础教程>第二版,在此分享自己的学习之路. ______欢迎转载:http://www.cnblogs.com/Marlowes/p/5437223.html______ Created on Marlowes 在Python中,有的名称会在前面和后面都加上两个下划线,这种写法很特别.前面几章中已经出现过一些这样的名称(如__future__),这种拼写表示名字有特殊含义,所以绝不要在自己的程序中使用这样的名字.在Python中,由这些名字组成的集合所包含的方法称

Python基础入门 (一)

一.关于版本的选择 Should i use Python 2 or Python 3 for my development activity?转载自Python官网 Short version: Python 2.x is legacy, Python 3.x is the present and future of the language Python 3.0 was released in 2008. The final 2.x version 2.7 release came out

Python 基础 - Day 4 Learning Note - Generator 生成器

列表生成器/列表解析 list comprehension 简单灵活地创建列表,通常和lambda(), map(), filter() 一起使用 通过列表生成式, 直接创建列表.但是,收到内容限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问几个元素,那其他的就白占空间.列表生成器能够一边循环一边计算,大大节省大量的空间.是生成器的一种. 只有调用,才能生成. 不支持切片操作,只能通过__next()___一个个取数字. 基本语法

python基础教程(第二版)

开始学习python,根据Python基础教程,把里面相关的基础章节写成对应的.py文件 下面是github上的链接 python基础第1章基础 python基础第2章序列和元组 python基础第3章使用字符串 python基础第4章字典 python基础第5章循环 python基础第6章函数和魔法参数 python基础第7章类 python基础第8章异常 python基础第9章魔法方法.属性和迭代器 python基础第11章文件 python基础第12章GUI(wxPython) pytho

python基础周作业

python基础周作业 1.执行python脚本的两种方法 脚本前面直接指定解释器 在脚本开始前声明解释器 2.简述位,字节的关系 每一个字节占用八个比特位 3, 简述ascii.unicode.utf- ‐8.gbk的关系 utf--‐8 <-- unicode <-- gbk <-- ascii 按此方向兼容 4..请写出"李杰"分别用utf- ‐8和gbk编码所占的位数 "李杰" 占用utf -8 占6字节 , gbk 占用4字节 5.pyt

Python基础(二)

Python基础(二) Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和xrange 编码与进制转换 Python 运算符 1.算术运算: 2.比较运算: 3.赋值运算: 4.逻辑运算:  5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483

Python之路【第三篇】:Python基础(二)

Python之路[第三篇]:Python基础(二) 内置函数 一 详细见python文档,猛击这里 文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 1 文件句柄 = file('文件路径', '模式') 注:python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open. 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作.

Python之路【第二篇】:Python基础(一)

Python之路[第二篇]:Python基础(一) 入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1:     name = 'wupeiqi' print  name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 1 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result = 值2 三.进制 二进制,01 八进

Python学习笔记(一)python基础与函数

1.python基础 1.1输入与输出 输出 用print加上字符串,就可以打印指定的文字或数字 >>> print 'hello, world' hello, world >>> print 300 300 >>> print 100+200 300 print语句也可以跟上多个字符串,用逗号","隔开,就可以连成一串输出: >>> print 'The quick brown fox', 'jumps over