Python导出微信群成员itchat

import itchat
itchat.auto_login(hotReload=True)#hotReload=True
for room in itchat.get_chatrooms(update=True)[0:]:
    chatroom=itchat.update_chatroom(room['UserName'],detailedMember=True)
    path="C:/Users/Administrator/Documents/group/"+room['NickName']+'.txt'
    try:
         with open(path,'a',encoding='utf-8') as f:
             i=1
             for friend in chatroom['MemberList']:
                if friend['DisplayName']=='':
                    friend['DisplayName']='No_DisplayName'
                f.write(repr(i)+'.'+friend['DisplayName']+'     '+friend['NickName']+'\n')
                i+=1
    except Exception as e:
              print(repr(e))
itchat.run()

非修改性序列算法

该算法不改动容器中元素的次序,也不改动元素值。一般通过input迭代器和forward迭代器完成工作,可用于所有的标准容器。

一.for_each算法:for_each(iterator begin,iterator end,proc op)

该算法对区间[begin,end)中的每个元素调用进程op

①不修改元素

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int a)
{
	cout << a << endl;
}
int main()
{
	vector<int>vi;
	for (int i = 1; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	for_each(vi.begin(), vi.end(), 
		[](int b) //lambda
		{
			cout <<b << endl;
		}
	);
	for_each(vi.begin(),vi.end(),print);//或者
	return 0;
}
/*
1
3
5
7
9
*/

②修改元素

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int a)
{
	cout << a << endl;
}
int main()
{
	vector<int>vi;
	for (int i = 1; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	for_each(vi.begin(), vi.end(), 
		[](int &b) //引用
		{
			b+=100;
		}
	);
	for_each(vi.begin(),vi.end(),print);
	return 0;
}
/*
101
103
105
107
109
*/

③使用其返回值:例如返回sum求和

二.元素计数

count(Iterator start,Iterator end,value)

count(Iterator start,Iterator end,Condition)

#include <iostream>
#include<vector>
#include<functional>//bind2nd()
#include<algorithm>
using namespace std;

bool isEven(int elem)
{
	return elem % 2 == 0;
}
int main()
{
	vector<int>vi;
	for (int i = 0; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	cout << count_if(vi.begin(),vi.end(),isEven)<<endl;//统计偶数的个数:5
	cout << count_if(vi.begin(), vi.end(), bind2nd(greater<int>(), 2));//统计比2大的数的个数:4
	return 0;
}

三.最大值最小值

最小值:

Iterator min_element(Iterator beg,Iterator end);

Iterator min_element(Iterator beg,Iterator end,comFunc op);

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

bool absless(int elem1,int elem2)
{
	return abs(elem1) < abs(elem2);
}

int main()
{
	vector<int>vi;
	for (int i = -8; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	cout << *min_element(vi.begin(), vi.end()) << endl;//输出向量中最小的元素:-8
	cout << *min_element(vi.begin(), vi.end(),absless) << endl;//输出向量绝对值中最小的元素:0
	return 0;
}

四.搜寻元素

1.搜寻第一个匹配元素

Iterator find(Iterator beg,Iterator end,value);

Iterator find_if(Iterator beg,Iterator end,value,ConditionFunc);

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;


int main()
{
	vector<int>vi;
	for (int i = -8; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	cout << *find(vi.begin(),vi.end(),0) << endl;//查找0,失败则返回end迭代器
	cout << *find_if(vi.begin(), vi.end(),bind2nd(greater<int>(),2)) << endl;//输出向量第一个大于2的元素:4
	return 0;
}

2.搜寻前n个连续匹配值

Iterator search_n(Iterator beg,Iterator end,Size count,value);返回连续count等于value的元素的起始位置

Iterator search_n(Iterator beg,Iterator end,Size count,value,BinaryPredicate op);返回连续count满足op的元素的起始位置

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;


int main()
{
	vector<int>vi;
	vector<int>::iterator it;
	for (int i = -2; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	vi.push_back(5);
	vi.push_back(5);
	it = search_n(vi.begin(), vi.end(), 3, 5);
	cout << distance(vi.begin(),it) << endl;//返回3个连续等于5的元素的起始位置:6
	it = search_n(vi.begin(), vi.end(), 3, 4, greater<int>());
	cout << distance(vi.begin(), it) << endl;//返回3个连续大于4的元素的起始位置:4
	return 0;
}

3.搜寻第一个子区间

Iterator search(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end);iterator1原串,iterator子串

Iterator search(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end,BinaryPredicate op);

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;

bool cmp(int elem,bool even)
{
	if (even)
	{
		return elem % 2 == 0;
	}
	else
		return elem % 2 == 1;
}

int main()
{
	vector<int>v1,v2;
	vector<int>::iterator it;
	bool check[] = { true,false,true };
	for (int i = -2; i < 10; i += 2)
	{
		v1.push_back(i);
	}
	for (int i = 4; i < 10; i += 2)
	{
		v2.push_back(i);
	}
	it = search(v1.begin(),v1.end(),v2.begin(),v2.end());
	cout << distance(v1.begin(),it) << endl;//返回子串在原串的起始位置:3
	it = search(v1.begin(),v1.end(),check,check+3,cmp);
	cout << distance(v1.begin(),it) << endl;//返回满足偶奇偶顺序的子串起始位置:6(即没有)
	return 0;
}

4.搜寻最后一个子区间(类似于3)

Iterator find_end(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end);iterator1原串,iterator子串

Iterator find_end(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end,BinaryPredicate op);

5.搜寻某些元素的第一次出现位置(类似于search)

Iterator find_first_of(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end);iterator1原串,iterator子串

Iterator find_first_of(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end,BinaryPredicate op);

6.搜寻两个连续相等的元素(或者连续满足条件的)

Iterator adjacent_find(Iterator beg,Iterator end);

Iterator adjacent_find(Iterator beg,Iterator end,Predicate op);

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;

bool doubles(int elem1,int elem2)
{
	return elem1 * 2 == elem2;
}

int main()
{
	vector<int>v1;
	vector<int>::iterator it;
	v1.push_back(-2);
	for (int i = -2; i < 10; i += 2)
	{
		v1.push_back(i);
	}
	it = adjacent_find(v1.begin(),v1.end());
	cout << distance(v1.begin(),it) << endl;//返回连续相等元素的起始位置:3
	it = adjacent_find(v1.begin(),v1.end(),doubles);
	cout << distance(v1.begin(),it) << endl;//返回第二个元素是第一个元素两倍起始位置:3
	return 0;
}

HTML简介

1.基本结构

<html></html>:html文档被其包裹

<head></head>:html头文件标记,不在浏览器中显示

<title></title>:html文件标题,网页主题

<body></body>:网页的主体部分,在浏览器中显示

<meta></meta>:网页的元信息,提供有关页面的原信息,比如针对搜索引擎和更新频度的描述和关键词。meta标记必须放在head元素里面

2.文档设置标记

多线程

一、创建

1.用threading模块创建多线程

①把一个函数传入并创建Thread实例,然后用start启动

import random,time,threading

def thread_run(urls):
print('Current %s is running'%threading.current_thread().name)
for url in urls:
print('%s---->%s'%(threading.current_thread().name,url))
time.sleep(random.random())
print('%s ended.'%threading.current_thread().name)

print('%s is running '%threading.current_thread().name)
t1=threading.Thread(target=thread_run,name='Thread_1',args=(['url_1','url_2','url_3'],))
t2=threading.Thread(target=thread_run,name='Thread_2',args=(['url_4','url_5','url_6'],))
t1.start()
t2.start()
t1.join()
t2.join()
print('%s ended.'%threading.current_thread().name)
# MainThread is running
# Current Thread_1 is running
# Thread_1---->url_1
# Current Thread_2 is running
# Thread_2---->url_4
# Thread_2---->url_5
# Thread_2---->url_6
# Thread_2 ended.
# Thread_1---->url_2
# Thread_1---->url_3
# Thread_1 ended.
# MainThread ended.

②从threading.Thread继承并创建线程类,然后重写__init__() 和run()方法

import random, time, threading


class myThread(threading.Thread):
    def __init__(self, name, urls):
        super().__init__(name=name)
        self.urls = urls

    def run(self):
        print('Current %s is running' % threading.current_thread().name)
        for url in self.urls:
            print('%s---->%s' % (threading.current_thread().name, url))
            time.sleep(random.random())
        print('%s ended.' % threading.current_thread().name)


print('%s is running' % threading.current_thread().name)
t1 = myThread(name='Thread_1', urls=['url_1', 'url_2', 'url_3'])
t2 = myThread(name='Thread_2', urls=['url_4', 'url_5', 'url_6'])
t1.start()
t2.start()
t1.join()
t2.join()
# MainThread is running
# Current Thread_1 is running
# Thread_1---->url_1
# Current Thread_2 is running
# Thread_2---->url_4
# Thread_2---->url_5
# Thread_2---->url_6
# Thread_1---->url_2
# Thread_2 ended.
# Thread_1---->url_3
# Thread_1 ended.
# MainThread ended.
2.使用threading.Thread
from threading import Thread

二、实例

def main(cand):
    pass

t1 = Thread(target=main, args=(cand_list_key[0],))
threads.append(t1)
t2 = Thread(target=main, args=(cand_list_key[1],))
threads.append(t2)
t3 = Thread(target=main, args=(cand_list_key[2],))
threads.append(t3)
t4 = Thread(target=main, args=(cand_list_key[3],))
threads.append(t4)
for t in threads:
    t.start()
for t in threads:  # 所有子线程结束了,再继续主线程
    t.join()
print('OK!')

多线程读写文件时,需要分块操作,每一块开一个线程。

ref:Python多线程循环

Python 机器学习库 NumPy 教程

Python 机器学习库 NumPy 教程

一.介绍

NumPy的数组类是ndarray,它有一个别名是 numpy.array,但这与Python标准库的array.array并不一样。后者仅仅是一个一维数组。而ndarray具有以下的属性:

  • ndarray.ndim:数组的维数。在Python世界中,维数称之为rank
  • ndarray.shape:数组的维度。这是一系列数字,长度由数组的维度(ndim)决定。例如:长度为n的一维数组的shape是n。一个n行m列的矩阵的shape是n,m
  • ndarray.size:数组中所有元素的数量
  • ndarray.dtype:数组中元素的类型,例如numpy.int32, numpy.int16或者numpy.float64
  • ndarray.itemsize:数组中每个元素的大小,单位为字节
  • ndarray.data:存储数组元素的缓冲。通常我们只需要通过下标来访问元素,而不需要访问缓冲
import numpy as np

a=np.array([1,2,3])
b=np.array([(1,2,3),(4,5,6)])

print('a = ',a)
print("a's ndim {}".format(a.ndim))
print("a's shape {}".format(a.shape))
print("a's size {}".format(a.size))
print("a's dtype {}".format(a.dtype))
print("a's itemsize {}".format(a.itemsize))
print(' ')
print('b = ',b)
print("b's ndim {}".format(b.ndim))
print("b's shape {}".format(b.shape))
print("b's size {}".format(b.size))
print("b's dtype {}".format(b.dtype))
print("b's itemsize {}".format(b.itemsize))

# a = [1 2 3]
# a's ndim 1
# a's shape (3,)
# a's size 3
# a's dtype int32
# a's itemsize 4
# 
# b = [[1 2 3]
#      [4 5 6]]
# b's ndim 2
# b's shape (2, 3)
# b's size 6
# b's dtype int32
# b's itemsize 4

二.特定array的创建

  • zeros:用来创建元素全部是0的数组
  • ones:用来创建元素全部是1的数组
  • empty:用来创建未初始化的数据,因此是内容是不确定的
  • arange:通过指定范围和步长来创建数组
  • linespace:通过指定范围和元素数量来创建数组
  • random:用来生成随机数
import numpy as np

a = np.zeros((2,3))
print('np.zeros((2,3)= \n{}'.format(a))

b = np.ones((2,3))
print('np.ones((2,3))= \n{}'.format(b))

c = np.empty((2,3))
print('np.empty((2,3))= \n{}'.format(c))

d = np.arange(1, 2, 0.3)
print('np.arange(1, 2, 0.3)= \n{}'.format(d))

e = np.linspace(1, 2, 7)
print('np.linspace(1, 2, 7)= \n{}'.format(e))

f = np.random.random((2,3))
print('np.random.random((2,3))= \n{}'.format(f))

# np.zeros((2,3)=
# [[0. 0. 0.]
#  [0. 0. 0.]]
# np.ones((2,3))=
# [[1. 1. 1.]
#  [1. 1. 1.]]
# np.empty((2,3))=
# [[1. 1. 1.]
#  [1. 1. 1.]]
# np.arange(1, 2, 0.3)=
# [1.  1.3 1.6 1.9]
# np.linspace(1, 2, 7)=
# [1.         1.16666667 1.33333333 1.5        1.66666667 1.83333333
#  2.        ]
# np.random.random((2,3))= 
# [[0.66485083 0.65070901 0.13474133]
#  [0.60233868 0.09831888 0.46165572]]

三.Shape与操作

  • reshape:根据已有数组和指定的shape,生成一个新的数组
  • vstack:用来将多个数组在垂直(v代表vertical)方向拼接(数组的维度必须匹配)
  • hstack:用来将多个数组在水平(h代表horizontal)方向拼接(数组的维度必须匹配)
  • hsplit:用来将数组在水平方向拆分
  • vsplit:用来将数组在垂直方向拆分
import numpy as np

zero_line = np.zeros((1,3))
one_column = np.ones((3,1))
print("zero_line = \n{}\n".format(zero_line))
print("one_column = \n{}\n".format(one_column))

a = np.array([(1,2,3), (4,5,6)])
b = np.arange(11, 20)
print("a = \n{}\n".format(a))
print("b = \n{}\n".format(b))

# zero_line =
# [[0. 0. 0.]]
# one_column =
# [[1.]
#  [1.]
#  [1.]]
# a =
# [[1 2 3]
#  [4 5 6]]
# b =
# [11 12 13 14 15 16 17 18 19]

b = b.reshape(3, -1)#一个3行3列的矩阵,第二参数设为-1,表示根据实际情况自动决定
print("b.reshape(3, -1) = \n{}\n".format(b))
# b.reshape(3, -1) =
# [[11 12 13]
#  [14 15 16]
#  [17 18 19]]

c = np.vstack((a, b, zero_line))#三个数组在垂直方向拼接
print("c = np.vstack((a,b, zero_line)) = \n{}\n".format(c))
# c = np.vstack((a,b, zero_line)) =
# [[ 1.  2.  3.]
#  [ 4.  5.  6.]
#  [11. 12. 13.]
#  [14. 15. 16.]
#  [17. 18. 19.]
#  [ 0.  0.  0.]]

a = a.reshape(3, 2)
print("a.reshape(3, 2) = \n{}\n".format(a))

d = np.hstack((a, b, one_column))#hstack进行水平方向的拼接
print("d = np.hstack((a,b, one_column)) = \n{}\n".format(d))
# a.reshape(3, 2) =
# [[1 2]
#  [3 4]
#  [5 6]]
# d = np.hstack((a,b, one_column)) =
# [[ 1.  2. 11. 12. 13.  1.]
#  [ 3.  4. 14. 15. 16.  1.]
#  [ 5.  6. 17. 18. 19.  1.]]

e = np.hsplit(d, 3) # Split a into 3 将数组d在水平方向拆分成3个数组
print("e = np.hsplit(d, 3) = \n{}\n".format(e))
# e = np.hsplit(d, 3) =
# [array([[1., 2.],
#        [3., 4.],
#        [5., 6.]]), array([[11., 12.],
#        [14., 15.],
#        [17., 18.]]), array([[13.,  1.],
#        [16.,  1.],
#        [19.,  1.]])]

f = np.hsplit(d, (1, 3)) # # Split a after the 1st and the 3rd column
#将数组d从第1列和第3列两个地方进行拆分
print("f = np.hsplit(d, (1, 3)) = \n{}\n".format(f))
# f = np.hsplit(d, (1, 3)) =
# [array([[1.],
#        [3.],
#        [5.]]), array([[ 2., 11.],
#        [ 4., 14.],
#        [ 6., 17.]]), array([[12., 13.,  1.],
#        [15., 16.,  1.],
#        [18., 19.,  1.]])]

g = np.vsplit(d, 3)#将数组d在垂直方向进行拆分
print("np.hsplit(d, 2) = \n{}\n".format(g))
# np.hsplit(d, 2) =
# [array([[ 1.,  2., 11., 12., 13.,  1.]]), array([[ 3.,  4., 14., 15., 16.,  1.]]), array([[ 5.,  6., 17., 18., 19.,  1.]])]

四.索引

import numpy as np

base_data = np.arange(100, 200)
print("base_data\n={}\n".format(base_data))
print("base_data[10] = {}\n".format(base_data[10]))
# base_data
# =[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#  118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#  136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#  154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#  172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#  190 191 192 193 194 195 196 197 198 199]
#
# base_data[10] = 110

every_five = np.arange(0, 100, 5)#创建一个包含了若干个下标的数组来获取目标数组中的元素
print("base_data[every_five] = \n{}\n".format(base_data[every_five]))
# base_data[every_five] =
# [100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185
#  190 195]

a = np.array([(1,2), (10,20)])#多维
print("a = \n{}\n".format(a))
print("base_data[a] = \n{}\n".format(base_data[a]))
# a =
# [[1  2]
#  [10 20]]
# base_data[a] =
# [[101 102]
#  [110 120]]

base_data2 = base_data.reshape(10, -1)#一个10X10的二维数组
print("base_data2 = np.reshape(base_data, (10, -1)) = \n{}\n".format(base_data2))
# base_data2 = np.reshape(base_data, (10, -1)) =
# [[100 101 102 103 104 105 106 107 108 109]
#  [110 111 112 113 114 115 116 117 118 119]
#  [120 121 122 123 124 125 126 127 128 129]
#  [130 131 132 133 134 135 136 137 138 139]
#  [140 141 142 143 144 145 146 147 148 149]
#  [150 151 152 153 154 155 156 157 158 159]
#  [160 161 162 163 164 165 166 167 168 169]
#  [170 171 172 173 174 175 176 177 178 179]
#  [180 181 182 183 184 185 186 187 188 189]
#  [190 191 192 193 194 195 196 197 198 199]]

#假设我们只指定了一个下标,则访问的结果仍然是一个数组。
print("base_data2[2] = \n{}\n".format(base_data2[2]))
#假设我们指定了两个下标,则访问得到的是其中的元素
print("base_data2[2, 3] = \n{}\n".format(base_data2[2, 3]))
#我们也可以通过”-1”来指定“最后一个”的元素
print("base_data2[-1, -1] = \n{}\n".format(base_data2[-1, -1]))
# base_data2[2] =
# [120 121 122 123 124 125 126 127 128 129]
# base_data2[2, 3] =
# 123
# base_data2[-1, -1] =
# 199

#通过”:“的形式来指定范围
print("base_data2[2, :] = \n{}\n".format(base_data2[2, :]))#获取下标为2的行的所有元素
print("base_data2[:, 3] = \n{}\n".format(base_data2[:, 3]))#获取下标为3的列的所有元素
print("base_data2[2:5, 2:4] = \n{}\n".format(base_data2[2:5, 2:4]))#获取下标为[2,5)行,下标为[2,4)列的所有元素
# base_data2[2, :] =
# [120 121 122 123 124 125 126 127 128 129]
# base_data2[:, 3] =
# [103 113 123 133 143 153 163 173 183 193]
# base_data2[2:5, 2:4] =
# [[122 123]
#  [132 133]
#  [142 143]]

五.数学运算

import numpy as np

base_data = (np.random.random((5, 5)) - 0.5) * 100
print("base_data = \n{}\n".format(base_data))

print("np.amin(base_data) = {}".format(np.amin(base_data)))
print("np.amax(base_data) = {}".format(np.amax(base_data)))
print("np.average(base_data) = {}".format(np.average(base_data)))
print("np.sum(base_data) = {}".format(np.sum(base_data)))
print("np.sin(base_data) = \n{}".format(np.sin(base_data)))
# base_data =
# [[-35.66944285   4.56307029   7.27959772  26.49144495   6.73664639]
#  [  0.89383741  15.6060851   -5.31012     -2.88094505  39.85015085]
#  [-14.30186595  -4.73183095 -43.68073461  27.57136779 -13.59920839]
#  [ -1.21448696 -26.7323584  -10.57146785  24.08097968   6.38022583]
#  [  9.60947876  44.01200782 -30.0014446  -19.35327035 -32.10892146]]
#
# np.amin(base_data) = -43.68073460775658
# np.amax(base_data) = 44.012007822691956
# np.average(base_data) = -1.0832481944987336
# np.sum(base_data) = -27.08120486246834
# np.sin(base_data) =
# [[ 0.8965524  -0.98887266  0.83952719  0.97759255  0.43807945]
#  [ 0.77948133  0.10170203  0.82661465 -0.25770633  0.83632963]
#  [-0.98646775  0.99981101  0.2970126   0.64648344 -0.85875647]
#  [-0.93719056 -0.99958471  0.9114068  -0.86829838  0.09688829]
#  [-0.18365243  0.0297063   0.98780776 -0.48268194 -0.63884415]]

六.矩阵

import numpy as np

base_data = np.floor((np.random.random((5, 5)) - 0.5) * 100)
print("base_data = \n{}\n".format(base_data))

print("base_data.T = \n{}\n".format(base_data.T))
print("base_data.transpose() = \n{}\n".format(base_data.transpose()))

matrix_one = np.ones((5, 5))
print("matrix_one = \n{}\n".format(matrix_one))

minus_one = np.dot(matrix_one, -1)#乘法
print("minus_one = \n{}\n".format(minus_one))

print("np.dot(base_data, minus_one) = \n{}\n".format(np.dot(base_data, minus_one)))#乘法

# base_data =
# [[ -1. -32.  -2.   6. -41.]
#  [-29.  16.  13.  19.  -5.]
#  [-25. -10. -18. -41.  48.]
#  [-48.  -3. -31.  46.  44.]
#  [-14. -37.  30.  27.  17.]]
# base_data.T =
# [[ -1. -29. -25. -48. -14.]
#  [-32.  16. -10.  -3. -37.]
#  [ -2.  13. -18. -31.  30.]
#  [  6.  19. -41.  46.  27.]
#  [-41.  -5.  48.  44.  17.]]
# base_data.transpose() =
# [[ -1. -29. -25. -48. -14.]
#  [-32.  16. -10.  -3. -37.]
#  [ -2.  13. -18. -31.  30.]
#  [  6.  19. -41.  46.  27.]
#  [-41.  -5.  48.  44.  17.]]
# matrix_one =
# [[1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]
#  [1. 1. 1. 1. 1.]]
# minus_one =
# [[-1. -1. -1. -1. -1.]
#  [-1. -1. -1. -1. -1.]
#  [-1. -1. -1. -1. -1.]
#  [-1. -1. -1. -1. -1.]
#  [-1. -1. -1. -1. -1.]]
# np.dot(base_data, minus_one) =
# [[ 70.  70.  70.  70.  70.]
#  [-14. -14. -14. -14. -14.]
#  [ 46.  46.  46.  46.  46.]
#  [ -8.  -8.  -8.  -8.  -8.]
#  [-23. -23. -23. -23. -23.]]

七.随机数

import numpy as np
#生成20个随机数,它们每一个都是[0.0, 1.0)之间
print("random: {}\n".format(np.random.random(20)));
#根据指定的shape生成随机数
print("rand: {}\n".format(np.random.rand(3, 4)));
#生成指定范围内([0, 100))的指定数量(20)的随机整数
print("randint: {}\n".format(np.random.randint(0, 100, 20)));
#对已有的数据([0, 1, 2, ..., 19])的顺序随机打乱顺序
print("permutation: {}\n".format(np.random.permutation(np.arange(20))));

# random: [0.95075516 0.87381298 0.44899112 0.59610248 0.96922852 0.79234569
#  0.15103866 0.04605819 0.81143152 0.10017509 0.09332543 0.2620653
#  0.46254723 0.57771349 0.30671189 0.6956188  0.47801939 0.83740912
#  0.95743037 0.84286029]
# 
# rand: [[0.49654786 0.68419816 0.23360294 0.97797805]
#  [0.63537403 0.71530047 0.40076013 0.10561536]
#  [0.42796733 0.10005927 0.41320619 0.90237485]]
#
# randint: [49 11 50 16 86 24 17 37 61 36 10 73 75  0 27 60 38 88 28 66]
#
# permutation: [ 4 16 19  5 14 10  0 18 17 13  9  1 15 11  2 12  7  3  8  6]