Python输入函数input()的总结与相应拓展(python input()、 python3 input())

Python的Print输出函数和Input输入函数 输出函数print、输入函数input的语法及各个参数实例 阅读详情

目录

一、正常形式

二、运用异常处理结构

三、接收多个数据

四、利用eval()函数


Python3里面输入函数是input(),该函数将所有的输入都当做string来处理,但是实际运用中,需要各种类型的数据,需要进行转换,且在转换过程中可能会存在各种问题。我针对我最近遇到的几个小习题做了些总结,作为菜鸟,水平一般,如果有不全或错误,望海涵。

 

一、正常形式

Python3提供了input()函数来接收用户从键盘的输入,input()函数将所有输入都作为string类型来处理。

例如:

num=input("请输入一个整数:")
print(type(num))

'''
执行结果如下:

请输入一个整数:2018
<class 'str'>
'''

亦或是

num=input("请输入一个浮点数:")
print(type(num))

'''
执行结果如下:

请输入一个浮点数:13.14
<class 'str'>
'''

也就是说,不管输入什么类型的数据,都返回一个string类型的数据。

二、运用异常处理结构

但是在实际使用中我们需要各种类型的数据,比如整型,最简单的方式就是用int()来转换。

比如,这题:

#编写程序:从键盘输入某个分钟数,将其转换为用小时和分钟表示

num=int(input("please input a number "))    #这里面就是用int()来转换
hour=num//60  #整除
min=num%60 #求余
print("hour is %.f"%hour)
print("min is %.f"%min)

'''
输出结果
please input a number 366
hour is 6
min is 6
'''

但是,在上题中,你如果输入一个“整数”可以顺利转换,但是如果你输入一个“英文字母串”,就会转换失败,这时候就需要运用Python里面的异常处理结构try……except……,且使用ValueError来接收抛出的错误。

#如果输入“字母”程序将无法执行,产生如下错误

please input a number abcd
Traceback (most recent call last):
  File "D:/Python/习题解析/Temp.py", line 1, in <module>
    num = int(input("please input a number"))
ValueError: invalid literal for int() with base 10: 'abcd'

运用异常处理后:

try:
    num=int(input("please input a number: "))
    hour=num//60
    min=num%60
    print("hour is %.f"%hour)
    print("min is %.f"%min)
except ValueError:  #由于值错误,而不能顺利转换,会抛出ValueError错误
    print("请输入一个整形数据!")

'''
执行结果如下:

please input a number: abcdefg
请输入一个整形数据!
'''

如果加上循环,就可以实现重复输入,直到得到符合要求的数据。例如下面这题:

#输入半径,求圆的面积

PI=3.1415926  #半径
while True:
    try:
        r=input("Please input a number:")
        r=float(r)  #如果不能转换成float类型,则抛出异常,执行except 后面的语句,不会执行下面这句break
        break     #如果上面顺利转换,没有抛出异常,执行break语句,结束循环,转到 area=r*r*PI
    except ValueError:
        print("请输入一个整数或者小数")
        continue  #结束本次循环,接着输入数据

area=r*r*PI
print("area is %.4f"%area)

'''
实验结果如下:

Please input a number:abc
请输入一个整数或者小数
Please input a number:123abc
请输入一个整数或者小数
Please input a number:1  2
请输入一个整数或者小数
Please input a number:10
area is 314.1593

'''

三、接收多个数据

实际运用中,我们可能会需要一次输入多个数据,或者说将一个数据当多个用,比如需要统计“一句句子”由多少个“单词”组成,这时候一句input()语句,要接收一整个句子,我们需要使用str.split()函数来处理。该函数具体可参考str.split()  ,split()函数运用指定的分隔符对字符串进行分割,返回一个列表,列表里面是分割好的子字符串。

#输入一句英文句子,统计其中包含多少个单词 (假设没有逗号和句号!!!)
s=input("please input a string:")  #输入句子
counter=0
for i in s.split(" "):   #用字符串s调用split方法,分割符为" "(空格)
    #返回列表,将列表里面的子字符串迭代出来。
    if i:
        counter+=1
print("The counter is:%.f"%counter)

'''
运行结果如下

please input a string:bu po lou lan zhong bu huan
The counter is:7

'''

但是实际的句子是有逗号和句号的,这时候也需要有分割,但是,是多分割。str.split()只能指定单个分隔符,如果需要指定多个分隔符,需要导入re模块,使用re.split()方法。具体可参考:Python中使用多个分隔符分隔字符串re.split

#输入一段英文,统计其中包含多少个单词
import re
s=input("please input a string:")
counter=0

for i in re.split(r'[\s,。]',s):  #多分割,分割附分别是 空格 逗号(英文) 句号
    if i:
        counter+=1

print("The counter is:%.f"%counter)

'''
运行结果如下

please input a string:ni,hao ma。wo hen hao
The counter is:6

'''

上述是只需要分割的情况,有时候即需要分割,又需要转换,这时候需要map()函数,该函数具体可参考:Python map() 函数  例如下题:

#输入平面上的两个点,计算两点的距离

import math

while True:
    try:
        p1=input("please the start point x1,y1:")  #输入第一个点,中间以逗号作为分割
        p2=input("please the start point x2,y2:")  #第二个点

        #map()函数,将p1.split(",")以逗号作为分割后得到的列表中的子字符串,全部转换为float型。
        #如果在map()里面转换失败,抛出ValueError错误

        x1,y1=map(float,p1.split(",")) #split本质返回一个列表
        x2,y2=map(float,p2.split(",")) #split本质返回一个列表

        distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
        print("distance=", distance)
        break

    except ValueError:
        print("数据类型错误,请重输!")
        continue

'''
执行结果如下:

please the start point x1,y1:1,2
please the start point x2,y2:a,b
数据类型错误,请重输!
please the start point x1,y1:1,2
please the start point x2,y2:5,4
distance= 4.47213595499958

'''

四、利用eval()函数

后来,我又遇到了一道题目是这样的:输入“ [[1,2,3],[4,5,6],[7,8,9]] ”,输出矩阵形式,刚开始,我确实想了很多办法,分割和删除各种组合,希望能去掉方括号,但是都失败了。后来突然想到了eval()这个神奇的函数,一步到位。

我们可以先看一下eval()这个函数的用法:

l = '[1,2,3,4,[5,6,7,8,9]]'
d = "{'a':123,'b':456,'c':789}"
t = '([1,3,5],[5,6,7,8,9],[123,456,789])'

print(type(l), type(eval(l)))  
print(type(d), type(eval(d)))
print(type(t), type(eval(t)))

'''
执行结果如下:

<class 'str'> <class 'list'>
<class 'str'> <class 'dict'>
<class 'str'> <class 'tuple'>

看出作用了吗?
'''

然后来解决上面这个问题:

#将3行3列的矩阵按其形状输出,例如,输入为[[1,2,3],[4,5,6],[7,8,9]],输出为:

str=input("pleased input a 3*3 array:")
print(str,type(str))
str=eval(str)
print(str,type(str))

'''
[[1,2,3],[4,5,6],[7,8,9]] <class 'str'>
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] <class 'list'>

虽然两次输出,样式看起来一致,但是类型已经由“字符串”转换成了“列表”
'''
#接下里只需要将列表里面的数迭代输出即可。
for x in str:
    s=""
    for y in x:
        s1="%6d"%y  #总共占6位,可忽略,不是这篇的重点
        s=s+s1
    print(s)

"""
运行结果如下:

pleased input a 3*3 array:[[1,2,3],[4,5,6],[7,8,9]]
[[1,2,3],[4,5,6],[7,8,9]] <class 'str'>
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] <class 'list'>
     1     2     3
     4     5     6
     7     8     9
"""

当然eval()的用法很灵活,但也不是都能用,具体自己把握。

本篇完。

另,如果有错误,或者你有更好的想法,欢迎留言交流。

 

 

 

python中如何键盘输入列表_python怎么输入一个列表 在python中可以使用方括号“[ ]”来表示列表,其输入列表的语法如“bicycles = ['trek'.'cannondale','redline']”,该语句表示创建了一个名为“bicycles”的列表。python中列表是什么?列表由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有 任何... 阅读详情

相关推荐

eval()函数内输入input()函数存在的问题

eval函数内输入input函数存在的问题eval()函数的功能如果在eval()函数内写入input()函数,存在以下情况此时如果是第一种情况,则只有输入的值为数字时才能正常运行计算,如果输入值为中文或英文字符串,则报错。如果为第二种情况,则输入中文、英文、数字皆不报错,此时类型为str eval()函数的功能 去掉参数中最外层引号并执行剩余语句 如果在eval()函数内写入input()函数,存在以下情况 eval(input()) eval(“input()”) 此时如果是第一种情况,则只有输

从日安声 3323

Python中输入和输出

一、标准输入输出1、打印到屏幕产生输出的最简单方法是使用print语句,可以通过用逗号分隔零个或多个表达式。这个函数传递表达式转换为一个字符串,如下结果写到标准输出 -print ("Python is really a great language,", "isn't it?")这将产生以下结果标准屏幕上 :Python is really a great language, isn't it?2

想作会飞的鱼的博客 6万+

武汉理工大学 Python程序设计第一章测验

在屏幕上输出Hello World! 下面正确的答案有: A.print(‘Hello World!’) C.print(‘Hello’ + ’ ’ + ‘World!’) D.print(‘Hello’, ‘World!’) 模仿以下代码,增加输入部分,输入自己的姓名,在屏幕上输出“欢迎你,某某某”(其中某某某用输入的姓名替换)‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬ name = ‘李明’ print(‘Hell

国家一级致癌物 9319

Python中基本输入和输出

博客中详细讲解了input()函数 和 print()函数更多不为人知的用法

程序员小十一的博客 2万+

基于yolov5的鸟类图像准确识别

本项目主要基于yolov5进行鸟类准确识别,主要识别白鹭家族,红脚鹬,反嘴鹬,环颈珩,小白鹭以及夜鹭六类鸟

weixin_46064184的博客 3825

PDS联合Modelsim仿真报错(Error loading design) line6报错 -novopt已禁用 (全网最详细!!!!!)

发现这个文档是Pango自动生成的,也就是说每一次我们点击"Run Behavior Simulation"的时候,PDS都会自动生成一次这个./run_behav_simulate.tcl文件,所以就算我们这一次把这个文件修改了,我点击"Run Behavior Simulation"想让它再跑一次正确的仿真,它此时又重新生成了这个带有-novopt的错误tcl文件,仿真依旧不能成功。问题真的是出在这里,那我们修改这个./run_behav_simulate.tcl文件就好了,但事实并非如此!

weixin_74058783的博客 3193

【Python入门】--输入

了解输入的语法,特点,以及常用的数据类型转换函数。

2301_78162399的博客 5478

汽车测试工程师工作准则

希望这份综合的工作规范能帮助你更好地规划职业发展。如果你对某个特定领域,比如自动驾驶测试或智能座舱测试,有进一步的兴趣,我很乐意提供更具体的信息。

qq_40844444的博客 460

python基本输入输出函数:input()、eval()、print()

1.eval()函数去掉最外侧的引号,并按照Python语句方式执行去掉引号后的字符内容,使用方式如下:<变量>=eval(<字符串>) eval(<字符串>) 能够以Python表达式的方式解析并执行字符串,并将返回结果输出。eval()函数将去掉字符串的两个引号,将其解释为一个变量。单引号,双引号,eval()函数都将其解释为int类型;三引号则解释...

lfrebecca的博客 1万+

在 SAP 中实现“租赁资产”管理,核心是把 IFRS16/ASC842 等新租赁准则的要求落地到系统

摘要:本文详细介绍了在SAP S/4HANA系统中实现IFRS16/ASC842租赁准则的具体操作流程。主要内容包括:1) 系统前置配置,如激活租赁会计组件、定义租赁类型和会计科目;2) 租赁合同创建与主数据维护;3) 初始确认的会计处理;4) 日常折旧计提和租金支付流程;5) 合同变更与终止管理。通过RE-FX-LM模块与固定资产模块的集成,系统可自动生成使用权资产和租赁负债,支持从合同创建到报表披露的全生命周期管理,确保符合新租赁准则要求。

金牌架构师 999

R语言机器学习算法实战系列(十五)随机森林生存预后模型+SHAP值 (Random Survival Forest + SHAP)

本教程运用随机森林生存分析筛选与患者生存状态显著相关特征。该方法借随机森林这一强大机器学习算法,构建多棵决策树并集成结果,以提高生存分析准确性,识别关键生物标志物。接着,利用选定特征构建风险得分,将多个特征转化为单一数值,量化患者风险水平。之后,依据风险得分将患者分为高、低风险亚型,为临床治疗提供个性化指导。最后,通过分析时间依赖的 ROC 曲线评估模型长期预测能力。此曲线计算不同阈值下的真正例率与假正例率,在多个时间点评估,全面展现模型长期稳定性与准确性。。并且增加了SHAP值评估特征重要性

专注生信领域 2643

Python2/3中的输入函数input()、raw_input()

Python中的标准输入函数 Python提供了内置的函数从标准输入读入一行文本,默认的标准输入是键盘。这里的内置函数指的是Python2中的input()、raw_input()函数和Pyhont3中的input()函数   Python2中的input()函数 输入数字不会报错并且保存为数字;直接输入字符会报错,要以引号形式输入,保存为字符串 参考以下一段代码: a = inpu...

Yucen的博客 1624

VMware系列:此平台不支持虚拟化 Intel-VT-x/EPT 或 AMD-V/RVI

VMware系列:此平台不支持虚拟化 Intel-VT-x/EPT 或 AMD-V/RVI

weixin_54626591的博客 1万+

Python输入函数 raw_input( ) 与 input()

一、raw_input() 在Python中,获取键盘输入的数据的方法是采用 raw_input 函数,那么这个 raw_input怎么用呢? >>> name = raw_input( ) kyle >>> name 'kyle' >>> stu_name = raw_input("请输入学生名字") 请输入学生名字kyle >>> stu_name 'kyle' >>> 注意: raw_input()的小括号

python学习者的博客 2096

OpenCV-Python实战(1)——OpenCV简介与图像处理基础

OpenCV 是一个的跨平台计算机视觉库,可以运行在 Linux、Windows 和 Mac OS 操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时也提供了 Python 接口,实现了图像处理和计算机视觉方面的很多通用算法。在本文中,将介绍 OpenCV 库,包括它的主要模块和典型应用场景,同时使用 OpenCV-Python 实战讲解图像处理基础要点。

盼小辉丶的博客 27万+

姿态传感器/陀螺仪之IIS2DLPC 应用驱动程序设计

* 转换为°C单位 (参考温度为25°C,灵敏度为16 LSB/°C) */

corlin6688的博客 225

Python中input()输入函数和print()输出函数的用法

函数是 Python 内置的一个函数,其主要作用是暂停程序的运行,并等待用户从键盘输入一些文本信息。通过它,我们可以将程序的执行结果展示给用户,从而实现与用户的交互。在编程中,将程序的执行结果或其他信息展示给用户,是实现人机交互的重要环节之一。函数,程序可以将变量的值、表达式的结果或其他信息展示给用户,以便用户理解程序的执行过程和输出结果。:用户输入的内容可能包含多余的空格或特殊字符,因此,在处理用户输入时,可能需要使用字符串的。函数实现程序与用户的互动操作,是构建用户友好程序的重要组件。

分享人工智能领域原创文章 1800

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

python用input输入字符_Python input()函数:获取用户输入的字符串

input() 是Python的内置函数,用于从控制台读取用户输入的内容。input() 函数总是以字符串的形式来处理用户输入的内容,所以用户输入的内容可以包含任何字符。input() 函数的用法为:str = input(tipmsg)说明:str 表示一个字符串类型的变量,input 会将读取到的字符串放入 str 中。大理石平台精度等级tipmsg 表示提示信息,它会显示在控制台上,告诉...

weixin_35174422的博客 5098

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

python2的input()函数的坑对比于python2的raw_input()函数及python3的input()函数

版权声明:转载请注明作者(独孤尚良dugushangliang)出处: https://blog.csdn.net/dugushangliang/article/details/89946012 鄙人接触python时已经宣布python2将在2020年不再提供官方支持,所以直接上手的python3,所以有些python2的一些坑还不了解。刚刚遇到一个问题debug了好久,找到了其原因和解...

dugushangliang的博客 1920

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

python2 input函数_Python2/3中的输入函数input()、raw_input()

Python中的标准输入函数Python提供了内置的函数从标准输入读入一行文本,默认的标准输入是键盘。这里的内置函数指的是Python2中的input()、raw_input()函数和Pyhont3中的input()函数Python2中的input()函数输入数字不会报错并且保存为数字;直接输入字符会报错,要以引号形式输入,保存为字符串参考以下一段代码:a = input()a输入:7 ...

weixin_39796116的博客 525

Python3:input()函数

转自:http://blog.csdn.net/u011475210 编  者:WordZzzz 一、Python2.x中raw_input( )和input( )函数 老规矩,本渣渣先贴出help信息,再进行讲解。 在Python2.x中raw_input( )和input( ),两个函数都存在,其中区别为: >>> help(raw_input) Help on built-...

ym & blog 960

python3中input输入浅谈_详解Python3中的 input() 函数

详解Python3中的 input() 函数一、知识介绍:1、input() 函数,接收任意输入,将所有输入默认为字符串处理,并返回字符串类型;2、可以用作文本输入,如用户名,密码框的值输入;3、语法:input("提示信息:") 。二、运用演示:1、接收任意输入,并返回字符串类型;>>>height = input("输入身高:") #运行输入身高: 170 ...

weixin_39554891的博客 830

python3中输入input_解决python3输入的坑——input()

如下所示:a,b,c,d = input()很简单的代码,如果输入为1 -1 -2 3结果会报错,原因在于input函数会将你的输入作为python脚本运行,那么输入就变成了1-1 -2 3,即0 -2 3结果当然是错误的了,解决办法就是将输入用引号括起来,将其作为字符串输入。即"1 -1 -2 3"这样结果就是a=1,b=-1,c=-2,d=3补充知识:python环境下的sublime tex...

weixin_39605521的博客 4190

python3中raw_input的用法_Python2中input()、raw_input()和Python3中input()

听了ALEX的某节课,说input()和raw_input()函数在Python2中没有区别,现在来探讨一下Python2中的input()、raw_input()函数和Pyhont3中的input()函数:Python2中的raw_input()函数,我们知道input()、raw_input()都是让用户输入的函数:input()函数:首先,我们先输入数字:>>> a = i...

weixin_39927683的博客 179

python中input输入字符串_详解Python3中的 input() 函数

一、知识介绍:1、input() 函数,接收任意输入,将所有输入默认为字符串处理,并返回字符串类型;2、可以用作文本输入,如用户名,密码框的值输入;3、语法:input("提示信息:") 。二、运用演示:1、接收任意输入,并返回字符串类型;>>>height = input("输入身高:") #运行输入身高: 170 #输入整数170>>...

weixin_39611506的博客 2277

Python2的input(), raw_input()和Python3的input()

前言 现在来探讨一下Python2中的input()、raw_input()函数和Pyhont3中的input()函数: Python2中的raw_input()函数,我们知道input()、raw_in...

colours2009的博客 127
上一篇: 分类问题的几个评价指标(Precision、Recall、F1-Score、Micro-F1、Macro-F1)
下一篇: Python输出函数print()总结(python print())
THEAQING
博客等级 码龄12年 699粉丝 24原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值