Python 学习笔记

Python学习笔记

1.系统操作

1.1 路径

  • 1.查找包的位置:print numpy.__file__
  • 2.查看包的帮助:print numpy.__doc__
  • 3.获取当前位置:print os.getcwd()
  • 4.创建一个目录:os.mkdirs("myfile")
               os.system("mkdir myfile")
  • 5.删除一个目录:os.rmtree("/home/dakar/myTemp")
               os.system("rm -rf /home/dakar/myTemp")
  • 6.切换当前路径:os.chdir("/home/")[真正的切换]
               os.system("cd /home/ && pwd")[note:这个不会切换当前路径,只是在子进程中切换]
               os.getcwd()
  • 7.列出路径、目录、文件:info=os.walk("/home/dakar/dowload")
  • 8.组合路径:os.path.join(os.getcwd(),"image","a.jpg") >>> /home/dakar/image/a.jpg

1.2 文件

  • 1.创建/打开:fp = open("test.txt",['a(追加读写),r+/w+/a+(读写),rb/wb/ab/rb+/wb+/ab+'])
           [note:w模式会清空文件内容]
           eg: fp = open("a.raw",rb+)
  • 2.读文件:[note:文件操作指针会自动偏移]
    • 1.读字节:fp.read(100)
    • 2.读一行:fp.readline()
    • 3.全部读:fp.readlines()
  • 3.写文件:
    • 1.写字符串:fp.write("Hello...")
    • 2.保存磁盘:fp.flush()
    • 3.关闭文件:fp.close() [note:关闭文件会才会更新文件]
  • 4.重命名:    os.rename(oldName,newName)
  • 5.复制文件:   shutil.copy(fromFile,NewDir_or_fileName)
                shutil.copytree(fromdir,NewDir)
  • 6.移动文件:   shutil.move(fromFile,Newdir)
  • 7.删除文件:       os.remove(fileName)
                shutil.rmtree("rmDir")
  • 8.文件修改时间: os.path.getmtime("./a.jpg")
  • 9.文件创建时间: os.path.getctime("./a.jpg")
  • 10.文件是否存在: os.path.exists("/home/dakar/a.jpg")
  • 11.判断是否是文件:os.path.isfile("/home/dakar/a.jpg")
  • 12.判断是否是目录:os.path.isdir()

数据分析

1.numpy

  • 1.新建矩阵
    • 1.全0:np.zeros((10,15),dtype=float)
    • 2.全1:np.ones((10,15),dtype=int)
    • 3.随机:np.random.random((10,15))[note:0~1]
             np.empty([10,15],dtype=float)[随机]
    • 4.对角:np.eye(10,dtype=float32)
    • 5.手动:np.asarray([[1,2,3],[11,22,33],[44,55,66]],dtype=float)
  • 2.矩阵拼接
# -*- coding: utf-8 -*-
import numpy as np
a=np.ones((2,3))
b=2*np.eye(3)
'''竖直拼接,b放在a后面'''
c=np.vstack((a,b))
print c.shape
k=np.ones((3,2))
'''水平拼接,b放在k右面'''
c=np.hstack((k,b))
print c.shape
#按轴拼接
'''在第一个轴上(行方向)拼接'''
c=np.concatenate((a,b),axis=0)
print c.shape
'''在第二个轴(列方向)上拼接'''
c=np.concatenate((a,b),axis=1)
print c.shape
  • 3.改变轴标
# -*- coding: utf-8 -*-
import numpy as np
a = np.empty((1231,3,512,512),dtype=np.uint)
'''np.rollaxis(矩阵,axis(要移动的轴),axis—D(目标轴))'''
b = np.rollaxis(a,0,4)#从左往右:axis—D(目标轴):0~len(a.shape)  eg: 0~4
print b.shape
b = np.rollaxis(b,3,0)#从右往左:axis—D(目标轴):0~len(a.shape)-1 eg:0~3
print b.shape
  • 4.矩阵转置:矩阵.transpose()

2.scipy

  • 1.数据操作
    • 1.图像读取:scipy.misc.imread("a.jpg") [note:(H,W,C),eg:(480,640,3)]
    • 2.图像保存:scipy.misc.imsave("save.jpg",pixdata)
    • 3.图像大小:scipy.misc.imresize(pixdata,(H,W,C))
    • 4.图像旋转:scipy.misc.imrotate(pixdata,90)
    • 5.图像显示:sicpy.misc.imshow(pixdata)
  • 2.统计计算
    • 1.Pearson线性相关系数:(Pearsonlinear correlation coefficient,PLCC)
            PLCC=scipy.stats.pearsonr(prediction,label)
    • 2.Spearman秩相关系数:(Spearman rankorder correlation coefficient,SROCC)
            SROCC=stats.spearmanr(prediction,label)

3.sklearn

  • 1.数据处理

    • 1.划分数据集:

      • 1.sklearn.model_selection.ShuffleSplit(n_splits=10,test_size=’default’,train_size=None,random_state=None)
        [note:更换了新的API,接口如下,train/test_size:比例/个数]

        >>> from sklearn.model_selection import ShuffleSplit
        >>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
        >>> y = np.array([1, 2, 1, 2])
        >>> rs=ShuffleSplit(n_splits=3, test_size=0.25, train_size=None,random_state=0)
        >>> rs.get_n_splits(X)
        3
        >>> print(rs)
        ShuffleSplit(n_splits=3, random_state=0, test_size=0.25, train_size=None)
        >>> for train_index, test_index in rs.split(X):
        ...    print "train_index:", train_index, "test_index:", test_index
        ...  
        train_index: [3 1 0] test_index: [2]
        train_index: [2 1 3] test_index: [0]
        train_index: [0 2 1] test_index: [3]
        >>> rs =ShuffleSplit(n_splits=3, train_size=0.5, test_size=1, random_state=0)
        >>> for train_index, test_index in rs.split(X):
        ...    print "train_index:", train_index, "test_index:", test_index
        ...  
        train_index: [3 1] test_index: [2]
        train_index: [2 1] test_index: [0]
        train_index: [0 2] test_index: [3]
      • 2.sklearn.model_selection.train_test_split()

        >>> import numpy as np
        >>> from sklearn.model_selection import train_test_split
        >>> X, y = np.arange(10).reshape((5, 2)), range(5)
        >>> X
        array([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
        >>> list(y)
        [0, 1, 2, 3, 4]
        >>> X_train, X_test, y_train, y_test = train_test_split(
        ...     X, y, test_size=0.33, random_state=42)
        ...
        >>> X_train
        array([[4, 5],
        [0, 1],
        [6, 7]])
        >>> y_train
        [2, 0, 3]
        >>> X_test
        array([[2, 3],
        [8, 9]])
        >>> y_test
        [1, 4]
        >>> train_test_split(y, shuffle=False)
        [[0, 1, 2], [3, 4]]
  • 2.分类
    • 1.SVC:sklearn.svm.SVC()

      >>> import numpy as np
      >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
      >>> y = np.array([1, 1, 2, 2])
      >>> from sklearn.svm import SVC
      >>> clf = SVC()
      >>> clf.fit(X, y)
      SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
      max_iter=-1, probability=False, random_state=None, shrinking=True,
      tol=0.001, verbose=False)
      >>> print clf.predict([[-0.8, -1]])
      >[1]
      >>>print clf.decision_function([[-0.8,-1]])
      >>>[-0.90840541]
  • 3.回归
    • 1.SVR:sklearn.svm.SVR()

      from sklearn import svm
      x=[[0,0],[2,2]]
      y=[0.2,0.6]
      svr=svm.SVR()
      svr.fit(x,y)
      result = svr.predict([[1,1]])
      print result
  • 4.模型
    • 1.sklearn.linear_model.LogisticRegression
  • 5.评价
    • 1.准确率:sklearn.metrics.accuracy_score(Y_true,Y_prediction)
    • 2.二分类:sklearn.metrics.roc_auc_score(Y_true,Y_probability)[note:Y_probability,预测一个类的概率]
              e.g:roc_auc_score([1,0,1,1],[0.6,0.2,0.7,0.9])
    • 3.多分类:sklearn.mertics.log_loss(Y_true,Y_probability) [note:Y_probability,所有类的概率]
              e.g:log_loss([1, 2, 2, 1], [[0.1, 0.9], [0.9, 0.1], [0.8, 0.2], [0.35, 0.65]])

4.matplotlib

  • 1.基本操作:import matplotlib.pyplot as plt
    • 1.限制坐标轴范围:plt.xlim(min,max)
                  plt.ylim(min,max)
    • 2.常用配置:plt.plot(x,y,”见下表”)
      颜色配置
      线条配置
    • 3.添加网格:plt.grid()
    • 4.添加图例:plt.legend(loc=1,fontsize=5)
               [note:前提将plt.plot(…,…,label=”LengendName”)加上]
    • 5.添加标签:plt.xlabel(“X_axisLabel”)
               plt.ylabel(“Y_axisLabel”)
               plt.titlel(“Title”)
    • 6.自动配置间隔:plt.tight_layout()
    • 7.保存图像:plt.savefig(“pictureName”)

与C/C++接口

  • 1.Python C/C++ API:
//1.封装C,实现判断回文数
#include<Python.h>
static PyObject *is_palindrome(PyObject *self,PyObject *args)
{
   int i,n;
   const char *text;
   int result;

   if(!PyArg_ParseTuple(args,"s",&text))
   {
      return NULL;
   }

   n = strlen(text);
   result =1;
   for(i=0;i<=n/2;i++) 
  {
    if(text[i] != text[n-i-1])
    {
       result = 0;
       break;
    }
  }
 return Py_BuildValue("i",result);
}

/*信息结构体*/
static PyMethodDef PalindromeMethods[] = {
  //函数名称            具体函数   参数类型       文档字符串
  {"is_palindrome",is_palindrome,METH_VARARGS,"Detect palindromes"},
   //结束符
  {NULL,NULL,0,NULL}
};
//初始化模块
PyMODINIT_FUNC initpalindrome(){
                 //模块名称
   Py_InitModule("palindrome",PalindromeMethods);
}

2.编译:gcc -I /usr/include/python2.7/ -shared -fPIC palindrome.c -o palindrome.so

# -*- coding: utf-8 -*-
#测试
from palindrome import is_palindrome
print is_palindrome('foobar')
print is_palindrome('deified')

常用脚本

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值