美文网首页
python数据分析学习笔记(三):文件读取&绘图&机器学习调包

python数据分析学习笔记(三):文件读取&绘图&机器学习调包

作者: yayalisa小可乐 | 来源:发表于2018-09-05 00:23 被阅读0次

    pandas read_csv函数

    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html 

    %matplotlib inline的作用

    %matplotlib inline是在使用jupyter notebook 或者 jupyter qtconsole的时候,才会经常用到。用处是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,或者生成一个figure画布的时候,可以直接在你的python console里面生成图像。省略plt.show()

    链接:https://www.jianshu.com/p/2dda5bb8ce7d

    sklearn中, fit,fit_transform,transform的区别与联系

    https://blog.csdn.net/ljyljyok/article/details/79722089

    matplotlib

    matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表

    一般用以下形式导入:import matplotlib.pyplot as plt

    一般用法:

    1、plt.figure(figsize=(8,4)):创建一个指定大小的figure,单位英寸,若不创建figure直接plot则会默认创建figure

    2、plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2):

    x和y一般为numpy创建的数组

    label : 给所绘制的曲线一个名字,此名字在图示(legend)中显示。只要在字符串前后添加"$"符号,matplotlib就会使用其内嵌的latex引擎绘制的数学公式。

    color : 指定曲线的颜色

    linewidth : 指定曲线的宽度

    3、plt.xlabel(""),plt.ylabel(""):设置坐标轴名字

    4、plt.title(""):设置图表标题

    5、plt.xlim(),plt.ylim():设置坐标轴范围

    6、plt.legend():显示图例

    7、plt.show():显示创建的所有绘图对象结果

    8、plt.subplot(numRows, numCols, plotNum):将整个绘图区域等分为numRows行 * numCols列个子区域,当绘图对象中有多个轴的时候,可以通过工具栏中的Configure Subplots按钮,交互式地调节轴之间的间距和轴与边框之间的距离。如果希望在程序中调节的话,可以调用subplots_adjust函数,它有left, right, bottom, top, wspace, hspace等几个关键字参数,这些参数的值都是0到1之间的小数,它们是以绘图区域的宽高为1进行正规化之后的坐标或者长度。

    9、以上是一般的基础用法,进阶用法是对Artist进行操作,Artists分为简单类型和容器类型两种。简单类型的Artists为标准的绘图元件,例如Line2D、 Rectangle、 Text、AxesImage 等等。而容器类型则可以包含许多简单类型的Artists,使它们组织成一个整体,例如Axis、 Axes、Figure等

    简单说每个独立部分都是一个独立的Artists,通过对每个Artists的属性进行设置(set_*),可以得到最终理想的结果,具体参考http://old.sebug.net/paper/books/scipydoc/matplotlib_intro.html

    特征处理:

    sklearn.preprocessing.LabelEncoder 可以把无序变量或者分类变量编码成数字

    测试集训练集区分 cross_validation.train_test_split

    from sklearn import cross_validation,preprocessing

    X= processed_df.drop(['survived'],axis=1).values

    y= processed_df['survived'].values

    X_train,X_test, y_train,y_test= cross_validation.train_test_split(X,y,test_size=0.2)

    gbdt的sklearn调用方法

    http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html

    from sklearn.ensemble import GradientBoostingClassifier

    gbdt=GradientBoostingClassifier(n_estimators=50)

    gbdt.fit(X_train, y_train)

    gbdt.score(X_test, y_test)

    features = pd.DataFrame()

    features['feature'] = ['pclass', 'sex', 'age', 'sibsp', 'parch', 'fare', 'embarked']

    features['importance'] = gbdt.feature_importances_

    features.sort_values(by=['importance'], ascending=True, inplace=True)

    features.set_index('feature', inplace=True)

    features.plot(kind='barh',figsize=(10,5),fontsize=10)

    相关文章

      网友评论

          本文标题:python数据分析学习笔记(三):文件读取&绘图&机器学习调包

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