…我很难相信。 以下是使python成为数据科学危险工具的10种方法。
词云
Python具有许多应用程序-Web开发,桌面GUI,软件开发,业务应用程序和科学/数字计算。 在本系列中,我们将重点介绍如何在Python中将数值计算用于数据科学和ML。
这不是一个全面的Python教程,而是旨在强调该语言对我们而言最重要的部分(其中某些部分通常不是Python教程的重点)。
在本教程中,我们将研究Python的以下基本功能:
- Python功能
2.数据类型和顺序
3.日期和时间
4. Lambda
5.地图
6.筛选
7.减少
8.拉链
9. for循环
10.清单理解
1. Python函数
函数是仅在调用时运行的代码块。 您可以将数据(称为参数)传递给函数。 让我们编写一个将两个数字相乘的函数。
#multiply two numbers using a python function def multiply(x,y): z = x*yreturn z
#call the function to multiply the numbers 2 and 3
multiply(4,3)
输出:12
2. Python数据类型和序列
Python具有内置的数据类型来存储数字和字符数据。 让我们看一些常见的类型。
type(' My name is Shimanto')
输出:str
type(5)
输出:int
type(5.0)
输出:浮点
type( None ) #None signifies 'no value' or 'empty value'
输出:NoneType
type(multiply) #multiply is a function we created previously
输出:功能
现在,让我们看一下如何存储数字和字符的列表,以及如何执行一些基本操作。
Unsplash上的照片
一世。 元组:它们是不可变的数据结构,不能像列表一样进行更改
a = (1,2,3,4)
type(a)
输出:元组
ii。 列表:它们是可变对象
b = [1,2,3,4]
type(b)
输出:列表
让我们在上面创建的列表b后面加上一个数字。
b.append(2.5) #append to list using this function print(b)
输出:[1,2,3,4,2.5]
循环浏览列表并打印数字
for number in b: #looping through list print(number)
输出:
1个 2 3 4 2.5
现在,让我们合并两个列表
[1,2,3] + [5,'bc','de'] #concatenate lists
输出:[1,2,3,5,'bc','de']
创建一个包含重复编号的列表。
[1,2]*3 #repeat lists
输出:[1、2、1、2、1、2、2]
检查您要搜索的对象是否在列表中。
3 in b #in operator to check if required object is in list
输出:真
将列表解压缩到单独的变量中。
a,b = ('bc','def')
print(a)
print(b)
输出:公元前 定义
iii。 字符串:存储字符对象的字符串
x = 'My name is Shimanto'
访问字符串中的字符:
x[0] #Access first letter
输出:'M'
x[0:2] #Accesses two letters
输出:“我的”
x[:-1] #Accesses everything except last letter
输出:“我的名字是shimant”
x[10:] #returns all the characters from 10th position till end
输出:'Shimanto'
现在,让我们连接两个字符串。
first = 'Harun'
last = 'Shimanto'
Name = first + ' ' + last #string concatenation print(Name)
输出:Harun Shimanto
仅显示第一个单词。
Name.split(' ')[0] #Show the first word
输出:“ Harun”
现在,仅显示字符串中的第二个单词
Name.split(' ')[1] #Show the second word
输出:“ Shimanto”
要将数字数据连接为字符串,请先将数字转换为字符串
#for concatenation convert objects to strings 'Harun' + str(2)
输出:Harun2
iv。 字典:字典是一个无序的集合,但已建立索引-它们具有键和值。
c = {"Name" : "Harun", "Height" : 175}
type(c)
输出:dict
打印词典中包含的数据
print(c)
输出: {'Name':'Harun','Height':175}
根据键访问字典值
c['Name'] #Access Name
输出:“ Harun”
c['Height']
输出:175
打印字典中的所有键
#print all the keys for i in c: print(i)
输出:名称 高度
打印字典中的所有值
for i in c.values(): print(i)
输出:哈伦 175
遍历字典中的所有项目
for name, height in c.items(): print(name) print(height)
输出:名称 哈伦 高度 175
3. Python日期和时间
以下模块可帮助我们以简单的方式操作日期和时间变量。
import datetime as dt import time as tm
以秒为单位打印当前时间(从1970年1月1日开始)
tm.time() #print current time in seconds from January 1, 1970
输出: 1533370235.0210752
#convert timestamp to datetime dtnow = dt.datetime.fromtimestamp(tm.time()) dtnow.year
产出:2018年
获取今天的日期
today = dt.date.today()
today
输出:datetime.date(2018,8,4)
从今天算起减去100天
delta = dt.timedelta(days=100)
today - delta
输出:datetime.date(2018,4,26)
4.地图功能
将给定函数应用于给定序列的每个项目后,Map函数将返回结果列表。 例如,让我们找到两对列表之间的最小值。
a = [1,2,3,5]
b = [8,9,10,11]
c = map(min,a,b) #Find the minimum between two pairs of lists
for item in c: print(item) #print the minimum of the pairs
输出1 2 3 5
5. Lambda函数
Lambda函数用于在Python中创建小型的一次性匿名函数对象。
function = lambda a,b,c : a+b+c #function to add three numbers function(5,6,8) #call the function
输出:19
6.过滤功能
过滤器提供了一种简便的方法来过滤出列表的所有元素。 Filter(语法:filter(function,list))需要一个函数作为其第一个参数,可以使用lambda 。 例如,让我们仅从列表中过滤掉大于5的数字
x = [0,2,3,4,5,7,8,9,10] #create a list
x2 = filter(lambda a : a>5, x) #filter using filter function
print(list(x2))
输出:[7,8,9,10]
7.减少功能
Reduce是用于对列表执行一些计算并返回结果的功能。 它将滚动计算应用于列表中的顺序值对。 例如,让我们计算列表中所有数字的乘积。
from functools import reduce #import reduce function
y = [6,7,8,9,10] #create list
reduce(lambda a,b : a*b,y) #use reduce
输出: 30240
8.邮编功能
Zip函数返回一个元组列表,其中第i个元组包含每个序列中的第i个元素。 让我们看一个例子。
a = [1,2,3,4] #create two lists
b = [5,6,7,8]
c = zip(a,b) #Use the zip function
print(list(c))
输出:[(1,5),(2,6),(3,7),(4,8)]
如果zip函数中使用的序列不相等,则返回的列表的长度将被截断为最短序列的长度。
a = [1,2] #create two lists b = [5,6,7,8]
c = zip(a,b) #Use the zip function print(c)
输出:[[1,5),(2,6)]
9. for循环
当您要重复固定次数的代码块时,通常使用for循环。
让我们使用一个for循环来打印从1到50的偶数列表。
#return even numbers from 1 to 50
even=[]
for i in range(50):
if i%2 ==0:
even.append(i)
else :
None
print(even) #print the list
输出: [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46 ,48]
10.清单理解
列表理解提供了一种创建列表的简便方法。 继续同一示例,让我们使用列表理解来创建一个从1到50的偶数列表。
even = [i for i in range(50) if i%2==0]
print(even)
输出: [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46 ,48]
我们查看的功能有助于理解用于数值计算的Python基本功能。 除了这些内置函数之外,还有其他库,例如Numpy和Pandas(我们将在以后的文章中介绍) ,它们在数据科学和机器学习中得到了广泛使用。
资源:
- Python 3.7.0文档
- Python专业化的应用数据科学。
在LinkedIn上连接,然后查看Github(如下)以获取完整的笔记本。
harunshimanto / Python-ML数据科学的危险工具 人们可以在GitHub上构建软件。 超过2千8百万的人使用GitHub来发现,发掘和贡献超过 github.com
您可以告诉我您的想法,如果您喜欢写作,请单击拍击👏按钮。
谢谢大家。