J'ai un dataframe appelé df
df['algorithms'] = df['Comments'].apply(lambda x: " ".join(x for x in x.split() if x in algorithms))
et une liste appelée algorithmes
Gender Country algorithms male USA male Canada hmm pca svm female USA clustering female Germany female Nigeria nltk male Ghana
Donc techniquement, pour chaque ligne de la colonne Commentaires , J'essaie d'extraire les mots qui apparaissent dans la liste des algorithmes. C'est ce que j'essaie d'accomplir
Gender Country algorithms male USA machine learning, fraud detection male Canada monte carlo method, hmm,pca, svm, neural net female USA clustering, cloud computing female Germany logistical regression, data management, fraud detection female Nigeria nltk, supervised machine learning male Ghana financial engineering, cross validation, time series
Cependant, c'est ce que j'obtiens
algorithms = ['machine learning','fraud detection', 'monte carlo method', 'time series', 'cross validation', 'supervised machine learning', 'logistical regression', 'nltk','clustering', 'data management','cloud computing','financial engineering']
des mots comme l'apprentissage automatique et la détection des fraudes n'apparaissent pas. en gros, tous les mots de 2 grammes
C'est le code que j'ai utilisé
Gender Country Comments male USA machine learning and fraud detection are a must learn male Canada monte carlo method is great and so is hmm,pca, svm and neural net female USA clustering and cloud computing female Germany logistical regression and data management and fraud detection female Nigeria nltk and supervised machine learning male Ghana financial engineering and cross validation and time series
4 Réponses :
Cela peut fonctionner pour vous:
def f(stringy): contained = filter((lambda x: x in stringy), algorithms) return ",".join(contained) df['algorithms'] = df['Comments'].apply(f)
Et vous pouvez parcourir toutes les chaînes d'entrée avec ceci.
Vous pouvez pandas.Series.str.findall
en combinaison avec join
.
import pandas as pd import re df['algo_new'] = df.algo.str.findall(f"({ '|'.join(ml) })") >> out col1 gender algo algo_new 0 usa male machine learning and fraud detection are a mus... [machine learning, fraud detection, clustering] 1 fr female monte carlo method is great and so is hmm,pca,... [monte carlo method] 2 arg male logistical regression and data management and ... [logistical regression, data management, fraud..
nous utilisons join
pour rejoindre vos chaînes dans votre liste ml
et ajoutez un |
entre chaque chaîne pour capturer la valeur 1 OU
valeur 2 etc. Ensuite, nous utilisons findall
pour trouver toutes les occurrences.
Veuillez noter qu'il utilise une f-string, vous aurez donc besoin de python 3.6+. Faites-moi savoir si vous avez une version inférieure de python.
Pour tous ceux qui s'intéressent aux benchmarks (puisque nous avons 3 réponses), en utilisant chaque solution avec 9,6 millions de lignes et en exécutant chacune 10 fois dans un row nous donne les résultats suivants:
Une autre solution possible:
#function to convert Comments field into a list of terms found in Algorithms list #it searches Comments field for occurrences of algorithm substrings def make_algo_list(comment): algo_list = [] for algo in algorithms: if algo in comment: algo_list.append(algo) return algo_list #apply function to create new column df['algorithms'] = df['Comments'].apply(lambda x: make_algo_list(x))
Flashtext peut également être utilisé pour ce processus d'extraction de mot-clé, qu'il s'agisse d'un bigramme ou de n'importe quel ngram ...
import pandas as pd from flashtext import KeywordProcessor df=pd.DataFrame(data = [['male', 'USA', 'machine learning and fraud detection are a must learn'], ['male', 'Canada','monte carlo method is great and so is hmm,pca, svm and neural net'], ['female','USA','clustering and cloud'], ['female','Germany', 'logistical regression and data management and fraud detection']] ,columns = ['Gender', 'Country','Comments']) algorithms = ['machine learning','fraud detection', 'monte carlo method', 'time series', 'cross validation', 'supervised machine learning', 'logistical regression', 'nltk','clustering', 'data management','cloud computing','financial engineering'] kp = KeywordProcessor() kp.add_keywords_from_list(algorithms) df['algorithm'] = df['Comments'].apply(lambda x : kp.extract_keywords(x)) #o/p df['algorithm'] Out[20]: 0 [machine learning, fraud detection] 1 [monte carlo method] 2 [clustering] 3 [logistical regression, data management, fraud... Name: algorithm, dtype: object
La question n'a en fait rien à voir avec
machine-learning
- merci de ne pas spammer les balises non pertinentes (supprimées).