FAFU机器学习 01 Basics of Python课件.pptx

上传人(卖家):最好的沉淀 文档编号:7259536 上传时间:2023-11-05 格式:PPTX 页数:102 大小:662.72KB
下载 相关 举报
FAFU机器学习 01 Basics of Python课件.pptx_第1页
第1页 / 共102页
FAFU机器学习 01 Basics of Python课件.pptx_第2页
第2页 / 共102页
FAFU机器学习 01 Basics of Python课件.pptx_第3页
第3页 / 共102页
FAFU机器学习 01 Basics of Python课件.pptx_第4页
第4页 / 共102页
FAFU机器学习 01 Basics of Python课件.pptx_第5页
第5页 / 共102页
点击查看更多>>
资源描述

1、机器学习基础(双语)Foundations of Machine Learning2023-11-4Foundations of Machine LearningLesson 1-1Requirements考勤:10分作业:10分实验:20分期末考试:60分2023-11-4Foundations of Machine LearningLesson 1-2ContentsPythonLinear RegressionLinear Model for ClassificationDecision TreekNN and BayesSupport Vector MachineEnsemble Le

2、arningClustering MethodsReinforcement LearningArtificial Neural NetworksApplications of Machine LearningFoundations of Machine LearningBasics of Python(一)College of Computer and Information ScienceFujian Agriculture and Forestry UniversityYiwen Zhong2023-11-4Foundations of Machine LearningBasics of

3、python(一)Installation,usage,and featuresData and operationControl flowFunctionModuleData structures2023-11-4Foundations of Machine LearningLesson 1-5Installation(安装Python)Installation on Windows(Windows环境下的安装)Visit https:/www.python.org/downloads/and download the latest version.The installation is j

4、ust like any other Windows-based software.使用anacondaAnaconda是一个开源的包、环境管理器,可以用于在同一个机器上安装不同版本的软件包及其依赖,并能够在不同的环境之间切换Anaconda包括Conda、Python以及一大堆安装好的工具包,比如:numpy、pandas等https:/ of Machine LearningLesson 1-6使用方式Using The Interpreter Prompt(利用解释器)print(Hello World“)Choosing An Editor to edit source file(编辑

5、源程序文件)and Using A Source File(在命令行运行源程序文件)Edit source file and save it as.pyOpen a terminal windowChange directory to where you saved the file,for example,cd/tmp/pyRun the program by entering the command python hello.py.Use IDLE(Integrated Development and Learning Environment)2023-11-4Foundations of

6、 Machine LearningLesson 1-7Features of PythonFeatures of Python(Python的特色的特色)Simple(简单)Easy to Learn(易学)Free and Open Source(免费、开源)High-level Language(高级语言)Portable(可移植性)Interpreted(解释型)Object Oriented(面向对象)Extensible(可扩展性)Embeddable(可嵌入性)Extensive Libraries(丰富的库)2023-11-4Foundations of Machine Lear

7、ningLesson 1-8BasicsComments(注释)Comments are any text to the right of the#symbol and is mainly useful as notes for the reader of the program.#Note that print is a statementprint(hello world)lUse as many useful comments as you can in your program to:explain assumptionsexplain important decisionsexpla

8、in important detailsexplain problems youre trying to solveexplain problems youre trying to overcome in your program,etc.2023-11-4Foundations of Machine LearningLesson 1-9Numbers(数)IntegersFloatsStrings(字符串):ImmutableSingle QuoteDouble QuotesTriple Quotesspecify multi-line strings using triple quotes

9、-(or ).2023-11-4Foundations of Machine LearningLesson 1-10The format methodSave the following lines as a file str_format.py:if _name_=_main_:age=20 name=Swaroop print(0 was 1 years old.format(name,age)print(Why does 0 use python?.format(name)2023-11-4Foundations of Machine LearningLesson 1-11Swaroop

10、 was 20 years oldWhy does Swaroop use python?Compare with:Name+“was“+str(age)+“years old.”The numbers are optional age=20 name=Swaroop print(was years old.format(name,age)print(Why does use python?.format(name)More specifications#decimal(.)precision of 3 for float(0.333)print(0:.3f.format(1.0/3)#fil

11、l with underscores(_)with the text centered#()to 11 11 width _hello_ print(0:_11.format(hello)#keyword based print(Why does xm use python?.format(xm=tom)Escape SequencesWhats your name?This is the first linenThis is the second lineThis is the first sentence.This is the second sentence.Raw StringrNew

12、lines are indicated by n2023-11-4Foundations of Machine LearningLesson 1-14Variable(变量)Variables are exactly what the name implies-their value can varyIdentifier Naming(标识符命名法)The first character of the identifier must be a letter of the alphabet or an underscore(_).The rest of the identifier name c

13、an consist of letters,underscores(_)or digits(0-9).Identifier names are case-sensitive.Logical And Physical LineA physical line is what you see when you write the program.A logical line is what Python sees as a single statement.Python implicitly assumes that each physical line corresponds to a logic

14、al line.Indentation(缩进)Whitespace is important in Python.Actually,whitespace at the beginning of the line is important(每行开始的空格非常重要).This is called indentation.Leading whitespace(spaces and tabs)at the beginning of the logical line is used to determine the indentation level of the logical line,which

15、in turn is used to determine the grouping of statements.This means that statements which go together must have the same indentation.Each such set of statements is called a block.i=5#Error below!print(Value is,i)print(I repeat,the value is,i)Operators and Expressions(操作符和表达式)Operators(操作符)+(plus-加加)A

16、dds two objects3+5 gives 8.a+b gives ab.-(minus减减)Gives the subtraction of one number from the other;if the first operand is absent it is assumed to be zero.-5.2 gives a negative number and 50-24 gives 26.*(multiply乘乘)Gives the multiplication of the two numbers or returns the string repeated that ma

17、ny times.2*3 gives 6.la*3 gives lalala.*(power乘方乘方)Returns x to the power of y3*4 gives 81(i.e.3*3*3*3)/(divide除除)Divide x by y13/3 gives 4 13/3 gives 4.333333333333333%(modulo取余取余)Returns the remainder of the division13%3 gives 1.-25.5%2.25 gives 1.5.(left shift左移左移)Shifts the bits of the number to

18、 the left by the number of bits specified.(Each number is represented in memory by bits or binary digits i.e.0 and 1)2 (right shift右移右移)Shifts the bits of the number to the right by the number of bits specified.11 1 gives 5.11 is represented in bits by 1011 which when right shifted by 1 bit gives 10

19、1which is the decimal 5.&(bit-wise AND按按为为与与)Bit-wise AND of the numbers5&3 gives 1.|(bit-wise OR按位或按位或)Bitwise OR of the numbers5|3 gives 7(bit-wise XOR按位异或按位异或)Bitwise XOR of the numbers5 3 gives 6(bit-wise invert按位逆按位逆转转)The bit-wise inversion of x is-(x+1)5 gives-6.(less than小于小于)Returns whether

20、 x is less than y.All comparison operators return True or False.Note the capitalization of these names.5 3 gives False and 3 5 gives True.Comparisons can be chained arbitrarily:3 5 (greater than大于大于)Returns whether x is greater than y5 3 returns True.If both operands are numbers,they are first conve

21、rted to a common type.Otherwise,it always returns False.=(less than or equal to小于或者等于小于或者等于)Returns whether x is less than or equal to yx=3;y=6;x=(greater than or equal to大于或者等于大于或者等于)Returns whether x is greater than or equal to yx=4;y=3;x=3 returns True.=(equal to等于等于)Compares if the objects are e

22、qualx=2;y=2;x=y returns True.x=str;y=stR;x=y returns False.x=str;y=str;x=y returns True.!=(not equal to不等于不等于)Compares if the objects are not equalx=2;y=3;x!=y returns True.not(boolean NOT非非)If x is True,it returns False.If x is False,it returns True.x=True;not x returns False.and(boolean AND与与)x an

23、d y returns False if x is False,else it returns evaluation of yx=False;y=True;x and y returns False since x is False.In this case,Python will not evaluate y since it knows that the left hand side of the and expression is False which implies that the whole expression will be False irrespective of the

24、 other values.This is called short-circuit evaluation.or(boolean OR或或)If x is True,it returns True,else it returns evaluation of yx=True;y=False;x or y returns True.Short-circuit evaluation applies here as well.orandnotin,not in,is,is not,=,!=,=|&+,-*,/,/,%+x,-x,x*xindex,x(arguments),x.atribute(exps

25、),exps,key:valueEvaluation OrderControl FlowThe if statementThe while StatementThe for loopThe break StatementThe continue StatementThe if statementif condition:statementselif condition:statementselse:statements编写一个程序,要求用户输入一个100分制的成绩,程序输出相应A、B、C和D的表示:大于或者等于90分:A大于或者等于80分,同时小于90分:B大于或者等于60分,同时小于80分:

26、C小于60分:D2023-11-4Foundations of Machine LearningLesson 1-29The while Statementwhile condition:statements2023-11-4Foundations of Machine LearningLesson 1-30控制循环运行的3种方式固定次数与用户进行交互方式设置特殊值的方式例如:编写一个统计课程平均成绩的程序2023-11-4Foundations of Machine LearningLesson 1-31编写一个程序,用来估算pi的值。import randomrandom.random()

27、2023-11-4Foundations of Machine LearningLesson 1-32编写一个程序,输出前50个素数,输出时每5个数一行2023-11-4Foundations of Machine LearningLesson 1-33The for loopAbout range(int from,int to,int step)range returns a sequence of numbers starting from the first number and up to the second number.For example,range(1,5)gives t

28、he sequence 1,2,3,4.By default,range takes a step count of 1.If we supply a third number to range,then that becomes the step count.For example,range(1,5,2)gives 1,3.Remember that the range extends up to the second number i.e.it does not include the second number.for in:else:2023-11-4Foundations of M

29、achine LearningLesson 1-34编写一个程序,输出乘法口诀表2023-11-4Foundations of Machine LearningLesson 1-35The break Statement2023-11-4Foundations of Machine LearningLesson 1-36if _name_=_main_:for i in range(1,5):if i%2=0:break print(i)else:print(for loop is over)The continue Statement2023-11-4Foundations of Machi

30、ne LearningLesson 1-37if _name_=_main_:for i in range(1,5):if i%2=0:continue print(i)else:print(for loop is over)FunctionsFunctions are defined using the def keyword.After this keyword comes an identifier name for the function,followed by a pair of parentheses which may enclose some names of variabl

31、es,and by the final colon that ends the line.Next follows the block of statements that are part of this function.2023-11-4Foundations of Machine LearningLesson 1-38def say_hello():#block belonging to the function print(hello world)#End of functionsay_hello()#call the functionsay_hello()#call the fun

32、ction again Function Parameters(函数的参数)2023-11-4Foundations of Machine LearningLesson 1-39def print_max(a,b):if a b:print(a,is maximum)elif a=b:print(a,is equal to,b)else:print(b,is maximum)Local Variables(局部变量)When you declare variables inside a function definition,they are not related in any way to

33、 other variables with the same names used outside the function-i.e.variable names are local to the function.This is called the scope of the variable.All variables have the scope of the block they are declared in starting from the point of definition of the name.2023-11-4Foundations of Machine Learni

34、ngLesson 1-40 x=50def func(x):print(x is,x)x=2 print(Changed local x to,x)func(x)print(x is still,x)2023-11-4Foundations of Machine LearningLesson 1-41x=50def func(x):print(x is,x)x=2 y=10 print(Changed local x to,x)func(x)Print(x is still,x)Print(y)The global statement(全局语句)2023-11-4Foundations of

35、Machine LearningLesson 1-42x=50def func():global x print(x is,x)x=2 print(Changed global x to,x)func()Print(Value of x is,x)Default Argument Values(参数的缺省值)2023-11-4Foundations of Machine LearningLesson 1-43def say(message,times=1):print(message*times)say(Hello)say(World,5)Keyword Arguments(命名实参)2023

36、-11-4Foundations of Machine LearningLesson 1-44def func(a,b=5,c=10):print(a is,a,and b is,b,and c is,c)func(3,7)func(25,c=24)func(c=50,a=100)VarArgs parameters(变长参数)2023-11-4Foundations of Machine LearningLesson 1-45def total(initial=5,*numbers,*keywords):count=initial for number in numbers:count+=n

37、umber for key in keywords:count+=keywordskey return countPrint(total(10,1,2,3,shapes=50,fruits=100)The return statement(返回语句)The return statement is used to return from a function i.e.break out of the function.We can optionally return a value from the function as well.return None2023-11-4Foundations

38、 of Machine LearningLesson 1-46DocStrings(文档字符串)2023-11-4Foundations of Machine LearningLesson 1-47def print_max(x,y):Prints the maximum of two numbers.The two values must be integers.#convert to integers,if possible x=int(x)y=int(y)if x y:print(x,is maximum)else:print(y,is maximum)print_max(3,5)pri

39、nt(print_max._doc_)编写一个程序,输出前50个素数,输出时每5个数一行2023-11-4Foundations of Machine LearningLesson 1-48密码必须满足以下规则:密码至少必须有一个大写和一个小写字符.密码至少有一个数字字符.至少有一个非字母数字字符写一个密码验证程序,用于检查一个字符串是否满足以上3条规则.你可能需要用到str的一下方法:isdigit(),isupper(),islower(),isalpha()2023-11-4Foundations of Machine LearningLesson 1-49Modules(模块)Ther

40、e are various methods of writing modules,but the simplest way is to create a file with a.py extension that contains functions and variables.A module can be imported by another program to make use of its functionality.2023-11-4Foundations of Machine LearningLesson 1-50import sysprint(The command line

41、 arguments are:)for i in sys.argv:print i print(nnThe PYTHONPATH is,sys.path,n)Byte-compiled.pyc files(字节编译的.pyc文件)Importing a module is a relatively costly affair,so Python does some tricks to make it faster.One way is to create byte-compiled files with the extension.pyc which is an intermediate fo

42、rm that Python transforms the program into(remember the introduction section on how Python works?).This.pyc file is useful when you import the module the next time from a different program-it will be much faster since a portion of the processing required in importing a module is already done.Also,th

43、ese byte-compiled files are platform-independent.2023-11-4Foundations of Machine LearningLesson 1-51The from import statement(from import 语句)If you want to directly import the argv variable into your program(to avoid typing the sys.everytime for it),then you can use the from sys import argv statemen

44、t.In general,you should avoid using this statement and use the import statement instead since your program will avoid name clashes and will be more readable.Example:from math import sqrtprint(Square root of 16 is,sqrt(16)2023-11-4Foundations of Machine LearningLesson 1-52A modules name(模块的名)Every mo

45、dule has a name and statements in a module can find out the name of their module.This is handy for the particular purpose of figuring out whether the module is being run standalone or being imported.Example:if _name_=_main_:print(This program is being run by itself)else:print(I am being imported fro

46、m another module)2023-11-4Foundations of Machine LearningLesson 1-53Making Your Own ModulesCreating your own modules is easy.This is because every Python program is also a module.You just have to make sure it has a.py extension.Example(mymodule.py):2023-11-4Foundations of Machine LearningLesson 1-54

47、def say_hi():print Hi,this is mymodule speaking._version_=0.1import mymodulemymodule.say_hi()Print(Version,mymodule._version_)The dir function(dir函数)You can use the built-in dir function to list the identifiers that an object defines.For example,for a module,the identifiers include the functions,cla

48、sses and variables defined in that module.2023-11-4Foundations of Machine LearningLesson 1-55$python import sys#get names of attributes in sys module dir(sys)_displayhook_,_doc_,argv,builtin_module_names,version,version_info#only few entries shown hereWhen you supply a module name to thedir()functio

49、n,it returns the list of the names defined in that module.When no argument is applied to it,it returns the list of names defined in the current module.2023-11-4Foundations of Machine LearningLesson 1-56#get names of attributes for current module dir()_builtins_,_doc_,_name_,_package_#create a new va

50、riable a a=5 dir()_builtins_,_doc_,_name_,_package_,a#delete/remove a name del a dir()_builtins_,_doc_,_name_,_package_Packages(包)By now,you must have started observing the hierarchy of organizing your programs.Variables usually go inside functions.Functions and global variables usually go modules.W

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(FAFU机器学习 01 Basics of Python课件.pptx)为本站会员(最好的沉淀)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|