IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 作业--numpy简单操作练习合集 -> 正文阅读

[Python知识库]作业--numpy简单操作练习合集

以下是顺序开始的numpy操作练习,水平欠佳,请多包涵。

import numpy as np
a = np.array([1,1,1,1])
b = np.array([[1],[1],[1],[1]])
print(a+b)
c = np.array([[1,1,1,1]])
print(c+b)

运行结果(不必要)

[[2 2 2 2]
 [2 2 2 2]
 [2 2 2 2]
 [2 2 2 2]]
[[2 2 2 2]
 [2 2 2 2]
 [2 2 2 2]
 [2 2 2 2]]
import numpy as np
W = np.array([[1,1,1],[2,2,2]])
#array[1,2]
W[:,1]
#array[2,2,2]
W[:,1] = np.array([5,5])
print(W)

运行结果

[[1 5 1]
 [2 5 2]]
import numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
p1 = np.delete(matrix, 1, 0)
print('>>>>p1>>>>\n',p1)
p2 = np.delete(matrix, 1, 1)
print('>>>>p2>>>>\n',p2)
p3 = np.delete(matrix, 1)
print('>>>>p3>>>>\n',p3)
p4 = np.delete(matrix, [0,1], 1)
print('>>>>p4>>>>\n',p4)

运行结果

>>>>p1>>>>
 [[ 1  2  3  4]
 [ 9 10 11 12]]
>>>>p2>>>>
 [[ 1  3  4]
 [ 5  7  8]
 [ 9 11 12]]
>>>>p3>>>>
 [ 1  3  4  5  6  7  8  9 10 11 12]
>>>>p4>>>>
 [[ 3  4]
 [ 7  8]
 [11 12]]
import numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
m1 = np.append(matrix,[[1,1,1,1]],axis=0)
print('>>>>m1>>>>\n',m1)
m2 = np.append(matrix,[[1],[1],[1]],axis=1)
print('>>>>m2>>>>\n',m2)
m3 = np.append(matrix,[1,1,1,1])
print('>>>>m3>>>>\n',m3)

运行结果

>>>>m1>>>>
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [ 1  1  1  1]]
>>>>m2>>>>
 [[ 1  2  3  4  1]
 [ 5  6  7  8  1]
 [ 9 10 11 12  1]]
>>>>m3>>>>
 [ 1  2  3  4  5  6  7  8  9 10 11 12  1  1  1  1]
import numpy as np
a1 = np.random.choice(7,5)
print(a1)
a2 = np.random.choice([0,1,2,3,4,5,6],5)
print(a2)
a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5)
print(a3)
a4 = np.random.choice([0,1,2,3,4,5,6],5,replace=False)
print(a4)
a5 = np.random.choice(np.array([0,1,2,3,4,5,6]),5,p=[0.1,0.1,0.1,0.1,0.1,0.1,0.4])
print(a5)

运行结果

[2 5 3 4 4]
[5 2 6 6 5]
[0 1 0 3 6]
[6 4 1 2 0]
[4 3 6 3 2]
import numpy as np
a = np.array([[1,1,1],[2,2,2],[0,3,6]])
print(a)
b1 = np.argmax(a)
print(b1)
b2 = np.argmax(a, axis=0)
print(b2)
b3 = np.argmax(a, axis=1)
print(b3)

运行结果

[[1 1 1]
 [2 2 2]
 [0 3 6]]
8
[1 2 2]
[0 0 2]
import numpy as np
y1 = np.linspace(-10.0,10.0)
print(y1)
y2 = np.linspace(1,10,10)
print(y2)
y3 = np.linspace(1,10,10,endpoint=False)
print(y3)
y4= np.linspace(1, 10, 6, retstep=True)
print(y4)

运行结果

[-10.          -9.59183673  -9.18367347  -8.7755102   -8.36734694
  -7.95918367  -7.55102041  -7.14285714  -6.73469388  -6.32653061
  -5.91836735  -5.51020408  -5.10204082  -4.69387755  -4.28571429
  -3.87755102  -3.46938776  -3.06122449  -2.65306122  -2.24489796
  -1.83673469  -1.42857143  -1.02040816  -0.6122449   -0.20408163
   0.20408163   0.6122449    1.02040816   1.42857143   1.83673469
   2.24489796   2.65306122   3.06122449   3.46938776   3.87755102
   4.28571429   4.69387755   5.10204082   5.51020408   5.91836735
   6.32653061   6.73469388   7.14285714   7.55102041   7.95918367
   8.36734694   8.7755102    9.18367347   9.59183673  10.        ]
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
[1.  1.9 2.8 3.7 4.6 5.5 6.4 7.3 8.2 9.1]
(array([ 1. ,  2.8,  4.6,  6.4,  8.2, 10. ]), 1.8)
import numpy as np
x = np.array([[1,2,3],[4,5,6],[1,2,3]])
x.flatten()
print(x)
x.ravel()
print(x)
x.ravel('F')
print(x)
x.flatten('F')
print(x)
x.flatten()[1] = 20
print(x)
x.ravel()[1] = 20
print(x)
x.reshape(1,-1)
print(x)
x = np.array([1,2,3,6,7,8])
x[None,:] 
print(x)
x[:,None]
print(x)
x[np.newaxis, :]
print(x)

运行结果

[[1 2 3]
 [4 5 6]
 [1 2 3]]
[[1 2 3]
 [4 5 6]
 [1 2 3]]
[[1 2 3]
 [4 5 6]
 [1 2 3]]
[[1 2 3]
 [4 5 6]
 [1 2 3]]
[[1 2 3]
 [4 5 6]
 [1 2 3]]
[[ 1 20  3]
 [ 4  5  6]
 [ 1  2  3]]
[[ 1 20  3]
 [ 4  5  6]
 [ 1  2  3]]
[1 2 3 6 7 8]
[1 2 3 6 7 8]
[1 2 3 6 7 8]
import numpy as np
x = np.array([[1,2,3],[2,3,4]])
np.prod(x)
print(x)
np.prod(x,axis=1)
print(x)
np.prod(x,axis=0)
print(x)

运行结果

[[1 2 3]
 [2 3 4]]
[[1 2 3]
 [2 3 4]]
[[1 2 3]
 [2 3 4]]
import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
y1 = np.maximum(0,x)
y2 = np.minimum(0,x)
x1 = x.copy()
x1[x1 < 0] = 0
x2 = x.copy()
x2[x2 > 0] = 0
print(x,y1,y2,x1,x2)

运行结果

[[ 1  2  3]
 [-3  2  4]
 [ 5 -2  9]] [[1 2 3]
 [0 2 4]
 [5 0 9]] [[ 0  0  0]
 [-3  0  0]
 [ 0 -2  0]] [[1 2 3]
 [0 2 4]
 [5 0 9]] [[ 0  0  0]
 [-3  0  0]
 [ 0 -2  0]]
import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x1 = x.copy()
x2 = x 
x2[x2>0] = 0
x3 = x[2] 
x3[2] = 100
print(x,x1,x2,x3)

运行结果

[[  0   0   0]
 [ -3   0   0]
 [  0  -2 100]] [[ 1  2  3]
 [-3  2  4]
 [ 5 -2  9]] [[  0   0   0]
 [ -3   0   0]
 [  0  -2 100]] [  0  -2 100]
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
np.zeros_like(x)
print(x)

运行结果

[[1 2 3]
 [4 5 6]]
import numpy as np
n = np.random.rand(3,4)
print(n)

运行结果

[[0.68349442 0.24978251 0.66088426 0.62908655]
 [0.3180283  0.84395154 0.47903128 0.85330565]
 [0.17018147 0.32764189 0.49378254 0.02179647]]
import numpy as np
x = np.random.randn(2,3)
y = np.multiply(0.1,np.random.randn(2,3))+0.5 
print(x,y)

运行结果

[[-0.07092933  1.3036258   0.52427686]
 [-0.31677738  0.27788768 -1.01646193]] [[0.67346834 0.43525842 0.38115849]
 [0.66065731 0.3363384  0.5394227 ]]
import numpy as np
z = np.random.randint(2,9,(2,3))
m = np.random.randint(9,size = (2,3))
print(z,m)

运行结果

[[7 5 4]
 [7 5 5]] [[3 6 2]
 [6 2 0]]

#判别

x = 'You are right'
type(x)



str
assert type(x)==str, 'x is not str'
x = [1,2,3]
type(x)
list
assert type(x)==str, ‘x is not str’
Traceback (most recent call last):
  File "<ipython-input-29-7613f9660e71>", line 1
    assert type(x)==str, ‘x is not str’
                          ^
SyntaxError: invalid character in identifier
import numpy as np

A = np.arange(95,99).reshape(2,2)

b = np.array([[[1,2],[3,4]],[[3,4],[7,8]],[[4,5],[1,2]]])
print(A,b)

运行结果

[[95 96]
 [97 98]] [[[1 2]
  [3 4]]

 [[3 4]
  [7 8]]

 [[4 5]
  [1 2]]]
import numpy as np
x = np.empty([3,2], dtype = int)
print (x)

运行结果

[[1587560704        607]
 [         0          0]
 [         1    6881396]]
import numpy as np
c = np.array([[1,2],[3,4]])
c.astype(np.float32)
print(c)

运行结果

[[1 2]
 [3 4]]
import numpy as np
x = np.array([1,3,5])
y = np.array([4,6])
XX,YY = np.meshgrid(x,y)
print(XX,YY)

运行结果

[[1 3 5]
 [1 3 5]] [[4 4 4]
 [6 6 6]]
import numpy as np
x = np.array([[3,4,5],[1,3,4]])
y = np.array([[1,1,1],[2,2,2]])
np.hstack((x,y)) 
print(x,y)
np.vstack((x,y)) 
print(x,y)

运行结果

[[3 4 5]
 [1 3 4]] [[1 1 1]
 [2 2 2]]
[[3 4 5]
 [1 3 4]] [[1 1 1]
 [2 2 2]]
import numpy as np
a = np.array([0.125,0.568,5.688])
np.round(a) 
#np.round(a,decimals = 2) 
#array([0.12, 0.57, 5.69])
#np.floor(a) 
#array([0., 0., 5.])
#np.ceil(a) 
#array([1., 1., 6.])

运行结果

array([0., 1., 6.])
 import numpy as np
 c = np.array([1,2,5,4])
 c[:,np.newaxis]
print(c)

运行结果

[1 2 5 4]
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
a = np.array([[1,2,3,6],[4,5,6,6]])
a1 = a.reshape((1,2,4))
b = np.array([[3,4,5,6],[1,2,3,4],[4,5,5,5]])
b1 = b.reshape((1,3,4)).transpose((1,0,2))
print(a,b,a1,b1,a1+b1)

运行结果

[[1 2 3 6]
 [4 5 6 6]] [[3 4 5 6]
 [1 2 3 4]
 [4 5 5 5]] [[[1 2 3 6]
  [4 5 6 6]]] [[[3 4 5 6]]

 [[1 2 3 4]]

 [[4 5 5 5]]] [[[ 4  6  8 12]
  [ 7  9 11 12]]

 [[ 2  4  6 10]
  [ 5  7  9 10]]

 [[ 5  7  8 11]
  [ 8 10 11 11]]]
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
a[0:7:2]

运行结果

array([2, 3, 5, 6])
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
a[0::2]

运行结果

array([2, 3, 5, 6])
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
a[::-1]

运行结果

array([7, 6, 5, 5, 4, 3, 2, 2])
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
s = slice(0,7,2)
a[s]

运行结果

array([2, 3, 5, 6])
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-09-04 17:28:29  更:2021-09-04 17:30:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 12:16:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码