numpy
import numpy as np
l1 = [1, 2, 3, 4, 5]
l2 = np.array(l1)
print(type(l2))
print(l2)
print(l2.ndim, l2.shape)
l2 = np.array([3, 4, 5])
print(l2)
l2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(l2.ndim, l2.shape)
print(l2)
l2 = np.array([[[1, 2, 1], [3, 4, 2]], [[5, 6, 3], [7, 8, 4]]])
print(l2.shape)
l3 = l2.reshape(2, 6)
print(l3)
l4 = l2.reshape(1, 2, 2, 3)
print(l4)
l5 = np.arange(1, 11).reshape(5, 2)
print(l5)
l5 = np.arange(1, 11, 2)
print(l5)
D:\Anaconda\anaconda\envs\tf\python.exe D:\pycharm\python\day5\numpyy.py
<class 'numpy.ndarray'>
[1 2 3 4 5]
1 (5,)
[3 4 5]
2 (3, 3)
[[1 2 3]
[4 5 6]
[7 8 9]]
(2, 2, 3)
[[1 2 1 3 4 2]
[5 6 3 7 8 4]]
[[[[1 2 1]
[3 4 2]]
[[5 6 3]
[7 8 4]]]]
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]]
[1 3 5 7 9]
进程已结束,退出代码0
numpy 运算
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10], [11, 12]]).reshape(2, 3)
C = A * B
print(C)
C = A - B
print(C)
C = A + B
print(C)
C = A / B
print(C)
C = np.sum(A, axis=1)
print(C)
C = np.sum(A, axis=0)
print(C)
print("---------------")
a = np.array([1, 2, 3, 4, 5]).reshape(5, 1)
b = np.array([6, 7, 8, 9, 10]).reshape(5, 1)
c = np.hstack((a, b))
print(c)
c = np.vstack((a, b))
print(c)
D:\Anaconda\anaconda\envs\tf\python.exe D:\pycharm\python\day5\numpy运算.py
[[ 7 16 27]
[40 55 72]]
[[-6 -6 -6]
[-6 -6 -6]]
[[ 8 10 12]
[14 16 18]]
[[0.14285714 0.25 0.33333333]
[0.4 0.45454545 0.5 ]]
[ 6 15]
[5 7 9]
---------------
[[ 1 6]
[ 2 7]
[ 3 8]
[ 4 9]
[ 5 10]]
[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
进程已结束,退出代码0
numpy 索引切片
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a, a[2], a[2:5])
b = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]])
print(b, b[0, 2], b[1, 3])
print(b[1, 2:])
print(b[:, 1::2])
print("-----------")
c = np.arange(24).reshape(3, 2, 4)
print(c)
print(c[:, 0, 1:3])
D:\Anaconda\anaconda\envs\tf\python.exe D:\pycharm\python\day5\numpy索引切片.py
[1 2 3 4 5 6] 3 [3 4 5]
[[1 2 3 4 5]
[6 7 8 9 0]] 3 9
[8 9 0]
[[2 4]
[7 9]]
-----------
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
[[16 17 18 19]
[20 21 22 23]]]
[[ 1 2]
[ 9 10]
[17 18]]
matplotlib 绘图
import random
import matplotlib.pyplot as plt
import numpy as np
"""
# 1.绘制直线
"""
x = np.array([1.2, 5.8, 9])
y = np.array([1.0, 8.5, 10])
plt.plot(x, y, c='b', lw=2)
plt.show()
"""
# 1.绘制散点(3, 3)
"""
x = np.random.normal(0, 5, 1000)
y = np.random.uniform(0, 2, 1000)
plt.scatter(x[0:500], y[0:500], s=50, c='g', marker='*', linewidths=3)
plt.scatter(x[500:], y[500:], s=50, c='b', marker='o')
plt.show()
"""
# 2.绘制正弦线
"""
x = np.arange(0, 6, 0.001)
y = np.sin(x)
plt.plot(x, y)
plt.show()
"""
3.多子图绘制
"""
plt.subplot(221)
x = np.array([2, 4])
y = np.array([3, 8])
plt.plot(x, y)
plt.subplot(222)
x = np.arange(0, 6, 0.01)
y = np.cos(x)
plt.plot(x, y)
plt.subplot(223)
x = np.array([2, 4, 6])
y = np.array([8, 10, 12])
plt.scatter(x, y)
plt.subplot(224)
x = np.array([0, 8, 9.8])
y = np.array([0, 8, 9.8])
plt.plot(x, y)
plt.show()
"""
4.绘制柱状图
"""
x_dat = ("jan", "feb", "mar", "apr", "may")
x_index = np.arange(len(x_dat))
y1 = (20, 10, 15, 18, 25)
y2 = (8, 25, 13, 10, 12)
plt.bar(x_index, height=y1, width=0.4, color='g', alpha=0.4, label="revenue")
plt.bar(x_index + 0.4,height=y2, width=0.4, color='r', alpha=0.4, label="expense")
plt.legend()
plt.xticks(x_index + 0.2, x_dat)
plt.show()





opencv图像处理,以及人脸识别
import cv2
"""
1.加载本地图片并显示
"""
img = cv2.imread("1.png")
print(type(img))
print(img.shape)
cv2.imshow("src", img)
if cv2.waitKey() == ord("q"):
print("按下的按键为q")
cv2.destroyAllWindows()
"""
2.图片的变换
"""
img = cv2.imread("1.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
gray = cv2.resize(src=gray, dsize=(640, 480))
print(gray.shape)
cv2.imwrite("g.png", gray)
cv2.imshow("gray", gray)
if cv2.waitKey() == ord("q"):
cv2.destroyAllWindows()
"""
3.图片的截取 点(36宽,15高) 框架( 101宽,42高)
"""
img = cv2.imread("1.png")
image = img[15:15+42, 36:36+101, :]
cv2.imshow("src", image)
if cv2.waitKey() == ord("q"):
print("按下的按键为q")
cv2.destroyAllWindows()
"""
4.操作摄像头
"""
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("摄像头打开失败")
exit(-1)
while True:
ret, img = cap.read()
if len(img) == 0:
print("没有图片信息,图片读取失败")
cap.release()
exit(-1)
cv2.imshow("src", img)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
"""
5.人脸检测
"""
path = "haarcascade_frontalface_default.xml"
classifier = cv2.CascadeClassifier(path)
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("摄像头打开失败")
exit(-1)
while True:
ret, img = cap.read()
if len(img) == 0:
print("没有图片信息,图片读取失败")
cap.release()
exit(-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
face = classifier.detectMultiScale(gray)
if len(face) != 0:
print(face)
for f in face:
x, y, w, h = f
cv2.rectangle(img, pt1=(x, y), pt2=(x+w, y+h),thickness=1, color=(122, 100, 30))
cv2.circle(img, center=(x+w//2, y+h//2), radius=w//2, color=(100, 133, 200))
cv2.imshow("src", img)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()