python_数据分析_numpy、matplotlib_1

代码演示和具体讲解
(array、numpy、ndim、zeros、ones、arange、reshape、randint、mean、median、std、matplotlib、normal、plot、grid)

distances = [10,15,17,26,20]
times = [0.3,0.47,0.55,1.2,1.0]

speeds = []
for i in range(len(distances)):
    speeds.append(distances[i]/times[i])
speeds
'''或者缩写成'''
[d/t for d,t in zip(distances, times)]

sum1 = sum([d/t for d,t in zip(distances, times)])#求和
sum1
#导入numpy模板,生成数组
import numpy as np
distances = np.array(distances)
type(distances)#数据类型
distances

A = np.array([[[1,2],[1,2]],[[3,4],[3,4]]])
A.ndim#确定数组维数

np.zeros(10,dtype=int)#生成一个大小为10的整型一维数组
np.ones(shape=(3,5),dtype=float)#生成一个3行5列的二维数组
np.arange(0,20,3)#生成一个从0开始20结束,步长为3的数组
arrays1 = np.linspace(0,1,10)#生成一个从0开始1结束,大小为10的数组
arrays1.reshape(5,2)#将数组arrays1转换为一个5行2列的二维数组

experiment= np.random.randint(0,2,size=10)#随机从[0,2)中选取一个整数,选取10次
print(experiment)
print(experiment.sum())#输出10次的和

coin_matrix = np.random.randint(0,2,size=(10000,10))
counts = coin_matrix.sum(axis=1)
print(counts[:25])
print(counts.mean())#平均值
print(np.median(counts))#中值
print(counts.min(),counts.max())
print(counts.std())#标准偏差

import matplotlib.pyplot as plt#导入绘图库
returns = np.random.normal(0.001,0.02,250)#此概率分布的均值为0.001,此概率分布的标准差为0.02,大小为250
initial_price = 100
price = initial_price*np.exp(returns.cumsum())#横坐标和纵坐标
plt.plot(price)#绘图
plt.grid();#显示网格

遇到的问题:module ‘matplotlib’ has no attribute 'plot
解决:将导入的包matplotlib改成matplotlib.pyplot即可

你可能感兴趣的:(python,numpy,random,数据分析)