美文网首页
Python学习笔记

Python学习笔记

作者: 未抵达catty | 来源:发表于2018-10-18 10:05 被阅读0次

1. x^y

x**y

2. range(a,b,c)

http://www.runoob.com/python/python-func-range.html
以a为首项(默认从0开始),c为公差(默认为1)且不超过b-1的等差数列

3. lambda匿名函数

https://blog.csdn.net/liang19890820/article/details/72846966

变量=lambda 函数参数(用逗号隔开):表达式
例:square=lambda x:x*x
等同于:def square(x):
          return  x*x

4. 导入Excel文件

import xlrd # 添加读取excel文件的功能
import pandas as pd
s=pd.read_excel('F:\data.xls')
print(s.head()) # 预览前五行数据
print(s.describe()) # 查看数据的描述统计量

5.pandas/dataframe/loc与iloc的用法区别

loc——通过行标签索引行数据
iloc——通过行号获取行数据
https://blog.csdn.net/a786150017/article/details/78573055
https://zhuanlan.zhihu.com/p/32378028

6.annotate:对图片上的某些点做标注

plt.annotate('threshold point', xy = (4, 1), xytext = (3, 1.8))
# annotate参数说明:'threshold point’:标注文本,可以随意替换;
# xy = (4, 1) :所要标注的点的坐标;
# xytext= (3, 1.8):标注文本所在位置 ;

7.升级pip version

当遇到安装库失败时,

Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

在命令行中输入python -m pip install --upgrade pip

8.python数据库操作

python2中操作MySQL的库:MySQLdb
python3中操作MySQL的库:pyMySQL

9.如何输出两位小数的百分数(如98.79%)

第一个百分号:和 .2f 相连,表示浮点数类型保留小数点后两位(四舍五入)格式化输出;
后面两个连续的百分号:最终会输出一个%号,实现对%的转义

a=0.998745
print('a=%.2f%%'%(a*100))

输出结果:

a=99.87%

10.错误:AttributeError: 'Series' object has no attribute 'reshape'

输入:

kmodel.fit(data.reshape((-1,1))) # 训练模型

原因:Series数据类型没有reshape函数
解决办法: 用values方法将Series对象转化成numpy的ndarray,再用ndarray的reshape方法.
重新输入:

kmodel.fit(data.values.reshape((-1,1))) # 训练模型

11.错误:AttributeError: 'DataFrame' object has no attribute 'sort'

输入:

c = pd.DataFrame(kmodel.cluster_centers_).sort(0) #输出聚类中心,并且排序(默认是随机序的)

解决方法:将“sort”改为“sort_values”
重新输入:

c = pd.DataFrame(kmodel.cluster_centers_).sort_values(0) #输出聚类中心,并且排序(默认是随机序的)

错误:AttributeError: module 'pandas' has no attribute 'rollingmean'

输入:

w = pd.rollingmean(c,2).iloc[1:] #相邻两项求中点,作为边界点

解决方法:

w = c.rolling(2).mean().iloc[1:] #相邻两项求中点,作为边界点

移动平均

13.reshape参数中'-1'的意义:

https://www.zhihu.com/question/52684594
'-1'代表未知,numpy会自己计算

14.pandas.dataframe转换为多维矩阵(numpy.ndarray)的两种方法

15.IndexError: boolean index did not match indexed array along dimension 0

输入:

print(u'有效特征为:%s' % ','.join(data.columns[rlr.get_support()]))

错误原因:
get_support()函数里有一个参数indices,默认为

  • indices=False,此时函数返回一个类型是boolean的数组
  • 如果indices是True,就返回一个整型数组
    解决方法:
print(u'有效特征为:%s' % ','.join(data.columns[rlr.get_support(indices=True)]))

16.Python运算符

https://my.oschina.net/junwuwei/blog/29625

相关文章

网友评论

      本文标题:Python学习笔记

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