MIT Python学习基础系列(一) - list

Lists
Ordered sequence of information, accessible by index
Denoted by [ ]
  • A list contains elements usually homogenous, not commonly contain mixed types
  • Elements can be changed -> mutable
  • Changing elements
    After the element is changed, it still points to the same object
  • Iterating over a list
 for i in L: 
     total += i

List operation

  • Add elements to end of list
L.append( )
  • Mutates the list

    Everything in python is an object, lists are objects
    Objects has functions and methods

    • Access this information by object_name.something()
  • Concatenate 2 lists L1.extend( [0, 6] )

    • Mutate with L.extend (some_list)
  • Remove

del ( L[index] ) #delete element at specific index
#removes element at end of list
#If no index is specified, removes the last element
#Or remove the element at the specified index

L.pop ( index) 
#removes a specific element
#Removes the first occurrence if the element occurs multiple times
#Returns error if the element is not in the list
L.remove (element)
  • Converts strings and back
#returns a list with every character from s in L
list(s) 

# split a string on a character parameter 
# if called without a parameter, splits on spaces
s.split( )      

#turn a list of characters into a string 
‘ ‘. join(L)  (‘_’.join(L) - > “a_b_c” )
  • reverse()

Mutation, Aliasing, Cloning

  • Lists in memory
  • Is an object in memory
  • Variable name points to object
  • Any variable pointing to that object is affected -> watch out the side effects

Aliases
append() has side effect
print is not ==

  • If two lists print the same thing, does not mean they are the same structure
  • Can test by mutating one and check

Cloning a list
Create a new list and copy every element

L2 = L1[:] #L2 is a copy of L1->different from L2 = L1

Sorting a list
Sort change the list, returns nothing
sorted() does not mutate list, must assign result to a variable
L.sort() changes L, sorted(L) gives a sorted version of L but L stays the same

Lists of lists of lists of…
Can have nested lists

Mutation and iteration

  • Avoid mutating a list as you are iterating over it
for e in L1: 
    if e in L2: L1.remove( e)
L1 = [1,2,3,4] L2 = [1,4,2,6]  -> L1 = [2,3,4]

When you remove 1 in L1, L1 changes and python will not see 2 in L1
Solution
Make a copy of L1 and iterate over L1_copy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
麻省理工学院(MIT)提供的机器学习课程是由著名学府MIT的教授所教授的。这门课程涵盖了机器学习的基本理论和数学基础,并结合了麻省理工学院在研项目的实际应用。这门课程的目标是向学生介绍医疗健康领域中的机器学习应用,包括临床数据的性质,以及机器学习在风险分层、疾病进展建模、精准医疗、诊断、亚型发现和改善临床工作流程方面的使用。 在这门课程中,学生将学习如何使用机器学习库,例如Scikit-Learn,来实现支持向量机(SVM)算法。SVM是一种常用的分类算法,可以通过Scikit-Learn库来实现。下面是一个简单的示例代码,演示了如何使用Scikit-Learn来训练一个基本的SVM分类器。 首先,我们导入必要的库并准备数据集。然后,我们使用Scikit-Learn库的SVM模型来训练分类器。训练过程包括将数据拟合到模型中,并在训练集上进行预测。最后,我们评估分类器的性能,并可视化分类结果。 这门机器学习课程提供了全面的学习资源,包括讲义和实例代码,帮助学生理解机器学习的基本概念和应用。无论是对于想要入门机器学习的初学者,还是对于想要在医疗健康领域应用机器学习的专业人士,这门课程都提供了宝贵的学习机会。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [机器学习mit课件](https://download.csdn.net/download/u010198718/8013755)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [svm支持向量机python代码](https://download.csdn.net/download/weixin_45725404/88247840)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [【MIT干货课程】医疗健康领域的机器学习](https://download.csdn.net/download/weixin_43909715/85467581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值