成都创新互联网站制作重庆分公司

c语言实现高斯核函数,c++高斯函数

用C语言实现瑞利分布,莱斯分布,高斯分布的分布函数

C语言中的random函数可以产生均匀分布的随机变量分布区间为(0,1),假设x1,x2是由random产生的随机变量,

专注于为中小企业提供成都网站建设、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业济源免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

则y=sqrt(-2*ln(x1))为瑞利分布

theta=2*pi*x2为(0,2*pi)的均匀分布

n1=y*cos(theta),n2=y*sin(theta)为两个独立的正太分布

z=sqrt((a+n1)^2+(b+n2)^2),为莱斯分布,a ,b为常数

用matlab编写一个高斯核密度函数

1、你是怎样调用的?照理说,如果是正常的调用,例如

yanyan(1.5,1:10,.1)

应该会出现Matrix must be square的提示,而不会是too many  input。

2、vectorize不是这样用的,它只能把一个char类型的表达式或sym、inline对象给替换成点运算,而对于一个double类型的数值,会强制转换为char类型,可能导致丢失精度(因为char的表示范围有限,即使在中文环境下,范围也只是0-65535,而且只能是整数)。

3、表达式写错了,把2h^2给放到指数函数外面了,也就是说

exp(-(x-y)^2)/(2*z^2)

应为

exp(-(x-y)^2/(2*z^2))

4、可以简单修改如下:

function c=yanyan(x,y,z)

n=length(y);

zz=sum(exp(-(x-y).^2/(2*z^2)));

yux=n*z*(2*pi)^0.5;

c=zz/yux;

但这样的写法不支持x为向量的情况,可以考虑改成

function c=yanyan(x,y,z)

n=length(y);

yux=n*z*(2*pi)^0.5;

c=arrayfun(@(x)sum(exp(-(x-y).^2/(2*z^2))),x)/yux;

高斯核函数RBF

5-11、高斯核函数RBF

import numpy as np

import matplotlib.pyplot as plt

from sklearn import datasets

from matplotlib.colors import ListedColormap

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

from sklearn.pipeline import Pipeline

from sklearn.model_selection import train_test_split

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False

x, y = datasets.make_moons(n_samples=1000, noise=0.25, random_state=2020)  # 生成1000个数据样本

plt.figure()

plt.scatter(x[y == 0, 0], x[y == 0, 1], color="r")

plt.scatter(x[y == 1, 0], x[y == 1, 1], color="g")

plt.title('散点图')

plt.show()

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=2020)

# 绘制边界曲线

def plot_decision_boundary(model, axis):

x0, x1 = np.meshgrid(

    np.linspace(axis[0], axis[1], int((axis[1] - axis[0]) * 100)).reshape(-1, 1),

    np.linspace(axis[2], axis[3], int((axis[3] - axis[2]) * 100)).reshape(-1, 1)

)

x_new = np.c_[x0.ravel(), x1.ravel()]

y_pre = model.predict(x_new)

zz = y_pre.reshape(x0.shape)

# 设置颜色

cus = ListedColormap(["#BA55D3", "#FF69B4", "#FFE4C4"])

plt.contourf(x0, x1, zz, cmap=cus)

def RBFkernelSVC(gamma):#高斯核函数RBF

return Pipeline([

    ("std", StandardScaler()),

    ("svc", SVC(kernel="rbf", gamma=gamma))

])

sv = RBFkernelSVC(gamma=1)

sv.fit(x_train, y_train)

plot_decision_boundary(sv, axis=([-1.8, 2.5, -1.4, 1.8]))

plt.scatter(x[y == 0, 0], x[y == 0, 1], color="r")

plt.scatter(x[y == 1, 0], x[y == 1, 1], color="g")

plt.title('高斯核函数RBF')

plt.show()

# 打印出分数

print(sv.score(x_test, y_test))

d = datasets.load_iris()

x = d.data

y = d.target

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=2020)

sv = RBFkernelSVC(gamma=10)

sv.fit(x_train, y_train)

# 打印出分数

print(sv.score(x_test, y_test))


分享名称:c语言实现高斯核函数,c++高斯函数
分享链接:http://cxhlcq.com/article/hcijps.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部