佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

12
返回列表 发新帖
楼主: 凌云

Python 数学与人工智能开发

[复制链接]
 楼主| 发表于 29-6-2016 11:16 PM | 显示全部楼层
本帖最后由 凌云 于 29-6-2016 11:25 PM 编辑

Chapter 1: Number 数字
K) Finding the Roots of a Quadratic Equation 求一元二次方程的根:
Screen Shot 2016-06-29 at 10.58.36 PM.png
一元二次方程式的一般形式是:
假设1 + 2x + 1 = 0,设a,b,c的值:
  1. >>> a = 1
  2. >>> b = 2
  3. >>> c = 1
复制代码


两个方程都有:
Screen Shot 2016-06-29 at 11.00.51 PM.png , 求D:
  1. >>> D = (b**2 – 4*a*c)**0.5
复制代码
求x1,x2 的值:
  1. >>> x_1 = (-b + D)/(2*a)
  2. >>> x_1
  3. -1.0
  4. >>> x_2 = (-b - D)/(2*a)
  5. >>> x_2
  6. -1.0
复制代码


可以写成一个函数:
  1. def roots(a, b, c):

  2.     D = (b*b - 4*a*c)**0.5
  3.     x_1 = (-b + D)/(2*a)
  4.     x_2 = (-b - D)/(2*a)

  5.     print('x1: {0}'.format(x_1))
  6.     print('x2: {0}'.format(x_2))

  7. if __name__ == '__main__': #当这个模组为主模组时
  8.     a = input('Enter a: ') #取使用者输入值,设成a
  9.     b = input('Enter b: ')
  10.     c = input('Enter c: ')
  11.     roots(float(a), float(b), float(c)) #使用函数
复制代码


运行:
  1. Enter a: 1
  2. Enter b: 1
  3. Enter c: 1
  4. x1: (-0.49999999999999994+0.8660254037844386j)
  5. x2: (-0.5-0.8660254037844386j)
复制代码

第一章完结。

回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 30-6-2016 12:02 PM | 显示全部楼层
本帖最后由 凌云 于 30-6-2016 12:27 PM 编辑

第二章:用图表视觉化数据


A)笛卡尔坐标系 (平面直角坐标)
Screen Shot 2016-06-30 at 11.53.45 AM.png

回复

使用道具 举报

 楼主| 发表于 30-6-2016 12:21 PM | 显示全部楼层
本帖最后由 凌云 于 30-6-2016 12:24 PM 编辑

第二章:B)Python 的 List 和 tuple

学过其它编程语言的网友应该知道 Array,但是python里面没有Array,只有List。
List 和 tuple 的差别在于tuple是不能改变数值的,它们可以使用的函数都一样。
  1. >>> simplelist = [1, 2, 3]
  2. >>> simplelist[0]
  3. 1
  4. >>> simplelist[1]
  5. 2
  6. >>> simplelist[2]
  7. 3
复制代码


List 可以存放String和其它物件,其它关于List的资料请参考相关Python书籍。
  1. >>> stringlist = ['a string','b string','c string']
  2. >>> stringlist[0]
  3. 'a string'
  4. >>> stringlist[1]
  5. 'b string'
  6. >>> stringlist[2]
  7. 'c string'
复制代码


这里介绍一个List的函数 append(), 用来加入数值到 List 中。
  1. >>> emptylist
  2. []
  3. >>> emptylist.append(1)
  4. >>> emptylist
  5. [1]
  6. >>> emptylist.append(2)
  7. >>> emptylist
  8. [1, 2]
复制代码


tuple 的创建方法是用 () 并且数值不可改变,其它都和 List 一样。
  1. >>> simpletuple = (1, 2, 3)
  2. >>> simpletuple[0]
  3. 1
  4. >>> simpletuple[1]
  5. 2
  6. >>> simpletuple[2]
  7. 3
复制代码


你也可以用 -1,-2 值来取得最后和最后第二个数值。
  1. simplelist[-1]
  2. simplelist[-2]
复制代码

迭代 (loop)的语法( List 和 Tuple 都可以):
  1. >>> l = [1, 2, 3]
  2. >>> for item in l:
  3.         print(item)
复制代码
Output 输出:
  1. 1
  2. 2
  3. 3
复制代码


也可以用 enumerate 内建函数来取得 index 和 value :
  1. >>> l = [1, 2, 3]
  2. >>> for index, item in enumerate(l):
  3.         print(index, item)
复制代码
Output 输出:
  1. 0 1
  2. 1 2
  3. 2 3
复制代码



回复

使用道具 举报

 楼主| 发表于 30-6-2016 12:55 PM | 显示全部楼层
本帖最后由 凌云 于 30-6-2016 12:56 PM 编辑

第二章:C)简单图表

我们现在要画一个经过三个点的线,分别是 (1,2),(2,4),(3,6)。
先把 x 和 y 设成两个list:
  1. >>> x_numbers = [1, 2, 3]
  2. >>> y_numbers = [2, 4, 6]
复制代码
然后创建图表:
  1. >>> from pylab import plot, show  #导入函数
  2. >>> plot(x_numbers, y_numbers)  #使用函数
复制代码
然后再用show()来画出并显现图表。
  1. >>> show()
复制代码
你就会看到:
Screen Shot 2016-06-30 at 11.32.05 AM.png


回复

使用道具 举报

 楼主| 发表于 30-6-2016 01:33 PM | 显示全部楼层
第二章:D)图表上的标示
只要加上 marker 的参数,值可以是:o, *, x, +
  1. >>> plot(x_numbers, y_numbers, marker='o')

复制代码
然后用 show() 函数,就可以看到:
Screen Shot 2016-06-30 at 11.32.39 AM.png

如果只要标示,不要线,可以这样写:
  1. >>> plot(x_numbers, y_numbers, 'o')
复制代码
同样的用 show() 过后,可以看到:
Screen Shot 2016-06-30 at 11.33.36 AM.png

回复

使用道具 举报

 楼主| 发表于 30-6-2016 01:57 PM | 显示全部楼层
本帖最后由 凌云 于 30-6-2016 02:01 PM 编辑

第二章:E)标示纽约的2000-2012年平均温度
把平均温度写成list,并且创建plot, 然后用 show() 函数画出图表:
  1. >>> nyc_temp = [53.9, 56.3, 56.4, 53.4, 54.5, 55.8, 56.8, 55.0, 55.3, 54.0, 56.7, 56.4, 57.3]
  2. >>> plot(nyc_temp, marker='o')
复制代码
你就会看到:
Screen Shot 2016-06-30 at 11.34.30 AM.png

图中没有显示年份,要显示年份,代码这样写:
  1. >>> nyc_temp = [53.9, 56.3, 56.4, 53.4, 54.5, 55.8, 56.8, 55.0, 55.3, 54.0, 56.7, 56.4, 57.3]
  2. >>> years = range(2000, 2013) #标示从2000 至 2012 的数值
  3. >>> plot(years, nyc_temp, marker='o')
复制代码
这样,x 值就变成了2000年到2012年的数值,用 show() 来画出图表:
  1. >>>show()
复制代码
Screen Shot 2016-06-30 at 11.36.07 AM.png





回复

使用道具 举报

Follow Us
 楼主| 发表于 30-6-2016 02:28 PM | 显示全部楼层
本帖最后由 凌云 于 1-7-2016 10:45 PM 编辑

第二章:F)比较纽约的月平均温度

首先我们把要比较的一整年的温度写成list,写三年的温度:
  1. >>> nyc_temp_2000 = [31.3, 37.3, 47.2, 51.0, 63.5, 71.3, 72.3, 72.7, 66.0, 57.0, 45.3, 31.1]
  2. >>> nyc_temp_2006 = [40.9, 35.7, 43.1, 55.7, 63.1, 71.0, 77.9, 75.8, 66.6, 56.2, 51.9, 43.6]
  3. >>> nyc_temp_2012 = [37.3, 40.9, 50.9, 54.8, 65.1, 71.0, 78.8, 76.7, 68.8, 58.0, 43.9, 41.5]
复制代码
然后用 range() 建一个月份的list,然后用plot() 创建图表:
  1. >>> months = range(1, 13)
  2. >>> plot(months, nyc_temp_2000, months, nyc_temp_2006, months, nyc_temp_2012)
复制代码
然后用 show() 画出图表:
  1. >>> show()
复制代码
你就会看到:
Screen Shot 2016-06-30 at 11.37.21 AM.png

也可以分成三行代码来写:
  1. >>> plot(months, nyc_temp_2000)
  2. [<matplotlib.lines.Line2D object at 0x7f1e51351810>]
  3. >>> plot(months, nyc_temp_2006)
  4. [<matplotlib.lines.Line2D object at 0x7f1e5ae8e390>]
  5. >>> plot(months, nyc_temp_2012)
  6. [<matplotlib.lines.Line2D object at 0x7f1e5136ccd0>]
  7. >>> show()
复制代码


但是我们有一个问题,就是三条线都一样颜色,为了把三条线用不同颜色标示出来,我们可以用 legend() 函数,先创建图表:
  1. >>> plot(months, nyc_temp_2000, months, nyc_temp_2006, months, nyc_temp_2012)
复制代码

然后倒入 legend() 函数,再使用函数,加入三年的标签:
  1. >>> from pylab import legend
  2. >>> legend([2000, 2006, 2012])
  3. <matplotlib.legend.Legend object at 0x7f2549d79410>
复制代码
然后用 show() 画出图表,会看到右上角多了标示,分别标示三条线的颜色:
  1. >>> show()
复制代码


然后加上 title,xlabel,ylabel,代码如下(>>>的是要打入的代码):
  1. >>> from pylab import plot, show, title, xlabel, ylabel, legend   #导入需要用到的函数
  2. >>> plot(months, nyc_temp_2000, months, nyc_temp_2006, months, nyc_temp_2012) #创建图表
  3. [<matplotlib.lines.Line2D object at 0x7f2549a9e210>, <matplotlib.lines.Line2D
  4. object at 0x7f2549a4be90>, <matplotlib.lines.Line2D object at 0x7f2549a82090>]
  5. >>> title('Average monthly temperature in NYC') #设title
  6. <matplotlib.text.Text object at 0x7f25499f7150>
  7. >>> xlabel('Month') #设xlabel
  8. <matplotlib.text.Text object at 0x7f2549d79210>
  9. >>> ylabel('Temperature') #设ylabel
  10. <matplotlib.text.Text object at 0x7f2549b8b2d0>

  11. >>> legend([2000, 2006, 2012]) #用不同颜色分别标示三条线
  12. <matplotlib.legend.Legend object at 0x7f2549a82910>
复制代码
最后用 show() 函数,画出图表:
Screen Shot 2016-06-30 at 2.26.11 PM.png

回复

使用道具 举报

发表于 14-8-2016 12:40 PM | 显示全部楼层
想起我是AI毕业的现在做的竟然完全不是AI 真是惭愧
回复

使用道具 举报


ADVERTISEMENT

发表于 14-8-2016 04:13 PM | 显示全部楼层
https://www.youtube.com/playlist ... AIlf5TxxFPzb-0zeVZ8
最近看到有人貼了 tensorflow的教學, 裡面CALL GOOGLE的 LIBRARY看起來這個東西變得很簡單下,

不過,我不懂裡面的THEORY+數學, 也搞不出什麼東西來 LOL
回复

使用道具 举报

发表于 17-8-2016 10:38 AM | 显示全部楼层
如果是股票分析,应该用哪一个machine learning library?
回复

使用道具 举报

 楼主| 发表于 4-2-2017 11:13 AM | 显示全部楼层
本帖最后由 凌云 于 4-2-2017 11:15 AM 编辑
雷洛 发表于 14-8-2016 04:13 PM
https://www.youtube.com/playlist?list=PLXO45tsB95cKI5AIlf5TxxFPzb-0zeVZ8
最近看到有人貼了 tensorflow的教學, 裡面CALL GOOGLE的 LIBRARY看起來這個東西變得很簡單下,

不過,我不懂裡面的THEORY+數學,  ...

其实里面的数学和理论很简单的,主要是,linear algebra, statistic, calculas.建议可以先看 Andrew Ng 在 coursera.org 的教程。
回复

使用道具 举报

 楼主| 发表于 4-2-2017 11:19 AM | 显示全部楼层
aaronkwok 发表于 17-8-2016 10:38 AM
如果是股票分析,应该用哪一个machine learning library?

Machine Learning 本身是没有针对某个领域的,它是一种 general 的算法,不同的是数据的输入和人为的调整。
回复

使用道具 举报

 楼主| 发表于 4-2-2017 11:21 AM | 显示全部楼层
username5 发表于 14-8-2016 12:40 PM
想起我是AI毕业的现在做的竟然完全不是AI 真是惭愧

现在在做什么?我觉得学了就不要浪费,这个领域是未来的主流,谷歌百度等都全力投入了。
回复

使用道具 举报

发表于 17-2-2017 04:42 PM | 显示全部楼层
等 lz 开发 Beta Go 来打败 Alpha Go
回复

使用道具 举报

发表于 7-10-2018 10:31 PM | 显示全部楼层
aaronkwok 发表于 17-8-2016 10:38 AM
如果是股票分析,应该用哪一个machine learning library?

你的股票分析开发了?
回复

使用道具 举报

发表于 15-10-2018 06:43 PM | 显示全部楼层
樓主有用Jupyter notebook來玩visualization嗎?

小弟最近也在學習中
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-4-2024 03:46 PM , Processed in 0.063411 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表