keyfall / xuexibiji

3 stars 0 forks source link

数据分析 #53

Open keyfall opened 3 years ago

keyfall commented 3 years ago

matplotlib

绘制折线图

from matplotlib import pyplot
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="C:\\Windows\\Fonts\\simfang.ttf")

y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]

x = range(11, 31)

# 设置图形大小
pyplot.figure(figsize=(20, 8), dpi=80)

pyplot.plot(x, y_1, label="自己", color="#F08080")
pyplot.plot(x, y_2, label="同桌", color="#DB7093")

# 设置x轴刻度
_xtick_labels = ["{}岁".format(i) for i in x]
pyplot.xticks(x, _xtick_labels, fontproperties=my_font)
pyplot.yticks(range(0, 9))

# 绘制网格
pyplot.grid(alpha=0.7)

#添加图例
pyplot.legend(prop=my_font, loc="upper left")

# 展示
pyplot.show()

绘制散点图

pyplot.scatter() image

绘制条形图

pyplot.bar() image

绘制直方图

pyplot.hist() image

image

keyfall commented 3 years ago

numpy

image

import numpy as np
import random

t1 = np.array([1, 2, 3])
print(t1)
print(t1)

t2 = np.array(range(10))
print(t2)
print(type(t2))

t3 = np.arange(4, 10, 2)
print(t3)
print(type(t3))

print(t3.dtype)

t4 = np.array(range(1, 4), dtype="i1")
print(t4)
print(t4.dtype)

t5 = np.array([1, 1, 0, 1, 0, 0], dtype=bool)
print(t5)
print(t5.dtype)

#astype转换dtype类型
t6 = t5.astype("int8")
print(t6)
print(t6.dtype)

t7 = np.array([random.random() for i in range(10)])
print(t7)
print(t7.dtype)

#取小数点后两位
t8 = np.round(t7, 2)
print(t8)
keyfall commented 3 years ago

numpy读取数据 image

numpy运算

import numpy as np

t1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(t1)

print(t1.shape)

t2 = np.arange(12)
print("t2:", t2)

t3 = t2.reshape((3, 4))
print("t3:", t3)

print(t3+2)

print(t3*2)

print(t3/2)

print("*"*100)
t4 = np.arange(4)
print(t3-t4)

t5 = np.arange(3).reshape((3, 1))
print(t3-t5)

numpy转置

import numpy as np

t1 = np.arange(24).reshape((4, 6))
print(t1)

print(t1.transpose())

print(t1.T)

print(t1.swapaxes(1, 0))
keyfall commented 3 years ago

记录一下transpose 用于转置的

import numpy as np

t1 = np.arange(24).reshape((2, 3, 4))
print(t1)

t2 = t1.transpose(2, 1, 0)
print(t2)

结果

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

[[[ 0 12]
  [ 4 16]
  [ 8 20]]

 [[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]]

transpose(x,y,z)中的x,y,z表示3维(2维就是0,1) 对应的是reshape的维数,比如上面是3维的,transpose就是3维 x,y,z分别对应的是0,1,2 如果2,1,0了,就是把x和z换位置,对应到修改t1的reshape就变成了reshape((4, 2, 3))了

keyfall commented 3 years ago

numpy取值

import numpy as np

t1 = np.array(range(70)).reshape(7, 10)

print(t1)

print("*"*100)

# #取行
# print(t1[2])
# print("*"*100)
#
# #取连续多行
# print(t1[2:])
#
# print("*"*100)
#
# #取不连续多行
# print(t1[[2, 4, 6]])

print(t1[:, 0])
print("*"*100)

#取连续的多列
print(t1[:, 2:])
print("*"*100)

#取不连续的多列
print(t1[:, [0, 2, 6]])
print("*"*100)

#取多个不相邻的点
print(t1[[0, 2], [0, 1]])
keyfall commented 3 years ago

更多索引方式

import numpy as np

t1 = np.array(range(70)).reshape(7, 10)

print(t1)

print("*"*100)

#t1中每个值是否小于10
print(t1 < 10)
print("*"*100)

#t1中小于10的赋予3
t2 = t1
t2[t2<10]=3
print(t2)
print("*"*100)

# t1中大于20的
t3 = t1[t1 > 20]
print(t3)
print("*"*100)

# numpy三元运算符where 小于30的换成100,大于30的换成300
print(np.where(t1<=30, 100, 300))
print("*"*100)

# clip裁剪 小于20的换成20,大于40的换成40
print(t1.clip(20, 40))