网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python三维函数 python三维数组怎么理解

python三维卷积可以用什么函数? matlab只要用convn

写了一个输入和卷积核dim=2是一样的(都是3)的卷积函数,可以试试多加一个for循环变成三维卷积

创新互联成立与2013年,我们提供高端网站建设、小程序制作、电商视觉设计、成都APP应用开发及网络营销搜索优化服务,在传统互联网与移动互联网发展的背景下,我们坚守着用标准的设计方案与技术开发实力作基础,以企业及品牌的互联网商业目标为核心,为客户打造具商业价值与用户体验的互联网+产品。

def conv3D(image, filter):

'''

三维卷积

:param image: 输入,shape为 [h,w,c], c=3

:param filter:  卷积核,shape为 [x,y,z], z=3

:return:

'''

h, w, c = image.shape

x, y, z = filter.shape

height_new = h - x + 1  # 输出 h

width_new = w - y + 1  # 输出 w

image_new = np.zeros((height_new, width_new), dtype=np.float)

for i in range(height_new):

for j in range(width_new):

r = np.sum(image[i:i+x, j:j+x, 0] * filter[:,:,0])

g = np.sum(image[i:i+y, j:j+y, 1] * filter[:,:,1])

b = np.sum(image[i:i+z, j:j+z, 2] * filter[:,:,2])

image_new[i, j] = np.sum([r,g,b])

image_new = image_new.clip(0, 255)

image_new = np.rint(image_new).astype('uint8')

return image_new

Python怎么生成三维数

1、创建一般的多维数组

import numpy as np

a = np.array([1,2,3], dtype=int)  # 创建1*3维数组   array([1,2,3])

type(a)  # numpy.ndarray类型

a.shape  # 维数信息(3L,)

a.dtype.name   # 'int32'

a.size   # 元素个数:3

a.itemsize  #每个元素所占用的字节数目:4

b=np.array([[1,2,3],[4,5,6]],dtype=int)  # 创建2*3维数组  array([[1,2,3],[4,5,6]])

b.shape  # 维数信息(2L,3L)

b.size   # 元素个数:6

b.itemsize   # 每个元素所占用的字节数目:4

c=np.array([[1,2,3],[4,5,6]],dtype='int16')  # 创建2*3维数组  array([[1,2,3],[4,5,6]],dtype=int16)

c.shape  # 维数信息(2L,3L)

c.size   # 元素个数:6

c.itemsize   # 每个元素所占用的字节数目:2

c.ndim  # 维数

d=np.array([[1,2,3],[4,5,6]],dtype=complex)    #  复数二维数组

d.itemsize  # 每个元素所占用的字节数目:16

d.dtype.name  # 元素类型:'complex128'

2、创建一般的多维数组

import numpy as np

a = np.array([1,2,3], dtype=int)  # 创建1*3维数组   array([1,2,3])

type(a)  # numpy.ndarray类型

a.shape  # 维数信息(3L,)

a.dtype.name   # 'int32'

a.size   # 元素个数:3

a.itemsize  #每个元素所占用的字节数目:4

b=np.array([[1,2,3],[4,5,6]],dtype=int)  # 创建2*3维数组  array([[1,2,3],[4,5,6]])

b.shape  # 维数信息(2L,3L)

b.size   # 元素个数:6

b.itemsize   # 每个元素所占用的字节数目:4

c=np.array([[1,2,3],[4,5,6]],dtype='int16')  # 创建2*3维数组  array([[1,2,3],[4,5,6]],dtype=int16)

c.shape  # 维数信息(2L,3L)

c.size   # 元素个数:6

c.itemsize   # 每个元素所占用的字节数目:2

c.ndim  # 维数

d=np.array([[1,2,3],[4,5,6]],dtype=complex)    #  复数二维数组

d.itemsize  # 每个元素所占用的字节数目:16

d.dtype.name  # 元素类型:'complex128'

3、创建特殊类型的多维数组

 a1 = np.zeros((3,4))    # 创建3*4全零二维数组

输出:

array([[ 0.,  0.,  0.,  0.],

[ 0.,  0.,  0.,  0.],

[ 0.,  0.,  0.,  0.]])

a1.dtype.name   # 元素类型:'float64'

a1.size  # 元素个数:12

a1.itemsize  # 每个元素所占用的字节个数:8

a2 = np.ones((2,3,4), dtype=np.int16)  # 创建2*3*4全1三维数组

a2 = np.ones((2,3,4), dtype='int16')     # 创建2*3*4全1三维数组

输出:

array([[[1, 1, 1, 1],

[1, 1, 1, 1],

[1, 1, 1, 1]],

[[1, 1, 1, 1],

[1, 1, 1, 1],

[1, 1, 1, 1]]], dtype=int16)

a3 = np.empty((2,3))  # 创建2*3的未初始化二维数组

输出:(may vary)

array([[ 1.,  2.,  3.],

[ 4.,  5.,  6.]])

a4 = np.arange(10,30,5)   # 初始值10,结束值:30(不包含),步长:5

输出:array([10, 15, 20, 25])

a5 = np.arange(0,2,0.3)    # 初始值0,结束值:2(不包含),步长:0.2

输出:array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

from numpy import pi

np.linspace(0, 2, 9)   # 初始值0,结束值:2(包含),元素个数:9

输出:

array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])

x = np.linspace(0, 2*pi, 9)

输出:

array([ 0.        ,  0.78539816,  1.57079633,  2.35619449,  3.14159265,

3.92699082,  4.71238898,  5.49778714,  6.28318531])

a = np.arange(6)

输出:

array([0, 1, 2, 3, 4, 5])

b = np.arange(12).reshape(4,3)

输出:

array([[ 0,  1,  2],

[ 3,  4,  5],

[ 6,  7,  8],

[ 9, 10, 11]])

c = np.arange(24).reshape(2,3,4)

输出:

array([[[ 0,  1,  2,  3],

[ 4,  5,  6,  7],

[ 8,  9, 10, 11]],

[[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]]]) 

使用numpy.set_printoptions可以设置numpy变量的打印格式

在ipython环境下,使用help(numpy.set_printoptions)查询使用帮助和示例

4、多维数组的基本操作

加法和减法操作要求操作双方的维数信息一致,均为M*N为数组方可正确执行操作。

a = np.arange(4)

输出:

array([0, 1, 2, 3])

b = a**2

输出:

array([0, 1, 4, 9])

c = 10*np.sin(a)

输出:

array([ 0.        ,  8.41470985,  9.09297427,  1.41120008])

n  35

输出:

array([ True,  True,  True,  True], dtype=bool)

A = np.array([[1,1],[0,1]])

B = np.array([[2,0],[3,4]])

C = A * B    # 元素点乘

输出:

array([[2, 0],

[0, 4]])

D = A.dot(B)   # 矩阵乘法

输出:

array([[5, 4],

[3, 4]])

E = np.dot(A,B)   # 矩阵乘法

输出:

array([[5, 4],

[3, 4]])

多维数组操作过程中的类型转换

When operating with arrays of different types, the type of the

resulting array corresponds to the more general or precise one (a

behavior known as upcasting)

即操作不同类型的多维数组时,结果自动转换为精度更高类型的数组,即upcasting

数组索引、切片和迭代

a = np.ones((2,3),dtype=int)      # int32

b = np.random.random((2,3))     # float64

b += a  # 正确 

a += b  # 错误

a = np.ones(3,dtype=np.int32)

b = np.linspace(0,pi,3)

c = a + b

d = np.exp(c*1j)

输出:

array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,

-0.54030231-0.84147098j])

d.dtype.name

输出:

'complex128'

多维数组的一元操作,如求和、求最小值、最大值等

a = np.random.random((2,3))

a.sum()

a.min()

a.max()

b = np.arange(12).reshape(3,4)

输出:

array([[ 0,  1,  2,  3],

[ 4,  5,  6,  7],

[ 8,  9, 10, 11]])

b.sum(axis=0)    # 按列求和

输出:

array([12, 15, 18, 21])

b.sum(axis=1)    # 按行求和

输出:

array([ 6, 22, 38])

b.cumsum(axis=0)   # 按列进行元素累加

输出:

array([[ 0,  1,  2,  3],

[ 4,  6,  8, 10],

[12, 15, 18, 21]])

b.cumsum(axis=1)   # 按行进行元素累加

输出:

array([[ 0,  1,  3,  6],

[ 4,  9, 15, 22],

[ 8, 17, 27, 38]])

universal functions

B = np.arange(3)

np.exp(B)

np.sqrt(B)

C = np.array([2.,-1.,4.])

np.add(B,C)

其他的ufunc函数包括:

all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor,inner, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var,vdot, vectorize, where

5. 数组索引、切片和迭代

a = np.arange(10)**3

a[2]

a[2:5]

a[::-1] # 逆序输出

for i in a:

print (i**(1/3.))

def f(x,y):

return 10*x+y

b = np.fromfunction(f,(5,4),dtype=int)

b[2,3]

b[0:5,1]

b[:,1]

b[1:3,:]

b[-1]

c = np.array([[[0,1,2],[10,11,12]],[[100,101,102],[110,111,112]]])

输出:

array([[[  0,   1,   2],

[ 10,  11,  12]],

[[100, 101, 102],

[110, 111, 112]]])

c.shape

输出:

(2L, 2L, 3L)

c[0,...]

c[0,:,:]

输出:

array([[ 0,  1,  2],

[10, 11, 12]])

c[:,:,2]

c[...,2]

输出:

array([[  2,  12],

[102, 112]])

for row in c:

print(row)

for element in c.flat:

print(element)

a = np.floor(10*np.random.random((3,4)))

输出:

array([[ 3.,  9.,  8.,  4.],

[ 2.,  1.,  4.,  6.],

[ 0.,  6.,  0.,  2.]])

a.ravel()

输出:

array([ 3.,  9.,  8., ...,  6.,  0.,  2.])

a.reshape(6,2)

输出:

array([[ 3.,  9.],

[ 8.,  4.],

[ 2.,  1.],

[ 4.,  6.],

[ 0.,  6.],

[ 0.,  2.]])

a.T

输出:

array([[ 3.,  2.,  0.],

[ 9.,  1.,  6.],

[ 8.,  4.,  0.],

[ 4.,  6.,  2.]])

a.T.shape

输出:

(4L, 3L)

a.resize((2,6))

输出:

array([[ 3.,  9.,  8.,  4.,  2.,  1.],

[ 4.,  6.,  0.,  6.,  0.,  2.]])

a.shape

输出:

(2L, 6L)

a.reshape(3,-1)

输出:

array([[ 3.,  9.,  8.,  4.],

[ 2.,  1.,  4.,  6.],

[ 0.,  6.,  0.,  2.]])

详查以下函数:

ndarray.shape, reshape, resize, ravel

6. 组合不同的多维数组

a = np.floor(10*np.random.random((2,2)))

输出:

array([[ 5.,  2.],

[ 6.,  2.]])

b = np.floor(10*np.random.random((2,2)))

输出:

array([[ 0.,  2.],

[ 4.,  1.]])

np.vstack((a,b))

输出:

array([[ 5.,  2.],

[ 6.,  2.],

[ 0.,  2.],

[ 4.,  1.]])

np.hstack((a,b))

输出:

array([[ 5.,  2.,  0.,  2.],

[ 6.,  2.,  4.,  1.]])

from numpy import newaxis

np.column_stack((a,b))

输出:

array([[ 5.,  2.,  0.,  2.],

[ 6.,  2.,  4.,  1.]])

a = np.array([4.,2.])

b = np.array([2.,8.])

a[:,newaxis]

输出:

array([[ 4.],

[ 2.]])

b[:,newaxis]

输出:

array([[ 2.],

[ 8.]])

np.column_stack((a[:,newaxis],b[:,newaxis]))

输出:

array([[ 4.,  2.],

[ 2.,  8.]])

np.vstack((a[:,newaxis],b[:,newaxis]))

输出:

array([[ 4.],

[ 2.],

[ 2.],

[ 8.]])

np.r_[1:4,0,4]

输出:

array([1, 2, 3, 0, 4])

np.c_[np.array([[1,2,3]]),0,0,0,np.array([[4,5,6]])]

输出:

array([[1, 2, 3, 0, 0, 0, 4, 5, 6]])

详细使用请查询以下函数:

hstack, vstack, column_stack, concatenate, c_, r_

7. 将较大的多维数组分割成较小的多维数组

a = np.floor(10*np.random.random((2,12)))

输出:

array([[ 9.,  7.,  9., ...,  3.,  2.,  4.],

[ 5.,  3.,  3., ...,  9.,  7.,  7.]])

np.hsplit(a,3)

输出:

[array([[ 9.,  7.,  9.,  6.],

[ 5.,  3.,  3.,  1.]]), array([[ 7.,  2.,  1.,  6.],

[ 7.,  5.,  0.,  2.]]), array([[ 9.,  3.,  2.,  4.],

[ 3.,  9.,  7.,  7.]])]

np.hsplit(a,(3,4))

输出:

[array([[ 9.,  7.,  9.],

[ 5.,  3.,  3.]]), array([[ 6.],

[ 1.]]), array([[ 7.,  2.,  1., ...,  3.,  2.,  4.],

[ 7.,  5.,  0., ...,  9.,  7.,  7.]])]

实现类似功能的函数包括:

hsplit,vsplit,array_split

8.  多维数组的复制操作

a = np.arange(12)

输出:

array([ 0,  1,  2, ...,  9, 10, 11])

not copy at all

b = a

b is a    # True

b.shape = 3,4

a.shape  # (3L,4L)

def f(x)   # Python passes mutable objects as references, so function calls make no copy.

print(id(x))   # id是python对象的唯一标识符

id(a)   # 111833936L

id(b)   # 111833936L

f(a)     # 111833936L

浅复制

c = a.view()

c is a   # False

c.base is a   # True

c.flags.owndata    # False

c.shape = 2,6

a.shape   # (3L,4L)

c[0,4] = 1234

print(a)

输出:

array([[   0,    1,    2,    3],

[1234,    5,    6,    7],

[   8,    9,   10,   11]])

s = a[:,1:3]

s[:] = 10

print(a)

输出:

array([[   0,   10,   10,    3],

[1234,   10,   10,    7],

[   8,   10,   10,   11]])

深复制

d = a.copy()

d is a   # False

d.base is a   # False

d[0,0] = 9999

print(a)

输出:

array([[   0,   10,   10,    3],

[1234,   10,   10,    7],

[   8,   10,   10,   11]])

numpy基本函数和方法一览

Array   Creation

arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r, zeros,zeros_like

Conversions

ndarray.astype, atleast_1d, atleast_2d, atleast_3d, mat

Manipulations

array_split, column_stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, ndarray.item, newaxis, ravel, repeat, reshape, resize,squeeze, swapaxes, take, transpose, vsplit, vstack

Questionsall, any, nonzero, where

Ordering

argmax, argmin, argsort, max, min, ptp, searchsorted, sort

Operations

choose, compress, cumprod, cumsum, inner, ndarray.fill, imag, prod, put, putmask, real, sum

Basic Statistics

cov, mean, std, var

Basic Linear Algebra

cross, dot, outer, linalg.svd, vdot

完整的函数和方法一览表链接:

使用Python画出一个三维的函数图像,数据来自于一个Excel表格?

raw_input获取的输入是字符串,不能直接用np.array,需要用split进行切分,然后强制转化成数值类型,才能用plot函数

我把你的代码稍微修改了一下,可能不太漂亮,不过能运行了

x=[1,2,3]

a

=

raw_input('function')

a

=

a.split('

')#依空格对字符串a进行切分,如果是用逗号分隔,则改成a.split(',')

b

=

[]

for

i

in

range(len(a)):#把切分好的字符强制转化成int类型,如果是小数,将int改为float

b.append(int(a[i]))

plt.plot(x,

b,

label='x',

color="green",

linewidth=1)


文章名称:python三维函数 python三维数组怎么理解
当前网址:http://bjjierui.cn/article/doeoeji.html

其他资讯