【Python】学习笔记总结(第二阶段(7-9)——汇总篇)
文章目錄
- 七、Python簡單爬蟲
- 1.重要知識(shí)與技能
- 2.使用re表達(dá)式抓取網(wǎng)頁文件
- 3.使用requests抓取網(wǎng)頁
- 4.使用re正則表達(dá)式提取數(shù)據(jù)
- 5.使用xPath工具提取數(shù)據(jù)
- 6.使用BeautifulSoup工具
- 八、Python經(jīng)典算法
- 0.Python畫圖
- 1.回歸-線性回歸
- 2.分類-k最近鄰
- 3.聚類
- 3.1.Kmeans(k均值聚類算法)
- 3.2.DBSCAN(基于密度的聚類算法)
- 3.3.層次聚類算法
- 4.降維
- 4.1.PCA算法
- 4.2.FA算法
- 5.學(xué)習(xí)(神經(jīng)網(wǎng)絡(luò))
- 5.1.BP神經(jīng)網(wǎng)絡(luò)
- 6.推薦算法
- 7.時(shí)間序列(視頻學(xué)習(xí))
- 九、數(shù)據(jù)庫與Python交互
- 1.連接MYSQL數(shù)據(jù)庫
- 1.1.創(chuàng)建表
- 1.2.插入數(shù)據(jù)
- 1.3.查詢數(shù)據(jù)
- 1.4.更新數(shù)據(jù)
- 1.5.刪除數(shù)據(jù)
- 1.6.執(zhí)行事務(wù)
- 1.7.讀取數(shù)據(jù)庫表數(shù)據(jù)并寫入excel
- 1.8.讀取excel數(shù)據(jù)并寫入數(shù)據(jù)庫表
- 2.Python人機(jī)交互(Tkinter圖形界面開發(fā))
- 2.1.創(chuàng)建root窗口
- 2.2.控件布局
- 2.3.實(shí)現(xiàn)commend
- 2.4.簡單測試案例
七、Python簡單爬蟲
1.重要知識(shí)與技能
重要知識(shí)與技能:
2.使用re表達(dá)式抓取網(wǎng)頁文件
import re myFile = open('Index.html','r',encoding='UTF-8') myContent = myFile.read() myFile.close() #myPatten = "<li>(.*)</li>" myPatten2 = "([a-zA-Z0-9_\.-]+@[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)+)" mylist = re.findall(myPatten2,myContent) print(mylist)3.使用requests抓取網(wǎng)頁
import requests myURL = 'https://www.3dmgame.com' myContent = requests.get(myURL).content.decode('UTF-8')4.使用re正則表達(dá)式提取數(shù)據(jù)
def Get3DMNews_WithRE():'''得到3DM網(wǎng)站的新聞內(nèi)容:return: 獲取的新聞內(nèi)容'''import requestsimport remyURL = 'https://www.3dmgame.com'myContent = requests.get(myURL).content.decode('UTF-8')myPartten = '<a href="(.*)" target="_blank" >(.*)</a>\n <span>(.*)</span>'myList = re.findall(myPartten,myContent)for item in myList :myNews = {}myNews['title'] = item[0]myNews['herf'] = item[1]myNews['time'] = item[2]print(myNews)passpass5.使用xPath工具提取數(shù)據(jù)
def Get3DMNews_WithXPATH():'''得到3DM網(wǎng)站的新聞內(nèi)容:return: 獲取的新聞內(nèi)容'''import requestsfrom lxml import htmlmyURL = 'https://www.3dmgame.com'myContent = requests.get(myURL).content.decode('UTF-8')etree = html.etreeeTreeHtml = etree.HTML(myContent)myList = eTreeHtml.xpath("//li")for item in myList :myNews = {}myNews['title'] = item.xpath('./a')[0].textmyNews['herf'] = item.xpath('./a/@href')[0]myNews['time'] = item.xpath('./span')[0].textprint(myNews)passpass6.使用BeautifulSoup工具
def Get3DMNews_WithBeautifulSoup():'''得到3DM網(wǎng)站的新聞內(nèi)容:return: 獲取的新聞內(nèi)容'''import requestsfrom bs4 import BeautifulSoupmyURL = 'https://www.3dmgame.com'myContent = requests.get(myURL).content.decode('UTF-8')bsHtml = BeautifulSoup(myContent,'html5lib')myList = bsHtml.find_all('div')[10].find_all('div')[8].find_all('div')[91].find_all('li')for item in myList :myNews = {}myNews['title'] = item.find('a').get_text()myNews['herf'] = item.find('a').get('href')myNews['time'] = item.find('span').get_text()print(myNews)passpass八、Python經(jīng)典算法
0.Python畫圖
Python畫圖
1.回歸-線性回歸
回歸-課程回顧
目的:找一條線,盡可能地?cái)M合數(shù)據(jù)點(diǎn),生成線性回歸模型,并進(jìn)行預(yù)測
解決什么樣的問題:要完成的任務(wù)是預(yù)測一個(gè)連續(xù)值的話,那這個(gè)任務(wù)就是回歸。是離散值的話就是分類
擬合(Fitting):就是說這個(gè)曲線能不能很好的描述某些樣本,并且有比較好的泛化能力。
過擬合(Overfitting):就是太過貼近于訓(xùn)練數(shù)據(jù)的特征了,在訓(xùn)練集上表現(xiàn)非常優(yōu)秀,近乎完美的預(yù)測/區(qū)分了所有的數(shù)據(jù),但是在新的測試集上卻表現(xiàn)平平,不具泛化性,拿到新樣本后沒有辦法去準(zhǔn)確的判斷。
欠擬合(UnderFitting):測試樣本的特性沒有學(xué)到,或者是模型過于簡單無法擬合或區(qū)分樣本。
過擬合和欠擬合的形象解釋
2.分類-k最近鄰
分類-課程回顧
目的:根據(jù)已知樣本進(jìn)行分類學(xué)習(xí),生成模型,并對(duì)測試樣本進(jìn)行預(yù)測
解決什么樣的問題:了解單個(gè)樣本信息特征以及其標(biāo)簽值,根據(jù)其生成模型,并對(duì)測試樣本進(jìn)行預(yù)測
3.聚類
聚類-課程回顧
目的:根據(jù)已知樣本進(jìn)行分類學(xué)習(xí),生成模型,并對(duì)測試樣本進(jìn)行預(yù)測
解決什么樣的問題:不了解單個(gè)樣本信息特征以及其標(biāo)簽值,根據(jù)其生成模型,并對(duì)測試樣本進(jìn)行預(yù)測
3.1.Kmeans(k均值聚類算法)
(k-means clustering algorithm)
3.2.DBSCAN(基于密度的聚類算法)
(Density-Based Spatial Clustering of Applications with Noise)
3.3.層次聚類算法
一篇
二篇
三篇
4.降維
降維-課程回顧
目的:某種映射方法,將原高維空間中的數(shù)據(jù)點(diǎn)映射到低維度的空間中
解決什么樣的問題:通過映射將數(shù)據(jù)降維后進(jìn)行分類,案例征友考量,案例文科指數(shù),理科指數(shù)(可以使用因子分析得到與原數(shù)據(jù)相關(guān)系數(shù))
4.1.PCA算法
4.2.FA算法
5.學(xué)習(xí)(神經(jīng)網(wǎng)絡(luò))
學(xué)習(xí)-課程回顧
目的:某種映射方法,將原高維空間中的數(shù)據(jù)點(diǎn)映射到低維度的空間中
解決什么樣的問題:案例圖像識(shí)別
5.1.BP神經(jīng)網(wǎng)絡(luò)
6.推薦算法
推薦-課程回顧
目的:利用用戶的一些行為,通過一些數(shù)學(xué)算法,推測出用戶可能喜歡的東西
解決什么樣的問題:案例推薦
7.時(shí)間序列(視頻學(xué)習(xí))
時(shí)間序列-課程回顧
目的:根據(jù)已有的歷史數(shù)據(jù)對(duì)未來進(jìn)行預(yù)測
解決什么樣的問題:案例股票
ADF檢驗(yàn)
差分
ADF檢驗(yàn)
反差分
引用自 https://www.runoob.com/python3/python3-mysql.html
九、數(shù)據(jù)庫與Python交互
1.連接MYSQL數(shù)據(jù)庫
1.1.創(chuàng)建表
import pymysql # 打開數(shù)據(jù)庫連接 db = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" )# 使用 cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor cursor = db.cursor()# 使用 execute() 方法執(zhí)行 SQL,如果表存在則刪除 cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")# 使用預(yù)處理語句創(chuàng)建表 sql = """CREATE TABLE EMPLOYEE (FIRST_NAME CHAR(20) NOT NULL,LAST_NAME CHAR(20),AGE INT, SEX CHAR(1),INCOME FLOAT )"""cursor.execute(sql)# 關(guān)閉數(shù)據(jù)庫連接 db.close()1.2.插入數(shù)據(jù)
import pymysql# 打開數(shù)據(jù)庫連接 db = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" )# 使用cursor()方法獲取操作游標(biāo) cursor = db.cursor()# SQL 插入語句 sql = """INSERT INTO EMPLOYEE(FIRST_NAME,LAST_NAME, AGE, SEX, INCOME)VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" # SQL 插入語句2 sql2 = "INSERT INTO EMPLOYEE(FIRST_NAME, \LAST_NAME, AGE, SEX, INCOME) \VALUES ('%s', '%s', %s, '%s', %s)" % \('Mac', 'Mohan', 20, 'M', 2000) try:# 執(zhí)行sql語句cursor.execute(sql)# 提交到數(shù)據(jù)庫執(zhí)行db.commit() except:# 如果發(fā)生錯(cuò)誤則回滾db.rollback()# 關(guān)閉數(shù)據(jù)庫連接 db.close()1.3.查詢數(shù)據(jù)
import pymysql# 打開數(shù)據(jù)庫連接 db = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" )# 使用cursor()方法獲取操作游標(biāo) cursor = db.cursor()# SQL 查詢語句 sql = "SELECT * FROM EMPLOYEE \WHERE INCOME > %s" % (1000) try:# 執(zhí)行SQL語句cursor.execute(sql)# 獲取所有記錄列表results = cursor.fetchall()for row in results:fname = row[0]lname = row[1]age = row[2]sex = row[3]income = row[4]# 打印結(jié)果print ("fname=%s,lname=%s,age=%s,sex=%s,income=%s" % \(fname, lname, age, sex, income )) except:print ("Error: unable to fetch data")# 關(guān)閉數(shù)據(jù)庫連接 db.close()1.4.更新數(shù)據(jù)
import pymysql# 打開數(shù)據(jù)庫連接 db = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" )# 使用cursor()方法獲取操作游標(biāo) cursor = db.cursor()# SQL 更新語句 sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M') try:# 執(zhí)行SQL語句cursor.execute(sql)# 提交到數(shù)據(jù)庫執(zhí)行db.commit() except:# 發(fā)生錯(cuò)誤時(shí)回滾db.rollback()# 關(guān)閉數(shù)據(jù)庫連接 db.close()1.5.刪除數(shù)據(jù)
import pymysql# 打開數(shù)據(jù)庫連接 db = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" )# 使用cursor()方法獲取操作游標(biāo) cursor = db.cursor()# SQL 刪除語句 sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20) try:# 執(zhí)行SQL語句cursor.execute(sql)# 提交修改db.commit() except:# 發(fā)生錯(cuò)誤時(shí)回滾db.rollback()# 關(guān)閉連接 db.close()1.6.執(zhí)行事務(wù)
# SQL刪除記錄語句 sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20) try:# 執(zhí)行SQL語句cursor.execute(sql)# 向數(shù)據(jù)庫提交db.commit() except:# 發(fā)生錯(cuò)誤時(shí)回滾db.rollback()1.7.讀取數(shù)據(jù)庫表數(shù)據(jù)并寫入excel
import pymysql,xlwtdef export_excel(table_name):conn = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" )cur = conn.cursor()sql = 'select * from %s;' %table_name#讀取數(shù)據(jù)cur.execute(sql)fileds = [filed[0] for filed in cur.description]#所有數(shù)據(jù)all_date = cur.fetchall() for result in all_date:print(result)#寫excelbook = xlwt.Workbook() #創(chuàng)建一個(gè)booksheet = book.add_sheet('result') #創(chuàng)建一個(gè)sheet表for col,filed in enumerate(fileds):sheet.write(0,col,filed)#從第一行開始寫row = 1for data in all_date:for col,filed in enumerate(data):sheet.write(row,col,filed)row += 1book.save('%s.xls' %table_name)passexport_excel('stocks')1.8.讀取excel數(shù)據(jù)并寫入數(shù)據(jù)庫表
import pymysql import xlrd conn = pymysql.connect("數(shù)據(jù)庫IP地址","用戶名","密碼","數(shù)據(jù)庫" ) cursor = conn .cursor()#讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫 book = xlrd.open_workbook("students.xls") sheet = book.sheet_by_name('Sheet1') query = 'insert into student_tbl (name, sex, minzu, danwei_zhiwu, phone_number, home_number) values (%s, %s, %s, %s, %s, %s)' for r in range(1, sheet.nrows):name = sheet.cell(r,0).valuesex = sheet.cell(r,1).valueminzu = sheet.cell(r,2).valuedanwei_zhiwu = sheet.cell(r,3).valuephone_number = sheet.cell(r,4).valuehome_number = sheet.cell(r,5).valuevalues = (name, sex, minzu, danwei_zhiwu, phone_number, home_number)# 執(zhí)行sql語句# 往SQL添加一條數(shù)據(jù)cursor.execute(query , values) print(values) cursor.close() db.commit() db.close()2.Python人機(jī)交互(Tkinter圖形界面開發(fā))
2.1.創(chuàng)建root窗口
#導(dǎo)入Tkinter包全部內(nèi)容 from tkinter import * #Tkinter根窗口實(shí)例化 root = Tk() #設(shè)置窗口標(biāo)題 root.title("my_Title")2.2.控件布局
#設(shè)置label控件 label1 = Label(root,text = 'Number:') label1.grid(row = 0, column = 0) #設(shè)置text控件 text1 = Text(root,width = 30 , height = 1) text1.grid(row = 1, column = 0)2.3.實(shí)現(xiàn)commend
#設(shè)置調(diào)用函數(shù) def myCalculate():a = int(text1.get("1.0",END))#從頭開始取到結(jié)尾sum = a * 3text2.delete('1.0',END)text2.insert(INSERT,sum)pass #設(shè)置Button button = Button(root,text="click sum",command = myCalculate) button.grid(row = 4, column = 0)2.4.簡單測試案例
#導(dǎo)入Tkinter包全部內(nèi)容 from tkinter import * #Tkinter根窗口實(shí)例化 root = Tk() #設(shè)置窗口標(biāo)題 root.title("my_Title") #設(shè)置label控件 label1 = Label(root,text = 'Number:') label1.grid(row = 0, column = 0) #設(shè)置text控件 text1 = Text(root,width = 30 , height = 1) text1.grid(row = 1, column = 0) #設(shè)置label控件 label2 = Label(root,text = 'Sum:') label2.grid(row = 2, column = 0) #設(shè)置text控件 text2 = Text(root,width = 30 , height = 1) text2.grid(row = 3, column = 0) #設(shè)置調(diào)用函數(shù) def myCalculate():a = int(text1.get("1.0",END))#從頭開始取到結(jié)尾sum = a * 3text2.delete('1.0',END)text2.insert(INSERT,sum)pass #設(shè)置Button button = Button(root,text="click sum",command = myCalculate) button.grid(row = 4, column = 0)mainloop()總結(jié)
以上是生活随笔為你收集整理的【Python】学习笔记总结(第二阶段(7-9)——汇总篇)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】学习笔记总结9(数据库与
- 下一篇: Python学习:基本概念