人工智能作业2022.7.3

1、如果列表a表示10点到12点的每一分 钟的气温如何绘制折线图观察每分钟气温的变化情况?

a= [random.randint(20,35) for i in range(120)]

from matplotlib import pyplot as plt
import random

from matplotlib import font_manager

# 设置中文
my_font = font_manager.FontProperties(fname="C:WINDOWSFONTSSIMHEI.TTF")

x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]

# 设置图片大小,分辨80
plt.figure(figsize=(20, 8), dpi=80)
# 绘制x,y
plt.plot(x, y)
# 调整x轴的刻度
_x = list(x)
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i - 60) for i in range(60, 120)]
# 取步长,数字和字符串一一对应,数据的长度一样,旋转45度
plt.xticks(_x[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)
# 添加描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度 单位(℃)", fontproperties=my_font)
plt.title("10点到12点每分钟的气温变化情况", fontproperties=my_font)
# 保存图片
plt.savefig("./t1.png")
# 展示
plt.show()

2、垂直堆叠数组a和数组b。

  • a = np.arange(10).reshape([2, -1])
  • b = np.repeat(1, 10).reshape([2, -1])
import numpy as np

a = np.arange(10).reshape([2, -1])
b = np.repeat(1, 10).reshape([2, -1])

print(a)
# [[0 1 2 3 4]
#  [5 6 7 8 9]]
print(b)
# [[1 1 1 1 1]
#  [1 1 1 1 1]]

# 方法1
print(np.concatenate([a, b], axis=0))
# [[0 1 2 3 4]
#  [5 6 7 8 9]
#  [1 1 1 1 1]
#  [1 1 1 1 1]]

# 方法2
print(np.vstack([a, b]))
# [[0 1 2 3 4]
#  [5 6 7 8 9]
#  [1 1 1 1 1]
#  [1 1 1 1 1]]

3、从功能上看,melt方法应当属于wide_to_long的一种特殊情况,即stubnames只有一类。请使用wide_to_long生成melt一节中的df_melted。(提示:对列名增加适当的前缀)

df = pd.DataFrame({'Class':[1,2],
                   'Name':['San Zhang', 'Si Li'],
                   'Chinese':[80, 90],
                   'Math':[80, 75]})

import pandas as pd
df = df.rename(columns={'Chinese': 'pre_Chinese', 'Math': 'pre_Math'})
pd.wide_to_long(df,
                stubnames=['pre'],
                i=['Class', 'Name'],
                j='Subject',
                sep='_',
                suffix='.+').reset_index().rename(columns={'pre': 'Grade'})

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>