У меня есть следующий код:
import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.grid_search import GridSearchCV
# Load ANSI file into pandas dataframe.
df = pd.read_csv(r'c:/papf.txt', encoding = 'latin1', usecols=['LAST_NAME', 'RACE'])
# Convert last name to lower case.
df['LAST_NAME'] = df['LAST_NAME'].str.lower()
# Remove the last name spaces.
df['LAST_NAME'] = df['LAST_NAME'].str.replace(' ', '')
# Remove all rows where race is NOT in African, White, Asian.
df = df.drop(df[~df['RACE'].isin(['African', 'White', 'Asian'])].index)
class AverageWordLengthExtractor(BaseEstimator, TransformerMixin):
"""Takes in dataframe, extracts last name column, outputs average word length"""
def __init__(self):
pass
def average_word_length(self, name):
"""Helper code to compute average word length of a name"""
return np.mean([len(word) for word in name.split()])
def transform(self, df, y=None):
"""The workhorse of this feature extractor"""
return df['LAST_NAME'].apply(self.average_word_length)
def fit(self, df, y=None):
"""Returns self unless something different happens in train and test"""
return self
# Split into train and test sets with 20% used for testing.
data_train, data_test, y_train, y_true = \
train_test_split(df['LAST_NAME'], df['RACE'], test_size=0.2)
# Build the pipeline.
ngram_count_pipeline = Pipeline([
('ngram', CountVectorizer(ngram_range=(1, 4), analyzer='char'))
])
pipeline = Pipeline([
('feats', FeatureUnion([
('ngram', ngram_count_pipeline), # can pass in either a pipeline
#('ngram', CountVectorizer(ngram_range=(1, 4), analyzer='char')),
('ave', AverageWordLengthExtractor()) # or a transformer
])),
('clf', LinearSVC()) # classifier
])
# Train the classifier.
classifier = LinearSVC()
model = pipeline.fit(data_train)
# Test the classifier.
y_test = model.predict(data_test)
# Print the accuracy percentage.
print(accuracy_score(y_true, y_test))
#one = ngram_counter.transform('chapman')
#print(model.predict(one))
Я придумал этот код, основанный на этом отличном сообщении в блоге Мишель Фуллвуд .
Однако сообщение в блоге не раскрывает следующую часть:
Обратите внимание, что первый элемент в FeatureUnion
это ngram_count_pipeline
. Это просто Pipeline
, созданный из преобразователя, извлекающего столбцы, и CountVectorizer
(средство извлечения столбцов необходимо сейчас, когда мы работаем с кадром данных Pandas, а не напрямую отправляем список имен дорог через конвейер).
Итак, мой вопрос: как я могу добавить n-грамм CountVectorizer
в качестве конвейера и как мне сделать часть извлечения столбцов?
Кроме того, как бы я использовал модель, чтобы делать предсказания для фамилии Чепмен?
Получение точности и вероятности для каждого выходного класса также было бы потрясающим.
Мои входные данные - это в основном фамилия с выходом гонки.
Я также получаю следующие предупреждения, которые я не знаю, как решить:
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\grid_search.py:42: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20. DeprecationWarning)
Я обновился до последней версии Anaconda (Python 3.6.5 | упакован в conda-forge | (по умолчанию, 6 апреля 2018 г., 16:13:16) [32-разрядная версия MSC v.1900 (Intel)] на win32), но это не решило предупреждение.
Пример данных CSV:
LAST_NAME,RACE
Ramaepadi,African
Motsamai,African
Van Rooyen,White
Khan,Asian
Du Plessis,White
Singh,Asian
Madlanga,African
Janse van Rensburg,