簡體   English   中英

如何在 Python 中將 PCA 用於術語文檔矩陣?

[英]How can I use the PCA for a term-document matrix in Python?

我有一個詞干列表:

text = ['uplink platz windows zukunft spiel effizient virtuell zukunft thema spiel zukunft lang serv spiel gemeinsam episod nachhor herunterlad kiosk brows app ios android fruh episod podcast',
'monat linux zufall eingebaut start einsatz jeweil sogenannt jeweil linux hardwar blick schlecht intel cor intel c’t schlussel linux zufall security arm teil amds million national syst spat chips entwicklung intel system googl tatsach einsatz welt angab memory linux chips', 
'redakteur video test gerat test apps redakteur video ausfuhr test', 
'windows richtig test hannov programmi stark netzwerk haufig bewerb kolleg entwickl les haufig frag lieb stell eigent kolleg link schlecht vergang ergebnis woch mail',
'webseit video frag hilft bewerb frag video', 'vergang onlin vergang onlin moglich datei moglich onlin gesetz angab vorlieg zugriff moglich lieb moglich lieb moglich entwickelt tim zustand tag antwort', 
'c’t kaspersky person erschein verbind kaspersky weis kaspersky nutz inhalt verbind haufig serv brows ungefahr brows modern kaspersky zugriff jeweil sit kaspersky schutz probl microsoft websit cod websit webseit vergang kaspersky mitt august herstell offenbar patch idee nutz googl rahm weis verschied verbind ergebnis prozent prozent herstell alternativ kaspersky kaspersky offent tag herstell probl moglich probl kaspersky person datenleck ausgab zufall id websit nutz probl id websit id websit einzeln besuch brows kaspersky notig websit moglich probl kaspersky verbesser kaspersky kaspersky juni juni automat lang id nutz rei', 
'ifa stand ding helf ifa besuch entwicklung erhalt vergang mensch markt weltweit grosst weiss fest ifa person besitz herstell hom gerat ifa gerat bess system system verfug hoh ifa notig quell probl entsprech oled million samsung divers vorlieg verfugbar preis herstell ifa modell kunftig samsung ifa herstell ifa smartphon eingebaut samsung besuch zukunft ifa ifa zukunft euro euro euro euro euro euro tag euro euro euro euro euro', 
'les les geschicht inhalt weis podcast ausgab redaktion story folg technisch zukunft geschicht geschicht ulrich hilgefort stori rss-feed podcast onlin buch usa deutschland hannov hannov lokal kurz onlin onlin zustand verschied projekt sprech geschicht hannov regelmass',
'bess august vergang erschein million serv spiel stark updat passend kart spiel spiel spiel bess speziell august besitz welt klassisch stand patch spat entwickl verbessert erreich inhalt august stund vollig offenbar arm team zockt serv vergang updat angab notig spiel notig']

我正在使用此函數將列表轉換為術語文檔矩陣:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
def tdm(data):
    vec = CountVectorizer()
    X = vec.fit_transform(data)
    df = pd.DataFrame(X.toarray(), columns=vec.get_feature_names())
    return df

如何使用 PCA 來減少生成矩陣的維度?

以下代碼將PCA應用於您的問題:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import PCA

def tdm(data):
    vec = CountVectorizer()
    X = vec.fit_transform(data)
    df = pd.DataFrame(X.toarray(), columns=vec.get_feature_names())
    return df

def find_principal_components(n, data):
    pca = PCA(n_components = n)
    principalComponents = pca.fit_transform(data)
    return pd.DataFrame(pca.components_, columns=data.columns)

text = ['uplink platz windows zukunft spiel effizient virtuell zukunft thema spiel zukunft lang serv spiel gemeinsam episod nachhor herunterlad kiosk brows app ios android fruh episod podcast',
'monat linux zufall eingebaut start einsatz jeweil sogenannt jeweil linux hardwar blick schlecht intel cor intel c’t schlussel linux zufall security arm teil amds million national syst spat chips entwicklung intel system googl tatsach einsatz welt angab memory linux chips', 
'redakteur video test gerat test apps redakteur video ausfuhr test', 
'windows richtig test hannov programmi stark netzwerk haufig bewerb kolleg entwickl les haufig frag lieb stell eigent kolleg link schlecht vergang ergebnis woch mail',
'webseit video frag hilft bewerb frag video', 'vergang onlin vergang onlin moglich datei moglich onlin gesetz angab vorlieg zugriff moglich lieb moglich lieb moglich entwickelt tim zustand tag antwort', 
'c’t kaspersky person erschein verbind kaspersky weis kaspersky nutz inhalt verbind haufig serv brows ungefahr brows modern kaspersky zugriff jeweil sit kaspersky schutz probl microsoft websit cod websit webseit vergang kaspersky mitt august herstell offenbar patch idee nutz googl rahm weis verschied verbind ergebnis prozent prozent herstell alternativ kaspersky kaspersky offent tag herstell probl moglich probl kaspersky person datenleck ausgab zufall id websit nutz probl id websit id websit einzeln besuch brows kaspersky notig websit moglich probl kaspersky verbesser kaspersky kaspersky juni juni automat lang id nutz rei', 
'ifa stand ding helf ifa besuch entwicklung erhalt vergang mensch markt weltweit grosst weiss fest ifa person besitz herstell hom gerat ifa gerat bess system system verfug hoh ifa notig quell probl entsprech oled million samsung divers vorlieg verfugbar preis herstell ifa modell kunftig samsung ifa herstell ifa smartphon eingebaut samsung besuch zukunft ifa ifa zukunft euro euro euro euro euro euro tag euro euro euro euro euro', 
'les les geschicht inhalt weis podcast ausgab redaktion story folg technisch zukunft geschicht geschicht ulrich hilgefort stori rss-feed podcast onlin buch usa deutschland hannov hannov lokal kurz onlin onlin zustand verschied projekt sprech geschicht hannov regelmass']

df = tdm(text)

print(df) # 9 rows x 170 columns

principalDF = find_principal_components(2, df)

print(principalDF) # 9 rows x 2 columns

應該注意的是,這並沒有做太多事情——知道PCA什么,它是否有效等等是一個比Stack Overflow 范圍更大的問題。

我建議檢查以下內容:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM