Numpy简介
创建
一般有三种创建方式:
1.从python的基础数据对象转化
import numpy as np
a=[1,2,3,4]
x1=np.array(a)
x1
Out[5]: array([1, 2, 3, 4])
type(x1)
Out[6]: numpy.ndarray
2.通过Numpy内生成的函数生成
x=np.arange(11)
x
Out[10]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
3.从硬盘文件读取数据
#第三种方法读取数据
#日期,开盘价,最高价,最低价,收盘价,日期,交易金额
#读取文件
x=np.loadtxt('000001.csv',delimiter=',',skiprows=1,usecols=(1,4,6),unpack=False)#delimi代表以什么分割 skiprows跳过第一行(第一行没数据) usecols(用哪几列) unpack=Flase()
x
Out[15]:
array([[3.25863000e+03, 3.35052000e+03, 5.31352384e+10],
[3.33080000e+03, 3.35145000e+03, 5.01661696e+10],
[3.32665000e+03, 3.37395000e+03, 3.91918880e+10],
[3.37196000e+03, 3.29346000e+03, 3.71131168e+10],
[3.27697000e+03, 3.28541000e+03, 4.10240864e+10],
[3.25821000e+03, 3.22932000e+03, 3.22064672e+10],
[3.22354000e+03, 3.23530000e+03, 2.30725760e+10],
[3.24234000e+03, 3.22244000e+03, 2.40190752e+10],
[3.22407000e+03, 3.33646000e+03, 2.82546240e+10],
[3.34360000e+03, 3.37650000e+03, 3.39876768e+10],
[3.18973000e+03, 3.11635000e+03, 4.01098784e+10],
[3.11456000e+03, 3.17305000e+03, 3.57080800e+10],
[3.18909000e+03, 3.32361000e+03, 4.10956032e+10],
[3.32732000e+03, 3.34334000e+03, 3.53382976e+10],
[3.35710000e+03, 3.35176000e+03, 3.66249248e+10],
[3.34726000e+03, 3.38318000e+03, 3.17540992e+10],
...........................................................
ndarray基本操作
c=np.arange(11)
c
Out[23]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
c+c
Out[24]: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
cc*2
c*2
Out[26]: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
c[3]
Out[27]: 3
c[-1]
Out[28]: 10
c[0:5]
Out[29]: array([0, 1, 2, 3, 4])
c[:5]
Out[30]: array([0, 1, 2, 3, 4])
c[:]
Out[31]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
c[::2]
Out[32]: array([ 0, 2, 4, 6, 8, 10])
c[::3]
Out[33]: array([0, 3, 6, 9])
c[::-1]
Out[34]: array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
常用函数
cc=np.random.randint(1,100,10)
c
Out[37]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
cc
Out[38]: array([53, 98, 44, 41, 79, 29, 73, 16, 71, 58])
#生成10个数,都在1-100中取样。
np.min(c)
Out[40]: 0
np.min(cc)#求最小值
Out[41]: 16
np.max(cc)#求最大值
Out[42]: 98
np.mean(cc)#求均值
Out[43]: 56.2
#排序
x=np.random.randint(1,100,10)
x
Out[46]: array([68, 30, 57, 90, 89, 37, 93, 58, 47, 56])
y=np.sort(x)
y
Out[48]: array([30, 37, 47, 56, 57, 58, 68, 89, 90, 93])
#使用sort()排序方法 x本身是没受影响的
#使用np.sort()排序方法 y本身是没受影响的,
y=x.sort()
y
#用ndarray自带的方法排序 y没有值 x是变化的
x
Out[54]: array([30, 37, 47, 56, 57, 58, 68, 89, 90, 93])
Matplotlib学习
六种基本图形的画法 快速入门
散点图
import numpy as np
import matplotlib.pyplot as plt
height=[161,170,182,175,173,156]
weight=[50,70,82,75,73,56]
plt.scatter(height,weight)
plt.show()

import numpy as np
import matplotlib.pyplot as plt
# height=[161,170,182,175,173,156]
# weight=[50,70,82,75,73,56]
#
# plt.scatter(height,weight)
# plt.show()
N=1000
x=np.random.randn(N)
y=np.random.randn(N)
plt.scatter(x,y)
plt.show()

N=1000
x=np.random.randn(N)
y=x+np.random.randn(N)*0.5
#相关性
plt.scatter(x,y)
plt.show()

例子 股票
open,close=np.loadtxt('000001.csv',delimiter=',',skiprows=1,usecols=(1,4),unpack=True)
change=close-open#收盘价-开盘价
change.shape
Out[58]: (242,)
change
Out[59]:
array([ 9.1890e+01, 2.0650e+01, 4.7300e+01, -7.8500e+01, 8.4400e+00,
-2.8890e+01, 1.1760e+01, -1.9900e+01, 1.1239e+02, 3.2900e+01,
-7.3380e+01, 5.8490e+01, 1.3452e+02, 1.6020e+01, -5.3400e+00,
3.5920e+01, -3.6890e+01, -1.9980e+01, 3.3000e+00, -6.3390e+01,
-1.9840e+01, 4.8820e+01, -3.8690e+01, -1.1468e+02, -4.4180e+01,
3.1610e+01, 5.1100e+01, 1.1940e+01, 1.5460e+01, 1.7020e+01,
1.6220e+01, 1.6030e+01, -2.7640e+01, 7.6210e+01, 1.3470e+01,
3.5600e+00, -5.4650e+01, 1.5350e+01, -1.5610e+01, -6.8500e+00,
7.8100e+01, -3.0200e+00, 1.3100e+00, 3.4510e+01, 1.3420e+01,
5.8140e+01, 3.3250e+01, 6.6800e+01, 6.2500e+00, 3.0240e+01,
4.7630e+01, -1.1600e+00, -2.0220e+01, 4.0160e+01, 4.9700e+00,

plt.scatter(yesterday,today,s=100,c='r' ,marker='<' ,alpha=0.5)#s代表面积#marker代表形状 c代表颜色 aplpha代表透明度
Out[70]: <matplotlib.collections.PathCollection at 0x165a20fbcc8>
plt.show()

折线图
折线图是用直线将各数据连接起来组成的图形
常用来观察数据随时间变化的趋势
例如股票价格、温度变化、等等
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-10,10,5)
y=x**2
plt.plot(x,y)
plt.show()

date,open,close=np.loadtxt('000001.csv',delimiter=',',converters={0:mdates.bytespdate2num('%m/%d/%Y')},skiprows=1,usecols=(0,1,4),unpack=True)
plt.plot(date,open)
plt.show()

plt.plot_date(date,open,'-')#改变线的形状
plt.show()

plt.plot_date(date,open,'-',color='red',marker='<')
plt.plot_date(date,close,'-',color='green',marker='<')
plt.show()

条形图
import numpy as np
import matplotlib.pyplot as plt
N=5
y=[20,10,30,25,15]
index=np.arange(N)
#left 改为x就不报错了
pl=plt.bar(x=index,height=y)
plt.show()

N=5
y=[20,10,30,25,15]
index=np.arange(N)
pl=plt.bar(x=0,bottom=index,height=0.5,color='red',width=y,orientation='horizontal')
plt.show()

index=np.arange(4)
sales_BJ=[52,55,63,53]
sales_SH=[44,66,55,41]
bar_width=0.3
plt.bar(index,sales_BJ,bar_width,color='b')
plt.bar(index+bar_width,sales_SH,bar_width,color='r')
plt.show()

plt.bar(index,sales_BJ,bar_width,color='b')
plt.bar(index,sales_SH,bar_width,color='r',bottom=sales_BJ)
plt.show()

直方图
由一系列高度不等的纵向条形组成,表示数据分布的情况
例如某年级同学身高分布情况
import numpy as np
import matplotlib.pyplot as plt
mu = 100 # mean of distribution均值
sigma = 20 # standard deviation of distribution标准差
x = mu + sigma * np.random.randn(2000)#随机函数生成大小为2000的数组
#hist函数 bins有几个直方 color默认是blue,normed对数组进行标准化
plt.hist(x, bins=10,color='red',normed=True)
plt.show()

x = np.random.randn(1000)+2
y = np.random.randn(1000)+3
#双变量直方图
plt.hist2d(x, y, bins=40)
plt.show()

饼状图
# Some data
#标签
labels = 'A', 'B', 'C', 'D'
#数据
fracs = [15, 30, 45, 10]
#aspect 1比1
plt.axes(aspect=1)
plt.pie(x=fracs,labels=labels,autopct='%.0f%%')

# Some data
#标签
labels = 'A', 'B', 'C', 'D'
#数据
fracs = [15, 30, 45, 10]
#aspect 1比1 shadow 阴影 explode距离圆心
plt.axes(aspect=1)
explode = (0, 0.05, 0, 0)
plt.pie(x=fracs,labels=labels,autopct='%.0f%%',explode=explode,shadow=True)

箱线图
箱型图又称为盒须图,是一种用作显示一组数据分散情况资料的统计图。
上边缘,上四分位数,中位数,下四分位数,下边缘,异常值。
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(100)
data = np.random.normal(size=1000, loc=0.0, scale=1.0)
plt.boxplot(data,sym='o',whis=1.5)
plt.show()
data = np.random.normal(size=(100, 4), loc=0.0, scale=1.0)
labels = ['A','B','C','D']
plt.boxplot(data, labels=labels)
plt.show()

#可以在同一张图内画几个数据
#4*1000数组 4个数组 每个数组有1000个数据
# 参数loc(float):正态分布的均值,对应着这个分布的中心。loc=0说明这一个以Y轴为对称轴的正态分布,
# 参数scale(float):正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线越高瘦。
# 参数size(int 或者整数元组):输出的值赋在shape里,默认为None。
data = np.random.normal(size=(1000, 4), loc=0.0, scale=1.0)
labels = ['A','B','C','D']
plt.boxplot(data, labels=labels)
plt.show()


本文详细介绍Numpy的创建、基本操作及常用函数,并通过实例展示如何利用Matplotlib绘制折线图、条形图、直方图、饼状图和箱线图,深入解析数据可视化技巧。
526

被折叠的 条评论
为什么被折叠?



