PHP Ds / Deque toArray () Function

PHP | toarray

Syntax:
public DsDeque::toArray (void): array
Parameters: this function takes no parameters.Return value:this function returns an array that contains all Deque elements in the same order.Below the program illustrate the Ds / Deque::toArray()function in PHP:Program 1:  
// Declare deque $deck = new DsDeque ([5, 6, 3, 2, 7, 1]); echo ( " Elements of Deque " );  
// Show Deque elements print_r ( $deck ); echo ( " Array elements " );  
// Use toArray() function for
// convert Deque to array print_r ( $deck -> toArray());  
?>
Exit:
Elements of Deque DsDeque Object ([0] = > 5 [1] = > 6 [2] = > 3 [3] = > 2 [4] = > 7 [5] = > 1) Array elements Array ( [0] = > 5 [1] = > 6 [2] = > 3 [3] = > 2 [4] = > 7 [5] = > 1)
Program 2:  
// Declare deque $deck = new DsDeque ([ "engineer" , "for" , "engineer" , "data structures" ]); echo ( " Elements of Deque " );  
// Show Deque elements var_dump ( $deck ); echo ( " Array elements " );  
// Use toArray() function for
// convert Deque to array var_dump ( $deck -> toArray());  
?>
Exit:
Elements of Deque object (DsDeque) # 1 (4) {[0] = > string (5) "engineer" [1] = > string (3) "for" [2] = > string (5) "engineer" [3] = > string (15) "data structures"} Array elements array (4) {[0] = > string (5) "engineer" [1] = > string (3) "for" [2] = > string (5) "engineer" [3] = > string (15) "data structures"}
Link: http : //php.net/manual/en/ds-deque.toarray.php



PHP Ds / Deque toArray () Function: StackOverflow Questions

Answer #1

Approach 1: You can use pandas" pd.get_dummies.

Example 1:

import pandas as pd
s = pd.Series(list("abca"))
pd.get_dummies(s)
Out[]: 
     a    b    c
0  1.0  0.0  0.0
1  0.0  1.0  0.0
2  0.0  0.0  1.0
3  1.0  0.0  0.0

Example 2:

The following will transform a given column into one hot. Use prefix to have multiple dummies.

import pandas as pd
        
df = pd.DataFrame({
          "A":["a","b","a"],
          "B":["b","a","c"]
        })
df
Out[]: 
   A  B
0  a  b
1  b  a
2  a  c

# Get one hot encoding of columns B
one_hot = pd.get_dummies(df["B"])
# Drop column B as it is now encoded
df = df.drop("B",axis = 1)
# Join the encoded df
df = df.join(one_hot)
df  
Out[]: 
       A  a  b  c
    0  a  0  1  0
    1  b  1  0  0
    2  a  0  0  1

Approach 2: Use Scikit-learn

Using a OneHotEncoder has the advantage of being able to fit on some training data and then transform on some other data using the same instance. We also have handle_unknown to further control what the encoder does with unseen data.

Given a dataset with three features and four samples, we let the encoder find the maximum value per feature and transform the data to a binary one-hot encoding.

>>> from sklearn.preprocessing import OneHotEncoder
>>> enc = OneHotEncoder()
>>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])   
OneHotEncoder(categorical_features="all", dtype=<class "numpy.float64">,
   handle_unknown="error", n_values="auto", sparse=True)
>>> enc.n_values_
array([2, 3, 4])
>>> enc.feature_indices_
array([0, 2, 5, 9], dtype=int32)
>>> enc.transform([[0, 1, 1]]).toarray()
array([[ 1.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.]])

Here is the link for this example: http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

Get Solution for free from DataCamp guru