第一章:基础知识
1. PyCharm
WIN7报错缺少文件:https://blog.csdn.net/lingaixuexi/article/details/80992542
2. 整除算法:// , 次幂 **
eg: 10//3 = 3 , 2 ** 3 = 8
3. input print 输入输出
4. import 导入模块
eg: import math 使用 floor math.floor(32.9) = 32 (取整)
5. pycharm不识别turtle下的解决方法
https://blog.csdn.net/qq_24504591/article/details/81907888
6. Pycharm调用Turtle时 窗口一闪而过
https://blog.csdn.net/Jamesjjjjj/article/details/80850669
7. 简单海龟绘图法
from turtle import *
import turtle
forward(100)
left(120)
forward(100)
left(120)
forward(100)
turtle.done()
8. Python 库参考手册(turtle文档)
https://docs.python.org/3/library/turtle.html
9. 字符转义
(1) 反斜杠 \
(2) 三引号 """ 或者 '''
(3) 前缀 r eg: r 'lets \ go ' 注:原始字符串不能以单个反斜杠结尾
10. 第一章结束 P21
11. 序列
切片:
numbers[0:10:2]. 开始位置,结束位置,步长。eg:步长为2 间隔一个选一个。
初始化: [none] * 10 长度为10的空序列
连接: +。不能拼接不同类型序列。
12. 矩阵
引入 numpy
from numpy import *;
import numpy as np;
a1 = array([1,2,3])
a2 = zeros((3,3),int);
a3 = mat(ones((2,4),int))
a4 = mat(random.rand(2,2))
a5 = mat(random.randint(1,10000,size=(3,3),dtype=long))
a6 = mat(eye(2,2,dtype=int))
a7 = mat(diag(a1))
a1 = mat([1,2])
a2 = mat([[1],[2]])
a3 = mat([1,2])
a4 = a3 * 2;
a5 = mat(eye(2,2) * 0.5)
a6 = mat([[1,2],[0,0]])
a7 = a6.getT();
a1 = mat([[1,2],[5,4],[3,6]])
#print(a1)
a2 = a1.sum(axis=1,dtype=int)
a4 = np.max(a1,0)
#print(a4)
a = mat(ones((3,3),int))
b = a[1:,1:]
c = mat([[1,2],[1,2]])
print(hstack((b,c)).dtype)
13.逻辑回归
https://baijiahao.baidu.com/s?id=1628902000717534995&wfr=spider&for=pc
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
data = np.array([[10,3,9,1],[9,1,7,1],[4,0,5.5,0],[6,1,8,1]])
st = StandardScaler()
data_std = st.fit_transform(data[:,:3])
lr = linear_model.LogisticRegression()
lr.fit(data_std,data[:,3])
#print(lr.coef_)
#print(lr.intercept_)
θ1 = lr.coef_[0][0]
θ2 = lr.intercept_
plt.scatter(data_std[:,0],data_std[:,1])
plt.plot(data_std[:,0],θ1*data_std[:,0]+θ2)
plt.show()
14.zip lambda map
a = [1,2,3]
b = [4,5,6]
print(list(zip(a,a,b)))
for i,j in zip(a,b):
print(i/2, j*2)
def fun1(x,y):
return (x+y)
fun2 = lambda x,y: x+y
print(fun1(1,2), fun2(2,5))
print(list(map(fun1, [1,2], [2,5])))
输出结果如下:
[(1, 1, 4), (2, 2, 5), (3, 3, 6)]
0.5 8
1.0 10
1.5 12
3 7
[3, 7]
15. try exception
16. copy(浅复制) deepcopy(深复制)
17. pickle 存储数据
18. set 找不同