py庫:numpy

http://www.numpy.org/  numpy官網python

http://cwiki.apachecn.org/pages/viewpage.action?pageId=10030181  scikit-learn 0.18 中文文檔(暫時先放這兒)apache

 

https://www.imooc.com/learn/727  Python在數據科學中的應用,比較基礎的視頻講解。講了python的list,numpy,matplotlib,pandas數組

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

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

 

 

 

 

 


 

pip install  numpy函數

當前版本是numpy-1.14.0spa

 

BMI計算:code

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

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])  # 取每行的第二、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() # 學生考試成績的直方圖

 

 

 

 

 

 

 

 

 

 

...

相關文章
相關標籤/搜索