logo资料库

CS231N课程中文讲义.pdf

第1页 / 共176页
第2页 / 共176页
第3页 / 共176页
第4页 / 共176页
第5页 / 共176页
第6页 / 共176页
第7页 / 共176页
第8页 / 共176页
资料共176页,剩余部分请下载后查看
CS231n 10 Andrej Karpathy ID Python Numpy CS231n Python Numpy Tutorial Flood Sung SunisDown Justin Johnson Python numpy, scipy, matplotlib Python Python Numpy Python Python Python Matlab  numpy for Matlab users IPython notebook Volodymyr Kuleshov Isaac Caswell CS 228 Python 课 程 笔 记 翻 译 : 教 程 译 者 注 : 本 文 智 能 单 元 首 发 , 翻 译 自 斯 坦 福 课 程 笔 记 , 由 课 程 教 师 授 权 进 行 翻 译 。 本 篇 教 程 由 杜 客 翻 译 完 成 , 、 、 巩 子 嘉 和 一 位 不 愿 透 露 的 知 友 对 本 翻 译 亦 有 贡 献 。 原 文 如 下 这 篇 教 程 由 创 作 。 我 们 将 使 用 编 程 语 言 来 完 成 本 课 程 的 所 有 作 业 。 是 一 门 伟 大 的 通 用 编 程 语 言 , 在 一 些 常 用 库 ( ) 的 帮 助 下 , 它 又 会 变 成 一 个 强 大 的 科 学 计 算 环 境 。 我 们 期 望 你 们 中 大 多 数 人 对 于 语 言 和 库 比 较 熟 悉 , 而 对 于 没 有 经 验 的 同 学 , 这 篇 教 程 可 以 帮 助 你 们 快 速 了 解 编 程 环 境 和 如 何 使 用 作 为 科 学 计 算 工 具 。 一 部 分 同 学 对 于 有 一 定 经 验 。 对 于 这 部 分 同 学 , 我 们 推 荐 阅 读 页 面 。 你 们 还 可 以 查 看 本 教 程 的 版 。 该 教 程 是 由 和 为 课 程 创 建 的 。 内 容 列 表 : 基 本 数 据 类 型 容 器 列 表 字 典 杜 客 个 月 前
Numpy SciPy MATLAB Matplotlib Python Python Python quicksort Python def quicksort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) / 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right) 集 合 元 组 函 数 类 数 组 访 问 数 组 数 据 类 型 数 组 计 算 广 播 图 像 操 作 文 件 点 之 间 的 距 离 绘 制 图 形 绘 制 多 个 图 形 图 像 是 一 种 高 级 的 , 动 态 类 型 的 多 范 型 编 程 语 言 。 很 多 时 候 , 大 家 会 说 看 起 来 简 直 和 伪 代 码 一 样 , 这 是 因 为 你 能 够 通 过 很 少 行 数 的 代 码 表 达 出 很 有 力 的 思 想 。 举 个 例 子 , 下 面 是 用 实 现 的 经 典 的 算 法 例 子 :
print quicksort([3,6,8,10,1,2,1]) # Prints "[1, 1, 2, 3, 6, 8, 10]" Python Python 2.7 3.4 2.7 3.4 3.0 2.7 python --version Python x = 3 print type(x) # Prints "" print x # Prints "3" print x + 1 # Addition; prints "4" print x - 1 # Subtraction; prints "2" print x * 2 # Multiplication; prints "6" print x ** 2 # Exponentiation; prints "9" x += 1 print x # Prints "4" x *= 2 print x # Prints "8" y = 2.5 print type(y) # Prints "" print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25" Python x++ x-- Python Python && || t = True f = False print type(t) # Prints "" print t and f # Logical AND; prints "False" 版 本 有 两 个 支 持 的 版 本 , 分 别 是 和 。 这 有 点 让 人 迷 惑 , 向 语 言 中 引 入 了 很 多 不 向 后 兼 容 的 变 化 , 下 的 代 码 有 时 候 在 下 是 行 不 通 的 。 在 这 个 课 程 中 , 我 们 使 用 的 是 版 本 。 如 何 查 看 版 本 呢 ? 使 用 命 令 。 基 本 数 据 类 型 和 大 多 数 编 程 语 言 一 样 , 拥 有 一 系 列 的 基 本 数 据 类 型 , 比 如 整 型 、 浮 点 型 、 布 尔 型 和 字 符 串 等 。 这 些 类 型 的 使 用 方 式 和 在 其 他 语 言 中 的 使 用 方 式 是 类 似 的 。 数 字 : 整 型 和 浮 点 型 的 使 用 与 其 他 语 言 类 似 。 需 要 注 意 的 是 , 中 没 有 和 的 操 作 符 。 也 有 内 置 的 长 整 型 和 复 杂 数 字 类 型 , 具 体 细 节 可 以 查 看 文 档 。 布 尔 型 : 实 现 了 所 有 的 布 尔 逻 辑 , 但 用 的 是 英 语 , 而 不 是 我 们 习 惯 的 操 作 符 ( 比 如 和 等 ) 。
print t or f # Logical OR; prints "True" print not t # Logical NOT; prints "False" print t != f # Logical XOR; prints "True" Python hello = 'hello' # String literals can use single quotes world = "world" # or double quotes; it does not matter. print hello # Prints "hello" print len(hello) # String length; prints "5" hw = hello + ' ' + world # String concatenation print hw # prints "hello world" hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting print hw12 # prints "hello world 12" s = "hello" print s.capitalize() # Capitalize a string; prints "Hello" print s.upper() # Convert a string to uppercase; prints "HELLO" print s.rjust(7) # Right-justify a string, padding with spaces; prints " hello" print s.center(7) # Center a string, padding with spaces; prints " hello " print s.replace('l', '(ell)') # Replace all instances of one substring with another; # prints "he(ell)(ell)o" print ' world '.strip() # Strip leading and trailing whitespace; prints "world" Containers container Python tuples Lists Python lists dictionaries sets xs = [3, 1, 2] # Create a list print xs, xs[2] # Prints "[3, 1, 2] 2" print xs[-1] # Negative indices count from the end of the list; prints "2" xs[2] = 'foo' # Lists can contain elements of different types print xs # Prints "[3, 1, 'foo']" 字 符 串 : 对 字 符 串 的 支 持 非 常 棒 。 字 符 串 对 象 有 一 系 列 有 用 的 方 法 , 比 如 : 如 果 想 详 细 查 看 字 符 串 方 法 , 请 看 文 档 。 容 器 译 者 注 : 有 知 友 建 议 翻 译 为 复 合 数 据 类 型 , 供 读 者 参 考 。 有 以 下 几 种 容 器 类 型 : 列 表 ( ) 、 字 典 ( ) 、 集 合 ( ) 和 元 组 ( ) 。 列 表 列 表 就 是 中 的 数 组 , 但 是 列 表 长 度 可 变 , 且 能 包 含 不 同 类 型 元 素 。
xs.append('bar') # Add a new element to the end of the list print xs # Prints x = xs.pop() # Remove and return the last element of the list print x, xs # Prints "bar [3, 1, 'foo']" Slicing Python nums = range(5) # range is a built-in function that creates a list of integers print nums # Prints "[0, 1, 2, 3, 4]" print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]" print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]" print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]" print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]" print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]" nums[2:4] = [8, 9] # Assign a new sublist to a slice print nums # Prints "[0, 1, 8, 8, 4]" Numpy Loops animals = ['cat', 'dog', 'monkey'] for animal in animals: print animal # Prints "cat", "dog", "monkey", each on its own line. enumerate animals = ['cat', 'dog', 'monkey'] for idx, animal in enumerate(animals): print '#%d: %s' % (idx + 1, animal) # Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line List comprehensions nums = [0, 1, 2, 3, 4] squares = [] for x in nums: squares.append(x ** 2) print squares # Prints [0, 1, 4, 9, 16] 列 表 的 细 节 , 同 样 可 以 查 阅 文 档 。 切 片 : 为 了 一 次 性 地 获 取 列 表 中 的 元 素 , 提 供 了 一 种 简 洁 的 语 法 , 这 就 是 切 片 。 在 数 组 的 内 容 中 , 我 们 会 再 次 看 到 切 片 语 法 。 循 环 : 我 们 可 以 这 样 遍 历 列 表 中 的 每 一 个 元 素 : 如 果 想 要 在 循 环 体 内 访 问 每 个 元 素 的 指 针 , 可 以 使 用 内 置 的 函 数 列 表 推 导 : 在 编 程 的 时 候 , 我 们 常 常 想 要 将 一 种 数 据 类 型 转 换 为 另 一 种 。 下 面 是 一 个 简 单 例 子 , 将 列 表 中 的 每 个 元 素 变 成 它 的 平 方 。 使 用 列 表 推 导 , 你 就 可 以 让 代 码 简 化 很 多 :
nums = [0, 1, 2, 3, 4] squares = [x ** 2 for x in nums] print squares # Prints [0, 1, 4, 9, 16] nums = [0, 1, 2, 3, 4] even_squares = [x ** 2 for x in nums if x % 2 == 0] print even_squares # Prints "[0, 4, 16]" Dictionaries , Java Map d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data print d['cat'] # Get an entry from a dictionary; prints "cute" print 'cat' in d # Check if a dictionary has a given key; prints "True" d['fish'] = 'wet' # Set an entry in a dictionary print d['fish'] # Prints "wet" # print d['monkey'] # KeyError: 'monkey' not a key of d print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A" print d.get('fish', 'N/A') # Get an element with a default; prints "wet" del d['fish'] # Remove an element from a dictionary print d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A" Loops d = {'person': 2, 'cat': 4, 'spider': 8} for animal in d: legs = d[animal] print 'A %s has %d legs' % (animal, legs) # Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs" iteritems d = {'person': 2, 'cat': 4, 'spider': 8} for animal, legs in d.iteritems(): print 'A %s has %d legs' % (animal, legs) # Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs" Dictionary comprehensions 列 表 推 导 还 可 以 包 含 条 件 : 字 典 字 典 用 来 储 存 ( 键 值 ) 对 , 这 和 中 的 差 不 多 。 你 可 以 这 样 使 用 它 : 想 要 知 道 字 典 的 其 他 特 性 , 请 查 阅 文 档 。 循 环 : 在 字 典 中 , 用 键 来 迭 代 更 加 容 易 。 如 果 你 想 要 访 问 键 和 对 应 的 值 , 那 就 使 用 方 法 : 字 典 推 导 : 和 列 表 推 导 类 似 , 但 是 允 许 你 方 便 地 构 建 字 典 。
nums = [0, 1, 2, 3, 4] even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0} print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}" Sets animals = {'cat', 'dog'} print 'cat' in animals # Check if an element is in a set; prints "True" print 'fish' in animals # prints "False" animals.add('fish') # Add an element to a set print 'fish' in animals # Prints "True" print len(animals) # Number of elements in a set; prints "3" animals.add('cat') # Adding an element that is already in the set does nothing print len(animals) # Prints "3" animals.remove('cat') # Remove an element from a set print len(animals) # Prints "2" Loops animals = {'cat', 'dog', 'fish'} for idx, animal in enumerate(animals): print '#%d: %s' % (idx + 1, animal) # Prints "#1: fish", "#2: dog", "#3: cat" Set comprehensions from math import sqrt nums = {int(sqrt(x)) for x in range(30)} print nums # Prints "set([0, 1, 2, 3, 4, 5])" Tuples d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys print d t = (5, 6) # Create a tuple print type(t) # Prints "" 集 合 集 合 是 独 立 不 同 个 体 的 无 序 集 合 。 示 例 如 下 : 和 前 面 一 样 , 要 知 道 更 详 细 的 , 查 看 文 档 。 循 环 : 在 集 合 中 循 环 的 语 法 和 在 列 表 中 一 样 , 但 是 集 合 是 无 序 的 , 所 以 你 在 访 问 集 合 的 元 素 的 时 候 , 不 能 做 关 于 顺 序 的 假 设 。 集 合 推 导 : 和 字 典 推 导 一 样 , 可 以 很 方 便 地 构 建 集 合 : 元 组 元 组 是 一 个 值 的 有 序 列 表 ( 不 可 改 变 ) 。 从 很 多 方 面 来 说 , 元 组 和 列 表 都 很 相 似 。 和 列 表 最 重 要 的 不 同 在 于 , 元 组 可 以 在 字 典 中 用 作 键 , 还 可 以 作 为 集 合 的 元 素 , 而 列 表 不 行 。 例 子 如 下 :
print d[t] # Prints "5" print d[(1, 2)] # Prints "1" Functions Python def def sign(x): if x > 0: return 'positive' elif x < 0: return 'negative' else: return 'zero' for x in [-1, 0, 1]: print sign(x) # Prints "negative", "zero", "positive" def hello(name, loud=False): if loud: print 'HELLO, %s' % name.upper() else: print 'Hello, %s!' % name hello('Bob') # Prints "Hello, Bob" hello('Fred', loud=True) # Prints "HELLO, FRED!" Classes Python class Greeter(object): # Constructor def __init__(self, name): self.name = name # Create an instance variable 文 档 有 更 多 元 组 的 信 息 。 函 数 函 数 使 用 来 定 义 函 数 : 我 们 常 常 使 用 可 选 参 数 来 定 义 函 数 : 函 数 还 有 很 多 内 容 , 可 以 查 看 文 档 。 类 对 于 类 的 定 义 是 简 单 直 接 的 :
分享到:
收藏