h3

机器学习第一节

1,面向对象初步了解简单语法

# -*- coding:utf-8 -*-
# 定义一个类
class Test:
    name = 'rcl'
    age = 22
    def add(self, a, b):
        print(self.name)
        res = a+b
        print(res)
    def minus(self, a, b):
        print(self.age)
        res = a -b
        print(res)

test = Test()
test.add(10,20)
test.minus(20,10)
print(test.name)
print(test.age)


class Test1:
    name = 'rcl'
    age = 12

    def __init__(self, name, age, height, weight):
        self.name = name
        self.age = age
        self.height = height
        self.weight = weight
    def add(self, a, b):
        print(self.name)
        res = a + b
        print(res)
test1 = Test1('aa', 22, 33, 44)
test1.add(22, 33)
print(test1.height)
View Code

2,try except 错误异常

# -*- coding:utf-8 -*-
# 捕获错误异常  try except

try:
    open('a.txt', 'r', encoding='utf-8')
except Exception as e:
    print(e) #[Errno 2] No such file or directory: 'a.txt'
View Code

3,map() 使用定义函数迭代处理参数, zip()合并两个单元

# -*- coding:utf-8 -*-
# zip 练习,将两个列表合到一起,单元为元组,分别放进去
list1 = [1,2,3]
list2 = [4,5,6]
# 也可以将多个元素合并到一起
list3 = zip(list1, list2)
print(list3) #<zip object at 0x00000000010BEF08>
print(list(list3)) #[(1, 4), (2, 5), (3, 6)]

for i,j,z in zip(list1,list1,list2):
    print(i/2, j*2, z+2)


def func(a,b):
    return (a+b)

res = map(func,[1,2,3],[4,5,6]) #1与4运算,2与5,3与6
print(list(res))
View Code

4,multiprocessing 多进程处理

5,threading 多线程

posted @ 2017-05-24 23:10  码上平天下  阅读(91)  评论(0)    收藏  举报