前言
NumPy是一个功能强大的Python库,主要用于对多维数组执行计算。NumPy这个词来源于两个单词-- Numerical
和Python
。NumPy提供了大量的库函数和操作,可以帮助使用者轻松地进行数值计算。这类数值计算广泛用于以下任务:
-
机器学习模型:在编写机器学习算法时,需要对矩阵进行各种数值计算。例如矩阵乘法、换位、加法等。NumPy提供了一个非常好的库,用于简单(在编写代码方面)和快速(在速度方面)计算。NumPy数组用于存储训练数据和机器学习模型的参数。
-
图像处理和计算机图形学:计算机中的图像表示为多维数字数组。NumPy成为同样情况下最自然的选择。实际上,NumPy提供了一些优秀的库函数来快速处理图像。例如,镜像图像、按特定角度旋转图像等。
-
数学任务:NumPy对于执行各种数学任务非常有用,如数值积分、微分、内插、外推等。因此,当涉及到数学任务时,它形成了一种基于Python的MATLAB的快速替代。
备注:本文主要介绍numpy在数据科学(数据分析和数据建模中的运用)
1.安装
pip install numpy
2.Numbers(数字类型):代表的是整数和浮点数,原理与其他语言相同
x = 3
print(type(x)) # Prints "<class 'int'>" 查看字段类型
print(x) # Prints "3" 输出结果,2.0不需要括号
print(x + 1) # Addition; prints "4" 加
print(x - 1) # Subtraction; prints "2" 减
print(x * 2) # Multiplication; prints "6" 乘
print(x * 3) # Divide prints '1' 除
print(x ** 2) # Exponentiation; prints "9" 2次方
x += 1 # 1+3
print(x) # Prints "4"
x *= 2 # 乘
print(x) # Prints "8"
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
3.Booleans(布尔类型): Python实现了所有常用的布尔逻辑运算符,但它使用的是英文单词而不是符号 (&&
, ||
, etc.):
t = True
f = False
print(type(t)) # Prints "<class 'bool'>"
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"
4.Strings(字符串类型)
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"
5.容器(Containers) :容器类型:列表、字典、集合和元组
5.1 列表(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']"
5.2 切片(Slicing):列表取数逻辑
nums = list(range(5)) # range is a built-in function that creates a list of integers
print(nums) # Prints "[0, 1, 2, 3, 4]" 从0开始计数
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]"
5.3 (循环)Loops: 循环遍历变量
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print(animal)
# Prints "cat", "dog", "monkey", each on its own line.
#列表推导式
print( [x for i in animails] )
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0] #偶数的2次方
print(even_squares) # Prints "[0, 4, 16]"
5.3.1 使用内置的 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
5.4 字典 字典存储(键,值)对,类似于Java中的Map
或Javascript中的对象
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.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"
5.4.1迭代词典中的键
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal] # animal表示key 键值
print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"
#访问键及其对应的值使用items()
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.items():
print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 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}"
5.6 集合(Sets) 集合是不同元素的无序集合
se = {11, 22, 33}
be = {22,44,55}
se.update(be) # 把se和be合并,得出的值覆盖se ,去重
print(se)
se.update([66, 77]) # 可增加迭代项
print(se)
#集合的循环,因为集合是无序的,故用enumerate
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 "{0, 1, 2, 3, 4, 5}"
5.7 元组(Tuples) 元组是(不可变的)有序值列表
d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys
t = (5, 6) # Create a tuple
print(type(t)) # Prints "<class 'tuple'>"
print(d[t]) # Prints "5"
print(d[(1, 2)]) # Prints "1"
6 类(Classes)
class Greeter(object):
# Constructor
def __init__(self, name):
self.name = name # Create an instance variable
# Instance method
def greet(self, loud=False):
if loud==True:
print('HELLO, %s!' % self.name.upper())
else:
print('Hello, %s' % self.name)
g = Greeter('Fred') # Construct an instance of the Greeter class
g.greet() # Call an instance method; prints "Hello, Fred" 默认值loud
g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"
7.数组的基本操作
# Input
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# Solution
arr[arr % 2 == 1] #操作类似列表,选出奇数
# > array([1, 3, 5, 7, 9])
# 不改变原始位置替换 where
arr = np.arange(10)
out = np.where(arr % 2 == 1, -1, arr)
print(arr)
print(out)
# [0 1 2 3 4 5 6 7 8 9]
# [ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1]
out.shape #查看数组结构
out.reshape(2,-1) # Setting to -1 automatically decides the number of cols
参考文章1:Numpy中文文档
参考文章2:python之numpy的基本使用