Skip to main content

Posts

Showing posts from September, 2022

Get weight and bias with get_weights method in TensorFlow, Keras, in Python

The 'tf.keras.layers.Layer' class has the method ' get_weights() ' to get parameters (weight and bias). For example, make a neural network for predicting handwritten digits as follows. import tensorflow as tf model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28,28,1)), tf.keras.layers.Dense(16, activation='softmax'), tf.keras.layers.Dense(16, activation='softmax'), tf.keras.layers.Dense(10, activation='softmax') ]) print(type(model)) print(type(model.layers[0])) print(type(model.layers[1])) print(type(model.layers[2])) print(type(model.layers[3])) # <class 'keras.engine.sequential.Sequential'> # <class 'keras.layers.core.flatten.Flatten'> # <class 'keras.layers.core.dense.Dense'> # <class 'keras.layers.core.dense.Dense'> # <class 'keras.layers.core.dense.Dense'> You can check the summary of the model with the summary() method. model...

Value Attribute of pandas.DataFrame and pandas.Series in Python

Make a copy of a dataframe If you want to make a copy, you may use the Value Attribute of pandas.DataFrame and pandas.Series like df = pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]]) a = df.values Noto that it does not give a copy, gives just a view. print(type(a)) print(np.shares_memory(df, a)) # <class 'numpy.ndarray'> # True Use .to_numpy() method to make a copy of a DataFrame or Series. But there is one thing to remain. Set the parameter of copy to be true. The default is false. view = df.to_numpy() print(type(view)) print(np.shares_memory(df, view)) # <class 'numpy.ndarray'> # True copy = df.to_numpy(copy=True) print(type(copy)) print(np.shares_memory(df, copy)) # <class 'numpy.ndarray'> # False

Useful Pandas Functions for Titanic Kaggle Competition

Pandas is widely used for data science, data analysis and machine learning. Here I will show tips in using Pandas to process the data set for Titanic Kaggle Competition. The pandas module has many useful methods or functions. Fist you should use pandas.read_csv to read a comma-separated values (csv) file into DataFrame. import pandas as pd train_data = pd.read_csv('/kaggle/input/titanic/train.csv') test_data = pd.read_csv('/kaggle/input/titanic/test.csv') When you want to make sure the elements in the data, use the function of pandas.DataFrame.head gives information of the first 5 rows in the data frame. train_data.head() PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked 0 892 3 Kelly, Mr. James male 34.5 0 0 330911 7.8292 NaN Q 1 893 3 Wilkes, Mrs. James (Ellen Needs) female 47.0 1 0 363272 7.0000 NaN S 2 894 2 Myles, Mr. Thomas Francis male 62.0 0 0 240276 9.6875 NaN Q 3 895 3 Wirz, Mr. Albert male 27.0 0 0 315154 8.6625 NaN S 4 896 3 Hirv...