numpy作业03

练习1:

在讲解 Matplotlib 的时候,我们使用以下代码绘制分组条形图。其中讲解到,三根柱子的位置需要同时往左或往右移动时,需要使用到列表推导式。实际上,duck不必,请使用numpy的所学来优化我们该部分代码。

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)

# 构建x,height
fruits = ["苹果","梨子","车厘子"]
Q1_sales = [1000,800,3000]
Q2_sales = [1200,700,2800]

# 柱子的对应索引位置 [0,1,2]
# 设置柱子宽度
width = 0.35
# rects1 = plt.bar(list(range(len(fruits))),Q1_sales,width)
# rects2 = plt.bar(list(range(len(fruits))),Q2_sales,width)

# 蓝黄柱子宽度都为0.35 怎么使蓝色显示左边  黄色显示右边
# 位置左移width/2  位置-width/2    [0-width/2,1-width/2,2-width/2]
po_1 = np.arange(3)-0.35/2          
#po_l = [i-width/2 for i in list(range(len(fruits)))]
plt.bar(po_1,Q1_sales,width,label="Q1")

po_1 = np.arange(3)-0.35/2                    
#po_r = [i+width/2 for i in list(range(len(fruits)))]
plt.bar(po_2,Q2_sales,width,label="Q2")

# 设置图例 
plt.legend()

# 定义auto_label函数来设置标签
def auto_label(po,sales):
    for x,y in zip(po,sales):
        plt.annotate(y,(x,y),(x-0.1,y+100))
    

# 数据标签
auto_label(po_1,Q1_sales)
auto_label(po_2,Q2_sales)

plt.xticks(list(range(len(fruits))),fruits)

plt.show()

  • 对列表推导式部分的优化
po_1 = np.arange(3)-0.35/2          
#po_l = [i-width/2 for i in list(range(len(fruits)))]
po_1 = np.arange(3)-0.35/2                    
#po_r = [i+width/2 for i in list(range(len(fruits)))]
  • 图形
    在这里插入图片描述
练习2 请完成以下基础练习:
  • np.arange(16).reshape(4,4)与2做减法
  • np.arange(16).reshape(4,4)与np.arange(16,32).reshape(4,4)做加法运算
  • np.arange(8).reshape(2,4)与np.arange(4)运算吗?
  • np.arange(8).reshape(2,4)与np.arange(4).reshape(1,4)运算吗?
  • np.arange(8).reshape(2,4)与np.arange(4).reshape(4,1)运算吗?
arr1 = np.arange(16).reshape(4,4)-2
print(arr1)  # 数组中的每一个元素都减2

arr2 = np.arange(16).reshape(4,4)-np.arange(16,32).reshape(4,4)
print(arr2)  # 对位相减

arr3 = np.arange(8).reshape(2,4)-np.arange(4)
print(arr3)  # 可以运算,符合广播机制  (2,4)与(1,4)

arr4 = np.arange(8).reshape(2,4)-np.arange(4).reshape(1,4)
print(arr4)   # 可以

arr5 = np.arange(8).reshape(2,4)-np.arange(4).reshape(4,1)
print(arr5)   # ValueError: operands could not be broadcast together with shapes (2,4) (4,1)  不符合广播机制
  • 结果如下
    [[-2 -1 0 1]
    [ 2 3 4 5]
    [ 6 7 8 9]
    [10 11 12 13]]
    [[-16 -16 -16 -16]
    [-16 -16 -16 -16]
    [-16 -16 -16 -16]
    [-16 -16 -16 -16]]
    [[0 0 0 0]
    [4 4 4 4]]
    [[0 0 0 0]
    [4 4 4 4]]
练习3

请完成以下基础练习:

  • 构建数组: np.arange(16).reshape(4,4)
  • 选择 第3列
  • 选择 1-3 列
  • 选择 2,4 列
  • 选择 4,7 两个点 将值改为 0
  • 过滤出 <5 的值
arr6 = np.arange(16).reshape(4,4) 
print(arr6)

# 选择第三列
print(arr6[:,2])

# 选择1-3列
print(arr6[:,0:3])

# 选择 2,4 列
print(arr6[:,(1,3)])

# 选择 4,7 两个点 将值改为 0
arr6[1,0] = 0
arr6[1,3] = 0
print(arr6)

# 过滤出 <5 的值
print(arr6[arr6 < 5])
  • 结果如下
    [[ 0 1 2 3]
    [ 4 5 6 7]
    [ 8 9 10 11]
    [12 13 14 15]]
    [ 2 6 10 14]
    [[ 0 1 2]
    [ 4 5 6]
    [ 8 9 10]
    [12 13 14]]
    [[ 1 3]
    [ 5 7]
    [ 9 11]
    [13 15]]
    [[ 0 1 2 3]
    [ 0 5 6 0]
    [ 8 9 10 11]
    [12 13 14 15]]
    [0 1 2 3 0 0]
练习4
  • 筛选出 成绩 大于60 并且 小于80 的数据
  • 筛选出 成绩 大于80 并且 小于90 的数据
  • 筛选出 成绩 大于90 的数据
score_data = np.loadtxt("scores.csv",delimiter=",",skiprows=1)
score_data
print(score_data[(score_data>60) & (score_data< 80) ])
print(score_data[(score_data>80) & (score_data< 90) ])
print(score_data[(score_data>90) ])
  • 结果如下
    [73. 62. 73. 67. 65. 79. 69. 72. 73. 77. 71. 74. 77. 67.]
    [83. 82. 89. 88. 87. 81.]
    [93. 98. 99. 96. 93. 96. 95.]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值