python的vq函數(shù) Python mq

Python基礎(chǔ)教程

2020年最新Python零基礎(chǔ)教程(高清視頻)百度網(wǎng)盤

為廣饒等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及廣饒網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站制作、廣饒網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

鏈接:

提取碼: 5kid 復(fù)制這段內(nèi)容后打開百度網(wǎng)盤手機App,操作更方便哦? ?

若資源有問題歡迎追問~ ?

如何用Python編寫一個素數(shù)環(huán)?

代碼:

n = int(input("請輸入最大數(shù)n:"))

lists = [[1]]#多個素數(shù)環(huán)

surplusnum = list(range(1,n+1)) #剩余的數(shù)

def sumisprime(x, y):

#x與y之和是否是素數(shù)

isprime=True#是否是素數(shù)

s = x + y#和

for i in range(2, int(s**0.5)+1):

#素數(shù)判定法:從2開始直到此數(shù)的開方內(nèi)的整數(shù)都不能被該數(shù)整除,則此數(shù)為素數(shù)

if s%i == 0:#能被整除

isprime = False#不是素數(shù)

break#跳出循環(huán)

return isprime#返回后否是素數(shù)(是:True,否:False)

changelast=lambda listx,addvalue:listx[0:-1]+[addvalue]#改變列表末尾的函數(shù)

while len(lists[0] if len(lists) else [0]*n) n:#當素數(shù)環(huán)長度小于最大數(shù)時

n2 = len(lists[0]) #n2為判定,理論當前列表長度最大值

for listn in lists:#遍歷各個可能的素數(shù)環(huán)

surplusnum=list(range(1,n+1))#默認值

for j in listn:#遍歷當前列表的數(shù)

surplusnum.remove(j)#剩余的數(shù)中刪除此數(shù)

for i in surplusnum:#遍歷剩余的數(shù)

if sumisprime(listn[n2-1], i):#最后一個數(shù)與它的和是素數(shù)

if len(listn) == n2:#如果現(xiàn)在這個列表是沒有被添加過的

listn.append(i)#增加在這個列表

else:#如果該列表已經(jīng)被添加過

lista = changelast(listn, i)#要加入的列表

if lista not in lists:#如果不在這個列表里

lists.append(lista)#添加到另一個列表

for listn in lists.copy():#防止lists被刪造成影響

if len(listn) != n2+1:#如果長度沒有達到預(yù)期(+1)

lists.remove(listn)#刪除該列表(取消此可能性)

if len(lists[0]) == n:#已經(jīng)符合條件

for listn in lists:#遍歷列表,檢查首尾

if sumisprime(listn[-1], listn[0]):#如果首尾相加等于素數(shù)

print(listn)#環(huán)成立,打印出來

break#結(jié)束循環(huán)

說明:經(jīng)試驗,都沒什么問題,n=12也能很快運算完(但我勸你不要打出來),如果你只需要1個素數(shù)環(huán),可以把break的縮進調(diào)到print(listn)并列。

Python數(shù)據(jù)挖掘從哪些

一. 基于Python的數(shù)據(jù)挖掘 基本架構(gòu)

1. matplotlib, 圖形化

2. pandas,數(shù)據(jù)挖掘的關(guān)鍵, 提供各種挖掘分析的算法

3. numpy, 提供基本的統(tǒng)計

scipy, 提供各種數(shù)學公式

4. python common lib,python基本框架

二. 環(huán)境搭建

1. 安裝python

2. 安裝pip

pandas依賴的pip版本,最低是8.0.0。如果pip是8以下的版本,如7.2.1,需要升級pip.

命令是“python -m pip install -U pip”,這是windows版本。

Linux是”pip install -U pip“

通過命令“pip --version”, 可以查看pip版本號

3. 安裝pandas

命令“pip install pandas", 這是windows版本。

Linux平臺可用

sudo apt-get install python-pandas

4. 安裝matplotlib

pip install matplotlib

三. 數(shù)據(jù)類型

pypython common type

string list tuple dict set

6鐘學列

list, tuple, string, unicode string, buffer object, xrange

pandas type

ndarray, series dateFrame

ndarray, 數(shù)組類型,新增原因:

list, tuple是基于指針+對象設(shè)計的。即list,tuple存儲的是void*指針,指針指向具體對象的數(shù)據(jù)。

因為是void*指針,所以二者可以存儲各種數(shù)據(jù)類型,即數(shù)據(jù)類型可以不統(tǒng)一。

雖然存儲豐富,但如果數(shù)據(jù)量過大時,即處理大數(shù)據(jù)時,有弊端。

1. 存儲空間大,浪費內(nèi)存。因為存兩部分,指針+數(shù)據(jù)

2. 讀取慢,通過index,找到指針;基于指針,找到數(shù)據(jù)

所以在大數(shù)據(jù)處理時,新增ndarray,數(shù)字類型,類似C++ 數(shù)組。存儲相同,讀取、修改快捷。

別名:array, 有利于節(jié)省內(nèi)存、提高CPU的計算時間,有豐富的處理函數(shù)

series,變長字典,

類似一維數(shù)組的對象;有數(shù)據(jù)和索引組成

新增原因:

dict是無序的,它的key和value存在映射關(guān)系。但key和value之間是不獨立的,存儲在一起。

如果需要對一項進行操作,會影響到另外一項。所以有了series, series的key和value是獨立的,獨立存儲。

series的key是定長有序的。通過series.key獲取整個索引, 通過series.values獲取所有values.

series的key,可以通過series.index.name,設(shè)置唯一的名稱。

series整體也可以設(shè)置唯一名稱,通過series.name

DataFrame:

1. 一個表格型的數(shù)據(jù)結(jié)構(gòu)

2. 含有一組有序的列(類似于index)

3. 可以認為是,共享一個index的Series集合

data1={'name':['java', 'c', 'python'], 'year': [2,2,3]}

frame = pd.DataFrame(data1)

------------------------------------------------

四. 基本的數(shù)據(jù)分析流程:

1. 數(shù)據(jù)的獲取

2. 數(shù)據(jù)準備--規(guī)格化,建立各種索引index

3. 數(shù)據(jù)的顯示、描述,用于調(diào)試

如df.index, df.values, df.head(n), df.tail(n) df.describe

4. 數(shù)據(jù)的選擇

index獲取, 切片獲取, 行、列獲取, 矩形區(qū)域獲取

index獲取,df.row1 或者 df['row1']

行列,df.loc[行l(wèi)ist, 列l(wèi)ist], 如df.loc[0:1,['co1','col2'] ]

通過二位索引,取二維左上角,df.iloc[0,0],也可以列表 df.iloc[0:2,0:2],取前2行。

5. 簡單的統(tǒng)計與處理

統(tǒng)計平均值、最大值等

6. Grouping 分組

df.groupby(df.row1)

7. Merge合并

append追加,

contact連接, 包含append功能,也可以兩個不同的二維數(shù)據(jù)結(jié)構(gòu)合并

join連接, SQL連接,基于相同字段連接,如 sql的where, a.row1 = b.row1

------------------------------------------------

五. 高級的數(shù)據(jù)處理與可視化:

1. 聚類分析

聚類是數(shù)據(jù)挖掘描述性任務(wù)和預(yù)測性任務(wù)的一個重要組成部分,它以相似性為基礎(chǔ),

把相似的對象通過靜態(tài)分類,分成不同的組別和子集。

在python中,有很多第三方庫提供了聚類算法。

聚類算法有很多, 其中K-均值算法,因為其簡單、快捷的特點,被廣泛使用。

基本原理是,

1. 查找某數(shù)據(jù)集的中心,

2. 使用均方差,計算距離。使得每一個數(shù)據(jù)點都收斂在一個組內(nèi);各個組是完全隔離的

案例:

from pylab import *

from scipy.cluster.vq import *

list1=[88,64,96,85]

list2=[92,99,95,94]

list3=[91,87,99,95]

list4 = [78,99,97,81]

list5=[88,78,98,84]

list6=[100,95,100,92]

tempdate = (list1, list2, list3, list4, list5, list6)

tempdate

([88, 64, 96, 85], [92, 99, 95, 94], [91, 87, 99, 95], [78, 99, 97, 81], [88, 78

, 98, 84], [100, 95, 100, 92])

date = vstack(tempdate)

date

array([[ 88, 64, 96, 85],

[ 92, 99, 95, 94],

[ 91, 87, 99, 95],

[ 78, 99, 97, 81],

[ 88, 78, 98, 84],

[100, 95, 100, 92]])

centroids,abc=kmeans(date,2) #查找聚類中心,第二個參數(shù)是設(shè)置分N類,如5類,則為5

centroids # 基于每列查找的中心點,可能是平均值

array([[88, 71, 97, 84],

[90, 95, 97, 90]])

result,cde=vq(date,centroids) #對數(shù)據(jù)集,基于聚類中心進行分類

result

array([0, 1, 1, 1, 0, 1])

2. 繪圖基礎(chǔ)

python描繪庫,包含兩部分,

繪圖api, matplotlib提供各種描繪接口。

集成庫,pylab(包含numpy和matplotlib中的常用方法),描繪更快捷、方便。

import numpy as np

import matplotlib.pyplot as plt

t = np.arange(0,10)

plt.plot(t, t+2)

plt.plot(t,t, 'o', t,t+2, t,t**2, 'o') #(x,y)一組,默認是折線;‘o'是散點,

plt.bar(t,t**2) # 柱狀圖

plt.show()

--------------------

import pylab as pl

t = np.arange(0,10)

plt.plot(t, t+2)

plt.show()

3. matplotlib圖像屬性控制

色彩、樣式

名稱: 圖、橫、縱軸,

plt.title('philip\'s python plot')

plt.xlabel('date')

plt.ylabel('value')

其他: pl.figure(figsize=(8,6),dpi=100)

pl.plot(x,y, color='red', linewidth=3, lable='line1')

pl.legend(loc='upper left')

子圖

pl.subplot(211) # 整體圖片,可以分為二維部分;

#第一個是圖的行,第二個是列;第三個是index, 從左上開始0遍歷 當前行,再下一行。

#如果是2位數(shù),如11,需要‘,’

axes(left, bottom, width, height) # 參數(shù)取值范圍是(0,1), left,是到左邊的距離,bottom是到下面的距離

4. pandas作圖

Series、DataFrame支持直接描繪,封裝了調(diào)用matplotlib的接口,如

series.close.plot()

df.close.plot() #具體參數(shù)類似matplotlib普通接口

屬性控制

類似matplotlib普通接口,修改各種圖片的類型,柱形圖、折線等

--------common-----------------

list, tuple, dict

--------numpy-----------------

ndarray, Series, DataFrame

Python隨機平均分組模型建立怎么寫

可以嘗試使用k-means?clustering:

import?scipy.cluster.vq as vq

import?collections

import?numpy?as np

def auto_cluster(data,threshold=0.1,k=1):

# There are more sophisticated ways of determining k

# See

data=np.asarray(data)

distortion=1e20

while distortionthreshold:

codebook,distortion=vq.kmeans(data,k)

k+=1

code,dist=vq.vq(data,codebook)

groups=collections.defaultdict(list)

for index,datum in zip(code,data):

groups[index].append(datum)

return groups

np.random.seed(784789)

N=20

weights=100*np.random.random(N)

groups=auto_cluster(weights,threshold=1.5,k=N//5)

for index,data in enumerate(sorted(groups.values(),key=lambda d: np.mean(d))):

print('{i}: 9hg4sdb'.format(i=index,d=data))

上面的代碼生成N個權(quán)重的隨機序列.

網(wǎng)站名稱:python的vq函數(shù) Python mq
轉(zhuǎn)載來于:http://www.muchs.cn/article20/hggico.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站Google、全網(wǎng)營銷推廣、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名