从第九章开始---
------------------------------------------------------------------------
类
定义类
class Network( ):
def __init__(self, sizes): 相当于是构造函数 要有self ,但是不传入值
self.num_layers = len(sizes)
self.sizes = sizes 类似于属性
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]
def feedforward(self, a): 一个方法,如果要用类的属性,必须先传入self
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a)+b)
return a
类的实例
nw=Network(5) 5是给sizes用的
类的继承
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender
定义Student类时,只需要把额外的属性加上,例如score:
class Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score
一定要用 super(Student, self).__init__(name, gender) 去初始化父类,否则,继承自 Person 的 Student 将没有 name 和 gender。
------------------------------------------------------------------------
文件
路径 open (r'C:\Users\DBT\Downloads\01.txt') 其中的r在有些时候要加上,不然报错
写入文件
附加 'a' 写入 'w'
------------------------------------------------------------------------
异常 try except
------------------------------------------------------------------------
数据可视化 matplotlib
import matplotlib.pyplot as plt
s=[1,2,3,4,5]
plt.plot(s)
plt.show()
修饰plt.plot(s,linewidth=5)
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------