懵懂的菜鸟

Stay hungry,Stay foolish.

导航

python随笔

三、numpy.vstack()函数

函数原型:numpy.vstack(tup)

等价于:np.concatenate(tup, axis=0) if tup contains arrays thatare at least 2-dimensional.

程序实例:

 

[python] view plain copy
 
  1. >>> a = np.array([1, 2, 3])  
  2. >>> b = np.array([2, 3, 4])  
  3. >>> np.vstack((a,b))  
  4. array([[1, 2, 3],  
  5.        [2, 3, 4]])  
  6.   
  7. >>>  
  8.   
  9. >>> a = np.array([[1], [2], [3]])  
  10. >>> b = np.array([[2], [3], [4]])  
  11. >>> np.vstack((a,b))  
  12. array([[1],  
  13.        [2],  
  14.        [3],  
  15.        [2],  
  16.        [3],  
  17.        [4]]

numpy.diag()返回一个矩阵的对角线元素,或者创建一个对角阵( diagonal array.)

 

使用数组的reshape方法,可以创建一个改变了尺寸的新数组,原数组的shape保持不变;

复制代码
 1 >>> a = np.array([1, 2, 3, 4]);b = np.array((5, 6, 7, 8));c = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]])
 2 >>> b
 3 array([5, 6, 7, 8])
 4 >>> c
 5 array([[ 1,  2,  3,  4],
 6        [ 4,  5,  6,  7],
 7        [ 7,  8,  9, 10]])
 8 >>> c.dtype
 9 dtype('int32')
10 >>> d = a.reshape((2,2))
11 >>> d
12 array([[1, 2],
13        [3, 4]])
14 >>> d = a.reshape((1,2))
15 Traceback (most recent call last):
16   File "<pyshell#27>", line 1, in <module>
17     d = a.reshape((1,2))
18 ValueError: total size of new array must be unchanged
19 >>> d = a.reshape((1,-1))
20 >>> d
21 array([[1, 2, 3, 4]])

>>> d = a.reshape((-1,1))
>>> d
array([[1],
[2],
[3],
[4]])

 

python中的not具体表示是什么:

在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法:
(1) not与逻辑判断句if连用,代表not后面的表达式为False的时候,执行冒号后面的语句。比如:
a = False
if not a:   (这里因为a是False,所以not a就是True)
    print "hello"
这里就能够输出结果hello
(2) 判断元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,这句话的意思是如果a不在列表b中,那么就执行冒号后面的语句,比如:
a = 5
b = [1, 2, 3]
if a not in b:
    print "hello"
这里也能够输出结果hello


二、raise 引发一个异常

例子:如果输入的数据不是整数,则引发一个ValueError

1
2
3
4
5
inputValue=input("please input a int data :")
if type(inputValue)!=type(1):
    raise ValueError
else:
    print inputValue

假设输入1.2,运行结果为:

please input a int data :1.2
Traceback (most recent call last):
File "C:/Users/lirong/PycharmProjects/untitled/openfile.py", line 3, in <module>
raise ValueError
ValueError

如果输入1,运行结果为:

please input a int data :1
1

 

array和asarray都可以将结构数据转化为ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会。

 

mat=x.T.dot(...)是先求这个3*3矩阵的转置(.T),再求与点积(.dot)

像这样:

dot(2)是点乘常数就不说了,

那个x.T.dot([1,2,3])就是x.T的

1*1+2*2+3*3=14

2*1+3*2+4*3=20

 

 

squeeze  除去size为1的维度

B = squeeze(A)

描述:

B = squeeze(A),B与A有相同的元素,但所有只有一行或一列的维度(a singleton dimension)被去除掉了。A singleton dimension的特征是size(A,dim) = 1。二维阵列不受squeeze影响; 如果 A 是一个row or column矢量或a scalar (1-by-1) value, then B = A.

比如,rand(4,1,3)产生一个均匀分布的阵列,共3页,每页4行1列,经过squeeze后,1列的那个维度就没有了,只剩下4行3列的一个二维阵列。而rand(4,2,3)因为没有1列或1行的维度,所有squeeze后没有变化。

 

numpy.real

numpy.real(val)[source]

Return the real part of the complex argument.

Parameters:

val : array_like

Input array.

Returns:

out : ndarray or scalar

The real component of the complex argument. If val is real, the type of val is used for the output. If val has complex elements, the returned type is float.

See also

real_if_closeimagangle

Examples

>>> a = np.array([1+2j, 3+4j, 5+6j])
>>> a.real
array([ 1.,  3.,  5.])
>>> a.real = 9
>>> a
array([ 9.+2.j,  9.+4.j,  9.+6.j])
>>> a.real = np.array([9, 8, 7])
>>> a
array([ 9.+2.j,  8.+4.j,  7.+6.j])
>>> np.real(1 + 1j)
1.0

numpy.clip(aa_mina_maxout=None)[source]

Clip (limit) the values in an array.

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

Parameters:

a : array_like

Array containing elements to clip.

a_min : scalar or array_like or None

Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.

a_max : scalar or array_like or None

Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None. If a_min or a_max are array_like, then the three arrays will be broadcasted to match their shapes.

out : ndarray, optional

The results will be placed in this array. It may be the input array for in-place clipping. out must be of the right shape to hold the output. Its type is preserved.

Returns:

clipped_array : ndarray

An array with the elements of a, but where values < a_min are replaced witha_min, and those > a_max with a_max.

See also

numpy.doc.ufuncs
Section “Output arguments”

Examples

>>> a = np.arange(10)
>>> np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, 3, 6, out=a)
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8)
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])


给定一个间隔,间隔之外的值被限制到间隔边缘。 例如,如果指定了[0,1]的间隔,小于0的值将变为0,大于1的值将变为1。

 

posted on 2017-08-17 23:14  懵懂的菜鸟  阅读(203)  评论(0编辑  收藏  举报