美文网首页
Python for Data Science

Python for Data Science

作者: 罗尹伊 | 来源:发表于2018-10-27 13:51 被阅读7次

Python List: A compound data type

1. Name a collection of values

2. Contain any type

3. Contain different type

4. Contain Sub List


List Sub List List Slicing 切片 可正数可倒数 包含头,不包含尾巴:List[0:6]  刚好是前6个数字!List[-6:]  就是倒数6个值 可省略第一个/最后一个指标 更改List里的元素:直接重新赋值,然后List就改变了 List里增加元素:fam=fam+["me",1.79] 删除List里的元素:del(), fam也直接改变了 把X赋值给了Y,y改了之后X也改了:因为是同一个容器(类似于只是改了个名字) 但是,如果你用list()函数赋值,就只是把容器x里的内容复制到了容器y,对y的改动就不影响x 同一行的不同命令用分号隔开 用分号隔开的两个命令不是同时执行的,所以删除了倒数第3个之后,原来的倒数第4个就又变成倒数第三个了(所以上图中的答案是错的,应该选第三个) Function max() 求最值的函数 通过Help(函数名)可以查询不同函数的原始代码 你觉得应该用函数解决的事情一定就有函数可以解决! ?max :也可以查询函数使用规则 key=None means that if you don't specify the key argument, it will be None. reverse=False means that if you don't specify the reverse argument, it will be False.

                                       Methods:Functions that belong to objects

List对应的method:index()   count() 字符串对应的method:  capitalize()   replace() 不同type的object有不同的method,不可以混用 list.append() 可以从list尾巴添加元素

str.upper()  全部大写

str.count('x')  在str里数有几个x

list.count('x')  同上

append(), that adds an element to the list it is called on,

remove(), that removes the first element of a list that matches the input, and

reverse(), that reverses the order of the elements in the list it is called on.

目前看到这里了

Package:分不同的module  包含特定的函数、方法和类型 可以直接在conna环境下装包,具体参考上文 import xx 就是导入某个特定的包,然后就可以用特定的函数、变量、类型,比如导入numpy后就可以用numpy.array, 它是numpy的数组 使用array函数创建时,参数必须是由方括号括起来的列表 import math之后就可以用math.pi这个常量 可以选择性的导入某个package里的某个函数:from xx import xx 

将角度转换为弧度:radians() 

the function inv():in the linalg subpackage of the scipy package  

The as word allows you to create a local name for the function you're importing

List不能很好支持数据科学的数学和统计操作 list**int 是不被支持的 numpy的数组完美支持数据操作:可以直接对整个数组进行操作 通过np.array将已有的list转化为np数组,然后直接进行数据运算

Elements-wise calculation 会智能对数据进行一一对应的计算

List可以装不同类的数据,array只能装同一类的数据:将所有输入的数值转化成了同一个类型 List的加法运算是添加元素,array的加法运算是元素的相加

                                                          NumPy Subsetting

通过array[array>23]可以找到array里所有大于23的元素 np.array(list)——>array print(bmi[light]):输出所有bmi<21的球员的bmi值

type coercion 强制类型转换

numpy arrays cannot contain elements with different types. 

True被强制转换成了1,False被强制转换成了0 array的切片和List的切片规则一样

ndarray=N-dimensional Array

array.shape 可以得到数组的行列数 array[0]默认是第一行 array[row,column] 所有行里的第二和第三个元素 第二行的所有列 array.shape 数组的行数和列数 每一行代表一个记录,每一列代表一个变量 可以通过建立一个新的array,然后通过数组运算直接更新/转换数据 np.mean()均值   np.median()中值   np.corrcoef() 相关系数 np.std() 方差 可以用来生成随机的符合特定分布的数据:np.round(np.random.normal(mean, sd, N),decimal) np.mean(array) np.corrcoef(array1,array2) array[array=='kk']       array[array!='KK']

1. list里每个元素用逗号隔开,如果元素本身是list,不要漏掉逗号

2. 大小写敏感

3. 空格敏感

# 注释

**乘方

Shift+Enter >Run

相关文章

网友评论

      本文标题:Python for Data Science

      本文链接:https://www.haomeiwen.com/subject/twrckftx.html