Numpy的基本操作

2021-08-16 17:33:13 字數 4286 閱讀 5167

# coding: utf-8

# in[1]:

import pandas

import numpy

# python中的list和array的不同之處

# python中的list是python的內建資料型別,array封裝在numpy中;

#list中的資料類不必相同的,而array的中的型別必須全部相同;

#在list中的資料型別儲存的是資料的存放的位址,簡單的說就是指標,並非資料,這樣儲存乙個list就太麻煩了,例如list1=[1,2,3,'a']需要4個指標和四個資料,增加了儲存和消耗cpu。numpy中封裝的array有很強大的功能,裡面存放的都是相同的資料型別

# in[4]:

numbers = numpy.array([1, 2, 3, 4])

print(numbers)

numbers.dtype

# in[5]:

# 如何取資料

#a = world_alcohol[2, 2]

# in[7]:

# 如何切片 單行中取某幾個數

vector = numpy.array([3, 4, 5, 6, 7])

print(vector[0:3]) #左閉右開的切片方式 只能取到第0個,第1個, 第二個

# in[13]:

# 如何切片 矩陣中取某一列

matrix = numpy.array([

[5, 15, 10], #注意錯誤別漏了逗號:list indices must be integers or slices, not tuple

[20, 25, 30],

[35, 40, 50]

])print(matrix[:,2]) #冒號選擇所有的行,選擇第3列

print(matrix[:, 0:2])#冒號選擇所有的行,選擇第1列&第2列

matrix == 15 #判斷

# in[18]:

# numpy計算問題

# 判 斷

vector = numpy.array([5, 10 , 15, 20])

equal_to_10 = vector == 10 #也可以當作索引

print(equal_to_10)

equal_to_10.dtype

# in[19]:

print(vector[equal_to_10]) #把索引用陣列傳入找出那個數

# in[27]:

# 矩陣中判斷同理

matrix = numpy.array([

[5, 15, 10], #注意錯誤別漏了逗號:list indices must be integers or slices, not tuple

[20, 25, 30],

[35, 40, 50]

])equal_to_25 = matrix == 25

print (equal_to_25)

print (matrix[equal_to_25, :])

# in[28]:

# 與或的操作 & |

vector = numpy.array([5, 10, 15, 20])

equal_to_ten_and_five = (vector == 5) & (vector == 10)

print(equal_to_ten_and_five) #沒有乙個數可以同時滿足等於5又等於10

# in[29]:

equal_to_ten_and_five = (vector == 5) | (vector == 10)

print(equal_to_ten_and_five)

# in[34]:

# 可以轉換資料型別

vector = numpy.array(["1", "2", "3"])

print(vector)

print(vector.dtype)

vector = vector.astype(float)

print(vector)

print(vector.dtype)

# in[35]:

# 可以求極值

print(vector.min())

# in[36]:

# 調出幫助檔案 numpy.array()

help(numpy.array)

# in[39]:

# 求和:比如按行或者按列

matrix = numpy.array([

[5, 15, 10], #注意錯誤別漏了逗號:list indices must be integers or slices, not tuple

[20, 25, 30],

[35, 40, 50]

])print(matrix.sum(axis = 0)) #按列求和,每一列的和

print(matrix.sum(axis = 1)) #按行求和,每一行的和

# in[43]:

#生成乙個隨機的15個數字

import numpy as np

print(np.arange(15))

# in[44]:

a = np.arange(15).reshape(3, 5) # 三行五列

a# in[46]:

a.shape #列印行與列

# in[47]:

a.ndim

# in[48]:

a.size

# in[50]:

np.zeros((3, 4)) #初始化乙個空矩陣,注意元組格式,讓它所有的3行4列為0, 預設為float型別

# in[51]:

np.ones((2, 3, 4), dtype = np.int32) #指定內部為int型別

# in[54]:

np.arange(10, 40, 5) #按步長生成隨機的數

# in[56]:

np.arange(12).reshape(4, 3) #可以進行reshape操作

# in[59]:

## 重要, 隨機模組

# 首先呼叫列np.random模組,然後呼叫了模組內的random函式,得到的是乙個矩陣,這裡是2行3列的矩陣

# 預設為區間為 -1 ~ +1

np.random.random((2, 3)) #2行3列隨機數字

# in[60]:

# linspace

from numpy import pi

np.linspace(0, 2*pi, 100) #得到指定區間(0 ~ 2*pi(pi等於3.14)),在這個區間取100個數字,間隔是一樣的

# in[63]:

np.sin(np.linspace(0, 2*pi, 20))

# #數**算

# 數**算

# in[64]:

# 科普一下 python中range函式 與 numpy包中的arange函式

# range( start, stop[,step])

# start 計數開始,預設從0開始,例如range(5) 等價於 range(0, 5)

# 函式返回乙個 range object

range(5)

# in[65]:

range(0, 5)

# in[66]:

c = [i for i in range(0, 5)] #從0到4開始,不包括5,預設間隔為1,range中間隔不能使用float

c# in[68]:

# numpy包中的arange()函式,

# arange([start,] stop[, step,], dtype = none)

# 根據start與stop指定的範圍以及step設定的步長,生成乙個ndarray型別

np.arange(3)

# in[72]:

np.arange(3, 7)

# in[73]:

np.arange(3, 7, 2)

# in[75]:

# 計算

a = np.array([20, 30, 40, 50])

b = np.arange(4) # 其實就是返回 array([0, 1, 2, 3])

c = a - b # 對應相減操作

print(a)

print(b)

print(c)

# in[76]:

# 求平方的操作

print(b**2)

# in[ ]:

# 做乙個矩陣的乘法 點乘還是算內積

numpy基本操作

算數運算子 等這些運算子為元素集,也就是說,它們只用於位置相同的元素之間,所得到的運算結果組成乙個新陣列 矩陣積 dot,表示矩陣乘機,表示對應元素相乘 np.dot a,b a.dot b 通用函式,通常叫做ufunc,它對陣列中的每個元素逐一進行操作,這表明,通用函式分別處理輸入陣列中的每個元素...

Numpy基本操作

索引 合併 分割 c a b c a b c a b c b 2 c 10 np.sin a c dot np.dot a,b 叉積 np.sum a np.min a np.max a np.sum a,axis 1 0行1列 np.min a,axis 0 np.max a,axis 1 np....

Numpy的基本操作(四)

有志者自有千計萬計,無志者只感千難萬難。用途 生成乙個指定型別的陣列 用法一 import numpy as np v np.arange 5 從0開始生成5個數作為陣列元素,步長為1 print v 執行結果 0 1 2 3 4 用法二 import numpy as np v np.arange...