import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
np.__version__
'1.19.5'
x = np.linspace(0, 2*np.pi, num=100) # 生成等差数列 [起始, 终止, 个数)
siny = np.sin(x)
cosy = np.cos(x)
plt.plot(x, siny)
plt.plot(x, cosy)
plt.show()
from myUtils.algo1 import Moead
m = Moead()
Moead()被调用
笔记+代码的形式
ndarray概述
Numpy最重要的一个特点就是其N维数组对象(即ndarray),该对象是一个快速而灵活的大数据集容器。 你可以利用这种数组对整块的数据执行一些数学运算。
ndarray是一个通用的同构数据多维容器,其中的所有元素必须是相同类型的。 每个数组都有一个shape(表示各维度大小的元组)和一个dtype(表示数组数据类型的对象):
我们将会介绍Numpy数组的基本用法,虽然说大多数数据分析工作不需要深入理解Numpy, 但精通面向数组的编程和思维方式是成为Python科学计算牛人的一大关键步骤。
注意: 我们将依照标准的Numpy约定,即总是使用import numpy as np. 当然你也可以为了不写np,而直接在代码中使用from numpy import *, 但是建议你最好还是不要养成这样的坏习惯。
- np.array函数创建数组
np.array([1, 23, 3])
array([ 1, 23, 3])
np.array([i for i in range(10)])
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a = np.array(list("adadsa"))
a
array(['a', 'd', 'a', 'd', 's', 'a'], dtype='<U1')
a.dtype
dtype('<U1')
np.array([[1, 23, 3], [1, 23, 3]])
array([[ 1, 23, 3],
[ 1, 23, 3]])
type(np.array([[1, 23, 3], [1, 23, 3]]))
numpy.ndarray
特殊创建numpy数组的方式
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((3, 4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
np.zeros((3, 4, 2))
array([[[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]]])
arr1 = np.array([[1, 23, 3], [1, 23, 3]])
arr1
array([[ 1, 23, 3],
[ 1, 23, 3]])
z2 = np.zeros_like(arr1)
z2
array([[0, 0, 0],
[0, 0, 0]])
z3 = np.zeros(arr1.shape) # numpy 都会有shape属性 几行几列
z3
array([[0., 0., 0.],
[0., 0., 0.]])
z2.dtype # numpy 都会有dtype属性 代表数据存储的类型
dtype('int64')
z3.dtype
dtype('float64')
arr4 = np.array([1, 23, 3], dtype=np.float32)
arr4
array([ 1., 23., 3.], dtype=float32)
arr4.dtype
dtype('float32')
np.ones(3)
array([1., 1., 1.])
np.ones(shape=(3,4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.ones_like(arr4)
array([1., 1., 1.], dtype=float32)
np.empty(shape=(3, 5)) #是不确定的值
array([[0. , 0. , 0.4472136 , 0.0531494 , 0.18257419],
[0.4472136 , 0.2125976 , 0.36514837, 0.4472136 , 0.4783446 ],
[0.54772256, 0.4472136 , 0.85039041, 0.73029674, 0.4472136 ]])
- arange()
list(range(1, 11, 2))
[1, 3, 5, 7, 9]
np.arange(1, 11, .2) # 步长可以是小数
array([ 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4, 2.6, 2.8, 3. ,
3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8, 5. , 5.2,
5.4, 5.6, 5.8, 6. , 6.2, 6.4, 6.6, 6.8, 7. , 7.2, 7.4,
7.6, 7.8, 8. , 8.2, 8.4, 8.6, 8.8, 9. , 9.2, 9.4, 9.6,
9.8, 10. , 10.2, 10.4, 10.6, 10.8])
numpy.ndarray具有的属性
x = np.arange(1, 16)
x # 向量vector
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
X = x.reshape((5, -1)) # -1代表自动推导, 不关心他
X # 矩阵 matrix
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15]])
x.dtype
dtype('int64')
x.shape
(15,)
X.shape
(5, 3)
x.ndim
1
X.ndim
2
x.size # 元素个数
15
X.size
15
- 使用astype函数转换数组类型
x.astype(np.float64)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,
14., 15.])
x
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x[3] = 8.8
x
array([ 1, 2, 3, 8, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x = x.astype(np.float64)
x[3] = 8.8
x
array([ 1. , 2. , 3. , 8.8, 5. , 6. , 7. , 8. , 9. , 10. , 11. ,
12. , 13. , 14. , 15. ])
访问和切片
x = np.arange(1, 16)
x
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
X = x.reshape((5, -1)) # -1代表自动推导, 不关心他
X # 矩阵 matrix
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15]])
x[3]
4
# X[行索引, 列索引]
X[2, 1]
8
X[2][1]
8
X[2, 1] = 99
X
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 99, 9],
[10, 11, 12],
[13, 14, 15]])
# 切片
x[1:5]
array([2, 3, 4, 5])
# X[行切片, 列切片]
X[3:, 1:]
array([[11, 12],
[14, 15]])
np.random.random((3, 4)) # 随机浮点数 [0, 1)
array([[0.32531763, 0.97286683, 0.13605365, 0.87378722],
[0.72856123, 0.01021173, 0.57008147, 0.96456312],
[0.63267244, 0.92675942, 0.37005066, 0.64050234]])
np.random.permutation(10)
array([1, 8, 6, 3, 5, 0, 9, 2, 7, 4])
np.random.permutation([23, 44, 55, 58]) # 随机排列
array([44, 58, 55, 23])
np.random.uniform(1, 10, 10) # 均匀分布的实数
array([2.17115861, 4.17886714, 4.00635544, 9.16631729, 3.89123296,
6.43859691, 9.21098333, 5.03591329, 1.90708684, 5.01710854])
np.random.normal(0, 1, 100) # 均值为 0 标准差1符合正太分布的数据100个样本
array([ 0.80874503, -0.16567968, -0.31064738, -0.22550773, 0.37932698,
0.83040908, -0.88427209, -0.99335963, 0.46813964, 0.37654572,
0.54520782, 1.37794139, -0.71043162, -0.57482279, 0.17595226,
1.47860665, -1.8603091 , -1.19547577, 0.60714864, -0.03280815,
0.47140451, -0.56777245, 0.79433052, -0.40138726, -0.07828743,
2.88221181, 1.33928883, 0.30186451, 0.35865335, -0.07563646,
-1.76584554, -1.15809066, 1.44621492, -0.64537123, 0.16925881,
0.68916059, 0.18941259, 0.15551734, 1.26532886, 0.01213274,
1.17924091, -0.00296781, 0.75804901, 0.04251398, -1.49625579,
-0.58042222, -1.70624751, 0.61857695, 0.73404283, 0.59031571,
-2.13722523, 0.92116394, 0.86172934, 0.48948469, -0.3456609 ,
-0.19461676, 1.29702127, -1.3135996 , -0.97560705, -1.03925359,
-0.13371475, -0.0483729 , 2.015915 , 0.85217527, 0.08686993,
1.95539724, -0.47070355, 0.24233953, -0.39236531, -0.48292054,
-1.76334204, 2.20154387, 1.1988139 , -0.58434844, -0.00473182,
-0.36896969, 1.5937328 , -0.54054604, -0.81957493, 0.5234989 ,
-1.83004188, -0.47820093, 1.47228562, -1.70552894, -0.86291653,
1.21401267, -0.03302287, -0.89393133, 0.90492114, 0.32176851,
0.55286362, 1.10050826, 0.01913985, 0.45115204, -0.00295445,
0.61936714, 0.27970513, -0.63907155, 0.6369553 , 0.63814795])
from random import randint, random
randint(1, 2) # suo
2
random()
0.2193982462858688
Z = np.random.randint(1, 100, (10, 2))
# Z.astype(np.ra)
Z
array([[92, 58],
[31, 17],
[38, 84],
[ 4, 56],
[20, 12],
[49, 97],
[ 3, 84],
[67, 5],
[92, 74],
[24, 99]])
Z[:,0] = Z[:,0]/100
Z
array([[ 0, 62],
[ 0, 59],
[ 0, 65],
[ 0, 39],
[ 0, 21],
[ 0, 8],
[ 0, 47],
[ 0, 15],
[ 0, 89],
[ 0, 27]])
正太分布
mu, sigma = 100, 15
x = mu + sigma*np.random.normal(0, 1, 10000)
x.shape
(10000,)
# 直方图 数据的分布
plt.hist(x,100, facecolor='g')
plt.show()
x = np.linspace(1, 100, 10)
y = 2*x + 10 + np.random.normal(0, 30, 10)
plt.scatter(x, y)
plt.show()
x = np.random.normal(0, 1, 10000)
y = np.random.normal(0, 1, 10000)
plt.scatter(x, y, alpha=0.1) # 设置透明度 0~1 越小约透明
plt.show()