散乱2

zip():

alst=['1','2','3']

blst=['a','b','c']

for z1,z2 in zip(alst,blst):
    print(z1,z2)


1 a
2 b
3 c

#enumerate()列举,枚举:

alst=['a','c','b','f']

for index,value in enumerate(alst):
    print(index,value)


0 a
1 c
2 b
3 f

可通过start参数自定义开始列举的序号数字,

for index,value in enumerate(alst,start=10):
    print(index,value)


10 a
11 c
12 b
13 f

iterating over file connections:

file=open('file.txt')
it=iter(file)
print(next(it))
输出:第一行
print(next(it))
输出:第二行

定义函数:

def jj(x,y):
    z=x**y
    h=y**x
    ttuple=(z,h)
    return ttuple

jj(3,2)
Out[8]: (9, 8)

type(jj(3,2))
Out[9]: tuple

定义函数时,添加参数默认值:
def power(number,pow=1):
    new_value=number**pow
    return new_value

def hh(x,y=2):
    return x**y

hh(8)
Out[11]: 64

hh(9,3)
Out[12]: 729

定义函数时,多重参数:

def ff(*y):
    jj=1
    for x in y:
        jj=jj*x
    return jj

ff(3,4,5)
Out[14]: 60

plt.yticks([0,2,4,6,8,10],['0万','2万','4万','6万','8万','10万']

import pandas as pd
adf=pd.DataFrame({'a':[3,5,2],'b':['78','23','11']})
adf['c']=adf.b.apply(len)

adf
Out[43]: 
   a   b  c
0  3  78  2
1  5  23  2
2  2  11  2

索引可自定义名称:

adf.index.name='inx'

adf
Out[46]: 
     a   b  c
inx          
0    3  78  2
1    5  23  2
2    2  11  2

iterrows():

for index,rows in adf.iterrows():
    print(index)
    print(rows)


0
a     3
b    78
c     2
Name: 0, dtype: object
1
a     5
b    23
c     2
Name: 1, dtype: object
2
a     2
b    11
c     2
Name: 2, dtype: object
 

这里的iterrows()返回值为元组,(index,row)

上面的代码里,for循环定义了两个变量,index,row,那么返回的元组,index=index,row=row.

如果for循环时,只定义一个变量:

for rows in adf.iterrows():
    print(rows)

那么row就是整个元组。输出结果可以看出:
(0, a     3
b    78
c     2
Name: 0, dtype: object)
(1, a     5
b    23
c     2
Name: 1, dtype: object)
(2, a     2
b    11
c     2
Name: 2, dtype: object)

Python map() 函数:

map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list, 并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。

map() 会根据提供的函数对指定序列做映射。

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

map() 函数语法:

map(function, iterable, ...)

参数
function -- 函数
iterable -- 一个或多个序列
返回值
Python 3.x 返回迭代器。


>>>def square(x) :            # 计算平方数
...     return x ** 2
... 
>>> list(map(square, [1,2,3,4,5]))   # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
 
# 提供了两个列表,对相同位置的列表数据进行相加
>>> list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))
[3, 7, 11, 15, 19]


 

广播允许这些二进制操作可以用于不同大小的数组。例如,可以简单地将一个标量(可以认为是一个零维的数组)和一个数组相加:
aa=np.arange(1,5)
aa+8
Out[19]: array([ 9, 10, 11, 12])


对两个数组的同时广播:
a=np.arange(3)
b=np.arange(3)[:,np.newaxis]

a+b
Out[22]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])

正如此前将一个值扩展或广播以匹配另外一个数组的形状,这里将a和b都进行了扩展来匹配一个公共的形状,最终的结果是一个二维数组。

Numpy数组按行或按列归一化:
给定一个数组,将各列(行)归一化(缩放到 [0,1] )
所谓(0,1)归一化,就是通过遍历feature vector里的每一个特征值的数据,将Max和Min的记录下来,并通过Max-Min作为基数(即Min=0,Max=1)进行数据的归一化处理。

通过从X数组的元素中减去这个均值实现归一化,X_centered=X-Xmean;为了进一步核对我们的处理是否正确,可以查看归一化的数组的均值是否接近0;

Conditionals in generator expressions:
列表:
alist=[x for x in range(10) if x%2==0]

alist    
Out[2]: [0, 2, 4, 6, 8]

元组:
alist=(x for x in range(10) if x%2==0)

alist    
Out[4]: <generator object <genexpr> at 0x0000000008F231B0>

list(alist)    
Out[5]: [0, 2, 4, 6, 8]

Conditionals in comprehensions:
[num**2 if num%2==0 else 0 for num in range(10)]
Out[6]: [0, 0, 4, 0, 16, 0, 36, 0, 64, 0]

Dict comprehensions:
a={num:-num for num in range(5)}

print(a)
{0: 0, 1: -1, 2: -2, 3: -3, 4: -4}

print(type(a))
<class 'dict'>

np.dot():

np.dot()函数主要有两个功能,向量点积和矩阵乘法,这里我就简单列举了三种最常用到的情况

1. np.dot(a, b), 其中a为一维的向量,b为一维的向量,当然这里a和b都是np.ndarray类型的, 此时因为是一维的所以是向量点积。

import numpy as np
 
a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 10])
print(np.dot(a, b))
 
output:
130
[Finished in 0.2s]
2. np.dot(a, b), 其中a为二维矩阵,b为一维向量,这时b会被当做一维矩阵进行计算

import numpy as np
 
a = np.random.randint(0,10, size = (5,5))
b = np.array([1,2,3,4,5])
print("the shape of a is " + str(a.shape))
print("the shape of b is " + str(b.shape))
print(np.dot(a, b))
 
 
output:
the shape of a is (5, 5)
the shape of b is (5,)
[42 85 50 81 76]
[Finished in 0.2s]
这里需要注意的是一维矩阵和一维向量的区别,一维向量的shape是(5, ), 而一维矩阵的shape是(5, 1), 若两个参数a和b都是一维向量则是计算的点积,但是当其中有一个是矩阵时(包括一维矩阵),dot便进行矩阵乘法运算,同时若有个参数为向量,会自动转换为一维矩阵进行计算。

3. np.dot(a ,b), 其中a和b都是二维矩阵,此时dot就是进行的矩阵乘法运算

import numpy as np
 
a = np.random.randint(0, 10, size = (5, 5))
b = np.random.randint(0, 10, size = (5, 3))
print("the shape of a is " + str(a.shape))
print("the shape of b is " + str(b.shape))
print(np.dot(a, b))
 
 
output:
the shape of a is (5, 5)
the shape of b is (5, 3)
[[ 66  80  98]
 [ 53  60  60]
 [ 65  84  85]
 [ 25 113 101]
 [ 42  78  77]]
[Finished in 0.2s]
--------------------- 
 

数组的转置:
a=np.arange(15).reshape((3,5))

a
Out[29]: 
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

a.T
Out[30]: 
array([[ 0,  5, 10],
       [ 1,  6, 11],
       [ 2,  7, 12],
       [ 3,  8, 13],
       [ 4,  9, 14]])

np.newaxis,增加维度:

import numpy as np

andarray=np.linspace(1, 10, 10)

andarray.shape
Out[3]: (10,)

andarray
Out[4]: array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

bndarray=andarray[:,np.newaxis]

bndarray
Out[6]: 
array([[ 1.],
       [ 2.],
       [ 3.],
       [ 4.],
       [ 5.],
       [ 6.],
       [ 7.],
       [ 8.],
       [ 9.],
       [10.]])

bndarray.shape
Out[7]: (10, 1)

cndarray=andarray[np.newaxis,:]

cndarray.shape
Out[9]: (1, 10)

cndarray
Out[10]: array([[ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]])

np.random.randint(1,100,size=(10,1))
Out[11]: 
array([[98],
       [92],
       [81],
       [ 2],
       [16],
       [60],
       [12],
       [37],
       [49],
       [ 5]])

np.random.randint(1,100,size=(1,10))
Out[12]: array([[51, 49, 55,  7, 44,  9, 26, 16, 92, 38]])

np.random.randint(1,100,size=(5,10))
Out[13]: 
array([[57, 12, 42, 15, 96,  4, 39, 33, 84, 16],
       [91,  4,  5, 34, 71, 62,  5, 48, 65, 10],
       [79, 14, 32, 67, 78, 32, 34, 81, 50, 22],
       [27, 11, 29, 29, 27, 65, 77, 35, 16, 99],
       [65, 45, 95, 27, 53, 63, 30, 31, 52, 55]])

猜你喜欢

转载自blog.csdn.net/gulie8/article/details/89303917
2-2
今日推荐