Python deep learning "Fluent python" Part 1

Source: Internet
Author: User
Tags abs scalar

Python deep learning "Fluent python" Part 1

From the beginning of last weekend to see this "fluent python", technology is slowly accumulated, Python is slowly to write elegant (pythonic).

Data model

Python Solitaire

import collections# 用来构建一个只有属性,没有方法的简单类,来代表扑克牌的号码和花色。Card = collections.namedtuple('Card', ['rank', 'suit'])class FrenchDeck:    # 扑克牌的号码    ranks = [str(n) for n in range(2,11) + list('JQKA')]    # 扑克牌的花色,分别是黑桃,方块,梅花,红桃    suits = 'spades diamonds clubs hearts'.split()    def __init__(self):        # 单下划线表示私有变量,不希望被外界更改        self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]    def __len__(self):        return len(self._cards)    def __getitem__(self, item):        return self._cards[item]

The focus is on the __len__ method and the __getitem__ method, which is the overloading of the system approach, which adds flexibility to Python.

It should be noted that traversal operations are not always explicit. What if a collection is not implemented? containsmethod, the? In operation will perform sequential traversal operations.

It uses the namedtuple simple record:

Namedtuple

We know that a tuple can represent an immutable collection, for example, a two-dimensional coordinate of a point can be expressed as:

>>> p = (1, 2)

However, it is difficult to see (1, 2) that this tuple is used to represent a coordinate.

Defining a class is a fuss, and namedtuple comes in handy:

>>> from collections import namedtuple>>> Point = namedtuple('Point', ['x', 'y'])>>> p = Point(1, 2)>>> p.x1>>> p.y2

Namedtuple is a function that creates a custom tuple object, specifies the number of tuple elements, and can refer to an element of a tuple using a property rather than an index.

In this way, we can easily define a data type with namedtuple, which has the invariant of tuple and can be referenced according to the attribute, which is very convenient to use.

You can verify that the created point object is a seed class of tuple:

>>> isinstance(p, Point)True>>> isinstance(p, tuple)True

Similarly, if you want to represent a circle with coordinates and radii, you can also define it with Namedtuple:

# namedtuple('名称', [属性list]):Circle = namedtuple('Circle', ['x', 'y', 'r'])

From Liaoche official website

By the way, attach the class memo in the other collections module:

Deque

When using the list to store data, accessing the elements by index is fast, but inserting and deleting elements is slow because list is linear and the data is large, and insertions and deletions are inefficient.

Deque is a two-way list for efficient insert and delete operations, suitable for queues and stacks:

>>> from collections import deque>>> q = deque(['a', 'b', 'c'])>>> q.append('x')>>> q.appendleft('y')>>> qdeque(['y', 'a', 'b', 'c', 'x'])

In addition to implementing the list's Append () and pop (), deque supports Appendleft () and Popleft (), which allows you to add or remove elements to the head very efficiently.

Defaultdict

When using Dict, if the referenced key does not exist, the keyerror is thrown. If you want the key to not exist, return a default value, you can use Defaultdict:

>>> from collections import defaultdict>>> dd = defaultdict(lambda: 'N/A')>>> dd['key1'] = 'abc'>>> dd['key1'] # key1存在'abc'>>> dd['key2'] # key2不存在,返回默认值'N/A'

Note The default value is returned by the calling function, and the function is passed in when the Defaultdict object is created.

In addition to returning a default value when key does not exist, Defaultdict's other behavior is exactly the same as Dict.

Ordereddict

When using Dict, key is unordered. We cannot determine the order of key when we iterate over the dict.

If you want to keep the key in order, you can use Ordereddict:

>>> from collections import OrderedDict>>> d = dict([('a', 1), ('b', 2), ('c', 3)])>>> d # dict的Key是无序的{'a': 1, 'c': 3, 'b': 2}>>> od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])>>> od # OrderedDict的Key是有序的OrderedDict([('a', 1), ('b', 2), ('c', 3)])

Note that the Ordereddict keys are sorted in the order in which they were inserted, not the key itself:

>>> od = OrderedDict()>>> od['z'] = 1>>> od['y'] = 2>>> od['x'] = 3>>> list(od.keys()) # 按照插入的Key的顺序返回['z', 'y', 'x']

Ordereddict can implement a FIFO (first in, out) dict, deleting the first added key when the capacity exceeds the limit:

from collections import OrderedDictclass LastUpdatedOrderedDict(OrderedDict):    def __init__(self, capacity):        super(LastUpdatedOrderedDict, self).__init__()        self._capacity = capacity    def __setitem__(self, key, value):        containsKey = 1 if key in self else 0        if len(self) - containsKey >= self._capacity:            last = self.popitem(last=False)            print('remove:', last)        if containsKey:            del self[key]            print('set:', (key, value))        else:            print('add:', (key, value))        OrderedDict.__setitem__(self, key, value)

Counter

Counter is a simple counter, for example, the number of statistical characters that appear:

>>> from collections import Counter>>> c = Counter()>>> for ch in 'programming':...     c[ch] = c[ch] + 1...>>> cCounter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})

Counter is actually also a subclass of Dict, the above results can be seen, the characters ' G ', ' m ', ' R ' each appeared two times, the other characters appear once.

Mathematical type

Basic vector operations include vector addition, vector modulo, vector and scalar multiplication, and so on. However, we want to use the built-in operations we used to do these operations, so we need to implement some special methods in our custom data Model +?abs?*.

from math import hypotclass Vector:    def __init__(self, x=0, y=0):        self.x = x        self.y = y    def __repr__(self):        return 'Vector(%r, %r)' % (self.x, self.y)    def __abs__(self):        return hypot(self.x, self.y)    def __bool__(self):        return bool(abs(self))    def __add__(self, other):        x = self.x + other.x        y = self.y + other.y        return Vector(x, y)    def __mul__(self, scalar):        return Vector(self.x * scalar, self.y * scalar)

After implementing these special methods, we can use the operators to manipulate our data model directly.

Example

v1 = Vector(2,4)v2 = Vector(2,1)v1 + v2 # Vector(4,5)v = Vector(3,4)abs(v) # 5.0v * 3 # Vector(9, 12)abs(v * 3) # 15.0

It is worth pointing out the repr method, if this method is not implemented. Printing a Vector object directly may output

<Vector object at 0x10e100070>.

When this method is defined, the output becomes quite readable.

Vector(3,4)

Of course, you can also define the behavior of STR from defining STR (x). If you want to implement only one function, it is recommended to implement the repr function, because when there is no str, __repr__ is called as a fallback.

Python deep learning "Fluent python" Part 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.