cs231n笔记 -- 1.python, scipy, matplotlib等

  1. 快排
# 快排:从小到大排
# 思想:
# 1.取中位数;
# 2.以中位数为基准,将数组分为两半;
# 3.分别对左右两半递归

def quick_sort(list_):
    if len(list_) <= 1:
        return list_
    midlle_num = list_[len(list_) // 2]
    right_list = [i for i in list_ if i > midlle_num]
    midlle_list = [i for i in list_ if i == midlle_num]
    left_list = [i for i in list_ if i < midlle_num]
    return quick_sort(left_list) + midlle_list + quick_sort(right_list)

if __name__ == '__main__':
    a = [5, 4, 6, 12, 10, 16]
    b = sorted(a)
    print(b)
    c = quick_sort(a)
    print(c)
  1. dict_operations
# 1.更“保险地”由键获取值
d = {'cat':'cute', 'dog':'furry'}
d['fish'] = 'wet'
c = d.get('fish', 'no')  # 如果存在键'fish',则返回对应的值;如果不存在键'fish',则返回'no'。
del d['fish']
e = d.get('fish', 'no')

print(c)  # wet
print(e)  # no


# 2.字典推导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}"
  1. string_operations
s = "hello"
print(s.capitalize())  # 首字母大写
print(s.upper())  # 全部统一为大写
print(s.rjust(7))  # 字符串右置,为凑够7个字符的长度,不够的以空格补充
print(s.center(7))  # 字符串居中,为凑够7个字符的长度,不够的以空格补充
  1. scipy
from scipy.misc import imread, imsave, imresize

# 1.读取图片
img = imread('./pics/dog.jpg')
print(img.shape)  # (750, 500, 3)
print(img.dtype)  # uint8

# 2.修改数组内容
# 广播机制
img_tinted = img * [1, 0.95, 0.9]  # RGB

# 3.resize
img_resized = imresize(img_tinted, (400, 400))

# 4.保存
imsave('./pics/new_dog.jpg', img_resized)

# ----- 计算集合中 点之间的距离 -----
import numpy as np
from scipy.spatial.distance import pdist, cdist, squareform

x = np.array([[0, 1],[1,0],[2,0]])
# 集合内部
d1 = squareform(pdist(x, 'euclidean'))  # 方形矩阵形式
# d = pdist(x, 'euclidean')
print(d1)

# 集合之间
d2 = squareform(cdist(x, x, 'euclidean'))
print(d2)
  1. matplotlib
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)

# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.
plt.show()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值