DataMining--Python基础入门

本文介绍了Python编程的基础命令,包括基本运算、循环与判断、自编函数等,并详细讲解了Python的数据类型如列表、元组、字典、集合,以及如何使用map()、reduce()、filter()函数进行数据处理。此外,还展示了如何导入库函数并进行重命名。

科学计算发行版:Python(x,y),集合了诸多包。

一、Python使用入门

1、基本命令

(1)基本运算

*,**分别表示乘法和幂运算,可进行多重赋值:

In [1]: 3*4
Out[1]: 12

In [2]: 3**4
Out[2]: 81

In [3]: a,bb,ccc=2,'python',[2,3,4]

In [4]: a
Out[4]: 2

In [5]: bb
Out[5]: 'python'

In [6]: ccc
Out[6]: [2, 3, 4]

多重赋值可针对不同类型,如数值、序列、字符串等。

(2)循环与判断

range(a,b,c)得到序列,间隔为c,首项为a,最大值为b-1;range(a)表示从0到a-1:

In [7]: range(1,11,2)
Out[7]: [1, 3, 5, 7, 9]

In [8]: range(13)
Out[8]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

  循环和判断: 

#for循环
for i in range(11):
    a=a+i**2
print a
#while循环
i=0;a=2;
while i<11:
    a=a+i**2;
    i=i+1;
print(a)
#if 判断
s=0;
if s in range(2):
    print u's 在里面'    #u表示Unicode字符串
else:
    print u's 不在里面'

 u'字符串'表示Unicode字符串;

 (3)自编函数

In [48]: def fun1(x):
    ...:     return x*3
    ...: 
    ...: fun1(3)
Out[48]: 9

In [49]: def fun2(x=1,y=2):#这里给出默认参数
    ...:     return [x+y,x-y] #返回列表
    ...: 
    ...: fun2()  #返回默认值
Out[49]: [3, -1]

In [50]: fun2(2,3)
Out[50]: [5, -1]

In [51]: def fun3(x,y)    :
    ...:     return x*y,x^y   #双重返回。类似于多重赋值#
    ...: 
    ...: fun3(2,4)
Out[51]: (8, 6)

另一种简洁的方法:lambda 

In [57]: f1=lambda x:x*x-3*x
    ...: f1(5)
Out[57]: 10

In [58]: f2=lambda x,y,z:x+y,x*z,y**z
    ...: f2(1,3,2) #lambda中的表达式不能含有命令,而且只限一条表达式,无法得到结果
Traceback (most recent call last):

  File "<ipython-input-58-e75be43f2eb7>", line 1, in <module>
    f2=lambda x,y,z:x+y,x*z,y**z

NameError: name 'x' is not defined

 

 2、Python数据类型:列表,元组,字典,集合

(1)元组/列表

两者的基本操作都一样,只是元组不可以修改。

In [62]: a=[1,2,3,4,'python',[1,2,3]];b=(11,22,33,44,'python',[1,2,3])

In [63]: a
Out[63]: [1, 2, 3, 4, 'python', [1, 2, 3]]

In [64]: b
Out[64]: (11, 22, 33, 44, 'python', [1, 2, 3])

In [2]: a[0]=0;a #列表a的第一个元素被修改
Out[2]: [0, 2, 3, 4, 'python', [1, 2, 3]]

In [3]: b[0]=0;b #元组b无法修改
Traceback (most recent call last):

  File "<ipython-input-3-e3fc51a1d10f>", line 1, in <module>
    b[0]=0;b

TypeError: 'tuple' object does not support item assignment]

In [10]: list('python')        #列举出对象的每一个元素,字符串拆分为列表
Out[10]: ['p', 'y', 't', 'h', 'o', 'n']

In [11]: tuple((1,2,'python')) #拆分为元组
Out[11]: (1, 2, 'python')

 

另外列表还有很多的操作方法,详见《Python基础教程(第二版)》。

(2)字典

In [16]: a={'First':'zhang','Second':'qiang'}

In [17]: a
Out[17]: {'First': 'zhang', 'Second': 'qiang'}

In [18]: a['First']
Out[18]: 'zhang'

In [19]: a['Second']
Out[19]: 'qiang'

In [20]: dict([['No1',11],['No2',22]])
Out[20]: {'No1': 11, 'No2': 22}

In [21]: b=dict([['No1',11],['No2',22]])

In [22]: b['No1']
Out[22]: 11

 

相当于给每一个下标的位置取了个名字。

(3)集合

集合的特点是没有重复元素,且满足集合的基本运算:{}表示集合,里面的元素会自动去重

In [27]: {1,2,3,3,3,2,1,2,4,5,5,2,3,45}
Out[27]: {1, 2, 3, 4, 5, 45}

In [28]: a=set([1,2,3,3,3,2,1,2,4,5,5,2,3,45])

In [29]: b={1,2,55,66,2,1,55,66,77}

In [30]: a|b   #集合的并
Out[30]: {1, 2, 3, 4, 5, 45, 55, 66, 77}

In [31]: a&b  #集合的交
Out[31]: {1, 2}

In [32]: a-b  #差集
Out[32]: {3, 4, 5, 45}

In [33]: a^b  # a∪b-a∩b
Out[33]: {3, 4, 5, 45, 55, 66, 77}

 

(4)map()、reduce()、filter()函数

In [34]: a=[1,2,3,44,55]

In [35]: b=[i*2+1 for i in a]

In [36]: b
Out[36]: [3, 5, 7, 89, 111]

 

其中的i可以认为是每一个下标,对每一个元素进行操作,返回b;map()函数将命令逐一应用,属于循环运算

In [39]: b=map(lambda x:x**2+2,a);
    ...: list(b)#这是python3.0中要求的
Out[39]: [3, 6, 11, 1938, 3027]

 

reduce()函数用于递归运算:

In [48]: a=range(-1,9,2)

In [49]: reduce(lambda x,y:x+y,range(-1,9,2)) #前后累加
Out[49]: 15

In [50]: reduce(lambda x,y:x*y,range(-1,9,2)) #前后连乘
Out[50]: -105

In [51]: reduce(lambda x,y:x+2*y+x**2,range(-1,9,2)) #前后累加
Out[51]: 27736

 

filter()依据条件函数进行过滤:

In [58]: a=range(-1,9,2);a
Out[58]: [-1, 1, 3, 5, 7]

In [59]: filter(lambda x:x>0 and x<6,a)
Out[59]: [1, 3, 5]

#选取元素中满足条件的,
In [61]: [a[i] for i in range(len(a)) if a[i]<6 and a[i]>0 ]
Out[61]: [1, 3, 5]

 

(5)库函数的导入于使用

苦和库中的函数均可重命名

In [66]: import pandas as pa

In [67]: from math import sin as zx

In [68]: zx(1)
Out[68]: 0.8414709848078965

 

转载于:https://www.cnblogs.com/datamaster/p/5238934.html

earning Data Mining with Python - Second Edition by Robert Layton English | 4 May 2017 | ASIN: B01MRP7VFV | 358 Pages | AZW3 | 2.85 MB Key Features Use a wide variety of Python libraries for practical data mining purposes. Learn how to find, manipulate, analyze, and visualize data using Python. Step-by-step instructions on data mining techniques with Python that have real-world applications. Book Description This book teaches you to design and develop data mining applications using a variety of datasets, starting with basic classification and affinity analysis. This book covers a large number of libraries available in Python, including the Jupyter Notebook, pandas, scikit-learn, and NLTK. You will gain hands on experience with complex data types including text, images, and graphs. You will also discover object detection using Deep Neural Networks, which is one of the big, difficult areas of machine learning right now. With restructured examples and code samples updated for the latest edition of Python, each chapter of this book introduces you to new algorithms and techniques. By the end of the book, you will have great insights into using Python for data mining and understanding of the algorithms as well as implementations. What you will learn Apply data mining concepts to real-world problems Predict the outcome of sports matches based on past results Determine the author of a document based on their writing style Use APIs to download datasets from social media and other online services Find and extract good features from difficult datasets Create models that solve real-world problems Design and develop data mining applications using a variety of datasets Perform object detection in images using Deep Neural Networks Find meaningful insights from your data through intuitive visualizations Compute on big data, including real-time data from the internet About the Author Robert Layton is a data scientist working mainly on text mining problems for industries including the finance, information security, and transport sectors. He runs dataPipeline to build algorithms for practical use, and Eurekative, helping bringing start-ups to life in regional Australia. He has presented at the last four PyCon AU conferences, at multiple international research conferences, and has been training in some capacity for five years. He has a PhD in cybercrime analytics from the Internet Commerce Security Laboratory at Federation University Australia, where he was the Inaugural Young Alumni of the Year in 2014 and is currently and Honorary Research Fellow. You can find him on LinkedIn at https://www.linkedin.com/in/drrobertlayton and on Twitter at @robertlayton. Robert writes regularly on data mining and cybercrime, in a private, consultancy, and a research capacity. Robert is an Official Member of the Ballarat Hackerspace, where he helps grow the future-tech sector in regional Victoria.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值