<jupyter_start><jupyter_text>OneHotEncoding for categorical data ```Users = age, locationage is floatlocation in ['Paris', 'Tokyo', 'New York']```<jupyter_code>X = np.array([[15.9, 1], # from Tokyo
              [21.5, 2], # from New York
              [31.3, 0], # from Paris
              [25.1, 2], # from New York
              [63.6, 1], # from Tokyo
              [14.4, 1], # from Tokyo
              ])

y = np.array([0, 1, 1, 1, 0, 0])
# Don't do this!
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(C=100).fit(X, y)
lr.score(X, y)
lr.coef_
from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(categorical_features=[1], sparse=False).fit(X)
X_one_hot = encoder.transform(X)
X_one_hot
lr = LogisticRegression().fit(X_one_hot, y)
lr.score(X_one_hot, y)<jupyter_output><empty_output><jupyter_text>```Users = age, locationage is floatlikes puppies in ['yes', 'no']location in ['Paris', 'Tokyo', 'New York']```<jupyter_code>X = np.array([[15.9, 1, 1], # likes puppies from Tokyo
              [21.5, 0, 2], # doesn't like puppies from New York
              [31.3, 0, 0], # doesn't like puppies from Paris
              [25.1, 1, 2], # likes puppies from New York
              [63.6, 0, 1], 
              [14.4, 1, 1], 
              ])
OneHotEncoder(categorical_features=[1, 2], sparse=False).fit(X).transform(X)
from sklearn.cross_validation import train_test_split
X_train, X_test = train_test_split(X, random_state=4)

print("X_train:\n%s" % X_train)
print("\nX_test:\n%s" % X_test)
encoder = OneHotEncoder(categorical_features=[1, 2], sparse=False).fit(X_train)
encoder.transform(X_test)
# BAD
OneHotEncoder(categorical_features=[1, 2], sparse=False).fit_transform(X_test)
X_train, X_test = train_test_split(X, random_state=1)

print("X_train:\n%s" % X_train)
print("\nX_test:\n%s" % X_test)
encoder = OneHotEncoder(categorical_features=[1, 2], sparse=False).fit(X_train)
encoder.transform(X_test)
encoder = OneHotEncoder(categorical_features=[1, 2], sparse=False, n_values=[2, 3]).fit(X_train)
encoder.transform(X_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15592417061611374, 'fraction_numerical': 0.03696682464454976, 'mean_word_length': 4.074519230769231, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '290778', 'license': 'Apache-2.0', 'n_tokens_mistral': 831, 'n_tokens_neox': 768, 'n_words': 170, 'path': 'scikit/Chapter 6/OneHotEncoder.ipynb', 'repo_name': 'KarthikKothareddy/Data-Science-Practice', 'size': '2110'}