机器学习实战《Machine Learing in Action》——第三章python知识点

featList = [example[i] for example in dataSet]

此句话应该拆成两块进行理解
第一句话:
for example in dataSet

  • dataset逐行提取

第二句话:
featList = [example[i]]

  • 数组的第i个元素
dataset = [[1, 1, 'yes'],
           [1, 1, 'yes'],
           [1, 0, 'no'],
           [0, 1, 'no'],
           [0, 1, 'no']]
a=[example[-1] for example in dataset]
print(a)

# results
['yes', 'yes', 'no', 'no', 'no']

set()

1

  • 删除重复元素,创建一个无序不重复的元素集
x = set('runoob')
print(x)

a=[1,2,3]
print(set(a))

# results
{
    
    'n', 'r', 'u', 'o', 'b'}
{
    
    1, 2, 3}

2

参考文章

  • set是集合,<class 'set'>
  • 空集合的创建必须set([])
a={
    
    } # 空字典
print(type(a))
b=set([]) # 空集合
print(type(b))

# results
<class 'dict'>
<class 'set'>
  • 集合的创建是{}set()
s = {
    
    1,2,3,1,2,3,4,5}
print(s)
print(type(s))
a=[1,2,1]
print(type(a))
b=set(a)
print(b)
print(type(b))

# results
{
    
    1, 2, 3, 4, 5}
<class 'set'>
<class 'list'>
{
    
    1, 2}
<class 'set'>
  • 集合内部的元素是互不相同的
s = {
    
    1,2,3,1,2,3,4,5}
print(s)

# results
{
    
    1, 2, 3, 4, 5}

单引号双引号

参考文章

dict()

直接创建字典

decisionNode = dict(boxstyle="sawtooth", fc="0.8")
print(decisionNode)

# results
{
    
    'fc': '0.8', 'boxstyle': 'sawtooth'}

利用zip()函数创建字典

a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]

d=dict(zip(a,b))
print(d)

# results
{
    
    1: 4, 2: 5, 3: 6}

zip()

参考文章
zip()函数将对象打包成元组

a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
ziped=zip(a,b)
print(type(ziped))
print(list(ziped))

print(list(zip(a,c))) # 元素个数与最短的列表一致

list()

zip()的一点小技巧,请看下面的代码块,吃不吃惊?
list()是将元组转换为列表,zip()生成的元组只能解压一次

a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]

ziped=zip(a,b)
print(type(ziped))
print(list(ziped))
print(list(ziped))

#results
<class 'zip'>
[(1, 4), (2, 5), (3, 6)]
[]

zip(*)

zip(*)可以理解为解压

a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]

ziped=zip(a,b)
print(type(ziped))
d,e=zip(*ziped)
print(d,e)
list(ziped)
print(list(ziped))

# results
<class 'zip'>
(1, 2, 3) (4, 5, 6)
[]

clf()

 # Clear figure清除所有轴,但是窗口打开,这样它可以被重复使用。

画图

  • fig = plt.figure() #新建figure

画图实例

参考文章

  • fig.add_axes 表示:新增子区域(该区域可以座落在figure内任意位置,且该区域可任意设置大小)
  • **axprops 表示:去掉坐标轴的标注
  • frameon 表示:是否覆盖下面的图层
import matplotlib.pyplot as plt
 
fig = plt.figure()
 
x = [1, 2, 3, 4, 5, 6, 7]
y = [10, 30, 40, 20, 50, 80, 60]
axprops = dict(xticks=[], yticks=[])
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height],**axprops)
ax1.plot(x, y, 'r')
ax1.set_title('area1')
 
left, bottom, width, height = 0.2, 0.2, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height],frameon=False,**axprops)     #叠加图层时frameon必须设置成False,不然会覆盖下面的图层
ax2.plot(x,y, 'b')
ax2.set_title('area2')
plt.show()

在这里插入图片描述

index() 与 find()

index()

字符串可以,list类型也可以,array类型不行

a=[1,2,3]
print(a.index(2))
b="ssfa"
print(b.index('s'))

# results
1
0

find()

字符串可以,但是list类型不行,array类型不行

b="sdfs"
print(b.find('s'))
a=[1,2,3]
a.find(2)

# results
0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-45f46c034be5> in <module>
      2 print(b.find('s'))
      3 a=[1,2,3]
----> 4 a.find(2)

AttributeError: 'list' object has no attribute 'find'

open() 与 read()

参考文章

f=open('p.txt','w')

# results
TypeError: write() argument must be str, not bytes

原因为:Python3给open函数添加了名为encoding的新参数,而这个新参数的默认值却是‘utf-8’。这样在文件句柄上进行read和write操作时,系统就要求开发者必须传入包含Unicode字符的实例,而不接受包含二进制数据的bytes实例。

f=open('p.txt','wb')  #python3 open()函数需要二进制写入

文件读取数据的时候也有类似的问题。解决这种问题的办法也相似:用’rb’模式(二进制模式)打开文件,而不要使用’r’模式。

import StringIO

Python3中出现“No module named ‘StringIO’”错误处理方法

  • Python2 可以import StringIO
  • Python3 只能:
    直接导入:form io import StringIO 使用:StringIO()
    导入io 模块:import io 使用时调用StringIO方法:io.StringIO()

Python3 中已将StringIO归入io,调用方法如下:

import io
iost = io.StringIO()

但是你觉的你的问题解决了吗?
哈哈,请往下看吧

string argument expected, got 'bytes’的解决方法

参考文章
将StringIO改为BytesIO即可

StringIO和BytesIO介绍

enumerate()

参考文章

  • for 循环使用 enumerate 用于提取参数的同时,顺便把索引值也拿出来
  • 也可以一下全部提出来,不过需要list()转一下
seq = ['one', 'two', 'three']
a=[1,2]
b=enumerate(seq)
print(list(b))
for i, element in enumerate(seq):
    print(i,type(i),element,type(element))
for i, element in enumerate(a):
    print(i,type(i),element,type(element))

# results
[(0, 'one'), (1, 'two'), (2, 'three')]
0 <class 'int'> one <class 'str'>
1 <class 'int'> two <class 'str'>
2 <class 'int'> three <class 'str'>

0 <class 'int'> 1 <class 'int'>
1 <class 'int'> 2 <class 'int'>

猜你喜欢

转载自blog.csdn.net/weixin_48622537/article/details/113007107