外包 | “numpy“ Assignment

外包 | “numpy” Assignment

相关资源下载

资源下载

Q3

用到的数据就一个csv文件
第一列: 记载着日期时间, 365天每天24小时的数据, 也就是有 365*24=8760 条数据, 不过是乱序的
第二列: 记载着某一天某个小时的气温
其他列: 用不到

作业要求: pure numpy to do it
在这里插入图片描述
 

Q3 - 1

找出数据的标题信息,并打印索引信息

# 用numpy 读取csv, 因为csv本质也是文本文件, 和txt一样
with open('./weatherHistory 2016.csv', 'r', encoding='utf-8') as f:
    data = np.loadtxt(f, dtype=str,delimiter = ",")
f.close()
data

在这里插入图片描述

# 根据索引打印信息
for index, title in enumerate(data[0][1:]):
    print(f'{index} {title}')

在这里插入图片描述
 

Q3 - 2

2.找出所有数据中最高气温和最低气温,以及对应的日期;

allTemp = data[:, 1][1:].astype(np.float16)
maxIndex = allTemp.argmax()
minIndex = allTemp.argmin()
maxTemp = allTemp[maxIndex]
minTemp = allTemp[minIndex]
maxDate = data[:, 0][1:][maxIndex]
minDate = data[:, 0][1:][minIndex]
print(f'最高气温:{maxTemp}, 日期:{maxDate}\n最低气温:{minTemp}, 日期:{minDate}')

 

Q3 - 3

在这里插入图片描述

  1. 整理时间, 对时间进行排序: 将字符类型时间转成时间戳, 然后根据时间戳大小从小到大进行排序

    # 处理时间
    import time
    newData = data.copy()[1:, :2]
    for i in range(len(newData)):
        newData[i][0] = time.mktime(time.strptime(newData[i][0][:19], "%Y-%m-%d %H:%M:%S"))
    # 根据第一列时间戳大小进行排序
    newData = newData[np.argsort(newData[:,0])]
    
  2. 将一天24h的数据重整到一行, 每一行就24列, 最后得到一个 366*24 的矩阵

    # 重整数组
    numDay = int(len(newData) / 24)
    reshapeTempArray = np.reshape(newData[:,1], (numDay, 24)).astype(np.float64)
    
  3. 绘图

    import matplotlib.pyplot as plt
    x = [i for i in range(366)]
    maxData = np.max(reshapeTempArray, axis=1)
    minData = np.min(reshapeTempArray, axis=1)
    avgData = np.average(reshapeTempArray, axis=1)
    plt.plot(x, maxData, label='max', c='blue')
    plt.plot(x, minData, label='min', c='orange')
    plt.plot(x, avgData, label='avg', c='green')
    plt.legend()
    plt.savefig('./figure1.png')
    plt.show()
    

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值