码迷,mamicode.com
首页 > 编程语言 > 详细

python基础-4

时间:2016-03-07 01:16:08      阅读:478      评论:0      收藏:0      [点我收藏+]

标签:

一.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 """

 

python基础-4

标签:

原文地址:http://www.cnblogs.com/wanderingzj/p/5249117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!