Numpy and Pandas initialization and modification

Following are the methods with which you can initialise an array
1. Initialise array
from numpy import ndarray
a = ndarray((5,),int)

2. Initialise char numpy array
import numpy as np
country = np.array(['USA', 'Japan', 'UK', '', 'India', 'China'])

3. Using numpy empty
chars = np.empty((3),dtype=str)

4. Make a list and convert to numpy array
country=['USA', 'Japan', 'UK', '', 'India', 'China']
ll=[]
for i in country:
  ll.append(i)
country=np.asarray(ll,dtype=str)


Methods to modify numpy array
1. Expand dimension
X_valcnn= np.expand_dims(X_val, axis=2)
2. Reshape
X_traincnn = np.reshape(X_traincnn, (X_traincnn.shape,48,48,1))
3. Make ndarray to 1D array
y_train=y_train.ravel()
4. Convert Dataframe/series to numpy
X_traincnn.to_numpy()

Comments

Popular posts from this blog

MS Word Page numbering for Reports

Wordcount project in Spark with JAVA using Maven and Gradle