Python下的Softmax回归函数的实现方法(推荐)

作者:jingxian 时间:2022-02-13 10:42:17 

Softmax回归函数是用于将分类结果归一化。但它不同于一般的按照比例归一化的方法,它通过对数变换来进行归一化,这样实现了较大的值在归一化过程中收益更多的情况。

Softmax公式

Python下的Softmax回归函数的实现方法(推荐)

Softmax实现方法1


import numpy as np
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
pass # TODO: Compute and return softmax(x)
x = np.array(x)
x = np.exp(x)
x.astype('float32')
if x.ndim == 1:
 sumcol = sum(x)
 for i in range(x.size):
  x[i] = x[i]/float(sumcol)
if x.ndim > 1:
 sumcol = x.sum(axis = 0)
 for row in x:
  for i in range(row.size):
   row[i] = row[i]/float(sumcol[i])
return x
#测试结果
scores = [3.0,1.0, 0.2]
print softmax(scores)

其计算结果如下:


[ 0.8360188 0.11314284 0.05083836]

Softmax实现方法2


import numpy as np
def softmax(x):
return np.exp(x)/np.sum(np.exp(x),axis=0)

#测试结果
scores = [3.0,1.0, 0.2]
print softmax(scores)

标签:softmax,回归,python
0
投稿

猜你喜欢

  • Python代码缩进和测试模块示例详解

    2021-08-31 06:49:10
  • python绘图库Matplotlib的安装

    2023-09-26 19:32:22
  • 在Win 2003中配置ASP.net环境

    2007-10-14 12:02:00
  • 如何用SQL语句来建表?

    2010-06-13 14:38:00
  • 小议sqlserver数据库主键选取策略

    2011-10-24 19:51:30
  • 同时安装sql2000和sql2005,经验点滴

    2008-03-04 17:56:00
  • 如何将服务器端变量转换为客户端的变量?

    2009-12-03 19:54:00
  • server.mappath方法详解

    2023-07-05 08:07:48
  • 菜鸟课堂:详述如何提高MySQL中数据装载效率

    2009-10-23 14:29:00
  • window.open被浏览器拦截后的自定义提示

    2007-11-23 12:31:00
  • 使用IP地址来统计在线人数方法

    2007-08-13 12:51:00
  • ubuntu 18.04搭建python环境(pycharm+anaconda)

    2023-09-23 20:01:56
  • Python中turtle库的使用实例

    2023-08-01 23:05:56
  • MySQL的一级防范检查列表

    2011-12-14 18:39:22
  • 404页面设计一样可以闪光

    2007-08-19 15:09:00
  • 30个最常用css选择器解析

    2011-06-16 20:36:37
  • php环境配置 php5 MySQL5 apache2 phpmyadmin安装与配置图文教程

    2023-11-14 22:08:47
  • asp函数判断服务器是否安装了某种组件

    2008-10-11 14:45:00
  • 细化解析:转换 SQL数据库时的疑难问题

    2009-02-05 15:38:00
  • mysql myisam优化设置

    2010-03-13 16:59:00
  • asp之家 网络编程 m.aspxhome.com