py库:numpy

http://www.numpy.org/  numpy官网

http://cwiki.apachecn.org/pages/viewpage.action?pageId=10030181  scikit-learn 0.18 中文文档(暂时先放这儿)

https://www.imooc.com/learn/727  Python在数据科学中的应用,比较基础的视频讲解。讲了python的list,numpy,matplotlib,pandas

http://python.jobbole.com/87471/  Python快速教程 - Numpy和Matplotlib篇

http://www.yiibai.com/numpy/  NumPy教程  广告忒多,(host里屏蔽一下咕咕广告)


pip install  numpy

当前版本是numpy-1.14.0

BMI计算:

numpy.array()是个很重要的方法

import numpy as np
n_height = np.array([1.75, 1.72, 1.68, 1.8, 1.7])
n_weight = np.array([60, 80, 70, 55, 75])
bmi = n_weight / n_height ** 2
print(bmi)
print(bmi[2])

pang = np.array( bmi[bmi > 23] )  # 通过比较运算符,进行元素的选择
print(pang)

二维numpy数组

数组内的元素必须是同类型,否则会被强制转换。因为数据类型一致,所以运算速度快。

import numpy as np
np_2d = np.array([
    [1.75, 1.72, 1.68, 1.8, 1.7],
    [60, 80, 70, 55, 75]
])
print(np_2d)
print(np_2d.shape)  # 2行5列   (2, 5)
print(np_2d[0][2])  # 第一行第三个     1.68
print(np_2d[0, 2])  # 同上         1.68
print(np_2d[:, 1:3])  # 取每行的第2、3列     [[ 1.72  1.68], [80.   70.  ]]
print(np_2d[1, :])  # 取第2行      [60. 80. 70. 55. 75.]
print(np.mean(np_2d[0, :]))  # 先把身高抽取出来,再计算平均身高         1.7299999999999998
print(np.median(np_2d[0, :]))   # 身高中位数(比如5000个人排队,最中间的那个人的身高)  1.72

数据的生成

import numpy as np
height = np.round(np.random.normal(1.75, 0.08, 100), 2)  # 随机生成身高
weight = np.round(np.random.normal(60.00, 10, 100), 2)  # 随机生成体重
np123 = np.column_stack((height, weight))
print(np123)
print(np.max(height), np.min(height)) # 身高最大最小值
print(np.max(weight), np.min(weight)) # 体重最大最小值

numpy数组的创建

  • numpy.empty
  • numpy.zeros
  • numpy.ones
  • numpy.arange(start, stop, step, dtype)
  • numpy.linspace(start, stop, num, endpoint, retstep, dtype)  此函数类似于arange()函数。 在此函数中,指定了范围之间的均匀间隔数量
  • numpy.reshape(arr, newshape, order')  此函数在不改变数据的条件下修改形状
  • ndarray.flatten(order)  此函数返回折叠为一维的数组副本(打平)
import numpy as np

print(np.zeros((3, 4)))            # 生成3行4列的0矩阵
print(np.ones((3, 4), dtype=np.int16))    # 生成3行4列的1矩阵
print(np.arange(10))              # 类似range     [0 1 2 3 4 5 6 7 8 9]
print(np.linspace(0, 60, 5))          # 等差数列,0到60之间,取5个值  [ 0. 15. 30. 45. 60.]
print(np.arange(6).reshape((2, 3)))     # [ [0 1 2]   [3 4 5] ]

 散点图

import numpy as np
import matplotlib.pyplot as plt

height = np.round(np.random.normal(1.75, 0.20, 100), 2)
weight = np.round(np.random.normal(60.32, 15, 100), 2)

plt.scatter(weight, height)  # 散点图
plt.show()

一些属性

import numpy as np

aaa = np.array([
    [9, 2, 3, 4],
    [5, 6, 7, 8]
])
print(aaa.ndim)  # 几维  2
print(aaa.shape)  # 几行几列    (2,4)
print(aaa.size)  # 多少个元素  8
print(aaa.dtype)  # 数值类型  int32
print(aaa.flatten())  # 将数组展平成一维数组    [9 2 3 4 5 6 7 8]

print(aaa.mean())  # 均值     5.5
print(aaa.max())  # 最大值     9

np.save('zz.npy', aaa)     # 保存到文件
q = np.load('zz.npy')    # 从文件读取
print(q)

例子: 直方图

from matplotlib import pyplot as plt
import numpy as np

a = np.array( [98.5, 96, 95, 94.5, 93.5, 94, 86.5, 92.5, 92, 90, 90.5, 95, 91.5, 89, 91, 94, 91, 82, 96.5, 89.5, 88, 82, 82, 84.5, 83.5, 87])
plt.hist(a, bins=[80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])
plt.show() # 学生考试成绩的直方图

...

原文地址:https://www.cnblogs.com/qq21270/p/8293378.html