Python点滴(二)

本文介绍了Python中的Lambda表达式使用方法、numpy库的基础操作如multivariate_normal函数的应用、数组拼接、均值计算等,同时涵盖了文件读写注意事项、常见内置函数如map、filter和reduce的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

x=(lambda a="fee",b="fie",c="foe":a+b+c)

<span style="font-size:14px;">>>> q=(lambda a="fee",b="fie",c="foe":a+b+c)
>>> q
<function <lambda> at 0x0000000004004F28>
>>> q()
'feefiefoe'</span>



random.multivariate_normal的用法

>>> mean = [0, 0]
>>> cov = [[1, 0], [0, 100]]  # diagonal covariance

>>> import matplotlib.pyplot as plt
>>> x, y = np.random.multivariate_normal(mean, cov, 5000).T
>>> plt.plot(x, y, 'x')
>>> plt.axis('equal')
>>> plt.show()
>>> import numpy
>>> mean=(1,2)
>>> cov=[[1,0],[0,1]]
>>> x=numpy.random.multivariate_normal(mean,cov,(3,3))
>>> x.shape
(3, 3, 2)
>>> x
array([[[ 1.71895712,  3.92988963],
        [ 1.02372258,  2.55739646],
        [ 0.64468087,  1.80704338]],

       [[-0.01013193,  1.99276314],
        [ 0.64457412,  1.03344298],
        [ 2.09588908,  0.99853057]],

       [[ 1.2729667 ,  3.06851866],
        [ 1.40927712,  1.74607984],
        [-0.34179999,  1.61347053]]])
>>> </span>


concatenate粘连

>> numpy.array(x0)
array([[1, 2],
       [4, 5]])
>>> numpy.array(x1)
array([[4, 5],
       [5, 6],
       [7, 9]])
>>> numpy.array([numpy.concatenate((x0[0],x1[0])),numpy.concatenate((x0[1],x1[1]))])
array([[1, 2, 4, 5],
       [4, 5, 5, 6]])
>>> 


坐标求均值
<span style="font-size:14px;">>> f=[[1,2],[3,4],[5,3]]
>>> f
[[1, 2], [3, 4], [5, 3]]
>>> numpy.mean(f,axis=1)
array([ 1.5,  3.5,  4. ])
>>> numpy.mean(f,axis=0)
array([ 3.,  3.])
>>> g=[[1,2],[2,3],[2,2],[4,1]]
>>> g
[[1, 2], [2, 3], [2, 2], [4, 1]]
>>> numpy.mean(g,axis=0)
array([ 2.25,  2.  ])
>>> numpy.mean(g,axis=1)
array([ 1.5,  2.5,  2. ,  2.5])
>>> </span></span>

numpy中的asarray用法

<span style="font-size:14px;">>> from numpy import asarray
>>> mylist=[1,2,3,4]
>>> mylist
[1, 2, 3, 4]
>>> 
>>> type(mylist)
<class 'list'>
>>> 
>>> 
>>> b=numpy.asarray(mylist)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    b=numpy.asarray(mylist)
NameError: name 'numpy' is not defined
>>> 
>>> b=asarray(mylist)
>>> 
>>> b
array([1, 2, 3, 4])
>>> type(b)
<class 'numpy.ndarray'>
>>> 
</span></span>


下面代码在Python 2.7运行正常,在Python 3.4运行出错,报错信息为:’str’ does not support the buffer interface;

with open("test.txt") as fp:
    line = fp.readline()
with open("test.out", 'wb') as fp:
    fp.write(line)

解决方法

原因是Python3x的string类型与Python2x的类型不相同,在Python3x中需要将str编码,如:

with open("test.txt") as fp:
    line = fp.readline()
with open("test.out", 'wb') as fp:
    fp.write(bytes(line, 'utf-8'))

如果你不想用b(binary)二进制模式写入,那么用t(text, 此为写入的默认模式)模式写入可以避免这个错误,如:

with open("test.txt") as fp:
    line = fp.readline()
with open("test.out", 'wt') as fp:
# with open("test.out", 'w') as fp:
    fp.write(line)


Python错误信息:missing parentheses in call to print

注意Python3中print后面要加圆括号 

print()



在进行数据分析时,data scientist经常会碰到这样的数据文件csv格式,CSV (Comma Separated Values)即逗号分隔数值,可以用excel打开,这是许多数据挖掘竞赛Kaggle,阿里天池,KDDCUP经常会采用的数据存储格式,通常里面存放的是大量样本中各个用户在不同类目下的记录数值等等;下面我们以示例的形式记录csv数据文件的一些简单操作:

首先我们创建一个excel的csv文件,里面存放数据如下:


代码及运行结果:


本来是('test.csv','rb')以二进制方式读入报错,后来改为'rt' text 方式读入


数据写入csv文件:



其中writerow()方法是一行一行写入,writerows方法是一次写入多行;

注意:如果文件'test.csv'事先存在,调用writer函数会先清空原文件中的内容,再执行writerow/writerows写入;



定义自己的分隔读取方式:分隔符为“|”








<span style="font-size:14px;">>>> def knights():
...  title='sir'
...  action=(lambda x: title+ ' '+x)
...  return action
...
>>> act=knights()
>>> act('robin')
'sir robin'
>>>
>>>
>>> L=[(lambda x: x**2),(lambda x: x**3),(lambda x: x**4)]
>>> for f in L:
...  print f(2)
...
4
8
16
>>> print L[0](3)
9
</span>

<span style="font-size:14px;">>>> counters=[1,2,3,4]
>>> updated=[]
>>> for x in counters:
...  updated.append(x+10)
...
>>> updated
[11, 12, 13, 14]
>>>
>>>
>>> def inc(x):return x+10
...
>>> map(inc,counters)    #map映射
[11, 12, 13, 14]
>>>
>>>
>>> map((lambda x:x+3),counters)
[4, 5, 6, 7]
>>>
>>>
>>> def mymap(func,seq):
...  res=[]
...  for x in seq:res.append(func(x))
...  return res
...
>>> mymap(inc,[1,2,3])
[11, 12, 13]
</span>


python数列加法    注意:部分代码在Python2环境没问题,但3下面可能报错

<span style="font-size:14px;">>>> map(pow,[1,2,3],[2,3,4])    #映射平方
[1, 8, 81]
>>>
>>>
>>> range(-5,5)
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>> filter((lambda x:x>0),range(-5,5))
[1, 2, 3, 4]
>>>
>>>
>>> res=[]
>>> for x in range(-5,5):
...  if x>0:
...   res.append(x)
...
>>> res
[1, 2, 3, 4]
>>>
>>>
>>> reduce((lambda x,y:x+y),[1,2,3,4])    #翻译为归纳?
10
>>> reduce((lambda x,y:x*y),[1,2,3,4])
24
>>>
KeyboardInterrupt
>>>
>>>
>>> L=[1,2,3,4]
>>> res=L[0]
>>> for x in L[1:]:
...  res=res+x
...
>>> res
10
>>>
>>>
>>> def myreduce(function,sequence):
...  tally=sequence[0]
...  for next in sequence[1:]:
...   tally=function(tally,next)
...  return tally
...
>>> myreduce((lambda x,y:x+y),[1,2,3,4,5])
15
</span>



<span style="font-size:14px;">>>> import operator
>>> reduce(operator.add,[2,4,6])
12
>>> chr(115)
's'
>>> ord('s')
115
>>> res=[]
>>> for x in 'spamx':
...  res.append(ord(x))
...
>>> res
[115, 112, 97, 109, 120]
>>>
>>>
>>> res=map(ord,'spam')
>>> res
[115, 112, 97, 109]
>>> res=[ord(x) for x in 'spam']
>>> res
[115, 112, 97, 109]
>>>
>>>
>>> map(ord,'spam')        #映射
[115, 112, 97, 109]
>>></span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值