简体   繁体   中英

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

I have a list of stemmed words:

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']

I am using this function to convert the list into a term-document matrix:

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

How can I use the PCA to reduce the dimensions of the generated matrix?

The following codes apply PCA to your problem:

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

It should be noted this isn't doing much -- knowing what PCA is, if its being effective, etc is a bigger question than the scope of Stack Overflow .

I suggest checking out the following:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM