This is a tutorial of how to use *args and **kwargs

For defining the default value of arguments that is not assigned in key words when calling the function:

  1. def func(**keywargs):
  2. if 'my_word' not in keywargs:
  3. word = 'default_msg'
  4. print(word)
  5. else:
  6. word = keywargs['my_word']
  7. print(word)

call this by:

  1. func()
  2. func(my_word='love')

you'll get:

  1. default_msg
  2. love

read more about *args and **kwargs in python: https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

Add more about default value if arg key is not assigned:

  1. def make_hastie_10_2(n_samples=12000, random_state=None):
  2. """Generates data for binary classification used in
  3. Hastie et al. 2009, Example 10.2.
  4. The ten features are standard independent Gaussian and
  5. the target ``y`` is defined by::
  6. y[i] = 1 if np.sum(X[i] ** 2) > 9.34 else -1
  7. Read more in the :ref:`User Guide <sample_generators>`.
  8. Parameters
  9. ----------
  10. n_samples : int, optional (default=12000)
  11. The number of samples.
  12. random_state : int, RandomState instance or None (default)
  13. Determines random number generation for dataset creation. Pass an int
  14. for reproducible output across multiple function calls.
  15. See :term:`Glossary <random_state>`.
  16. Returns
  17. -------
  18. X : array of shape [n_samples, 10]
  19. The input samples.
  20. y : array of shape [n_samples]
  21. The output values.
  22. References
  23. ----------
  24. .. [1] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical
  25. Learning Ed. 2", Springer, 2009.
  26. See also
  27. --------
  28. make_gaussian_quantiles: a generalization of this dataset approach
  29. """
  30. rs = check_random_state(random_state)
  31. shape = (n_samples, 10)
  32. X = rs.normal(size=shape).reshape(shape)
  33. y = ((X ** 2.0).sum(axis=1) > 9.34).astype(np.float64, copy=False)
  34. y[y == 0.0] = -1.0
  35. return X, y

kwargs - Key words arguments in python function的更多相关文章

  1. Faiss in python and GPU报错:NotImplementedError: Wrong number or type of arguments for overloaded function 'new_GpuIndexFlatL2'.

    最近在玩faiss,运行这段代码的时候报错了: res = faiss.StandardGpuResources()flat_config = 0index = faiss.GpuIndexFlatL ...

  2. Mock an function to modify partial return value by special arguments on Python

    Mock an function to modify partial return value by special arguments on Python python mock一个带参数的方法,修 ...

  3. Python Function Note

    Python Function Note #汉诺塔问题Python实现 def my_move(n, a, b, c): if n == 1: print(a + ' --> ' + c) el ...

  4. redis学习 (key)键,Python操作redis 键 (二)

    # -*- coding: utf-8 -*- import redis #这个redis 连接不能用,请根据自己的需要修改 r =redis.Redis(host=") 1. delete ...

  5. python function with variadic arguments or keywords(dict) 可变参数与关键字参数

    *args 表示任意个普通参数,调用的时候自动组装为一个tuple **kwags 表示任意个字典类型参数, 调用的时候自动组装成一个dict args和kwags是两个约定俗成的用法. 变长参数可以 ...

  6. python function parameter

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  7. python Function

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  8. Default arguments and virtual function

    Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...

  9. elike.python.function()

    将python用于基本的科学计算,能完全替代matlab.就最近写的一个物理模型程序来看,用python建立的物理模型的可控性,代码的层次性都优于matlab,只不过python没有matlab那样的 ...

随机推荐

  1. DAY 4 下午

    一些图论的知识(主要补充一下之前不了解的和比较重要) 竞赛图:完全图上的边加方向 仙人掌:每一条边至多属于一个环 前序:中左右 中序:左中右 后序:左右中 先加进去无向边 把每一个联通块看成一个大点 ...

  2. 线性回归 r python 比较

    w http://blog.sina.cn/dpool/blog/s/blog_70f632090101bp8u.html

  3. 007-elasticsearch5.4.3【一】概述、Elasticsearch 访问方式、Elasticsearch 面向文档、常用概念

    一.概述 Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上. Elasticsearch 也是使用 Java 编写的,它的内部使用 L ...

  4. (转) intellij idea部署web项目时的位置(Tomcat)

    这篇文章说的比较好: 原文地址:https://blog.csdn.net/zmx729618/article/details/78340566 1.当你项目启动的时候console能看到项目运行的位 ...

  5. 去掉IE浏览器里的脚本控件提示

    如果是你在网站制作网站让后进行测试,直接在IE浏览器中打开本地含有脚本或者ActiveX控件的页面时,IE就会弹出一个提示栏,说:”为了有利于保护安全性,Internet Explorer己限制此网页 ...

  6. 解决gson解析long自动转为科学计数的问题

    不废话,直接上代码: public class GsonUtils { public static Gson getMapGson(){ Gson gson=new GsonBuilder().reg ...

  7. UVa 11582 Colossal Fibonacci Numbers! 紫书

    思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161  的代码: #include <cstdio> # ...

  8. Monte Carlo Control

    Problem of State-Value Function Similar as Policy Iteration in Model-Based Learning, Generalized Pol ...

  9. jenkins 启动报错

    daemon: fatal: refusing to execute unsafe program: /usr/java/jdk1.8.0/bin/java (/usr/java/jdk1.8.0/b ...

  10. sql中的sp_helptext、sp_help 、sp_depends

    sp_help:用于显示参数清单和其数据类型. sp_depends:用于显示存储过程依据的对象或者依据存储过程的对象. sp_helptext:用于显示存储过程的定义文本