栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python(5):TypeError: xxx() got an unexpected keyword argument ‘xxx‘

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python(5):TypeError: xxx() got an unexpected keyword argument ‘xxx‘

定义了一个python函数,调用时出现报错如下:

Traceback (most recent call last):
  File "gaussian_kernel.py", line 18, in 
    print(gaussian_kernel_vectorization(x, x, l=500, sigma=10))
TypeError: gaussian_kernel_vectorization() got an unexpected keyword argument 'sigma'

原因是:函数参数传递出错,gaussian_kernel_vectorization()函数定义的参数名sigma_f和调用gaussian_kernel_vectorization()函数传递的参数名sigma不一致。(注:c++里面传递函数参数时不需要考虑参数名称,但需要保持参数类型一致或者参数类型可以传递)

代码如下:

# -*- coding:utf-8 -*-
import numpy as np

def gaussian_kernel(x1, x2, l=1.0, sigma_f=1.0):
    """Easy to understand but inefficient."""
    m, n = x1.shape[0], x2.shape[0]
    dist_matrix = np.zeros((m, n), dtype=float)
    for i in range(m):
        for j in range(n):
            dist_matrix[i][j] = np.sum((x1[i] - x2[j]) ** 2)
    return sigma_f ** 2 * np.exp(- 0.5 / l ** 2 * dist_matrix)

def gaussian_kernel_vectorization(x1, x2, l=1.0, sigma_f=1.0):
    """More efficient approach."""
    dist_matrix = np.sum(x1**2, 1).reshape(-1, 1) + np.sum(x2**2, 1) - 2 * np.dot(x1, x2.T)
    return sigma_f ** 2 * np.exp(-0.5 / l ** 2 * dist_matrix)

x = np.array([700, 800, 1029]).reshape(-1, 1)
print(x)
# print(gaussian_kernel_vectorization(x, x, l=500, sigma=10))#改前
print(gaussian_kernel_vectorization(x, x, l=500, sigma_f=10))

改后程序代码跑通:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号