<jupyter_start><jupyter_text>To NumPyUnderstand how DataFrames can be converted to 2-D NumPy arrays.  A. Machine learningThe DataFrame object is great for storing a dataset and performing data analysis in Python. However, most machine learning frameworks (e.g. TensorFlow), work directly with NumPy data. Furthermore, the NumPy data used as input to machine learning models must solely contain quantitative values.Therefore, to use a DataFrame's data with a machine learning model, we need to convert the DataFrame to a NumPy matrix of quantitative data. So even the categorical features of a DataFrame, such as gender and birthplace, must be converted to quantitative values.  B. Indicator featuresWhen converting a DataFrame to a NumPy matrix of quantitative data, we need to find a way to modify the categorical features in the DataFrame.The easiest way to do this is to convert each categorical feature into a set of indicator features for each of its categories. The indicator feature for a specific category represents whether or not a given data sample belongs to that category.The code below shows a DataFrame with indicator features.<jupyter_code># predefined non-indicator DataFrame
print('{}\n'.format(df))

# predefined indicator Dataframe
print('{}\n'.format(indicator_df))<jupyter_output><empty_output><jupyter_text>In the code above, the DataFrame df has a single categorical feature called Color. The corresponding indicator features for Color are shown in indicator_df.Note that an indicator feature contains 1 when the row has that particular category, and 0 if the row does not.  C. Converting to indicatorsIn pandas, we convert each categorical feature of a DataFrame to indicator features with the get_dummies function. The function takes in a DataFrame as its required argument, and returns the DataFrame with each of its categorical features converted to indicator features.The code below demonstrates how to use the get_dummies function.<jupyter_code># predefined df
print('{}\n'.format(df))

converted = pd.get_dummies(df)
print('{}\n'.format(converted.columns))

print('{}\n'.format(converted[['teamID_BOS',
                               'teamID_PIT']]))
print('{}\n'.format(converted[['lgID_AL',
                               'lgID_NL']]))<jupyter_output><empty_output><jupyter_text>Note that the indicator features have the original categorical feature's label as a prefix. This makes it easy to see where each indicator feature originally came from.  D. Converting to NumPyAfter converting all the categorical features to indicator features, the DataFrame should have all quantitative data. We can then convert to a NumPy matrix using the values function.The code below converts a DataFrame, df into a NumPy matrix.<jupyter_code># predefined indicator df
print('{}\n'.format(df))

n_matrix = df.values
print(repr(n_matrix))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06102635228848821, 'fraction_numerical': 0.0010402219140083217, 'mean_word_length': 4.997920997920998, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '573643', 'license': 'MIT', 'n_tokens_mistral': 737, 'n_tokens_neox': 696, 'n_words': 362, 'path': '2.Data Analysis with pandas/13.To NumPy.ipynb', 'repo_name': 'mayank8200/Numpy-Tutorials-for-Machine-Learning', 'size': '2884'}