ML numpy、pandas、matplotlib的使用
生活随笔
收集整理的這篇文章主要介紹了
ML numpy、pandas、matplotlib的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Numpy的使用
創建Numpy數組
import numpy as np # 創建一維數組 np.array([1, 2, 3]) # 創建二維數組 np.array([[1, 1, 2, 3], [3, 2, 3, 3]]) # 創建數組元素都為1的數組 np.ones(shape=(2, 3)) # 創建數組元素都為5的數組 np.full(shape=(2, 3), fill_value=5) # 創建等差數列數組 np.linspace(1, 100, 20) # 創建步長相等的數組 np.arange(0, 100, step=2)np.random.seed(10) # 設置隨機種子 np.random.randint(0, 100, size=(2, 4)) # 數組中的元素都為整數,范圍在0到100之間 np.random.randn(2, 4) # 創建標準正態分布的數組 創建Numpy數組 查看numpy數組的元素 # 二維數組的查看方式 arr[2][3] = 4arr = np.random.randint(30, 300, size=(4, 5)) # 創建一個4行5列的二維數組 # 取前兩行 arr[0:2] # 取前兩列 arr[:,0: 2]# 查看數組形狀 arr.shape # 改變數組形狀 arr.reshape(2, 10) 查看numpy數組的元素 數組的合并 import matplotlib.pyplot as plt img_arr = plt.imread('F:/1.png') plt.imshow(img_arr) # 把圖片進行縱向合并 arr1 = np.concatenate((img_arr, img_arr), axis = 0) plt.imshow(arr1) # 把圖片進行橫向合并 arr2 = np.hstack((img_arr, img_arr)) plt.imshow(arr2)img = np.split(img_arr, [10, 250], axis=0)[1] img = np.split(img, [770, 970], axis=1)[1] plt.imshow(img) 數組的合并 廣播機制 m = np.ones((2, 3)) # 創建元素都為1的數組 a = np.arange(3) # 創建長度為3的等差數列 m + a # 把a的每個元素對應相加 arr = np.random.randint(0, 100, size=(4, 7)) # 1. 快速排序 np.sort(arr) # 每一行進行排序 np.sort(arr, axis=0) # 每一列進行排序 廣播機制?
Pandas的使用
創建Series
# 使用字典創建 dic = {'math': 100,'english': 90, } Series(data=dic) # 使用numpy創建Series df = DataFrame(data=np.random.randint(30, 40, size=(3, 5)), index=['a', 'b', 'c'], columns=['A', 'B', 'C', 'D', 'E']) Series的創建查看Series
s = Series(data={'a': 'zhangsan', 'b': 'lisi'}) # 查看所有元素 s # 查看某個元素 s['a'] s.loc['a'] Series的查看創建DateFrame
# 使用numpy創建 df = DataFrame(data=np.random.randint(30, 40, size=(3, 5)), index=['a', 'b', 'c'], columns=['A', 'B', 'C', 'D', 'E']) # 使用字典創建 dic = {'java': [60, 70, 80],'python': [90, 100, 100] } df = DataFrame(data=dic, index=['張三', '李四', '王五']) DataFrame的創建查看DateFrame
dic = {'java': [60, 70, 80],'python': [100, 200, 200] } df = DataFrame(data=dic, index=['張三', '李四', '王五']) df['java'] # 按列查看 df.loc['李四'] # 按行查看 df.iloc[1] # 按照行下標查看 查看DataFrameDataFrame關于缺失值的處理
import numpy as np import pandas as pd from pandas import DataFrame, Series np.random.seed(10) df = DataFrame(np.random.randint(50, 200, size=(4, 5)), index=['a', 'b', 'c', 'd'], columns=['a', 's', 'd', 'd', 'f']) df.loc['a', 'a'] = np.nan # 把其中的某些值賦值為np.nan df.iloc[2, 4] = None df.iloc[3, 0] = None df.isnull().any(axis=1) # 查看某一列是否全部為空值 df.notnull().all(axis=1) 處理缺失值Matplotlib的使用
利用公式? x^2 + y^2 < 1,畫一個圓:
import numpy as np import matplotlib.pyplot as pltdata = 2*np.random.rand(10000, 2)-1 # 在-1到1之間顯示 print(data)x = data[:, 0] y = data[:, 1]# 顯示圓形 x^2 + y^2 < 1 其中idx中滿足的點即為true,不滿足的點為false idx = x**2 + y**2 < 1.000 # print(idx) # numpy中很少使用循環,可以直接對數組進行操作 plt.plot(x[idx], y[idx], 'ro', markersize=1) plt.show() 畫一個圓利用公式? x^2 + y^2 < 1, x^2 + y^2<0.5^2,做差值,畫一個圓環:
import numpy as np import matplotlib.pyplot as pltdata = 2*np.random.rand(10000, 2)-1 print(data)x = data[:, 0] y = data[:, 1]# 畫一個圓環 idx = x**2 + y**2 < 1.000 hole = x**2 + y**2 < 0.5**2 idx = np.logical_and(idx, ~hole) plt.plot(x[idx], y[idx], 'ro', markersize=1) plt.show() 圓環直方圖的顯示
import numpy as np import matplotlib.pyplot as pltdata = np.random.random(10000)print(data.shape) # 顯示形狀 np.set_printoptions(edgeitems=5000, suppress=True) plt.hist(data, bins=20, color='r', edgecolor='k') # 屬性:edgecolor為邊界顏色 plt.show() 直方圖中心極限定理
import numpy as np import matplotlib.pyplot as pltN = 10000 times = 100 z = np.zeros(N) for i in range(times):z += np.random.random(N) z /= times plt.hist(z, bins=20, color='b', edgecolor='k') plt.show() 中心極限定理?
轉載于:https://www.cnblogs.com/abc23/p/11020532.html
總結
以上是生活随笔為你收集整理的ML numpy、pandas、matplotlib的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NOIP2017TG D1T2 时间复杂
- 下一篇: [计算机组成原理] Booth算法 ——