matlab如何创建元组_[cs231n] Python Numpy 教程

代码Jupyter Notebook文件:code​github.com

注:本文翻译自cs231n课程的Numpy教程(Python 3.5),原文Python Numpy Tutorial

本教程由Justin Johnson提供。

本课程的所有作业都将使用Python编程语言。Python本身是一种很棒的通用编程语言,但是在一些流行的库(numpy、scipy、matplotlib)的帮助下,它成为了一个强大的科学计算环境。

你们中的一些人可能有Matlab方面的知识,在这种情况下,我们也推荐Matlab用户页面的numpy。Numpy for Matlab

内容目录:Python基本数据类型

容器列表

字典

集合

元组

函数

Numpy数组

数组访问

数据类型

数组计算

广播

SciPy图像操作

Matlab文件

点之间的距离

Matplotlib绘制图形

绘制多个图形

图像

Python

Python是一种高级的、动态类型的多范型编程语言。Python代码经常被说成是伪代码,因为它允许您用很少的几行代码来表达非常强大的思想,同时又非常易读。举个例子,下面是一个用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.5。有点令人困惑的是,Python 3.0对该语言引入了不向后兼容的更改,因此为2.7编写的代码在3.5以下可能无法工作,反之亦然。对于这个教程,所有代码都将使用Python 3.5。

如何查看版本呢?在命令行运行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"

你可以在文档查看所有字符串方法。

容器

Python有以下几种容器类型:列表(lists)、字典(dictionaries)、集合(sets)和元组(tuples)。

译者注:创建列表用[],创建字典、集合用{},创建元组用()。

列表Lists

列表就是Python中的数组,但是列表长度可变,且能包含不同类型的元素。

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 "[3, 1, 'foo', 'bar']"

x = xs.pop() # Remove and return the last element of the list

print(x, xs) # Prints "bar [3, 1, 'foo']"

你可以在文档中查阅列表的细节。

切片Slicing:除了一次访问一个列表元素外,Python还提供了简洁的语法来访问子列表,这就是切片。

nums = list(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, 9, 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):

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值