first
This commit is contained in:
commit
8df544c088
|
@ -0,0 +1,115 @@
|
|||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# celery beat schedule file
|
||||
celerybeat-schedule
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
# enumerate 可以加索引
|
||||
fruit = ["apple",'orange',"banana","balabala"]
|
||||
|
||||
for i,x in enumerate(fruit):
|
||||
print(i,x)
|
||||
|
||||
# 反方向输出
|
||||
for i,x in enumerate(reversed(fruit)):
|
||||
print(i,x)
|
||||
|
||||
# 按照字典顺序输出
|
||||
for i,x in enumerate(sorted(fruit)):
|
||||
print(i,x)
|
||||
###
|
|
@ -0,0 +1,5 @@
|
|||
推荐编程环境
|
||||
http://repl.it/
|
||||
aaaaaaaaaaaaa
|
||||
bbbbbbbbbbbb
|
||||
cccccccccccc
|
|
@ -0,0 +1,4 @@
|
|||
#自动关闭文件
|
||||
with open('mytest.txt') as file_in:
|
||||
for line in file_in:
|
||||
print(line)
|
|
@ -0,0 +1,24 @@
|
|||
# 原始
|
||||
def fibonacci(n):
|
||||
a = 0
|
||||
b = 1
|
||||
nums = []
|
||||
for _ in range(n):
|
||||
nums.append(a)
|
||||
a,b = b,a+b
|
||||
return nums
|
||||
|
||||
print(fibonacci(12))
|
||||
|
||||
def fibonacci(n):
|
||||
a = 0
|
||||
b = 1
|
||||
# nums = []
|
||||
for _ in range(n):
|
||||
yield a
|
||||
# nums.append(a)
|
||||
a,b = b,a+b
|
||||
# return nums
|
||||
|
||||
for i in fibonacci(12):
|
||||
print(i)
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
score = int(input())
|
||||
if score >=60:
|
||||
print("pass")
|
||||
else:
|
||||
print("fail")
|
||||
|
||||
s = "pass" if score>=60 else "fail"
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
# 都变成大写
|
||||
fruit = ["apple",'orange',"banana","balabala"]
|
||||
print(fruit)
|
||||
fruit = [x.upper() for x in fruit]
|
||||
print(fruit)
|
||||
|
||||
#挑选出以b开头的水果
|
||||
b = []
|
||||
for x in fruit:
|
||||
if x.startswith("B"):
|
||||
b.append(x)
|
||||
print(b)
|
||||
|
||||
b = [x.lower() for x in fruit if x.startswith("B")]
|
||||
print(b)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
# 交换变量
|
||||
# 原始
|
||||
a = 1
|
||||
b =2
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp
|
||||
|
||||
# 实际
|
||||
|
||||
a,b = b,a
|
|
@ -0,0 +1,14 @@
|
|||
a = {"aaa":111,"bbb":222,"ccc":333}
|
||||
b = {"ddd":444,"eee":555,"fff":666}
|
||||
|
||||
# 合并字典
|
||||
c = {}
|
||||
for x in a:
|
||||
c[x] = a[x]
|
||||
for x in b:
|
||||
c[x] = b[x]
|
||||
print(c)
|
||||
|
||||
# 改变后
|
||||
c = {**a,**b}
|
||||
print(c)
|
|
@ -0,0 +1,4 @@
|
|||
name = "zhang san"
|
||||
|
||||
a,b = name.split()
|
||||
print(a,b)
|
|
@ -0,0 +1,11 @@
|
|||
name ="Tom"
|
||||
age = 16
|
||||
|
||||
print("Hello Myname is "+name+" and I am "+str(age))
|
||||
|
||||
print("Hello Myname is {} and I am {}".format(name,age))
|
||||
|
||||
print("Hello Myname is {0} and My age is {1}".format(name,age))
|
||||
|
||||
# 最常用 fstring
|
||||
print(f"Hello Myname is {name} and I am {age+1}")
|
|
@ -0,0 +1,121 @@
|
|||
木兰宽松许可证, 第1版
|
||||
|
||||
木兰宽松许可证, 第1版
|
||||
2019年8月 http://license.coscl.org.cn/MulanPSL
|
||||
|
||||
您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第1版(“本许可证”)的如下条款的约束:
|
||||
|
||||
0. 定义
|
||||
|
||||
“软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
|
||||
|
||||
“贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
|
||||
|
||||
“法人实体”是指提交贡献的机构及其“关联实体”。
|
||||
|
||||
“关联实体”是指,对“本许可证”下的一方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
|
||||
|
||||
“贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
|
||||
|
||||
1. 授予版权许可
|
||||
|
||||
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
|
||||
|
||||
2. 授予专利许可
|
||||
|
||||
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括仅因您或他人修改“贡献”或其他结合而将必然会侵犯到的专利权利要求。如您或您的“关联实体”直接或间接地(包括通过代理、专利被许可人或受让人),就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
|
||||
|
||||
3. 无商标许可
|
||||
|
||||
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。
|
||||
|
||||
4. 分发限制
|
||||
|
||||
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
|
||||
|
||||
5. 免责声明与责任限制
|
||||
|
||||
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
|
||||
|
||||
条款结束。
|
||||
|
||||
如何将木兰宽松许可证,第1版,应用到您的软件
|
||||
|
||||
如果您希望将木兰宽松许可证,第1版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
|
||||
|
||||
1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
|
||||
|
||||
2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
|
||||
|
||||
3, 请将如下声明文本放入每个源文件的头部注释中。
|
||||
|
||||
Copyright (c) [2019] [name of copyright holder]
|
||||
[Software Name] is licensed under the Mulan PSL v1.
|
||||
You can use this software according to the terms and conditions of the Mulan PSL v1.
|
||||
You may obtain a copy of Mulan PSL v1 at:
|
||||
http://license.coscl.org.cn/MulanPSL
|
||||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
PURPOSE.
|
||||
See the Mulan PSL v1 for more details.
|
||||
|
||||
|
||||
Mulan Permissive Software License,Version 1
|
||||
|
||||
Mulan Permissive Software License,Version 1 (Mulan PSL v1)
|
||||
August 2019 http://license.coscl.org.cn/MulanPSL
|
||||
|
||||
Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v1 (this License) with following terms and conditions:
|
||||
|
||||
0. Definition
|
||||
|
||||
Software means the program and related documents which are comprised of those Contribution and licensed under this License.
|
||||
|
||||
Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
|
||||
|
||||
Legal Entity means the entity making a Contribution and all its Affiliates.
|
||||
|
||||
Affiliates means entities that control, or are controlled by, or are under common control with a party to this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
|
||||
|
||||
Contribution means the copyrightable work licensed by a particular Contributor under this License.
|
||||
|
||||
1. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
|
||||
|
||||
2. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed, excluding of any patent claims solely be infringed by your or others’ modification or other combinations. If you or your Affiliates directly or indirectly (including through an agent, patent licensee or assignee), institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
|
||||
|
||||
3. No Trademark License
|
||||
|
||||
No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in section 4.
|
||||
|
||||
4. Distribution Restriction
|
||||
|
||||
You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
|
||||
|
||||
5. Disclaimer of Warranty and Limitation of Liability
|
||||
|
||||
The Software and Contribution in it are provided without warranties of any kind, either express or implied. In no event shall any Contributor or copyright holder be liable to you for any damages, including, but not limited to any direct, or indirect, special or consequential damages arising from your use or inability to use the Software or the Contribution in it, no matter how it’s caused or based on which legal theory, even if advised of the possibility of such damages.
|
||||
|
||||
End of the Terms and Conditions
|
||||
|
||||
How to apply the Mulan Permissive Software License,Version 1 (Mulan PSL v1) to your software
|
||||
|
||||
To apply the Mulan PSL v1 to your work, for easy identification by recipients, you are suggested to complete following three steps:
|
||||
|
||||
i. Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
|
||||
ii. Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
|
||||
iii. Attach the statement to the appropriate annotated syntax at the beginning of each source file.
|
||||
|
||||
Copyright (c) [2019] [name of copyright holder]
|
||||
[Software Name] is licensed under the Mulan PSL v1.
|
||||
You can use this software according to the terms and conditions of the Mulan PSL v1.
|
||||
You may obtain a copy of Mulan PSL v1 at:
|
||||
http://license.coscl.org.cn/MulanPSL
|
||||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
PURPOSE.
|
||||
|
||||
See the Mulan PSL v1 for more details.
|
|
@ -0,0 +1,8 @@
|
|||
from collections import defaultdict
|
||||
person = defaultdict(lambda : 'Key Not found') # 初始默认所有key对应的value均为‘Key Not Found’
|
||||
|
||||
person['name'] = 'xiaobai'
|
||||
person['age'] = 18
|
||||
|
||||
print ("The value of key 'name' is : ",person['name'])
|
||||
print ("The value of key 'adress' is : ",person['city'])
|
|
@ -0,0 +1,8 @@
|
|||
from collections import Counter
|
||||
|
||||
count_list = Counter(['B','B','A','B','C','A','B','B','A','C']) #计数list
|
||||
print (count_list)
|
||||
|
||||
|
||||
count_tuple = Counter((2,2,2,3,1,3,1,1,1)) #计数tuple
|
||||
print(count_tuple)
|
|
@ -0,0 +1,2 @@
|
|||
hello neuedu!
|
||||
今天天气真好
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,34 @@
|
|||
# 在需要在容器两端的更快的添加和移除元素的情况下,可以使用deque.
|
||||
|
||||
# deque就是一个可以两头操作的容器,类似list但比列表速度更快
|
||||
|
||||
from collections import deque
|
||||
d = deque()
|
||||
d.append(1)
|
||||
d.append(2)
|
||||
d.append(3)
|
||||
|
||||
print(len(d))
|
||||
print(d[0])
|
||||
print(d[-1])
|
||||
|
||||
|
||||
d = deque([i for i in range(5)])
|
||||
print(len(d))
|
||||
# Output: 5
|
||||
|
||||
d.popleft() # 删除并返回最左端的元素
|
||||
# Output: 0
|
||||
|
||||
d.pop() # 删除并返回最右端的元素
|
||||
# Output: 4
|
||||
|
||||
print(d)
|
||||
# Output: deque([1, 完整例子, 3])
|
||||
|
||||
d.append(100) # 从最右端添加元素
|
||||
|
||||
d.appendleft(-100) # 从最左端添加元素
|
||||
|
||||
print(d)
|
||||
# Output: deque([-100, 1, 完整例子, 3, 100])
|
|
@ -0,0 +1,22 @@
|
|||
# for 循环语句
|
||||
|
||||
for letter in 'www.neuedu.com':
|
||||
print(letter)
|
||||
|
||||
for value in range(1, 5):
|
||||
print(value)
|
||||
|
||||
for num in range(10,20): # 迭代 10 到 20 之间的数字
|
||||
for i in range(2,num): # 根据因子迭代
|
||||
if num%i == 0: # 确定第一个因子
|
||||
j=num/i # 计算第二个因子
|
||||
print ('%d 是一个合数' % num)
|
||||
break # 跳出当前循环
|
||||
else: # 循环的 else 部分
|
||||
print ('%d 是一个质数' % num)
|
||||
|
||||
# 打印九九乘法表
|
||||
for i in range(1, 10):
|
||||
for j in range(1, i+1):
|
||||
print('{}x{}={}\t'.format(i, j, i*j), end='')
|
||||
print()
|
|
@ -0,0 +1,10 @@
|
|||
41--------1072
|
||||
49--------1068
|
||||
77--------1063
|
||||
69--------1062
|
||||
42--------1057
|
||||
55--------1056
|
||||
74--------1049
|
||||
59--------1049
|
||||
44--------1045
|
||||
33--------1044
|
|
@ -0,0 +1,28 @@
|
|||
person = ('xiaobai', 18)
|
||||
print(person[0],person[1])
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
Person = namedtuple('Person', 'name age city') # 类似于定义class
|
||||
xiaobai = Person(name="xiaobai", age=18, city="paris") # 类似于新建对象
|
||||
print(xiaobai)
|
||||
|
||||
|
||||
import collections
|
||||
# 将纸牌定义为具名元组,每个纸牌都有等级和花色
|
||||
Card = collections.namedtuple('Card', 'rank suit')
|
||||
|
||||
class FrenchDeck:
|
||||
# 等级2-A
|
||||
ranks = [str(n) for n in range(2,11)] + list('JQKA')
|
||||
# 花色红黑方草
|
||||
suits = 'spades diamonds clubs hearts'.split()
|
||||
# 构建纸牌
|
||||
def __init__(self):
|
||||
self.cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]
|
||||
|
||||
|
||||
french_deck = FrenchDeck()
|
||||
print(french_deck.cards[0])
|
||||
print(french_deck.cards[0].rank)
|
||||
print(french_deck.cards[0].suit)
|
|
@ -0,0 +1,27 @@
|
|||
# 概念:包就是一个文件夹,里面包含了若干py文件以及一个_init_.py文件。
|
||||
|
||||
# 方式一:from 包名 import 模块名
|
||||
from syu import module
|
||||
module.output()
|
||||
|
||||
# 方式二:from 包名.模块名 import 函数名
|
||||
from syu.module import output
|
||||
output()
|
||||
|
||||
# 方式三 :import 包名.模块名
|
||||
import syu.module as tt
|
||||
tt.output()
|
||||
|
||||
# 方式四:from 包名 import *
|
||||
# 前提是:将 init.py 文件中写入all变量(写入方式同模块导入的写入方式) 。 变量当中写入哪个模块则导入哪个模块,不写则什么都不导入 使用时:模块名.函数名() _init_.py
|
||||
from syu import *
|
||||
module.output()
|
||||
|
||||
# 方式五:import 包名
|
||||
# 前提是:在包里面的init.py 文件里写入 from . import 模块名 init.py里面导入哪个模块 通过本方式就能使用哪个模块
|
||||
# from . import module
|
||||
import syu
|
||||
syu.module.output()
|
||||
|
||||
# from syu import *
|
||||
# module1.output1()
|
|
@ -0,0 +1,3 @@
|
|||
__all__ = ['module1']
|
||||
def output():
|
||||
print('hello syu')
|
|
@ -0,0 +1,2 @@
|
|||
def output():
|
||||
print('hello syu')
|
|
@ -0,0 +1,2 @@
|
|||
def output1():
|
||||
print('hello syu')
|
|
@ -0,0 +1,16 @@
|
|||
# 一个.py文件就称为一个模块
|
||||
# 方式一:import 模块名
|
||||
import module
|
||||
module.output()
|
||||
# 方式二 :from 模块名 import 函数名
|
||||
from module import output
|
||||
output()
|
||||
# 方式三: from 模块名 import *
|
||||
from module import *
|
||||
output()
|
||||
# 方式四:from 模块名 import 函数名 as tt(自定义)
|
||||
from module import output as tt
|
||||
tt()
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
# While 循环语句
|
||||
count = 1
|
||||
sum = 0
|
||||
while count <= 100:
|
||||
sum = sum + count
|
||||
count = count + 1
|
||||
print(sum)
|
||||
|
||||
count = 1
|
||||
sum = 0
|
||||
while count <= 100:
|
||||
sum = sum + count
|
||||
if sum > 1000: #当 sum 大于 1000 的时候退出循环
|
||||
break
|
||||
count = count + 1
|
||||
print(sum)
|
||||
|
||||
count = 1
|
||||
sum = 0
|
||||
while count <= 100:
|
||||
if count % 2 == 0: # 双数时跳过输出
|
||||
count = count + 1
|
||||
continue
|
||||
sum = sum + count
|
||||
count = count + 1
|
||||
print(sum)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
x = (1,2,3)
|
||||
|
||||
x =(3)
|
||||
print(type(x))
|
||||
x =(3,)
|
||||
print(type(x))
|
|
@ -0,0 +1,29 @@
|
|||
|
||||
a_num = [0.01,"zhangsan",[1,2,3]]
|
||||
a_num+=[2]
|
||||
print(a_num)
|
||||
print(a_num*2)
|
||||
# 使用列表的pop()方法删除并返回指定(默认为最后一个)位置上的元素,如果给定的索引超出了列表的范围则抛出异常。
|
||||
print(a_num.pop())
|
||||
print(a_num.pop(1))
|
||||
print(a_num)
|
||||
|
||||
# 删除首次出现的指定元素,如果列表中不存在要删除的元素,则抛出异常
|
||||
x = [1,2,2,3,4]
|
||||
x.remove(2)
|
||||
print(x)
|
||||
|
||||
|
||||
a_list = list("Hello")
|
||||
print(a_list)
|
||||
|
||||
a_list.append('w')
|
||||
print(a_list)
|
||||
|
||||
a_list.insert(1,2)
|
||||
print(a_list)
|
||||
|
||||
# 统计指定元素在列表对象中出现的次数。
|
||||
x =[1,2,3,3,4,5]
|
||||
print(x.count(3))
|
||||
print(3 in x)
|
|
@ -0,0 +1,11 @@
|
|||
# 列表切片
|
||||
|
||||
colors=["red","blue","green"]
|
||||
|
||||
colors[0] =="red"
|
||||
colors[-1]=="green"
|
||||
colors[1]=="blue"
|
||||
|
||||
aList = [3,5,7,9,11]
|
||||
del aList[:3]
|
||||
print(aList)
|
|
@ -0,0 +1,42 @@
|
|||
aList = [3, 4, 5, 6, 7, 9, 11, 13, 15, 17]
|
||||
import random
|
||||
random.shuffle(aList) #随机降序
|
||||
|
||||
print(aList)
|
||||
aList.sort() #默认是升序排序
|
||||
aList.sort(reverse = True) #降序排序
|
||||
print(aList)
|
||||
|
||||
|
||||
# 返回可迭代的zip对象
|
||||
aList = [1, 2, 3]
|
||||
bList = [4, 5, 6]
|
||||
cList = zip(aList, bList)
|
||||
print(list(cList))
|
||||
|
||||
# enumerate()
|
||||
for item in enumerate('abcdef'):
|
||||
print(item)
|
||||
|
||||
# 遍历列表的三种方式
|
||||
a = ['a','b','c','d','e','f']
|
||||
for i in a:
|
||||
print(i)
|
||||
for i in range(len(a)):
|
||||
print(i,a[i])
|
||||
for i,ele in enumerate(a):
|
||||
print("0000000 ",i,ele)
|
||||
|
||||
# 列表推导式
|
||||
lis = [i for i in range(100)]
|
||||
print(lis)
|
||||
|
||||
# 使用列表推导式实现嵌套列表的平铺
|
||||
vec = [[1,2,3], [4,5,6], [7,8,9]]
|
||||
lis = [num for elem in vec for num in elem]
|
||||
print ("1111111111 ",lis)
|
||||
|
||||
# 过滤不符合条件的元素 从列表中选择符合条件的元素组成新的列表
|
||||
aList = [-1,-4,6,7.5,-2.3,9,-11]
|
||||
lis = [i for i in aList if i>0]
|
||||
print ("2222222222 ",lis)
|
|
@ -0,0 +1,25 @@
|
|||
# 写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表
|
||||
|
||||
products = [['iphone',6888],['三星',3000],['小米',2500]]
|
||||
shopping_car = []
|
||||
flag = True
|
||||
while flag:
|
||||
print("******商品列表******")
|
||||
for index,i in enumerate(products):
|
||||
print("%s. %s| %s" %(index,i[0],i[1]))
|
||||
choice = input("请输入您想购买的商品的编号:")
|
||||
if choice.isdigit():#isdigit()判断变量是什么类型
|
||||
choice = int(choice)
|
||||
if choice>=0 and choice<len(products):
|
||||
shopping_car.append(products[choice])
|
||||
print("已经将%s加入购物车" %(products[choice]))
|
||||
else:
|
||||
print("该商品不存在")
|
||||
elif choice == "q":
|
||||
if len(shopping_car)>0:
|
||||
print("您打算购买以下商品:")
|
||||
for index,i in enumerate(shopping_car):
|
||||
print("%s. %s| %s" %(index,i[0],i[1]))
|
||||
else:
|
||||
print("您的购物车中没有添加商品")
|
||||
flag = False
|
|
@ -0,0 +1,62 @@
|
|||
# 实例1—— 学生注册
|
||||
i = 10
|
||||
f = 10.5
|
||||
b = True
|
||||
|
||||
print(i+f+b)
|
||||
|
||||
# 不同类型变量之间的计算
|
||||
first_name = "三"
|
||||
last_name = "张"
|
||||
print(first_name + last_name)
|
||||
|
||||
print("-" * 50)
|
||||
|
||||
first_name = "zhang"
|
||||
x = 10
|
||||
print(str(x)+first_name)
|
||||
|
||||
# 变量的输入
|
||||
# 1. 输入苹果单价
|
||||
price_str = input("请输入苹果价格:")
|
||||
|
||||
# 完整例子. 要求苹果重量
|
||||
weight_str = input("请输入苹果重量:")
|
||||
|
||||
# 3. 计算金额
|
||||
# 1> 将苹果单价转换成小数
|
||||
price = float(price_str)
|
||||
|
||||
# 完整例子> 将苹果重量转换成小数
|
||||
weight = float(weight_str)
|
||||
|
||||
# 3> 计算付款金额
|
||||
money = price * weight
|
||||
|
||||
print(money)
|
||||
|
||||
name ='小明'
|
||||
student_no = 12
|
||||
scale = 0.2
|
||||
|
||||
print("我的名字叫 %s,请多多关照!" % name)
|
||||
print("我的学号是 %06d" % student_no)
|
||||
print("苹果单价 %.02f 元/斤,购买 %.02f 斤,需要支付 %.02f 元" % (price, weight, money))
|
||||
print("数据比例是 %.02f%%" % (scale * 100))
|
||||
|
||||
str = "{}曰:学而时习之,不亦说乎。".format("孔子")
|
||||
print(str)
|
||||
|
||||
# 变量的命名
|
||||
|
||||
# 关键字
|
||||
import keyword
|
||||
print(keyword.kwlist)
|
||||
|
||||
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
|
||||
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while',
|
||||
'with', 'yield']
|
||||
|
||||
# 变量的命名规则
|
||||
userName = 'zhangsan'
|
||||
print(userName)
|
|
@ -0,0 +1,56 @@
|
|||
a_dict = {'server': 'db.neuedu.com', 'database': 'mysql'}
|
||||
# 使用dict()利用已有数据创建字典
|
||||
keys = ['a', 'b', 'c', 'd']
|
||||
values = [1, 2, 3, 4]
|
||||
dictionary = dict(zip(keys, values))
|
||||
print(dictionary)
|
||||
|
||||
x = dict() #空字典
|
||||
# 使用dict()根据给定的键、值创建字典
|
||||
d = dict(name='Dong', age=37)
|
||||
print(d)
|
||||
|
||||
# 以给定内容为键,创建值为空的字典
|
||||
adict = dict.fromkeys(['name', 'age', 'sex'])
|
||||
print(adict,adict['name'])
|
||||
|
||||
# 使用字典对象的items()方法可以返回字典的键、值对列表
|
||||
aDict={'name':'Dong', 'sex':'male', 'age':37}
|
||||
|
||||
# 使用字典对象的keys()方法可以返回字典的键列表
|
||||
for item in aDict.items():
|
||||
print(item)
|
||||
print(aDict.keys(),aDict.values())
|
||||
|
||||
# 使用字典对象的setdefault()方法返回指定“键”对应的“值”,如果字典中不存在该“键”,就添加一个新元素并设置该“键”对应的“值”。
|
||||
aDict ={'name' : 'Wang','sex' : 'male'}
|
||||
aDict.setdefault('age','28') #增加新元素
|
||||
aDict['address'] = 'SDIBT' #增加新元素
|
||||
print(aDict)
|
||||
|
||||
# 使用字典对象的update方法将另一个字典的键、值对添加到当前字典对象
|
||||
aDict = {'age': 37, 'score': [98, 97], 'name': 'Dong', 'sex': 'male'}
|
||||
print(aDict.items())
|
||||
aDict.update({'a':'a','b':'b'})
|
||||
print(aDict)
|
||||
|
||||
# 判断一个key是否在字典中
|
||||
d = {'name':'tom', 'age':10, 'Tel':110}
|
||||
print ('name' in d.keys())
|
||||
print ('name' in d)
|
||||
|
||||
# 无序字典
|
||||
x = dict()
|
||||
x['a'] = 3
|
||||
x['b'] = 5
|
||||
x['c'] = 8
|
||||
|
||||
# 有序字典
|
||||
import collections
|
||||
x = collections.OrderedDict()
|
||||
|
||||
|
||||
# 字典pop()方法
|
||||
x = {'a':1,'b':2}
|
||||
x.pop('a')
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# 例1
|
||||
strings = ['import','is','with','if','file','exception','liuhu']
|
||||
d = {key: val for val,key in enumerate(strings)}
|
||||
# 用字典推导式以字符串以及其长度位置建字典
|
||||
s = {strings[i]: len(strings[i]) for i in range(len(strings))}
|
||||
k = {k:len(k)for k in strings} #相比上一个写法简单很多
|
||||
print(d)
|
||||
# {'import': 0, 'is': 1, 'with': 完整例子, 'if': 3, 'file': 4, 'exception': 5, 'liuhu': 6}
|
||||
print(s)
|
||||
# {'import': 6, 'is': 完整例子, 'with': 4, 'if': 完整例子, 'file': 4, 'exception': 9, 'liuhu': 5}
|
||||
print(k)
|
||||
# {'import': 6, 'is': 完整例子, 'with': 4, 'if': 完整例子, 'file': 4, 'exception': 9, 'liuhu': 5}
|
||||
|
||||
# 例2
|
||||
mc = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
|
||||
mca = {k.lower(): mc.get(k.lower(), 0) + mc.get(k.upper(), 0) for k in mc.keys()}
|
||||
|
||||
# mcase_frequency == {'a': 17, 'z': 3, 'b': 34} she
|
|
@ -0,0 +1,32 @@
|
|||
# 1.1. 时间处理模块
|
||||
import time
|
||||
print(time.time())
|
||||
|
||||
|
||||
import datetime
|
||||
a = datetime.datetime(year=2000, month=1, day=1, hour=12)
|
||||
b = datetime.datetime(2000, 1, 1, 12, 0)
|
||||
print(a,b,datetime.datetime.today(),datetime.datetime.now(),datetime.datetime.now().timestamp(),datetime.datetime.now().weekday())
|
||||
|
||||
# 获取当前工作目录
|
||||
import sys
|
||||
|
||||
print(sys.path[0])
|
||||
# 获取执行命令的位置
|
||||
import os
|
||||
print(os.getcwd())
|
||||
|
||||
# 路径拼接
|
||||
import os
|
||||
|
||||
print(os.path.join('/Users/pangao', 'test.txt'))
|
||||
|
||||
# /Users/pangao/test.txt'
|
||||
|
||||
# 路径拆分
|
||||
import os
|
||||
|
||||
print(os.path.split('/Users/pangao/test.txt'),os.path.splitext('/Users/pangao/test.txt'))
|
||||
|
||||
# ('/Users/pangao/', 'test.txt')
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# 文件重命名
|
||||
import os
|
||||
os.rename('test.txt', 'test.py') #重命名
|
||||
# 删除文件
|
||||
import os
|
||||
|
||||
os.remove('test.txt') #删除
|
||||
# 复制文件
|
||||
import shutil
|
||||
|
||||
shutil.copyfile('test.txt', 'test.py')
|
||||
|
||||
# 遍历文件夹下的文件
|
||||
import os
|
||||
|
||||
for filename in os.listdir('/'):
|
||||
print(filename)
|
|
@ -0,0 +1,19 @@
|
|||
# 异常类型捕获演练 —— 要求用户输入整数
|
||||
while True:
|
||||
try:
|
||||
num = int(input("请输入整数:"))
|
||||
result = 8 / num
|
||||
print(result)
|
||||
except ValueError:
|
||||
print("请输入正确的整数")
|
||||
except ZeroDivisionError:
|
||||
print("除 0 错误")
|
||||
continue
|
||||
print("111111111111111")
|
||||
except Exception as result:
|
||||
print("未知错误 %s" % result)
|
||||
|
||||
else:
|
||||
print("正常执行")
|
||||
finally:
|
||||
print("执行完成,但是不保证正确")
|
|
@ -0,0 +1,12 @@
|
|||
def demo1():
|
||||
return int(input("请输入一个整数:"))
|
||||
|
||||
def demo2():
|
||||
return demo1()
|
||||
|
||||
try:
|
||||
print(demo2())
|
||||
except ValueError:
|
||||
print("请输入正确的整数")
|
||||
except Exception as result:
|
||||
print("未知错误 %s" % result)
|
|
@ -0,0 +1,21 @@
|
|||
def input_password():
|
||||
|
||||
# 1\. 提示用户输入密码
|
||||
pwd = input("请输入密码:")
|
||||
|
||||
# 完整例子\. 判断密码长度,如果长度 >= 8,返回用户输入的密码
|
||||
if len(pwd) >= 8:
|
||||
return pwd
|
||||
|
||||
# 3\. 密码长度不够,需要抛出异常
|
||||
# 1> 创建异常对象 - 使用异常的错误信息字符串作为参数
|
||||
ex = Exception("密码长度不够")
|
||||
|
||||
# 完整例子> 抛出异常对象
|
||||
raise ex
|
||||
|
||||
try:
|
||||
user_pwd = input_password()
|
||||
print(user_pwd)
|
||||
except Exception as result:
|
||||
print("发现错误:%s" % result)
|
|
@ -0,0 +1,21 @@
|
|||
# 用numpy读取文件
|
||||
import numpy as np
|
||||
data = np.genfromtxt('data.txt',delimiter=' ')
|
||||
print(data)
|
||||
|
||||
# 1\. 打开 - 文件名需要注意大小写
|
||||
file = open("data.txt",encoding='utf-8')
|
||||
print(file)
|
||||
# 完整例子\. 读取
|
||||
text = file.read()
|
||||
print(text)
|
||||
# 3\. 关闭
|
||||
file.close()
|
||||
# write 方法 —— 写入文件
|
||||
# 打开文件
|
||||
f = open("abc.txt", "w")
|
||||
print(f)
|
||||
f.write("hello neuedu!\n")
|
||||
f.write("今天天气真好")
|
||||
# 关闭文件
|
||||
f.close()
|
|
@ -0,0 +1,23 @@
|
|||
import random
|
||||
|
||||
f = open('data.txt', 'w+')
|
||||
for i in range(100000):
|
||||
f.write(str(random.randint(1,100)) + '\n')
|
||||
print(f.read())
|
||||
f.close()
|
||||
|
||||
from collections import Counter
|
||||
dict={}
|
||||
f = open('data.txt', 'r+')
|
||||
for i in f:
|
||||
if i not in dict:
|
||||
dict[i] = 1
|
||||
else:
|
||||
dict[i] = dict[i] + 1
|
||||
d = Counter(dict)
|
||||
with open('mostNum.txt', 'w+') as k:
|
||||
for i in d.most_common(10):
|
||||
k.write(f'{i[0].strip()}--------{i[1]}\n')
|
||||
k.seek(0, 0)
|
||||
print(k.read())
|
||||
f.close()
|
|
@ -0,0 +1,16 @@
|
|||
class Person:
|
||||
def __init__(self, name, age):
|
||||
self.name = name
|
||||
self.__age = age
|
||||
|
||||
#定义对私有属性的get方法,获取私有属性
|
||||
def getAge(self):
|
||||
return self.__age
|
||||
|
||||
#定义对私有属性的重新赋值的set方法,重置私有属性
|
||||
def setAge(self,age):
|
||||
self.__age = age
|
||||
|
||||
person1 = Person("tom",19)
|
||||
person1.setAge(20)
|
||||
print(person1.name,person1.getAge()) #tom 20
|
|
@ -0,0 +1,12 @@
|
|||
class MyException(Exception): #让MyException类继承Exception
|
||||
def __init__(self,name,age):
|
||||
self.name = name
|
||||
self.age = age
|
||||
try:
|
||||
#知识点:主动抛出异常,就是实例化一个异常类
|
||||
raise MyException("zhansgan",19) #实例化一个异常,实例化的时候需要传参数
|
||||
except MyException as obj: #这里体现一个封装,
|
||||
print(obj.age,obj.name) #捕获的就是MyException类携带过来的信息
|
||||
|
||||
except Exception as obj: #万能捕获,之前的可能捕获不到,这里添加Exception作为保底
|
||||
print(obj)
|
|
@ -0,0 +1,18 @@
|
|||
import random
|
||||
|
||||
random.randint(0, 10)
|
||||
random.random()
|
||||
|
||||
s = 'helloWorld'
|
||||
random.choice(s)
|
||||
|
||||
# random.sample(population, k):在一个序列或者集合中选择k个随机元素()
|
||||
random.sample('12345', 2)
|
||||
|
||||
# random.uniform(a, b):产生一个指定范围内的随机浮点数 若a < b,随机数n范围:a <= n <= b; 若a > b,随机数n范围:a<= n <= b;
|
||||
random.uniform(1,10)
|
||||
random.randrange(1, 10, 1) #[1,10)之间随机整数
|
||||
|
||||
# random.shuffle(x, random=None):将列表顺序打乱;
|
||||
l = ['C', 'C++', 'Java', 'C#', 'Python']
|
||||
random.shuffle(l)
|
|
@ -0,0 +1,23 @@
|
|||
# // map,保存最终分组结果
|
||||
# // key保存的是第几组,value是该组对应的国家集合
|
||||
import random
|
||||
groupNum2Countrys = {}
|
||||
|
||||
strCountrys = "科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚,日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利";
|
||||
countryList = strCountrys.split(",")
|
||||
|
||||
for i in range(4):
|
||||
lstGroup = []
|
||||
# // 分第1组
|
||||
# // 随机从集合中选出一个国家,放到第1组里;然后将这个选出的国家,从原来集合中干掉(删除)
|
||||
# // 重复以上步骤4次
|
||||
for j in range(4):
|
||||
selectIndex = random.randint(0,len(countryList)-1)
|
||||
lstGroup.append(countryList[selectIndex])
|
||||
countryList.remove(countryList[selectIndex])
|
||||
|
||||
groupNum2Countrys[i+1] = lstGroup
|
||||
|
||||
for key,value in groupNum2Countrys.items():
|
||||
print('第' + str(key) + '组')
|
||||
print(value)
|
|
@ -0,0 +1,20 @@
|
|||
word="I'm a boby, I'm a girl. When it is true, it is ture. that are cats, the red is red."
|
||||
word=word.replace(',','').replace('.','')
|
||||
word=word.split()
|
||||
print(word)
|
||||
print('第1种方法')
|
||||
setword=set(word)
|
||||
for i in setword:
|
||||
count=word.count(i)
|
||||
print(i,'出现次数:',count)
|
||||
print('第2种方法')
|
||||
dict = {}
|
||||
for key in word:
|
||||
dict[key] = dict.get(key, 0) + 1
|
||||
print(dict)
|
||||
print('第3种方法')
|
||||
from collections import Counter
|
||||
|
||||
result = Counter(dict)
|
||||
print(result)
|
||||
print(result.most_common(3))
|
|
@ -0,0 +1,40 @@
|
|||
# 4
|
||||
# YaoLin 87 82 Y N 0
|
||||
# ChenRuiyi 88 78 N Y 1
|
||||
# LiXin 92 88 N N 0
|
||||
# ZhangQin 83 87 Y N 1
|
||||
|
||||
num = int(input('请输入学生数量:'))
|
||||
std_dict = {}
|
||||
for i in range(num):
|
||||
line = input(f'请输入第{i+1}名学生信息')
|
||||
stu_lst = line.split(' ')
|
||||
std_dict[stu_lst[0]] = stu_lst[1:]
|
||||
|
||||
# print(std_dict)
|
||||
reward_dict = {}
|
||||
#开始计算每一位学生的奖学金
|
||||
for stu_name,stu_info in std_dict.items():
|
||||
if int(stu_info[0]) > 80 and int(stu_info[4]) > 0:
|
||||
reward_dict[stu_name] = reward_dict.get(stu_name,0) + 8000
|
||||
if int(stu_info[0]) > 85 and int(stu_info[1]) > 80:
|
||||
reward_dict[stu_name] = reward_dict.get(stu_name, 0) + 4000
|
||||
if int(stu_info[0]) > 90:
|
||||
reward_dict[stu_name] = reward_dict.get(stu_name, 0) + 2000
|
||||
if int(stu_info[0]) > 85 and stu_info[3] == 'Y':
|
||||
reward_dict[stu_name] = reward_dict.get(stu_name, 0) + 1000
|
||||
if int(stu_info[1]) > 80 and stu_info[2] == 'Y':
|
||||
reward_dict[stu_name] = reward_dict.get(stu_name, 0) + 850
|
||||
|
||||
sum = 0
|
||||
max = 0
|
||||
max_stu = ''
|
||||
for stu_name,money in reward_dict.items():
|
||||
if max < money:
|
||||
max = money
|
||||
max_stu = stu_name
|
||||
sum += money
|
||||
|
||||
print(max_stu)
|
||||
print(max)
|
||||
print(sum)
|
|
@ -0,0 +1,43 @@
|
|||
# 集合的创建
|
||||
a_set = set(range(8,14))
|
||||
#自动去除重复
|
||||
b_set = set([0, 1, 2, 3, 0, 1, 2, 3, 7, 8])
|
||||
#空集合
|
||||
c_set = set()
|
||||
|
||||
# 集合元素的增加与删除
|
||||
s = {1,2,3}
|
||||
s.add(3)
|
||||
s.update({3,4,5})
|
||||
s.remove(3)
|
||||
print(s)
|
||||
|
||||
# 集合操作
|
||||
a_set = set([8, 9, 10, 11, 12, 13])
|
||||
b_set = {0, 1, 2, 3, 7, 8}
|
||||
# 并集
|
||||
print(a_set | b_set,a_set.union(b_set) )
|
||||
#交集
|
||||
print(a_set & b_set,a_set.intersection(b_set) )
|
||||
#差集
|
||||
print(a_set.difference(b_set),a_set - b_set)
|
||||
|
||||
# 测试是否为子集
|
||||
x = {1, 2, 3}
|
||||
y = {1, 2, 5}
|
||||
z = {1, 2, 3, 4}
|
||||
print(x.issubset(y),x.issubset(z))
|
||||
|
||||
# 使用集合快速提取序列中单一元素
|
||||
import random
|
||||
listRandom = [random.choice(range(500)) for i in range(100)]
|
||||
noRepeat = []
|
||||
for i in listRandom :
|
||||
if i not in noRepeat :
|
||||
noRepeat.append(i)
|
||||
print(len(listRandom),len(noRepeat))
|
||||
newSet = set(listRandom)
|
||||
|
||||
# 集合推导式
|
||||
s = {x.strip() for x in (' he ', 'she ', ' I')}
|
||||
print(s)
|
|
@ -0,0 +1,42 @@
|
|||
class Cat:
|
||||
"""这是一个猫类"""
|
||||
def eat(self):
|
||||
print(f"小猫爱吃鱼,我是{self.name},self的地址是{id(self)}")
|
||||
def drink(self):
|
||||
print("小猫在喝水")
|
||||
|
||||
tom = Cat()
|
||||
print(f'tom对象的id是{id(tom)}')
|
||||
tom.name = "Tom"
|
||||
tom.eat()
|
||||
print('-'*60)
|
||||
lazy_cat = Cat()
|
||||
print(f'lazy_cat对象的id是{id(lazy_cat)}')
|
||||
lazy_cat.name = "大懒猫"
|
||||
lazy_cat.eat()
|
||||
|
||||
class Cat:
|
||||
def __init__(self, name):
|
||||
print("初始化方法 %s" % name)
|
||||
self.name = name
|
||||
|
||||
tom = Cat("Tom")
|
||||
lazy_cat = Cat("大懒猫")
|
||||
|
||||
|
||||
# _str_ 方法,如果在开发中,希望使用 print 输出 对象变量 时,能够打印 自定义的内容,就可以利用 __str__ 这个内置方法了
|
||||
|
||||
class Cat:
|
||||
|
||||
def __init__(self, new_name):
|
||||
|
||||
self.name = new_name
|
||||
|
||||
print("%s 来了" % self.name)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "我是小猫:%s" % self.name
|
||||
|
||||
tom = Cat("Tom")
|
||||
print(tom)
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
def CarInfo(type,price):
|
||||
print ("the car's type %s,price:%d"%(type,price))
|
||||
|
||||
print('函数方式(面向过程)')
|
||||
CarInfo('passat',250000)
|
||||
CarInfo('ford',280000)
|
||||
|
||||
|
||||
class Car:
|
||||
def __init__(self,type,price):
|
||||
self.type = type
|
||||
self.price = price
|
||||
|
||||
def printCarInfo(self):
|
||||
print ("the car's Info in class:type %s,price:%d"%(self.type,self.price))
|
||||
print('面向对象')
|
||||
carOne = Car('passat',250000)
|
||||
carTwo = Car('ford',250000)
|
||||
carOne.printCarInfo()
|
||||
carTwo.printCarInfo()
|
|
@ -0,0 +1,30 @@
|
|||
class Person(object):
|
||||
def __init__(self, name, gender):
|
||||
self.name = name
|
||||
self.gender = gender
|
||||
print("init")
|
||||
|
||||
def __call__(self, friend):
|
||||
print ('My name is %s...' % self.name)
|
||||
print ('My friend is %s...' % friend)
|
||||
|
||||
p = Person('Bob', 'male')
|
||||
p('Tim')
|
||||
|
||||
class Fib(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
def __call__(self,num):
|
||||
a,b = 0,1;
|
||||
self.l=[]
|
||||
|
||||
for i in range (num):
|
||||
self.l.append(a)
|
||||
a,b= b,a+b
|
||||
return self.l
|
||||
def __str__(self):
|
||||
return str(self.l)
|
||||
|
||||
|
||||
f = Fib()
|
||||
print(f(10))
|
|
@ -0,0 +1,3 @@
|
|||
aaaaaaaaaaaaa
|
||||
bbbbbbbbbbbb
|
||||
cccccccccccc
|
|
@ -0,0 +1,63 @@
|
|||
class Resource():
|
||||
def __enter__(self):
|
||||
print('===connect to resource===')
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
print('===close resource connection===')
|
||||
print(exc_type)
|
||||
print(exc_val)
|
||||
print(exc_tb)
|
||||
return True
|
||||
|
||||
def operate(self):
|
||||
1/0
|
||||
|
||||
with Resource() as res:
|
||||
res.operate()
|
||||
|
||||
|
||||
import contextlib
|
||||
|
||||
@contextlib.contextmanager
|
||||
def open_func(file_name):
|
||||
# __enter__方法
|
||||
print('open file:', file_name, 'in __enter__')
|
||||
file_handler = open(file_name, 'r')
|
||||
|
||||
try:
|
||||
yield file_handler
|
||||
except Exception as exc:
|
||||
# deal with exception
|
||||
print('the exception was thrown')
|
||||
finally:
|
||||
print('close file:', file_name, 'in __exit__')
|
||||
file_handler.close()
|
||||
|
||||
return
|
||||
|
||||
with open_func('mytest.txt') as file_in:
|
||||
for line in file_in:
|
||||
1/0
|
||||
print(line)
|
||||
|
||||
|
||||
import contextlib
|
||||
|
||||
@contextlib.contextmanager
|
||||
def open_func(file_name):
|
||||
# __enter__方法
|
||||
print('open file:', file_name, 'in __enter__')
|
||||
file_handler = open(file_name, 'r')
|
||||
|
||||
# 【重点】:yield
|
||||
yield file_handler
|
||||
|
||||
# __exit__方法
|
||||
print('close file:', file_name, 'in __exit__')
|
||||
file_handler.close()
|
||||
return
|
||||
|
||||
with open_func('mytest.txt') as file_in:
|
||||
for line in file_in:
|
||||
print(line)
|
|
@ -0,0 +1,10 @@
|
|||
Foo = type('Foo', (), {'bar':True})
|
||||
|
||||
|
||||
# class Foo(object):
|
||||
# bar = True
|
||||
|
||||
def echo_bar(self): # 定义了一个普通的函数
|
||||
print(self.bar)
|
||||
|
||||
FooChild = type('FooChild', (Foo,), {'echo_bar': echo_bar}) #让FooChild类中的echo_bar属性,指向了上面定义的函数
|
|
@ -0,0 +1,10 @@
|
|||
class User(ORM):
|
||||
# 定义数据表的字段
|
||||
name = CharField()
|
||||
age = IntField()
|
||||
|
||||
if __name__ == '__main__':
|
||||
user = User()
|
||||
user.name = "seanlee"
|
||||
user.age = 21
|
||||
user.save() # 调用save函数框架自动将数据插入到数据库中
|
|
@ -0,0 +1,28 @@
|
|||
class A:
|
||||
def spam(self):
|
||||
print('A.spam')
|
||||
|
||||
class B(A):
|
||||
def spam(self):
|
||||
print('B.spam')
|
||||
super().spam() # Call parent spam()
|
||||
|
||||
class C:
|
||||
def __init__(self):
|
||||
self.x = 0
|
||||
|
||||
class D(C):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.y = 1
|
||||
d = D()
|
||||
print(d.y)
|
||||
|
||||
class Base:
|
||||
def __init__(self):
|
||||
print('Base.__init__')
|
||||
|
||||
class A(Base):
|
||||
def __init__(self):
|
||||
Base.__init__(self)
|
||||
print('A.__init__')
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
|
||||
|
||||
###
|
||||
# 1. 创建logger实例,如果参数为空则返回 root logger
|
||||
###
|
||||
|
||||
logger = logging.getLogger('aiotest')
|
||||
# 设置总日志级别, 也可以给不同的handler设置不同的日志级别
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
###
|
||||
# 完整例子. 创建Handler, 输出日志到控制台和文件
|
||||
###
|
||||
|
||||
# 控制台日志和日志文件使用同一个Formatter
|
||||
formatter = logging.Formatter(
|
||||
'%(asctime)s - %(filename)s[line:%(lineno)d] - <%(threadName)s %(thread)d>' +
|
||||
'- <Process %(process)d> - %(levelname)s: %(message)s'
|
||||
)
|
||||
|
||||
# 日志文件FileHandler
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
log_dest = os.path.join(basedir, 'logs') # 日志文件所在目录
|
||||
if not os.path.isdir(log_dest):
|
||||
os.mkdir(log_dest)
|
||||
filename = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) + '.log' # 日志文件名,以当前时间命名
|
||||
file_handler = logging.FileHandler(os.path.join(log_dest, filename), encoding='utf-8') # 创建日志文件handler
|
||||
file_handler.setFormatter(formatter) # 设置Formatter
|
||||
file_handler.setLevel(logging.INFO) # 单独设置日志文件的日志级别
|
||||
|
||||
# 控制台日志StreamHandler
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(formatter)
|
||||
# stream_handler.setLevel(logging.INFO) # 单独设置控制台日志的日志级别,注释掉则使用总日志级别
|
||||
|
||||
###
|
||||
# 3. 将handler添加到logger中
|
||||
###
|
||||
|
||||
logger.addHandler(file_handler)
|
||||
logger.addHandler(stream_handler)
|
||||
|
||||
|
||||
# 使用时:
|
||||
from logger import logger
|
||||
|
||||
logger.info('This is info message')
|
|
@ -0,0 +1,10 @@
|
|||
import logging
|
||||
|
||||
|
||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
|
||||
logging.debug('This is debug message')
|
||||
logging.info('This is info message')
|
||||
logging.warning('This is warning message')
|
||||
logging.error('This is error message')
|
||||
logging.critical('This is critical message')
|
|
@ -0,0 +1,20 @@
|
|||
class Base:
|
||||
def __init__(self):
|
||||
print('Base.__init__')
|
||||
|
||||
class A(Base):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
print('A.__init__')
|
||||
|
||||
class B(Base):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
print('B.__init__')
|
||||
|
||||
class C(A,B):
|
||||
def __init__(self):
|
||||
super().__init__() # Only one call to super() here
|
||||
print('C.__init__')
|
||||
|
||||
print(C.__mro__)
|
|
@ -0,0 +1,34 @@
|
|||
class People:
|
||||
country='China'
|
||||
def __init__(self,name):
|
||||
self.name=name
|
||||
|
||||
def people_info(self):
|
||||
print('%s is xxx' %(self.name))
|
||||
|
||||
obj=People('aaa')
|
||||
|
||||
print(hasattr(People,'country'))
|
||||
#返回值:True
|
||||
print('country' in People.__dict__)
|
||||
#返回值:True
|
||||
print(hasattr(obj,'people_info'))
|
||||
#返回值:True
|
||||
print(People.__dict__)
|
||||
|
||||
class Foo:
|
||||
def run(self):
|
||||
while True:
|
||||
cmd=input('cmd>>: ').strip()
|
||||
if hasattr(self,cmd):
|
||||
func=getattr(self,cmd)
|
||||
func()
|
||||
|
||||
def download(self):
|
||||
print('download....')
|
||||
|
||||
def upload(self):
|
||||
print('upload...')
|
||||
|
||||
obj=Foo()
|
||||
obj.run()
|
|
@ -0,0 +1,8 @@
|
|||
class Spring(object):
|
||||
#season 为类的属性
|
||||
season = "the spring of class"
|
||||
print(Spring.__dict__)
|
||||
print(Spring.__dict__['season'])
|
||||
# 实例属性的__dict__是空的,因为season是属于类属性
|
||||
s= Spring()
|
||||
print(s.__dict__)
|
|
@ -0,0 +1,34 @@
|
|||
class Person(object):
|
||||
name = 'python'
|
||||
age = 18
|
||||
|
||||
def __init__(self):
|
||||
self.sex = 'boy'
|
||||
self.like = 'papapa'
|
||||
|
||||
@staticmethod
|
||||
def stat_func():
|
||||
print ('this is stat_func')
|
||||
|
||||
@classmethod
|
||||
def class_func(cls):
|
||||
print ('class_func')
|
||||
|
||||
class Hero(Person):
|
||||
name = 'super man'
|
||||
age = 1000
|
||||
|
||||
def __init__(self):
|
||||
super(Hero, self).__init__()
|
||||
self.is_good = 'yes'
|
||||
self.power = 'fly'
|
||||
|
||||
person = Person()
|
||||
print ('Person.__dict__: ', Person.__dict__)
|
||||
print ('person.__dict__: ', person.__dict__)
|
||||
hero = Hero()
|
||||
print ('Hero.__dict__: ', Hero.__dict__ )
|
||||
print ('hero.__dict__: ', hero.__dict__)
|
||||
|
||||
# 类的普通方法、类方法、静态方法、全局变量以及一些内置的属性都是放在类对象dict里,而实例对象中存储了一些self.xxx的一些东西
|
||||
# 类对象的dict虽然没有继承父类的,但是实例对象继承了父类的实例属性
|
|
@ -0,0 +1,36 @@
|
|||
class Foo:
|
||||
x=1
|
||||
def __init__(self,y):
|
||||
self.y=y
|
||||
|
||||
def __getattr__(self, item):
|
||||
print('----> from getattr:你找的属性不存在')
|
||||
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
print('----> from setattr')
|
||||
# self.key=value #这就无限递归了,你好好想想
|
||||
self.__dict__[key]=value #应该使用它
|
||||
|
||||
def __delattr__(self, item):
|
||||
print('----> from delattr')
|
||||
# del self.item #无限递归了
|
||||
self.__dict__.pop(item)
|
||||
|
||||
def __getattribute__(self, item):
|
||||
print('----> __getattribute__')
|
||||
return super().__getattribute__(item)
|
||||
|
||||
#__setattr__添加/修改属性会触发它的执行
|
||||
f1=Foo(10)
|
||||
print(f1.__dict__) # 因为你重写了__setattr__,凡是赋值操作都会触发它的运行,你啥都没写,就是根本没赋值,除非你直接操作属性字典,否则永远无法赋值
|
||||
f1.z=3
|
||||
print(f1.__dict__)
|
||||
|
||||
#__delattr__删除属性的时候会触发
|
||||
f1.__dict__['a']=3#我们可以直接修改属性字典,来完成添加/修改属性的操作
|
||||
del f1.a
|
||||
print(f1.__dict__)
|
||||
|
||||
#__getattr__只有在使用点调用属性且属性不存在的时候才会触发
|
||||
# f1.xxxxxx
|
|
@ -0,0 +1,19 @@
|
|||
class Student(object):
|
||||
def __new__(cls,*args,**kwargs):
|
||||
print('我是new函数!') #这是为了追踪new的执行过程
|
||||
print(type(cls)) #这是为了追踪new的执行过程
|
||||
return object.__new__(cls) #调用父类的(object)的new方法,返回一个Student实例,这个实例传递给init的self参数
|
||||
|
||||
def __init__(self,name,age):
|
||||
self.name=name
|
||||
self.age=age
|
||||
print('我是init')
|
||||
|
||||
def study(self):
|
||||
print('我爱学习!')
|
||||
|
||||
if __name__=='__main__':
|
||||
s=Student('张三',25)
|
||||
print(s.name)
|
||||
print(s.age)
|
||||
s.study()
|
|
@ -0,0 +1,47 @@
|
|||
'''
|
||||
desc:尝试定义一种新的数据类型
|
||||
等差数列
|
||||
'''
|
||||
class ArithemeticSequence:
|
||||
def __init__(self, start=0, step=1):
|
||||
print('Call function __init__')
|
||||
|
||||
self.start = start
|
||||
self.step = step
|
||||
self.myData = {}
|
||||
|
||||
# 定义获取值的方法
|
||||
def __getitem__(self, key):
|
||||
print('Call function __getitem__')
|
||||
|
||||
try:
|
||||
return self.myData[key]
|
||||
except KeyError:
|
||||
return self.start + key * self.step
|
||||
|
||||
# 定义赋值方法
|
||||
def __setitem__(self, key, value):
|
||||
print('Call function __setitem__')
|
||||
self.myData[key] = value
|
||||
|
||||
# 定义获取长度的方法
|
||||
def __len__(self):
|
||||
print('Call function __len__')
|
||||
|
||||
return len(self.myData)
|
||||
|
||||
# 定义删除元素的方法
|
||||
def __delitem__(self, key):
|
||||
print('Call function __delitem__')
|
||||
del self.myData[key]
|
||||
|
||||
|
||||
s = ArithemeticSequence(1, 2)
|
||||
print(s[0])
|
||||
print(s[1])
|
||||
print(s[2])
|
||||
print(s[3])# 这里应该执行self.start+key*self.step,因为没有3这个key
|
||||
s[3] = 100 # 进行赋值
|
||||
print(s[3]) # 前面进行了赋值,那么直接输出赋的值100
|
||||
print(len(s))
|
||||
del s[3] # 删除3这个key
|
|
@ -0,0 +1,8 @@
|
|||
import re
|
||||
|
||||
line = 'asdf fjdk; afed, fjek,asdf, foo'
|
||||
|
||||
print(re.split(r'[;,\s]\s*', line))
|
||||
# 正则模式表示 ;或,或空白字符且它们的后面再跟0个或多个空白字符
|
||||
['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import re
|
||||
import reprlib
|
||||
|
||||
RE_WORD = re.compile('\w+')
|
||||
|
||||
class Sentence:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
self.words = RE_WORD.findall(text)
|
||||
|
||||
def __repr__(self):
|
||||
return 'Sentence(%s)' % reprlib.repr(self.text)
|
||||
|
||||
def __iter__(self):
|
||||
return SentenceIterator(self.words) # 迭代协议要求__iter__返回一个迭代器
|
||||
|
||||
|
||||
class SentenceIterator:
|
||||
def __init__(self, words):
|
||||
self.words = words
|
||||
self.index = 0
|
||||
|
||||
def __next__(self):
|
||||
try:
|
||||
word = self.words[self.index] # 获取 self.index 索引位(从0开始)上的单词。
|
||||
except IndexError:
|
||||
raise StopIteration() # 如果 self.index 索引位上没有单词,那么抛出 StopIteration 异常
|
||||
self.index += 1
|
||||
return word
|
||||
|
||||
def __iter__(self):
|
||||
return self # 返回迭代器本身
|
|
@ -0,0 +1,38 @@
|
|||
class Typed:
|
||||
def __init__(self, key, expected_type): # 构造函数接收所传入的参数和参数类型
|
||||
self.key = key
|
||||
self.expected_type = expected_type
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
print('get方法')
|
||||
return instance.__dict__[self.key] # 从底层字典获取值
|
||||
|
||||
def __set__(self, instance, value):
|
||||
print('set方法')
|
||||
if not isinstance(value, self.expected_type): # 类型判断
|
||||
raise TypeError('%s 传入的类型不是%s' % (self.key, self.expected_type)) # 格式化抛出异常
|
||||
instance.__dict__[self.key] = value # 修改底层字典
|
||||
|
||||
def __delete__(self, instance):
|
||||
print('delete方法')
|
||||
instance.__dict__.pop(self.key)
|
||||
|
||||
|
||||
class People:
|
||||
name = Typed('name', str) # p1.__set__() self.__set__(),触发描述符__set__方法,设置参数类型传给构造函数
|
||||
age = Typed('age', int) # p1.__set__() self.__set__()
|
||||
salary = Typed('salary', float) # p1.__set__() self.__set__()
|
||||
def __init__(self, name, age, salary):
|
||||
self.name = name
|
||||
self.age = age
|
||||
self.salary = salary
|
||||
|
||||
# p1=People('alex','13',13.3)#类型有误,报错
|
||||
p1 = People('alex', 13, 13.3)
|
||||
print(p1.__dict__)
|
||||
print(p1.name)
|
||||
p1.name = 'egon'
|
||||
print(p1.__dict__)
|
||||
del p1.name
|
||||
print(p1.__dict__)
|
||||
# print(p1.name) # 相应的键值对已在底层字典中删除了,报错
|
|
@ -0,0 +1,17 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
x = np.linspace(0, 2 * np.pi, 50)
|
||||
|
||||
ax1 = plt.subplot(2, 1, 1) # (行,列,活跃区)
|
||||
plt.plot(x, np.sin(x), 'r')
|
||||
|
||||
ax2 = plt.subplot(2, 3, 4)
|
||||
plt.plot(x, 2 * np.sin(x), 'g')
|
||||
|
||||
ax3 = plt.subplot(2, 3, 5, sharey=ax2)
|
||||
plt.plot(x, np.cos(x), 'b')
|
||||
|
||||
ax4 = plt.subplot(2, 3, 6, sharey=ax2)
|
||||
plt.plot(x, 2 * np.cos(x), 'y')
|
||||
|
||||
plt.show()
|
|
@ -0,0 +1,12 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
k = 500
|
||||
x = np.random.rand(k)
|
||||
y = np.random.rand(k)
|
||||
size = np.random.rand(k) * 50 # 生成每个点的大小
|
||||
colour = np.arctan2(x, y) # 生成每个点的颜色大小
|
||||
plt.scatter(x, y, s=size, c=colour)
|
||||
plt.colorbar() # 添加颜色栏
|
||||
|
||||
plt.show()
|
|
@ -0,0 +1,13 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
k = 10
|
||||
x = np.arange(k)
|
||||
y = np.random.rand(k)
|
||||
plt.bar(x, y) # 画出 x 和 y 的柱状图
|
||||
|
||||
# 增加数值
|
||||
for x, y in zip(x, y):
|
||||
plt.text(x, y , '%.2f' % y, ha='center', va='bottom')
|
||||
|
||||
plt.show()
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
x = np.linspace(0, 2 * np.pi, 50)
|
||||
ax1 = plt.subplot(2, 2, 1) # (行,列,活跃区)
|
||||
plt.title('111111111')
|
||||
plt.plot(x, np.sin(x), 'r',label="sin(x)")
|
||||
plt.legend(loc='best')
|
||||
ax2 = plt.subplot(2, 2, 2, sharey=ax1) # 与 ax1 共享y轴
|
||||
plt.plot(x, 2 * np.sin(x), 'g',label="sin(x)")
|
||||
plt.legend(loc='best')
|
||||
ax3 = plt.subplot(2, 2, 3)
|
||||
plt.plot(x, np.cos(x), 'b',label="sin(x)")
|
||||
plt.legend(loc='best')
|
||||
ax4 = plt.subplot(2, 2, 4, sharey=ax3) # 与 ax3 共享y轴
|
||||
plt.plot(x, 2 * np.cos(x), 'y',label="sin(x)")
|
||||
plt.legend(loc='best')
|
||||
plt.show()
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
x = np.linspace(0, 2 * np.pi, 50)
|
||||
y = np.sin(x)
|
||||
|
||||
plt.plot(x, y)
|
||||
plt.show()
|
||||
plt.plot(x, y)
|
||||
plt.plot(x, y * 2)
|
||||
plt.title("sin(x) & 2sin(x)")
|
||||
plt.show()
|
||||
|
||||
plt.plot(x, y, label="sin(x)")
|
||||
plt.plot(x, y * 2, label="2sin(x)")
|
||||
# plt.legend()
|
||||
plt.legend(loc='best')
|
||||
plt.show()
|
|
@ -0,0 +1,14 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
x = ['北京', '上海', '深圳', '广州']
|
||||
y = [60000, 58000, 50000, 52000]
|
||||
plt.plot(x, y)
|
||||
plt.show()
|
||||
|
||||
|
||||
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
|
||||
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
|
||||
|
||||
plt.plot(x, y)
|
||||
plt.show()
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
import numpy as np
|
||||
nparr = np.array([i for i in range(10)])
|
||||
|
||||
np.zeros(10)
|
||||
|
||||
print(nparr,)
|
|
@ -0,0 +1,87 @@
|
|||
# 双y轴曲线图例合并是一个棘手的操作,现以MNIST案例中loss/accuracy绘制曲线。
|
||||
import tensorflow as tf
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
x_data = tf.compat.v1.placeholder(tf.float32, [None, 784])
|
||||
y_data = tf.compat.v1.placeholder(tf.float32, [None, 10])
|
||||
x_image = tf.reshape(x_data, [-1, 28, 28, 1])
|
||||
|
||||
# convolve layer 1
|
||||
filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))
|
||||
bias1 = tf.Variable(tf.truncated_normal([6]))
|
||||
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME')
|
||||
h_conv1 = tf.nn.sigmoid(conv1 + bias1)
|
||||
maxPool2 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
# convolve layer 完整例子
|
||||
filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16]))
|
||||
bias2 = tf.Variable(tf.truncated_normal([16]))
|
||||
conv2 = tf.nn.conv2d(maxPool2, filter2, strides=[1, 1, 1, 1], padding='SAME')
|
||||
h_conv2 = tf.nn.sigmoid(conv2 + bias2)
|
||||
maxPool3 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
# convolve layer 3
|
||||
filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))
|
||||
bias3 = tf.Variable(tf.truncated_normal([120]))
|
||||
conv3 = tf.nn.conv2d(maxPool3, filter3, strides=[1, 1, 1, 1], padding='SAME')
|
||||
h_conv3 = tf.nn.sigmoid(conv3 + bias3)
|
||||
|
||||
# full connection layer 1
|
||||
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80]))
|
||||
b_fc1 = tf.Variable(tf.truncated_normal([80]))
|
||||
h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120])
|
||||
h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
|
||||
|
||||
# full connection layer 完整例子
|
||||
W_fc2 = tf.Variable(tf.truncated_normal([80, 10]))
|
||||
b_fc2 = tf.Variable(tf.truncated_normal([10]))
|
||||
y_model = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
|
||||
|
||||
cross_entropy = - tf.reduce_sum(y_data * tf.log(y_model))
|
||||
|
||||
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)
|
||||
|
||||
sess = tf.InteractiveSession()
|
||||
correct_prediction = tf.equal(tf.argmax(y_data, 1), tf.argmax(y_model, 1))
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
|
||||
sess.run(tf.global_variables_initializer())
|
||||
|
||||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
|
||||
|
||||
fig_loss = np.zeros([1000])
|
||||
fig_accuracy = np.zeros([1000])
|
||||
|
||||
start_time = time.time()
|
||||
for i in range(1000):
|
||||
batch_xs, batch_ys = mnist.train.next_batch(200)
|
||||
if i % 100 == 0:
|
||||
train_accuracy = sess.run(accuracy, feed_dict={x_data: batch_xs, y_data: batch_ys})
|
||||
print("step %d, train accuracy %g" % (i, train_accuracy))
|
||||
end_time = time.time()
|
||||
print("time:", (end_time - start_time))
|
||||
start_time = end_time
|
||||
print("********************************")
|
||||
train_step.run(feed_dict={x_data: batch_xs, y_data: batch_ys})
|
||||
fig_loss[i] = sess.run(cross_entropy, feed_dict={x_data: batch_xs, y_data: batch_ys})
|
||||
fig_accuracy[i] = sess.run(accuracy, feed_dict={x_data: batch_xs, y_data: batch_ys})
|
||||
print("test accuracy %g" % sess.run(accuracy, feed_dict={x_data: mnist.test.images, y_data: mnist.test.labels}))
|
||||
|
||||
# 绘制曲线
|
||||
fig, ax1 = plt.subplots()
|
||||
ax2 = ax1.twinx()
|
||||
lns1 = ax1.plot(np.arange(1000), fig_loss, label="Loss")
|
||||
# 按一定间隔显示实现方法
|
||||
# ax2.plot(200 * np.arange(len(fig_accuracy)), fig_accuracy, 'r')
|
||||
lns2 = ax2.plot(np.arange(1000), fig_accuracy, 'r', label="Accuracy")
|
||||
ax1.set_xlabel('iteration')
|
||||
ax1.set_ylabel('training loss')
|
||||
ax2.set_ylabel('training accuracy')
|
||||
# 合并图例
|
||||
lns = lns1 + lns2
|
||||
labels = ["Loss", "Accuracy"]
|
||||
# labels = [l.get_label() for l in lns]
|
||||
plt.legend(lns, labels, loc=7)
|
||||
plt.show()
|
|
@ -0,0 +1,33 @@
|
|||
import numpy as np
|
||||
import matplotlib as mpl
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn import datasets
|
||||
|
||||
|
||||
# 加载数据集load_xxx
|
||||
iris = datasets.load_iris()
|
||||
# 是一个字典
|
||||
iris.keys()
|
||||
# dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])
|
||||
# 数据文档
|
||||
print(iris.DESCR)
|
||||
print(iris.data,iris.data.shape,iris.target,iris.target_names)
|
||||
|
||||
# 所有的行,取前两列
|
||||
X = iris.data[:,:2]
|
||||
# X[:,0]这样可以取出第0列
|
||||
plt.scatter(X[:,0],X[:,1])
|
||||
plt.show()
|
||||
|
||||
y = iris.target
|
||||
# marker 是形状
|
||||
for i,colors,marker in [(0,"red","o"),(1,"blue","+"),(2,"green","x")]:
|
||||
plt.scatter(X[y==i,0],X[y==i,1],color=colors,marker=marker)
|
||||
plt.show()
|
||||
|
||||
# 使用另外两个维度
|
||||
X = iris.data[:,2:]
|
||||
# marker 是形状
|
||||
for i,colors,marker in [(0,"red","o"),(1,"blue","+"),(2,"green","x")]:
|
||||
plt.scatter(X[y==i,0],X[y==i,1],color=colors,marker=marker)
|
||||
plt.show()
|
|
@ -0,0 +1,28 @@
|
|||
# DataFrame 是一个带有索引的二维数据结构,每列可以有自己的名字,并且可以有不同的数据类型。
|
||||
# 你可以把它想象成一个 excel 表格或者数据库中的一张表,DataFrame 是最常用的 Pandas 对象。
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
index = pd.Index(data=["Tom", "Bob", "Mary", "James"], name="name")
|
||||
|
||||
data = {
|
||||
"age": [18, 30, 25, 40],
|
||||
"city": ["BeiJing", "ShangHai", "GuangZhou", "ShenZhen"]
|
||||
}
|
||||
|
||||
user_info = pd.DataFrame(data=data, index=index)
|
||||
print(user_info)
|
||||
# 先构建一个二维数组,然后再生成一个列名称列表。
|
||||
data = [[18, "BeiJing"],
|
||||
[30, "ShangHai"],
|
||||
[25, "GuangZhou"],
|
||||
[40, "ShenZhen"]]
|
||||
columns = ["age", "city"]
|
||||
|
||||
user_info = pd.DataFrame(data=data, index=index, columns=columns)
|
||||
print(user_info)
|
||||
|
||||
# 访问行
|
||||
|
||||
print(user_info.loc["Tom"],user_info.iloc[1:3],user_info["age"])
|
|
@ -0,0 +1,15 @@
|
|||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
# Series 是一个带有 名称 和索引的一维数组
|
||||
# 一个 Series 包括了 data、index 以及 name
|
||||
user_age = pd.Series(data=[18, 30, 25, 40])
|
||||
user_age.index = ["Tom", "Bob", "Mary", "James"]
|
||||
user_age.index.name = "name"
|
||||
user_age.name="user_age_info"
|
||||
print(user_age)
|
||||
|
||||
# 构建索引
|
||||
name = pd.Index(["Tom", "Bob", "Mary", "James"], name="name")
|
||||
# 构建 Series
|
||||
user_age = pd.Series(data=[18, 30, 25, 40], index=name, name="user_age_info")
|
|
@ -0,0 +1,16 @@
|
|||
# 导入相关库
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from io import StringIO
|
||||
|
||||
pd.read_csv("user_info.csv")
|
||||
|
||||
|
||||
pd.read_csv("user_info.csv", index_col="name")
|
||||
|
||||
# 除了可以从文件中读取,我们还可以从 StringIO 对象中读取
|
||||
data="name,age,birth,sex\nTom,18.0,2000-02-10,\nBob,30.0,1988-10-17,male"
|
||||
print(data)
|
||||
pd.read_csv(StringIO(data))
|
||||
data = "name|age|birth|sex~Tom|18.0|2000-02-10|~Bob|30.0|1988-10-17|male"
|
||||
pd.read_csv(StringIO(data), sep="|", lineterminator="~")
|
|
@ -0,0 +1,3 @@
|
|||
name,age,birth,sex
|
||||
Tom,18.0,2000-02-10,
|
||||
Bob,30.0,1988-10-17,male
|
|
|
@ -0,0 +1,36 @@
|
|||
# import numpy and pandas
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
# used for dates
|
||||
import datetime
|
||||
from datetime import datetime, date
|
||||
|
||||
# Set formattign options
|
||||
pd.set_option('display.notebook_repr_html', False)
|
||||
pd.set_option('display.max_columns', 7)
|
||||
pd.set_option('display.max_rows', 10)
|
||||
pd.set_option('display.width', 60)
|
||||
|
||||
# bring in matplotlib for graphics
|
||||
import matplotlib.pyplot as plt
|
||||
# 从雅虎获得股票数据
|
||||
# pip install pandas_datareader
|
||||
|
||||
# import data reader package
|
||||
import pandas_datareader as pdr
|
||||
|
||||
# read data from Yahoo! Finance for a specific
|
||||
# stock specified by ticker and between the start and end dates
|
||||
def get_stock_data(ticker, start, end):
|
||||
# read the data
|
||||
data = pdr.data.DataReader(ticker, 'yahoo', start, end)
|
||||
|
||||
# rename this column
|
||||
data.insert(0, "Ticker", ticker)
|
||||
return data
|
||||
|
||||
# request the three years of data for MSFT
|
||||
start = datetime(2012, 1, 1)
|
||||
end = datetime(2014, 12, 31)
|
||||
get_stock_data("MSFT", start, end)[:5]
|
|
@ -0,0 +1,55 @@
|
|||
# 导入算法包以及数据集
|
||||
import numpy as np
|
||||
from sklearn import datasets
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.metrics import classification_report,confusion_matrix
|
||||
import operator
|
||||
import random
|
||||
def knn(x_test, x_data, y_data, k):
|
||||
# 计算样本数量
|
||||
x_data_size = x_data.shape[0]
|
||||
# 复制x_test
|
||||
np.tile(x_test, (x_data_size,1))
|
||||
# 计算x_test与每一个样本的差值
|
||||
diffMat = np.tile(x_test, (x_data_size,1)) - x_data
|
||||
# 计算差值的平方
|
||||
sqDiffMat = diffMat**2
|
||||
# 求和
|
||||
sqDistances = sqDiffMat.sum(axis=1)
|
||||
# 开方
|
||||
distances = sqDistances**0.5
|
||||
# 从小到大排序
|
||||
sortedDistances = distances.argsort()
|
||||
classCount = {}
|
||||
for i in range(k):
|
||||
# 获取标签
|
||||
votelabel = y_data[sortedDistances[i]]
|
||||
# 统计标签数量
|
||||
classCount[votelabel] = classCount.get(votelabel,0) + 1
|
||||
# 根据operator.itemgetter(1)-第1个值对classCount排序,然后再取倒序
|
||||
sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1), reverse=True)
|
||||
# 获取数量最多的标签
|
||||
return sortedClassCount[0][0]
|
||||
# 载入数据
|
||||
iris = datasets.load_iris()
|
||||
# x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.完整例子) #分割数据0.2为测试数据,0.8为训练数据
|
||||
#打乱数据
|
||||
data_size = iris.data.shape[0]
|
||||
index = [i for i in range(data_size)]
|
||||
random.shuffle(index)
|
||||
iris.data = iris.data[index]
|
||||
iris.target = iris.target[index]
|
||||
#切分数据集
|
||||
test_size = 40
|
||||
x_train = iris.data[test_size:]
|
||||
x_test = iris.data[:test_size]
|
||||
y_train = iris.target[test_size:]
|
||||
y_test = iris.target[:test_size]
|
||||
predictions = []
|
||||
for i in range(x_test.shape[0]):
|
||||
predictions.append(knn(x_test[i], x_train, y_train, 5))
|
||||
print(classification_report(y_test, predictions))
|
||||
print(confusion_matrix(y_test,predictions))
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.cluster import KMeans
|
||||
from sklearn.datasets import make_blobs
|
||||
plt.figure(figsize=(12, 12))
|
||||
n_samples = 1500
|
||||
random_state = 170
|
||||
X, y = make_blobs(n_samples=n_samples, random_state=random_state)
|
||||
# Incorrect number of clusters
|
||||
y_pred = KMeans(n_clusters=2, random_state=random_state).fit_predict(X)
|
||||
plt.subplot(221)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
|
||||
plt.title("Incorrect Number of Blobs")
|
||||
# Anisotropicly distributed data
|
||||
transformation = [[ 0.60834549, -0.63667341], [-0.40887718, 0.85253229]]
|
||||
X_aniso = np.dot(X, transformation)
|
||||
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_aniso)
|
||||
plt.subplot(222)
|
||||
plt.scatter(X_aniso[:, 0], X_aniso[:, 1], c=y_pred)
|
||||
plt.title("Anisotropicly Distributed Blobs")
|
||||
# Different variance
|
||||
X_varied, y_varied = make_blobs(n_samples=n_samples,
|
||||
cluster_std=[1.0, 2.5, 0.5],
|
||||
random_state=random_state)
|
||||
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_varied)
|
||||
plt.subplot(223)
|
||||
plt.scatter(X_varied[:, 0], X_varied[:, 1], c=y_pred)
|
||||
plt.title("Unequal Variance")
|
||||
# Unevenly sized blobs
|
||||
X_filtered = np.vstack((X[y == 0][:500], X[y == 1][:100], X[y == 2][:10]))
|
||||
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_filtered)
|
||||
plt.subplot(224)
|
||||
plt.scatter(X_filtered[:, 0], X_filtered[:, 1], c=y_pred)
|
||||
plt.title("Unevenly Sized Blobs")
|
||||
plt.show()
|
|
@ -0,0 +1,62 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import operator
|
||||
# 已知分类的数据
|
||||
x1 = np.array([3,2,1])
|
||||
y1 = np.array([104,100,81])
|
||||
x2 = np.array([101,99,98])
|
||||
y2 = np.array([10,5,2])
|
||||
scatter1 = plt.scatter(x1,y1,c='r')
|
||||
scatter2 = plt.scatter(x2,y2,c='b')
|
||||
# 未知数据
|
||||
x = np.array([18])
|
||||
y = np.array([90])
|
||||
scatter3 = plt.scatter(x,y,c='k')
|
||||
#画图例
|
||||
plt.legend(handles=[scatter1,scatter2,scatter3],labels=['labelA','labelB','X'],loc='best')
|
||||
plt.show()
|
||||
# 已知分类的数据
|
||||
x_data = np.array([[3,104],
|
||||
[2,100],
|
||||
[1,81],
|
||||
[101,10],
|
||||
[99,5],
|
||||
[81,2]])
|
||||
y_data = np.array(['A','A','A','B','B','B'])
|
||||
x_test = np.array([18,90])
|
||||
# 计算样本数量
|
||||
x_data_size = x_data.shape[0]
|
||||
x_data_size
|
||||
# 复制x_test
|
||||
np.tile(x_test, (x_data_size,1))
|
||||
# 计算x_test与每一个样本的差值
|
||||
diffMat = np.tile(x_test, (x_data_size,1)) - x_data
|
||||
diffMat
|
||||
# 计算差值的平方
|
||||
sqDiffMat = diffMat**2
|
||||
sqDiffMat
|
||||
# 求和
|
||||
sqDistances = sqDiffMat.sum(axis=1)
|
||||
sqDistances
|
||||
# 开方
|
||||
distances = sqDistances**0.5
|
||||
distances
|
||||
# 从小到大排序
|
||||
sortedDistances = distances.argsort()
|
||||
sortedDistances
|
||||
classCount = {}
|
||||
# 设置k
|
||||
k = 5
|
||||
for i in range(k):
|
||||
# 获取标签
|
||||
votelabel = y_data[sortedDistances[i]]
|
||||
# 统计标签数量
|
||||
classCount[votelabel] = classCount.get(votelabel,0) + 1
|
||||
classCount
|
||||
# 根据operator.itemgetter(1)-第1个值对classCount排序,然后再取倒序
|
||||
sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1), reverse=True)
|
||||
sortedClassCount
|
||||
# 获取数量最多的标签
|
||||
knnclass = sortedClassCount[0][0]
|
||||
knnclass
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
# 载入数据
|
||||
data = np.genfromtxt("data.csv", delimiter=",")
|
||||
x_data = data[:,0]
|
||||
y_data = data[:,1]
|
||||
plt.scatter(x_data,y_data)
|
||||
plt.show()
|
||||
print(x_data.shape)
|
||||
# 数据中心化
|
||||
def zeroMean(dataMat):
|
||||
# 按列求平均,即各个特征的平均
|
||||
meanVal = np.mean(dataMat, axis=0)
|
||||
newData = dataMat - meanVal
|
||||
return newData, meanVal
|
||||
newData,meanVal=zeroMean(data)
|
||||
# np.cov用于求协方差矩阵,参数rowvar=0说明数据一行代表一个样本
|
||||
covMat = np.cov(newData, rowvar=0)
|
||||
# 协方差矩阵
|
||||
print(covMat)
|
||||
# np.linalg.eig求矩阵的特征值和特征向量
|
||||
eigVals, eigVects = np.linalg.eig(np.mat(covMat))
|
||||
# 特征值
|
||||
print(eigVals)
|
||||
# 特征向量
|
||||
print(eigVects)
|
||||
# 对特征值从小到大排序
|
||||
eigValIndice = np.argsort(eigVals)
|
||||
print(eigValIndice)
|
||||
top = 1
|
||||
# 最大的top个特征值的下标
|
||||
n_eigValIndice = eigValIndice[-1:-(top+1):-1]
|
||||
print(n_eigValIndice)
|
||||
# 最大的n个特征值对应的特征向量
|
||||
n_eigVect = eigVects[:,n_eigValIndice]
|
||||
print(n_eigVect)
|
||||
# 低维特征空间的数据
|
||||
lowDDataMat = newData*n_eigVect
|
||||
print(lowDDataMat)
|
||||
# 利用低纬度数据来重构数据
|
||||
reconMat = (lowDDataMat*n_eigVect.T) + meanVal
|
||||
print(reconMat)
|
||||
# 载入数据
|
||||
data = np.genfromtxt("data.csv", delimiter=",")
|
||||
x_data = data[:,0]
|
||||
y_data = data[:,1]
|
||||
plt.scatter(x_data,y_data)
|
||||
# 重构的数据
|
||||
x_data = np.array(reconMat)[:,0]
|
||||
y_data = np.array(reconMat)[:,1]
|
||||
plt.scatter(x_data,y_data,c='r')
|
||||
plt.show()
|
||||
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
32.502345269453031,31.70700584656992
|
||||
53.426804033275019,68.77759598163891
|
||||
61.530358025636438,62.562382297945803
|
||||
47.475639634786098,71.546632233567777
|
||||
59.813207869512318,87.230925133687393
|
||||
55.142188413943821,78.211518270799232
|
||||
52.211796692214001,79.64197304980874
|
||||
39.299566694317065,59.171489321869508
|
||||
48.10504169176825,75.331242297063056
|
||||
52.550014442733818,71.300879886850353
|
||||
45.419730144973755,55.165677145959123
|
||||
54.351634881228918,82.478846757497919
|
||||
44.164049496773352,62.008923245725825
|
||||
58.16847071685779,75.392870425994957
|
||||
56.727208057096611,81.43619215887864
|
||||
48.955888566093719,60.723602440673965
|
||||
44.687196231480904,82.892503731453715
|
||||
60.297326851333466,97.379896862166078
|
||||
45.618643772955828,48.847153317355072
|
||||
38.816817537445637,56.877213186268506
|
||||
66.189816606752601,83.878564664602763
|
||||
65.41605174513407,118.59121730252249
|
||||
47.48120860786787,57.251819462268969
|
||||
41.57564261748702,51.391744079832307
|
||||
51.84518690563943,75.380651665312357
|
||||
59.370822011089523,74.765564032151374
|
||||
57.31000343834809,95.455052922574737
|
||||
63.615561251453308,95.229366017555307
|
||||
46.737619407976972,79.052406169565586
|
||||
50.556760148547767,83.432071421323712
|
||||
52.223996085553047,63.358790317497878
|
||||
35.567830047746632,41.412885303700563
|
||||
42.436476944055642,76.617341280074044
|
||||
58.16454011019286,96.769566426108199
|
||||
57.504447615341789,74.084130116602523
|
||||
45.440530725319981,66.588144414228594
|
||||
61.89622268029126,77.768482417793024
|
||||
33.093831736163963,50.719588912312084
|
||||
36.436009511386871,62.124570818071781
|
||||
37.675654860850742,60.810246649902211
|
||||
44.555608383275356,52.682983366387781
|
||||
43.318282631865721,58.569824717692867
|
||||
50.073145632289034,82.905981485070512
|
||||
43.870612645218372,61.424709804339123
|
||||
62.997480747553091,115.24415280079529
|
||||
32.669043763467187,45.570588823376085
|
||||
40.166899008703702,54.084054796223612
|
||||
53.575077531673656,87.994452758110413
|
||||
33.864214971778239,52.725494375900425
|
||||
64.707138666121296,93.576118692658241
|
||||
38.119824026822805,80.166275447370964
|
||||
44.502538064645101,65.101711570560326
|
||||
40.599538384552318,65.562301260400375
|
||||
41.720676356341293,65.280886920822823
|
||||
51.088634678336796,73.434641546324301
|
||||
55.078095904923202,71.13972785861894
|
||||
41.377726534895203,79.102829683549857
|
||||
62.494697427269791,86.520538440347153
|
||||
49.203887540826003,84.742697807826218
|
||||
41.102685187349664,59.358850248624933
|
||||
41.182016105169822,61.684037524833627
|
||||
50.186389494880601,69.847604158249183
|
||||
52.378446219236217,86.098291205774103
|
||||
50.135485486286122,59.108839267699643
|
||||
33.644706006191782,69.89968164362763
|
||||
39.557901222906828,44.862490711164398
|
||||
56.130388816875467,85.498067778840223
|
||||
57.362052133238237,95.536686846467219
|
||||
60.269214393997906,70.251934419771587
|
||||
35.678093889410732,52.721734964774988
|
||||
31.588116998132829,50.392670135079896
|
||||
53.66093226167304,63.642398775657753
|
||||
46.682228649471917,72.247251068662365
|
||||
43.107820219102464,57.812512976181402
|
||||
70.34607561504933,104.25710158543822
|
||||
44.492855880854073,86.642020318822006
|
||||
57.50453330326841,91.486778000110135
|
||||
36.930076609191808,55.231660886212836
|
||||
55.805733357942742,79.550436678507609
|
||||
38.954769073377065,44.847124242467601
|
||||
56.901214702247074,80.207523139682763
|
||||
56.868900661384046,83.14274979204346
|
||||
34.33312470421609,55.723489260543914
|
||||
59.04974121466681,77.634182511677864
|
||||
57.788223993230673,99.051414841748269
|
||||
54.282328705967409,79.120646274680027
|
||||
51.088719898979143,69.588897851118475
|
||||
50.282836348230731,69.510503311494389
|
||||
44.211741752090113,73.687564318317285
|
||||
38.005488008060688,61.366904537240131
|
||||
32.940479942618296,67.170655768995118
|
||||
53.691639571070056,85.668203145001542
|
||||
68.76573426962166,114.85387123391394
|
||||
46.230966498310252,90.123572069967423
|
||||
68.319360818255362,97.919821035242848
|
||||
50.030174340312143,81.536990783015028
|
||||
49.239765342753763,72.111832469615663
|
||||
50.039575939875988,85.232007342325673
|
||||
48.149858891028863,66.224957888054632
|
||||
25.128484647772304,53.454394214850524
|
|
|
@ -0,0 +1,36 @@
|
|||
from sklearn.neural_network import MLPClassifier
|
||||
from sklearn.datasets import load_digits
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.metrics import classification_report,confusion_matrix
|
||||
from sklearn import decomposition
|
||||
import matplotlib.pyplot as plt
|
||||
digits = load_digits()#载入数据
|
||||
x_data = digits.data #数据
|
||||
y_data = digits.target #标签
|
||||
x_train,x_test,y_train,y_test = train_test_split(x_data,y_data) #分割数据1/4为测试数据,3/4为训练数据
|
||||
mlp = MLPClassifier(hidden_layer_sizes=(100,50) ,max_iter=500)
|
||||
mlp.fit(x_train,y_train )
|
||||
predictions = mlp.predict(x_test)
|
||||
print(classification_report(predictions, y_test))
|
||||
print(confusion_matrix(predictions, y_test))
|
||||
pca = decomposition.PCA()
|
||||
pca.fit(x_data)
|
||||
# 方差
|
||||
pca.explained_variance_
|
||||
# 方差占比
|
||||
pca.explained_variance_ratio_
|
||||
variance = []
|
||||
for i in range(len(pca.explained_variance_ratio_)):
|
||||
variance.append(sum(pca.explained_variance_ratio_[:i+1]))
|
||||
plt.plot(range(1,len(pca.explained_variance_ratio_)+1), variance)
|
||||
plt.show()
|
||||
pca = decomposition.PCA(whiten=True,n_components=0.8)
|
||||
pca.fit(x_data)
|
||||
pca.explained_variance_ratio_
|
||||
x_train_pca = pca.transform(x_train)
|
||||
mlp = MLPClassifier(hidden_layer_sizes=(100,50) ,max_iter=500)
|
||||
mlp.fit(x_train_pca,y_train )
|
||||
x_test_pca = pca.transform(x_test)
|
||||
predictions = mlp.predict(x_test_pca)
|
||||
print(classification_report(predictions, y_test))
|
||||
print(confusion_matrix(predictions, y_test))
|
|
@ -0,0 +1,58 @@
|
|||
from sklearn.neural_network import MLPClassifier
|
||||
from sklearn.datasets import load_digits
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.metrics import classification_report,confusion_matrix
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
digits = load_digits()#载入数据
|
||||
x_data = digits.data #数据
|
||||
y_data = digits.target #标签
|
||||
x_train,x_test,y_train,y_test = train_test_split(x_data,y_data) #分割数据1/4为测试数据,3/4为训练数据
|
||||
print(x_data.shape)
|
||||
mlp = MLPClassifier(hidden_layer_sizes=(100,50) ,max_iter=500)
|
||||
mlp.fit(x_train,y_train)
|
||||
# 数据中心化
|
||||
def zeroMean(dataMat):
|
||||
# 按列求平均,即各个特征的平均
|
||||
meanVal = np.mean(dataMat, axis=0)
|
||||
newData = dataMat - meanVal
|
||||
return newData, meanVal
|
||||
def pca(dataMat,top):
|
||||
# 数据中心化
|
||||
newData,meanVal=zeroMean(dataMat)
|
||||
# np.cov用于求协方差矩阵,参数rowvar=0说明数据一行代表一个样本
|
||||
covMat = np.cov(newData, rowvar=0)
|
||||
# np.linalg.eig求矩阵的特征值和特征向量
|
||||
eigVals, eigVects = np.linalg.eig(np.mat(covMat))
|
||||
# 对特征值从小到大排序
|
||||
eigValIndice = np.argsort(eigVals)
|
||||
# 最大的n个特征值的下标
|
||||
n_eigValIndice = eigValIndice[-1:-(top+1):-1]
|
||||
# 最大的n个特征值对应的特征向量
|
||||
n_eigVect = eigVects[:,n_eigValIndice]
|
||||
# 低维特征空间的数据
|
||||
lowDDataMat = newData*n_eigVect
|
||||
# 利用低纬度数据来重构数据
|
||||
reconMat = (lowDDataMat*n_eigVect.T) + meanVal
|
||||
# 返回低维特征空间的数据和重构的矩阵
|
||||
return lowDDataMat,reconMat
|
||||
lowDDataMat,reconMat = pca(x_data,2)
|
||||
# 重构的数据
|
||||
x = np.array(lowDDataMat)[:,0]
|
||||
y = np.array(lowDDataMat)[:,1]
|
||||
plt.scatter(x,y,c='r')
|
||||
plt.show()
|
||||
predictions = mlp.predict(x_data)
|
||||
# 重构的数据
|
||||
x = np.array(lowDDataMat)[:,0]
|
||||
y = np.array(lowDDataMat)[:,1]
|
||||
plt.scatter(x,y,c=y_data)
|
||||
plt.show()
|
||||
lowDDataMat,reconMat = pca(x_data,3)
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
x = np.array(lowDDataMat)[:,0]
|
||||
y = np.array(lowDDataMat)[:,1]
|
||||
z = np.array(lowDDataMat)[:,2]
|
||||
ax = plt.figure().add_subplot(111, projection = '3d')
|
||||
ax.scatter(x, y, z, c = y_data, s = 10) #点为红色三角形
|
||||
plt.show()
|
|
@ -0,0 +1,118 @@
|
|||
0.051267,0.69956,1
|
||||
-0.092742,0.68494,1
|
||||
-0.21371,0.69225,1
|
||||
-0.375,0.50219,1
|
||||
-0.51325,0.46564,1
|
||||
-0.52477,0.2098,1
|
||||
-0.39804,0.034357,1
|
||||
-0.30588,-0.19225,1
|
||||
0.016705,-0.40424,1
|
||||
0.13191,-0.51389,1
|
||||
0.38537,-0.56506,1
|
||||
0.52938,-0.5212,1
|
||||
0.63882,-0.24342,1
|
||||
0.73675,-0.18494,1
|
||||
0.54666,0.48757,1
|
||||
0.322,0.5826,1
|
||||
0.16647,0.53874,1
|
||||
-0.046659,0.81652,1
|
||||
-0.17339,0.69956,1
|
||||
-0.47869,0.63377,1
|
||||
-0.60541,0.59722,1
|
||||
-0.62846,0.33406,1
|
||||
-0.59389,0.005117,1
|
||||
-0.42108,-0.27266,1
|
||||
-0.11578,-0.39693,1
|
||||
0.20104,-0.60161,1
|
||||
0.46601,-0.53582,1
|
||||
0.67339,-0.53582,1
|
||||
-0.13882,0.54605,1
|
||||
-0.29435,0.77997,1
|
||||
-0.26555,0.96272,1
|
||||
-0.16187,0.8019,1
|
||||
-0.17339,0.64839,1
|
||||
-0.28283,0.47295,1
|
||||
-0.36348,0.31213,1
|
||||
-0.30012,0.027047,1
|
||||
-0.23675,-0.21418,1
|
||||
-0.06394,-0.18494,1
|
||||
0.062788,-0.16301,1
|
||||
0.22984,-0.41155,1
|
||||
0.2932,-0.2288,1
|
||||
0.48329,-0.18494,1
|
||||
0.64459,-0.14108,1
|
||||
0.46025,0.012427,1
|
||||
0.6273,0.15863,1
|
||||
0.57546,0.26827,1
|
||||
0.72523,0.44371,1
|
||||
0.22408,0.52412,1
|
||||
0.44297,0.67032,1
|
||||
0.322,0.69225,1
|
||||
0.13767,0.57529,1
|
||||
-0.0063364,0.39985,1
|
||||
-0.092742,0.55336,1
|
||||
-0.20795,0.35599,1
|
||||
-0.20795,0.17325,1
|
||||
-0.43836,0.21711,1
|
||||
-0.21947,-0.016813,1
|
||||
-0.13882,-0.27266,1
|
||||
0.18376,0.93348,0
|
||||
0.22408,0.77997,0
|
||||
0.29896,0.61915,0
|
||||
0.50634,0.75804,0
|
||||
0.61578,0.7288,0
|
||||
0.60426,0.59722,0
|
||||
0.76555,0.50219,0
|
||||
0.92684,0.3633,0
|
||||
0.82316,0.27558,0
|
||||
0.96141,0.085526,0
|
||||
0.93836,0.012427,0
|
||||
0.86348,-0.082602,0
|
||||
0.89804,-0.20687,0
|
||||
0.85196,-0.36769,0
|
||||
0.82892,-0.5212,0
|
||||
0.79435,-0.55775,0
|
||||
0.59274,-0.7405,0
|
||||
0.51786,-0.5943,0
|
||||
0.46601,-0.41886,0
|
||||
0.35081,-0.57968,0
|
||||
0.28744,-0.76974,0
|
||||
0.085829,-0.75512,0
|
||||
0.14919,-0.57968,0
|
||||
-0.13306,-0.4481,0
|
||||
-0.40956,-0.41155,0
|
||||
-0.39228,-0.25804,0
|
||||
-0.74366,-0.25804,0
|
||||
-0.69758,0.041667,0
|
||||
-0.75518,0.2902,0
|
||||
-0.69758,0.68494,0
|
||||
-0.4038,0.70687,0
|
||||
-0.38076,0.91886,0
|
||||
-0.50749,0.90424,0
|
||||
-0.54781,0.70687,0
|
||||
0.10311,0.77997,0
|
||||
0.057028,0.91886,0
|
||||
-0.10426,0.99196,0
|
||||
-0.081221,1.1089,0
|
||||
0.28744,1.087,0
|
||||
0.39689,0.82383,0
|
||||
0.63882,0.88962,0
|
||||
0.82316,0.66301,0
|
||||
0.67339,0.64108,0
|
||||
1.0709,0.10015,0
|
||||
-0.046659,-0.57968,0
|
||||
-0.23675,-0.63816,0
|
||||
-0.15035,-0.36769,0
|
||||
-0.49021,-0.3019,0
|
||||
-0.46717,-0.13377,0
|
||||
-0.28859,-0.060673,0
|
||||
-0.61118,-0.067982,0
|
||||
-0.66302,-0.21418,0
|
||||
-0.59965,-0.41886,0
|
||||
-0.72638,-0.082602,0
|
||||
-0.83007,0.31213,0
|
||||
-0.72062,0.53874,0
|
||||
-0.59389,0.49488,0
|
||||
-0.48445,0.99927,0
|
||||
-0.0063364,0.99927,0
|
||||
0.63265,-0.030612,0
|
|
@ -0,0 +1,15 @@
|
|||
import matplotlib.pyplot as plt
|
||||
from sklearn import datasets
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
x_data, y_data = datasets.make_circles(n_samples=500, factor=.3, noise=.10)
|
||||
plt.scatter(x_data[:,0], x_data[:,1], c=y_data)
|
||||
plt.show()
|
||||
z_data = x_data[:,0]**2 + x_data[:,1]**2
|
||||
ax = plt.figure().add_subplot(111, projection = '3d')
|
||||
ax.scatter(x_data[:,0], x_data[:,1], z_data, c = y_data, s = 10) #点为红色三角形
|
||||
#显示图像
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
from sklearn import svm
|
||||
x = [[3, 3], [4, 3], [1, 1]]
|
||||
y = [1, 1, -1]
|
||||
model = svm.SVC(kernel='linear')
|
||||
model.fit(x, y)
|
||||
# 打印支持向量
|
||||
print(f"打印支持向量 :{model.support_vectors_}")
|
||||
# 第2和第0个点是支持向量
|
||||
print(f"model.support_{model.support_}")
|
||||
# 有几个支持向量
|
||||
print(f"model.n_support_{model.n_support_}")
|
||||
print(f"属于第几个分类:{model.predict([[4,3]])}")
|
||||
print(f"超平面属性 {model.coef_},截距:{model.intercept_}")
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn import svm
|
||||
# 创建40个点
|
||||
x_data = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
|
||||
y_data = [0]*20 +[1]*20
|
||||
plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
|
||||
plt.show()
|
||||
#fit the model
|
||||
model = svm.SVC(kernel='linear')
|
||||
model.fit(x_data, y_data)
|
||||
model.coef_
|
||||
model.intercept_
|
||||
# 获取分离平面
|
||||
plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
|
||||
x_test = np.array([[-5],[5]])
|
||||
d = -model.intercept_/model.coef_[0][1]
|
||||
k = -model.coef_[0][0]/model.coef_[0][1]
|
||||
y_test = d + k*x_test
|
||||
plt.plot(x_test, y_test, 'k')
|
||||
plt.show()
|
||||
model.support_vectors_
|
||||
# 画出通过支持向量的分界线
|
||||
b1 = model.support_vectors_[0]
|
||||
y_down = k*x_test + (b1[1] - k*b1[0])
|
||||
b2 = model.support_vectors_[-1]
|
||||
y_up = k*x_test + (b2[1] - k*b2[0])
|
||||
plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
|
||||
x_test = np.array([[-5],[5]])
|
||||
d = -model.intercept_/model.coef_[0][1]
|
||||
k = -model.coef_[0][0]/model.coef_[0][1]
|
||||
y_test = d + k*x_test
|
||||
plt.plot(x_test, y_test, 'k')
|
||||
plt.plot(x_test, y_down, 'r--')
|
||||
plt.plot(x_test, y_up, 'b--')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.metrics import classification_report
|
||||
from sklearn import svm
|
||||
# 载入数据
|
||||
data = np.genfromtxt("LR-testSet2.txt", delimiter=",")
|
||||
x_data = data[:,:-1]
|
||||
y_data = data[:,-1]
|
||||
def plot():
|
||||
x0 = []
|
||||
x1 = []
|
||||
y0 = []
|
||||
y1 = []
|
||||
# 切分不同类别的数据
|
||||
for i in range(len(x_data)):
|
||||
if y_data[i]==0:
|
||||
x0.append(x_data[i,0])
|
||||
y0.append(x_data[i,1])
|
||||
else:
|
||||
x1.append(x_data[i,0])
|
||||
y1.append(x_data[i,1])
|
||||
# 画图
|
||||
scatter0 = plt.scatter(x0, y0, c='b', marker='o')
|
||||
scatter1 = plt.scatter(x1, y1, c='r', marker='x')
|
||||
#画图例
|
||||
plt.legend(handles=[scatter0,scatter1],labels=['label0','label1'],loc='best')
|
||||
plot()
|
||||
plt.show()
|
||||
# fit the model
|
||||
# C和gamma
|
||||
# 'linear', 'poly', 'rbf', 'sigmoid'
|
||||
model = svm.SVC(kernel='rbf', C=2, gamma=1)
|
||||
model.fit(x_data, y_data)
|
||||
model.score(x_data,y_data)
|
||||
# 获取数据值所在的范围
|
||||
x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
|
||||
y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1
|
||||
# 生成网格矩阵
|
||||
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
|
||||
np.arange(y_min, y_max, 0.02))
|
||||
z = model.predict(np.c_[xx.ravel(), yy.ravel()])# ravel与flatten类似,多维数据转一维。flatten不会改变原始数据,ravel会改变原始数据
|
||||
z = z.reshape(xx.shape)
|
||||
# 等高线图
|
||||
cs = plt.contourf(xx, yy, z)
|
||||
plot()
|
||||
plt.show()
|
||||
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,163 @@
|
|||
# # 支持向量机(1)
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy import stats
|
||||
import seaborn as sns; sns.set()
|
||||
# ### 支持向量基本原理
|
||||
# 如何解决这个线性不可分问题呢?咱们给它映射到高维来试试
|
||||
# $z=x^完整例子+y^完整例子$.
|
||||
#随机来点数据
|
||||
#其中 cluster_std是数据的离散程度
|
||||
from sklearn.datasets.samples_generator import make_blobs
|
||||
X, y = make_blobs(n_samples=50, centers=2,
|
||||
random_state=0, cluster_std=0.60)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
# 随便的画几条分割线,哪个好来这?
|
||||
#随便的画几条分割线,哪个好来这?
|
||||
xfit = np.linspace(-1, 3.5)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
for m, b in [(1, 0.65), (0.5, 1.6), (-0.2, 2.9)]:
|
||||
plt.plot(xfit, m * xfit + b, '-k')
|
||||
#限制一下X的取值范围
|
||||
plt.xlim(-1, 3.5);
|
||||
# ## Support Vector Machines: 最小化 *雷区*
|
||||
xfit = np.linspace(-1, 3.5)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
|
||||
yfit = m * xfit + b
|
||||
plt.plot(xfit, yfit, '-k')
|
||||
plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none',
|
||||
color='#AAAAAA', alpha=0.4)
|
||||
plt.xlim(-1, 3.5)
|
||||
plt.show()
|
||||
|
||||
# ### 训练一个基本的SVM
|
||||
#分类任务
|
||||
from sklearn.svm import SVC
|
||||
#线性核函数 相当于不对数据进行变换
|
||||
model = SVC(kernel='linear')
|
||||
model.fit(X, y)
|
||||
#绘图函数
|
||||
def plot_svc_decision_function(model, ax=None, plot_support=True):
|
||||
if ax is None:
|
||||
ax = plt.gca()
|
||||
xlim = ax.get_xlim()
|
||||
ylim = ax.get_ylim()
|
||||
# 用SVM自带的decision_function函数来绘制
|
||||
x = np.linspace(xlim[0], xlim[1], 30)
|
||||
y = np.linspace(ylim[0], ylim[1], 30)
|
||||
Y, X = np.meshgrid(y, x)
|
||||
xy = np.vstack([X.ravel(), Y.ravel()]).T
|
||||
P = model.decision_function(xy).reshape(X.shape)
|
||||
# 绘制决策边界
|
||||
ax.contour(X, Y, P, colors='k',
|
||||
levels=[-1, 0, 1], alpha=0.5,
|
||||
linestyles=['--', '-', '--'])
|
||||
# 绘制支持向量
|
||||
if plot_support:
|
||||
ax.scatter(model.support_vectors_[:, 0],
|
||||
model.support_vectors_[:, 1],
|
||||
s=300, linewidth=1, alpha=0.2);
|
||||
ax.set_xlim(xlim)
|
||||
ax.set_ylim(ylim)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
plot_svc_decision_function(model)
|
||||
# * 这条线就是我们希望得到的决策边界啦
|
||||
# * 观察发现有3个点做了特殊的标记,它们恰好都是边界上的点
|
||||
# * 它们就是我们的*support vectors*(支持向量)
|
||||
# * 在Scikit-Learn中, 它们存储在这个位置 ``support_vectors_``(一个属性)
|
||||
model.support_vectors_
|
||||
# * 观察可以发现,只需要支持向量我们就可以把模型构建出来
|
||||
# * 接下来我们尝试一下,用不同多的数据点,看看效果会不会发生变化
|
||||
# * 分别使用60个和120个数据点
|
||||
def plot_svm(N=10, ax=None):
|
||||
X, y = make_blobs(n_samples=200, centers=2,
|
||||
random_state=0, cluster_std=0.60)
|
||||
X = X[:N]
|
||||
y = y[:N]
|
||||
model = SVC(kernel='linear', C=1E10)
|
||||
model.fit(X, y)
|
||||
ax = ax or plt.gca()
|
||||
ax.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
ax.set_xlim(-1, 4)
|
||||
ax.set_ylim(-1, 6)
|
||||
plot_svc_decision_function(model, ax)
|
||||
# 分别对不同的数据点进行绘制
|
||||
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
|
||||
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
|
||||
for axi, N in zip(ax, [60, 120]):
|
||||
plot_svm(N, axi)
|
||||
axi.set_title('N = {0}'.format(N))
|
||||
plt.show()
|
||||
|
||||
# * 左边是60个点的结果,右边的是120个点的结果
|
||||
# * 观察发现,只要支持向量没变,其他的数据怎么加无所谓!
|
||||
# ### 引入核函数的SVM
|
||||
# * 首先我们先用线性的核来看一下在下面这样比较难的数据集上还能分了吗?
|
||||
from sklearn.datasets.samples_generator import make_circles
|
||||
# 绘制另外一种数据集
|
||||
X, y = make_circles(100, factor=.1, noise=.1)
|
||||
#看看这回线性和函数能解决嘛
|
||||
clf = SVC(kernel='linear').fit(X, y)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
plot_svc_decision_function(clf, plot_support=False);
|
||||
# * 坏菜喽,分不了了,那咋办呢?试试高维核变换吧!
|
||||
#加入了新的维度r
|
||||
from mpl_toolkits import mplot3d
|
||||
r = np.exp(-(X ** 2).sum(1))
|
||||
# 可以想象一下在三维中把环形数据集进行上下拉伸
|
||||
def plot_3D(elev=30, azim=30, X=X, y=y):
|
||||
ax = plt.subplot(projection='3d')
|
||||
ax.scatter3D(X[:, 0], X[:, 1], r, c=y, s=50, cmap='autumn')
|
||||
ax.view_init(elev=elev, azim=azim)
|
||||
ax.set_xlabel('x')
|
||||
ax.set_ylabel('y')
|
||||
ax.set_zlabel('r')
|
||||
plot_3D(elev=45, azim=45, X=X, y=y)
|
||||
#加入高斯核函数
|
||||
clf = SVC(kernel='rbf')
|
||||
clf.fit(X, y)
|
||||
#这回厉害了!
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
plot_svc_decision_function(clf)
|
||||
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
|
||||
s=300, lw=1, facecolors='none');
|
||||
# 使用这种核支持向量机,我们学习一个合适的非线性决策边界。这种核变换策略在机器学习中经常被使用!
|
||||
# ### 调节SVM参数: Soft Margin问题
|
||||
# ### 调节C参数
|
||||
# * 当C趋近于无穷大时:意味着分类严格不能有错误
|
||||
# * 当C趋近于很小的时:意味着可以有更大的错误容忍
|
||||
# 这份数据集中cluster_std稍微大一些,这样才能体现出软间隔的作用
|
||||
X, y = make_blobs(n_samples=100, centers=2,
|
||||
random_state=0, cluster_std=0.8)
|
||||
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
#加大游戏难度的数据集
|
||||
X, y = make_blobs(n_samples=100, centers=2,
|
||||
random_state=0, cluster_std=0.8)
|
||||
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
|
||||
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
|
||||
# 选择两个C参数来进行对别实验,分别为10和0.1
|
||||
for axi, C in zip(ax, [10.0, 0.1]):
|
||||
model = SVC(kernel='linear', C=C).fit(X, y)
|
||||
axi.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
plot_svc_decision_function(model, axi)
|
||||
axi.scatter(model.support_vectors_[:, 0],
|
||||
model.support_vectors_[:, 1],
|
||||
s=300, lw=1, facecolors='none');
|
||||
axi.set_title('C = {0:.1f}'.format(C), size=14)
|
||||
X, y = make_blobs(n_samples=100, centers=2,
|
||||
random_state=0, cluster_std=1.1)
|
||||
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
|
||||
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
|
||||
|
||||
# 选择不同的gamma值来观察建模效果
|
||||
for axi, gamma in zip(ax, [10.0, 0.1]):
|
||||
model = SVC(kernel='rbf', gamma=gamma).fit(X, y)
|
||||
axi.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
|
||||
plot_svc_decision_function(model, axi)
|
||||
axi.scatter(model.support_vectors_[:, 0],
|
||||
model.support_vectors_[:, 1],
|
||||
s=300, lw=1, facecolors='none');
|
||||
axi.set_title('gamma = {0:.1f}'.format(gamma), size=14)
|
||||
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
# ## Example: Face Recognition
|
||||
#
|
||||
# As an example of support vector machines in action, let's take a look at the facial recognition problem.
|
||||
# We will use the Labeled Faces in the Wild dataset, which consists of several thousand collated photos of various public figures.
|
||||
#读取数据集
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy import stats
|
||||
import seaborn as sns; sns.set()
|
||||
from sklearn.datasets import fetch_lfw_people
|
||||
faces = fetch_lfw_people(min_faces_per_person=60)
|
||||
#看一下数据的规模
|
||||
print(faces.target_names)
|
||||
print(faces.images.shape)
|
||||
# Let's plot a few of these faces to see what we're working with:
|
||||
# * 每个图的大小是 [62×47]
|
||||
# * 在这里我们就把每一个像素点当成了一个特征,但是这样特征太多了,用PCA降维一下吧!
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.decomposition import PCA
|
||||
from sklearn.pipeline import make_pipeline
|
||||
#降维到150维
|
||||
pca = PCA(n_components=150, whiten=True, random_state=42)
|
||||
svc = SVC(kernel='rbf', class_weight='balanced')
|
||||
#先降维然后再SVM
|
||||
model = make_pipeline(pca, svc)
|
||||
from sklearn.model_selection import train_test_split
|
||||
Xtrain, Xtest, ytrain, ytest = train_test_split(faces.data, faces.target,
|
||||
random_state=40)
|
||||
# 使用grid search cross-validation来选择我们的参数
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
param_grid = {'svc__C': [1, 5, 10],
|
||||
'svc__gamma': [0.0001, 0.0005, 0.001]}
|
||||
grid = GridSearchCV(model, param_grid)
|
||||
# get_ipython().run_line_magic('time', 'grid.fit(Xtrain, ytrain)')
|
||||
# print(grid.best_params_)
|
||||
model = grid.best_estimator_
|
||||
yfit = model.predict(Xtest)
|
||||
yfit.shape
|
||||
from sklearn.metrics import classification_report
|
||||
print(classification_report(ytest, yfit,
|
||||
target_names=faces.target_names))
|
||||
# * 精度(precision) = 正确预测的个数(TP)/被预测正确的个数(TP+FP)
|
||||
# * 召回率(recall)=正确预测的个数(TP)/预测个数(TP+FN)
|
||||
# * F1 = 完整例子*精度*召回率/(精度+召回率)
|
||||
from sklearn.metrics import confusion_matrix
|
||||
mat = confusion_matrix(ytest, yfit)
|
||||
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False,
|
||||
xticklabels=faces.target_names,
|
||||
yticklabels=faces.target_names)
|
||||
plt.xlabel('true label')
|
||||
plt.ylabel('predicted label')
|
||||
plt.show()
|
||||
# * 这样显示出来能帮助我们查看哪些人更容易弄混
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue