Skip to main content

Posts

How to use TFRecord format?

When dealing with the TFRecord format throughout TensorFlow's API, several objects (classes) come out. However, there are surprisingly many, so it seems that it is difficult to understand. Now let's show the objects hierarchically. tf.Example tf.train.Features tf.train.Feature tf.train.BytesList tf.train.FloatList tf.train.Int64List Fundamentally, a tf.train.Example is a {"string": tf.train.Feature} mapping. The tf.train.Feature message type can accept one of the three types( tf.train.BytesList , tf.train.FloatList , and tf.train.Int64List ). Each function takes a scalar input value and returns a tf.train.Feature containing one of the three list types. See https://www.tensorflow.org/tutorials/load_data/tfrecord#creating_a_tftrainexample_message import tensorflow as tf int64_list = tf.train.Int64List(value=[1, 2, 3, 4]) # Or dict can be taken. # int64_list = dict(value=[1, 2, 3, 4]) int64_feature = tf.trai...

Public, Protected, and Private variables in Python

Object-oriented languages, such as C++ and Java, control the access to class resources by public, private, and protected keywords. Python can also introduce a concept of public, protected, and private. But all the variables in Python are public. Private variables of the class are denied access from the environment outside the class, while public variables are accessible from outside the class. Protected variables of a class are accessible from within the class and are also available to its sub-classes. No other environment is permitted access to it. This enables specific resources of the parent class to be inherited by the child class. Declaration methods of valiables Public variables Public variables (generally methods declared in a class) are accessible from outside the class. All the variables in a Python class are public by default. Any variables can be accessed from outside the class environment. Protected variables Python's convention to make an instance variabl...

Iterator object in Python

Python iterable object The 'list' object is one of iterable objects. This makes it possible to consume its elements using a for loop. fruits = ['apple', 'orange', 'grape', 'peach'] print(type(fruits)) for elm in fruits: print(elm) # class 'list' # apple # orange # grape # peach Explicitly create a Python iterator using iter, which is a Built-in Function in Python. iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iterable protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this ...

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...

Select all numeric types from Pandas DataFrame in Python

To extract numeric types from Pandas DataFrame, Use select_types method . DataFrame.select_dtypes(include=None, exclude=None) import numpy as np import pandas as pd df = pd.DataFrame({'a': [1, 2] * 3, 'b': [True, False] * 3, 'c': [1.0, 2.0] * 3, 'd': ['one','two'] * 3}) To select all numeric types, set the parameter 'include' to np.number or 'number'. df.select_dtypes(include='number') df.select_dtypes(include=np.number) a c 0 1 1.0 1 2 2.0 2 1 1.0 3 2 2.0 4 1 1.0 5 2 2.0 You can also use the Python built-in types such as int and float, or 'int' and 'float' as string. df.select_dtypes(include=int) df.select_dtypes(include='int') a 0 1 1 2 2 1 3 2 4 1 5 2 df.select_dtypes(include=float) df.select_dtypes(include='float') c 0 1.0 1 2.0 2 1.0 3 2.0 4 1.0 5 2.0 To select strings you...