机器学习笔记(2)

pickle

pickle.dump(obj, file, [,protocol])
注解:将对象obj保存到文件file中去。
protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示;1:老式的二进制协议;2:2.3版本引入的新二进制协议,较以前的更高效。其中协议0和1兼容老版本的python。protocol默认值为0。
file:对象保存到的类文件对象。file必须有write()接口, file可以是一个以’w’方式打开的文件或者一个StringIO对象或者其他任何实现write()接口的对象。如果protocol>=1,文件对象需要是二进制模式打开的。

pickle.load(file)
注解:从file中读取一个字符串,并将它重构为原来的python对象。
file:类文件对象,有read()和readline()接口。

ndimage.imread

scipy.ndimage.imread(fname, flatten=False, mode=None)[source]
Read an image from a file as an array.

Parameters:
fname : str
Image file name, e.g. test.jpg, or a file object.
flatten : bool, optional
If true, convert the output to grey-scale. Default is False.
mode : str, optional
mode to convert image to, e.g. RGB.
Returns:
img_array : ndarray
The different colour bands/channels are stored in the third dimension, such that a grey-image is MxN, an RGB-image MxNx3 and an RGBA-image MxNx4.
Raises:
ImportError
If the Python Imaging Library (PIL) can not be imported.
a=([3.234,34,3.777,6.33])

ndarray 和list的转化

a为python的list类型
将a转化为numpy的array:
np.array(a)
array([ 3.234, 34. , 3.777, 6.33 ])

将a转化为python的list
a.tolist()

ndarray 属性

ndarray.ndim:数组的维数
ndarray.shape:数组每一维的大小
ndarray.size:数组中全部元素的数量
ndarray.dtype:数组中元素的类型(numpy.int32, numpy.int16, and numpy.float64等)
ndarray.itemsize:每个元素占几个字节

np.random.permutation(x)

np.random.permutation(x)
随机排列x,如果x是多维向量,则只排列第一维

参数:
x : int or array_like
If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the elements randomly.
Returns:
out : ndarray
Permuted sequence or array range.

>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
>>> np.random.permutation([1, 4, 9, 12, 15])
array([15,  1,  9,  4, 12])
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.permutation(arr)
array([[6, 7, 8],
       [0, 1, 2],
       [3, 4, 5]])

os.stat

stat 系统调用时用来返回相关文件的系统状态信息的。
可以这么理解,os.stat是将文件的相关属性读出来,然后用stat模块来处理,处理方式有多重,就要看看stat提供了什么了。
看一下stat中有哪些属性:

import os
>>> print os.stat("/root/python/zip.py")
(33188, 2033080, 26626L, 1, 0, 0, 864, 1297653596, 1275528102, 1292892895)
>>> print os.stat("/root/python/zip.py").st_mode   #权限模式
33188
>>> print os.stat("/root/python/zip.py").st_ino   #inode number
2033080
>>> print os.stat("/root/python/zip.py").st_dev    #device
26626
>>> print os.stat("/root/python/zip.py").st_nlink  #number of hard links
1
>>> print os.stat("/root/python/zip.py").st_uid    #所有用户的user id
0
>>> print os.stat("/root/python/zip.py").st_gid    #所有用户的group id
0
>>> print os.stat("/root/python/zip.py").st_size  #文件的大小,以位为单位
864
>>> print os.stat("/root/python/zip.py").st_atime  #文件最后访问时间
1297653596
>>> print os.stat("/root/python/zip.py").st_mtime  #文件最后修改时间
1275528102
>>> print os.stat("/root/python/zip.py").st_ctime  #文件创建时间
1292892895

注意打乱顺序数据集,使分类结果更可信

猜你喜欢

转载自blog.csdn.net/zsz_shsf/article/details/78442185