how to use onehotcoding
<p>so im trying to do a project that asks to do one hot coding for a certain part. but i have no idea how to use it. ive been using google to try and understand but i just cant understand. my question is below.</p>

<p>Now, we want to use the categorical features as well! Thus, we have to perform
OneHotEncoding for the categorical features. To do this, each categorical feature should
be replaced with dummy columns in the feature table (one column for each possible value
of a categorical feature), and then encode it in a binary manner such that at most only
one of the dummy columns can take â1â at a time (and zero for the rest). For example,
âGenderâ can take two values âmâ and âfâ. Thus, we need to replace this feature (in the
feature table) by two columns titled âmâ and âfâ. Wherever, we have a male subject, we
can put â1â and â0â in the columns âmâ and âfâ. Wherever, we have a female subject, we
can put â0â and â1â in the columns âmâ and âfâ. (Hint: you will need 4 columns to encode
âChestPainâ and 3 columns to encode âThalâ).</p>

<p>my code so far is this ,</p>

<pre><code># a- Read the dataset from the following URL:
# and assign it to a Pandas DataFrame 

heart_d = pd.read_csv("C:/Users/Michael/Desktop/HW2/Heart_s.csv")


feature_cols = ['Age','RestBP','Chol','RestECG','MaxHR','Oldpeak']
X = heart_d[feature_cols]

y = heart_d['AHD']

# Randomly splitting the original dataset into training set and testing set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=3)
</code></pre>

<p>this works so far , but now i have to use that one hot encoding for the catagorical stuff , but im totally lost with how that works.
the 3 categorical features in the dataset are (Gender, ChestPain,
Thal). i tried doin this</p>

<pre><code>df_cp = pd.get_dummies(heart_d['ChestPain'])
df_g = pd.get_dummies(heart_d['Gender'])
df_t = pd.get_dummies(heart_d['Thal'])

df_new = pd.concat([df, df_cp,df_g,df_t ], axis=1)
</code></pre>

<p>but im not sure thats working , when i run my classifications i get the same answer for everything</p>

<p>I guess you may use <a href="http://scikit-learn.org/" rel="nofollow">scikit-learn</a> for data train, here is one-hot encoder example in <a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html" rel="nofollow">it</a>:</p>

<pre><code>from sklearn.preprocessing import OneHotEncoder
&gt;&gt;&gt; enc = OneHotEncoder()
&gt;&gt;&gt; enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])  
OneHotEncoder(categorical_features='all', dtype=&lt;... 'numpy.float64'&gt;,
   handle_unknown='error', n_values='auto', sparse=True)
&gt;&gt;&gt; enc.n_values_
array([2, 3, 4])
&gt;&gt;&gt; enc.feature_indices_
array([0, 2, 5, 9])
&gt;&gt;&gt; enc.transform([[0, 1, 1]]).toarray()
array([[ 1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.]])
</code></pre>

<p>==== UPDATE ====</p>

<p>I write one detail example about how to use one-hot encoder for string attributes, with <a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.DictVectorizer.html" rel="nofollow">DictVectorizer</a> </p>

<pre><code>import pandas as pd
from sklearn.feature_extraction import DictVectorizer as DV        

d = [
    {'country':'A', 'Gender':'M'},
    {'country':'B', 'Gender':'F'},                                 
    {'country':'C', 'Gender':'F'}
]               
df = pd.DataFrame(d)                                               
print df        
test_d = [
    {'country':'A', 'Gender':'F'},                                 
    {'country':'B', 'Gender':'F'}

]                                                                  
test_df = pd.DataFrame(test_d)
print test_df                                                      

train_x = df.T.to_dict().values()                                  
vx = DV(sparse=False)

transform_x = vx.fit_transform(train_x)
print 'transform_train_df'
print transform_x

test_x = test_df.T.to_dict().values()
transform_test_x = vx.transform(test_x)
print 'transform_test_df'
print transform_test_x                                             
</code></pre>

<p>output:</p>

<pre><code>  Gender country
0      M       A
1      F       B
2      F       C
  Gender country
0      F       A
1      F       B
transform_train_df
[[ 0.  1.  1.  0.  0.]
 [ 1.  0.  0.  1.  0.]
 [ 1.  0.  0.  0.  1.]]
transform_test_df
[[ 1.  0.  1.  0.  0.]
 [ 1.  0.  0.  1.  0.]]
</code></pre>

python scikit-learn jupyter jupyter-notebook