189 8069 5689

python分段三维函数,python求分段函数

python 绘制三维图形、三维数据散点图

1. 绘制3D曲面图

创新互联建站,为您提供网站建设公司网站制作公司、网站营销推广、网站开发设计,对服务成都玻璃钢坐凳等多个行业拥有丰富的网站建设及推广经验。创新互联建站网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!

from matplotlib import pyplot as plt

import numpy as np

from mpl_toolkits.mplot3d import Axes3D

fig=plt.figure()

ax=Axes3D(fig)

x=np.arange(-4,4,0.25)

y=np.arange(-4,4,0.25)

x,y=np.meshgrid(x,y)

r=np.sqrt(x**2, y**2)

z=np.sin(r)

//绘面函数

ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=“rainbow”

plt.show()

2.绘制三维的散点图(表述一些数据点分布)

4a.mat数据地址:http blog.csdn.net/eddy_zhang/article/details/50496164

from matplotlib import pyplot as plt

import scipy.io as sio

from mpl_toolkits.mplot3d import Axes3D

matl=‘4a.mat’

data=sio.loadmat(matl)

m=data[‘data’]

x,y,z=m[0],m[1],m[2]

//创建一个绘图工程

ax=plt.subplot(111,project=‘3D’)

//将数据点分成三部分画,在颜色上有区分度

ax.scatter(x[:1000], y[:1000], z[:1000],c=‘y’ )//绘制数据点

ax.scatter(x[1000:4000], y[1000:4000], z[1000:4000],c=‘r’ )//绘制数据点

ax.scatter(x[4000:], y[4000:], z[4000:],c=‘g’ )//绘制数据点

ax.set_zlable(‘z’)//坐标轴

ax.set_ylable(‘y’)//坐标轴

ax.set_xlable(‘x’)

plt.show()

python编程这个怎么弄?

分段函数的代码用python实现如下:

x=eval(input('输入x的值:'))

if x!=0:

y=1/(2*x-1)

else:

y=0

print(y)

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三维卷积可以用什么函数? matlab只要用convn

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

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画出一个三维的函数图像,数据来自于一个Excel表格?

可以的。 python利用matplotlib这个库,先定义一个空图层,然后声明x,y,z的值,x,y,z赋相应的列的值,最后建立标签,标题即可。最后,excel安装运行python的插件,运行python。

如何用python编写一个求分段函数的值的程序

1、首先打开python的编辑器软件,编辑器的选择可以根据自己的喜好,之后准备好一个空白的python文件:

2、接着在空白的python文件上编写python程序,这里假设当x>1的时候,方程为根号下x加4,当x-1时,方程为5乘以x的平方加3。所以在程序的开始需要引入math库,方便计算平方和开方,之后在函数体重写好表达式就可以了,最后调用一下函数,将结果打印出来:

3、最后点击软件内的绿色箭头,运行程序,在下方可以看到最终计算的结果,以上就是python求分段函数的过程:


文章标题:python分段三维函数,python求分段函数
网页URL:http://cdxtjz.cn/article/dsgjehh.html

其他资讯