Python语言在数据分析、挖掘场景中常用特性

python语言:
简要概括一下python语言在数据分析、挖掘场景中常用特性:
列表(可以被修改),元组(不可以被修改)
字典(结构)
集合(同数学概念上的集合)
函数式编程(主要由lambda()、map()、reduce()、filter()构成)
python数据分析常用库:
python数据挖掘相关扩展库
numpy
提供真正的数组,相比python内置列表来说速度更快,numpy也是scipy、matplotlib、pandas等库的依赖库,内置函数处理数据速度是c语言级别的,因此使用中应尽量使用内置函数。
示例:numpy基本操作
import numpy as np # 一般以np为别名 a = np.array([2, 0, 1, 5]) print(a) print(a[:3]) print(a.min()) a.sort() # a被覆盖 print(a) b = np.array([[1, 2, 3], [4, 5, 6]]) print(b*b)
输出:
[2 0 1 5] [2 0 1] 0 [0 1 2 5] [[ 1 4 9] [16 25 36]]
scipy
numpy和scipy让python有了matlab味道。scipy依赖于numpy,numpy提供了多维数组功能,但只是一般的数组并不是矩阵。比如两个数组相乘时,只是对应元素相乘。scipy提供了真正的矩阵,以及大量基于矩阵运算的对象与函数。
scipy包含功能有最优化、线性代数、积分、插值、拟合、特殊函数、快速傅里叶变换、信号处理、图像处理、常微分方程求解等常用计算。
示例:scipy求解非线性方程组和数值积分
# 求解方程组 from scipy.optimize import fsolve def f(x): x1 = x[0] x2 = x[1] return [2 * x1 - x2 ** 2 - 1, x1 ** 2 - x2 - 2] result = fsolve(f, [1, 1]) print(result) # 积分 from scipy import integrate def g(x): # 定义被积函数 return (1 - x ** 2) ** 0.5 pi_2, err = integrate.quad(g, -1, 1) # 输出积分结果和误差 print(pi_2 * 2, err)
输出:
[ 1.91963957 1.68501606] 3.141592653589797 1.0002356720661965e-09
matplotlib
python中著名的绘图库,主要用于二维绘图,也可以进行简单的三维绘图。
示例:matplotlib绘图基本操作
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 10000) # 自变量x,10000为点的个数 y = np.sin(x) + 1 # 因变量y z = np.cos(x ** 2) + 1 # 因变量z plt.figure(figsize=(8, 4)) # 设置图像大小 # plt.rcparams['font.sans-serif'] = 'simhei' # 标签若有中文,则需设置字体 # plt.rcparams['axes.unicode_minus'] = false # 保存图像时若负号显示不正常,则添加该句 # 两条曲线 plt.plot(x, y, label='$\sin (x+1)$', color='red', linewidth=2) # 设置标签,线条颜色,线条大小 plt.plot(x, z, 'b--', label='$\cos x^2+1$') plt.xlim(0, 10) # x坐标范围 plt.ylim(0, 2.5) # y坐标范围 plt.xlabel(time(s)) # x轴名称 plt.ylabel(volt) # y轴名称 plt.title(matplotlib sample) # 图的标题 plt.legend() # 显示图例 plt.show() # 显示作图结果
输出:
pandas
pandas是python下非常强大的数据分析工具。它建立在numpy之上,功能很强大,支持类似sql的增删改查,并具有丰富的数据处理函数,支持时间序列分析功能,支持灵活处理缺失数据等。
pandas基本数据结构是series和dataframe。series就是序列,类似一维数组,dataframe则相当于一张二维表格,类似二维数组,它每一列都是一个series。为定位series中的元素,pandas提供了index对象,类似主键。
dataframe本质上是series的容器。
示例:pandas简单操作
import pandas as pd s = pd.series([1, 2, 3], index=['a', 'b', 'c']) d = pd.dataframe([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]], columns=['a', 'b', 'c']) d2 = pd.dataframe(s) print(s) print(d.head()) # 预览前5行 print(d.describe()) # 读取文件(路径最好别带中文) df=pd.read_csv(g:\\data.csv, encoding=utf-8) print(df)
输出:
a 1 b 2 c 3 dtype: int64 a b c 0 1 2 3 1 4 5 6 2 7 8 9 3 10 11 12 4 13 14 15 a b c count 6.000000 6.000000 6.000000 mean 8.500000 9.500000 10.500000 std 5.612486 5.612486 5.612486 min 1.000000 2.000000 3.000000 25% 4.750000 5.750000 6.750000 50% 8.500000 9.500000 10.500000 75% 12.250000 13.250000 14.250000 max 16.000000 17.000000 18.000000 empty dataframe columns: [1068, 12, 蔬果, 1201, 蔬菜, 120104, 花果, 20150430, 201504, dw-1201040010, 散称, 生鲜, 千克, 0.973, 5.43, 2.58, 否] index: []
scikit-learn
scikit-learn依赖numpy、scipy和matplotlib,是python中强大的机器学习库,提供了诸如数据预处理、分类、回归、聚类、预测和模型分析等功能。
示例:创建线性回归模型
from sklearn.linear_model import linearregression model= linearregression() print(model)
所有模型都提供的接口:
model.fit():训练模型,监督模型是fit(x,y),无监督模型是fit(x)
监督模型提供的接口:
model.predict(x_new):预测新样本model.predict_proba(x_new):预测概率,仅对某些模型有用(lr)
无监督模型提供的接口:
model.ransform():从数据中学到新的“基空间”model.fit_transform():从数据中学到的新的基,并将这个数据按照这组“基”进行转换
scikit-learn本身自带了一些数据集,如花卉和手写图像数据集等,下面以花卉数据集举个栗子,训练集包含4个维度——萼片长度、宽度,花瓣长度和宽度,以及四个亚属分类结果。
示例:
from sklearn import datasets # 导入数据集 from sklearn import svm iris = datasets.load_iris() # 加载数据集 clf = svm.linearsvc() # 建立线性svm分类器 clf.fit(iris.data, iris.target) # 用数据训练模型 print(clf.predict([[5, 3, 1, 0.2], [5.0, 3.6, 1.3, 0.25]]))
输出:
[0 0]
keras
keras是基于theano的深度学习库,它不仅可以搭建普通神经网络,还可以搭建各种深度学习模型,如自编码器、循环神经网络、递归神经网络、卷积神经网络等,运行速度也很快,简化了搭建各种神经网络模型的步骤,允许普通用户轻松搭建几百个输入节点的深层神经网络,定制度也很高。
示例:简单的mlp(多层感知器)
from keras.models import sequential from keras.layers.core import dense, dropout, activation from keras.optimizers import sgd model = sequential() # 模型初始化 model.add(dense(20, 64)) # 添加输入层(20节点)、第一隐藏层(64节点)的连接 model.add(activation('tanh')) # 第一隐藏层用tanh作为激活函数 model.add(dropout(0.5)) # 使用dropout防止过拟合 model.add(dense(64, 64)) # 添加第一隐藏层(64节点)、第二隐藏层(64节点)的连接 model.add(activation('tanh')) # 第二隐藏层用tanh作为激活函数 model.add(dense(64, 1)) # 添加第二隐藏层(64节点)、输出层(1节点)的连接 model.add(activation('sigmod')) # 第二隐藏层用sigmod作为激活函数 sgd=sgd(lr=0.1,decay=1e-6,momentum=0.9,nesterov=true) # 定义求解算法 model.compile(loss='mean_squared_error',optimizer=sgd) # 编译生成模型,损失函数为平均误差平方和 model.fit(x_train,y_train,nb_epoch=20,batch_size=16) # 训练模型 score = model.evaluate(x_test,y_test,batch_size=16) # 测试模型
参考:
keras中文文档
如何计算两个文档的相似度(二)
genism
genism主要用来处理语言方面的任务,如文本相似度计算、lda、word2vec等。
示例:
import logging from gensim import models logging.basicconfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.info) sentences = [['first', 'sentence'], ['second', 'sentence']] # 将分好词的句子按列表形式输入 model = models.word2vec(sentences, min_count=1) # 用以上句子训练词向量模型 print(model['sentence']) # 输出单词sentence的词向量
输出:
2017-10-24 19:02:40,785 : info : collecting all words and their counts 2017-10-24 19:02:40,785 : info : progress: at sentence #0, processed 0 words, keeping 0 word types 2017-10-24 19:02:40,785 : info : collected 3 word types from a corpus of 4 raw words and 2 sentences 2017-10-24 19:02:40,785 : info : loading a fresh vocabulary 2017-10-24 19:02:40,785 : info : min_count=1 retains 3 unique words (100% of original 3, drops 0) 2017-10-24 19:02:40,785 : info : min_count=1 leaves 4 word corpus (100% of original 4, drops 0) 2017-10-24 19:02:40,786 : info : deleting the raw counts dictionary of 3 items 2017-10-24 19:02:40,786 : info : sample=0.001 downsamples 3 most-common words 2017-10-24 19:02:40,786 : info : downsampling leaves estimated 0 word corpus (5.7% of prior 4) 2017-10-24 19:02:40,786 : info : estimated required memory for 3 words and 100 dimensions: 3900 bytes 2017-10-24 19:02:40,786 : info : resetting layer weights 2017-10-24 19:02:40,786 : info : training model with 3 workers on 3 vocabulary and 100 features, using sg=0 hs=0 sample=0.001 negative=5 window=5 2017-10-24 19:02:40,788 : info : worker thread finished; awaiting finish of 2 more threads 2017-10-24 19:02:40,788 : info : worker thread finished; awaiting finish of 1 more threads 2017-10-24 19:02:40,788 : info : worker thread finished; awaiting finish of 0 more threads 2017-10-24 19:02:40,789 : info : training on 20 raw words (0 effective words) took 0.0s, 0 effective words/s 2017-10-24 19:02:40,789 : warning : under 10 jobs per worker: consider setting a smaller `batch_words' for smoother alpha decay [ -1.54225400e-03 -2.45212857e-03 -2.20486755e-03 -3.64410551e-03 -2.28137174e-03 -1.70348200e-03 -1.05830852e-03 -4.37875278e-03 -4.97106137e-03 3.93485563e-04 -1.97932171e-03 -3.40653211e-03 1.54990738e-03 8.97102174e-04 2.94041773e-03 3.45200230e-03 -4.60584508e-03 3.81468004e-03 3.07120802e-03 2.85422982e-04 7.01598416e-04 2.69670971e-03 4.17246483e-03 -6.48593705e-04 1.11404411e-03 4.02203249e-03 -2.34672683e-03 2.35153269e-03 2.32632101e-05 3.76200466e-03 -3.95653257e-03 3.77303245e-03 8.48884694e-04 1.61545759e-03 2.53374409e-03 -4.25464474e-03 -2.06338940e-03 -6.84972096e-04 -6.92955102e-04 -2.27969326e-03 -2.13766913e-03 3.95324081e-03 3.52649018e-03 1.29243149e-03 4.29229392e-03 -4.34781052e-03 2.42843386e-03 3.12117115e-03 -2.99768522e-03 -1.17538485e-03 6.67148328e-04 -6.86432002e-04 -3.58940102e-03 2.40547652e-03 -4.18888079e-03 -3.12567432e-03 -2.51603196e-03 2.53451476e-03 3.65199335e-03 3.35336081e-03 -2.50071986e-04 4.15537134e-03 -3.89242987e-03 4.88173496e-03 -3.34603712e-03 3.18462006e-03 1.57053335e-04 3.51517834e-03 -1.20337342e-03 -1.81524854e-04 3.57784083e-05 -2.36600707e-03 -3.77405947e-03 -1.70441647e-03 -4.51521482e-03 -9.47134569e-04 4.53894213e-03 1.55767589e-03 8.57840874e-04 -1.12304837e-03 -3.95945460e-03 5.37869288e-04 -2.04461766e-03 5.24829782e-04 3.76719423e-03 -4.38512256e-03 4.81262803e-03 -4.20147832e-03 -3.87057988e-03 1.67581497e-03 1.51928759e-03 -1.31744961e-03 3.28474329e-03 -3.28777428e-03 -9.67226923e-04 4.62622894e-03 1.34165725e-03 3.60148447e-03 4.80416557e-03 -1.98963983e-03]

NIDays成年礼:用平台化系统描绘物联时代
动力电池拆解需要什么设备
中国航天系列:东方红一号
UltraFast 设计方法--赛灵思推荐最佳实践总结
LLM时代NLP研究何去何从?
Python语言在数据分析、挖掘场景中常用特性
VR市场的前景走势将会如何
卢旺达正在采用区块链技术,跟踪采矿场的开采和供应
你了解半导体吗?知道半导体的发展史吗?
我国在西昌成功发射北斗导航卫星系统第32颗
Omnitron验证创新的MEMS扫描镜工艺
Synopsys在武汉设立全球性的知识产权(IP)研究开发中心
微型直流减速电机角度控制器的作用
极视角宣布完成C1轮融资 Qualcomm创投曾进一步加持
动力电池回收的必要性以及回收技术
锦利国际,除夕夜、18187198989
什么是NI Multisim
基于NI CompactRIO与LabVIEW的电力系统的高
“移动PC”和笔记本电脑有什么区别?
红外成像技术发展渐入佳境 助力检测乳腺癌