Skip to content

Instantly share code, notes, and snippets.

@watermouth
Last active February 10, 2018 05:52
Show Gist options
  • Select an option

  • Save watermouth/a683551ee0a9841024436aebffa121fe to your computer and use it in GitHub Desktop.

Select an option

Save watermouth/a683551ee0a9841024436aebffa121fe to your computer and use it in GitHub Desktop.
  • to plot grayscale image data, it's nice to code
# image_data.shape must be (a, b)
plt.imshow(image_data, cmap=matplotlib.cm.binary, interpolation="nearest")
plt.show()

binary color map: matplotlib.cm.binary

  • to concatenate tuples
tuple_a = (10,20,30)
tuple_not_intended = (1, tuple_a)
tuple_intended = (1,) + typle_a
  • to concatenate numpy arrays in a list
array_list = [np.arange(3), np.arange(4)]
np.concatenate(array_list, axis=0)
  • difference between list(nd.array) and list().append
    converting nd.array to list results in list of nd.array whose dimension is reduced.
z = np.arange(10).reshape((-1,1))
list(z) == [i for i in z] # comparision between two lists, results in a True.

appending nd.array to list results in list of the nd.array with no changes for it.

z = np.arange(10).reshape((-1,1))
x = list()
x.append(z)
x[0] == z # comparison between two nd.arrays results in an array, whose shape is (10,1), of single True.
  • list's append returns None. it's inplace operation.
x = list().append(1)
x is None # True
y = list()
y.append(1)
[1] == y # True
  • save and load python data using pkl file
my_data = [1,2,3]
from sklearn.externals import joblib
joblib.dump(my_data, "my_data.pkl")
my_data = joblib.load("my_data.pkl")
  • to make an list of copy of an element
# simple list
[1] * 3 == [1,1,1] # True
# general approach
v = np.arange(3)
repeated_values_list = [v for i in range(2)] # [array([0, 1, 2]), array([0, 1, 2])]
# if you want to concatenate
x = np.concatenate(repeated_values_list) # array([0, 1, 2, 0, 1, 2])
>>> def f(x,y):
...     return 10*x+y
...
>>> b = np.fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])

When fewer indices are provided than the number of axes, the missing indices are considered complete slices:

>>>
>>> b[-1]                                  # the last row. Equivalent to b[-1,:]
array([40, 41, 42, 43])

https://docs.scipy.org/doc/numpy-dev/user/basics.indexing.html#basics-indexing

  • to define a function which receives a variable number of arguments,
def foo(a, *hoge):
    print(a)
    print(list(hoge))
    print(hoge)
foo(1, 2, "three")

>>>1
>>>[2, 'three']
>>>(2, 'three')
  • to define a function which receives arguments by keyword,
def bar(a, **hoge):
    print(a)
    print(hoge)
    print(type(hoge))
    if hoge.get("action") == "sum":
        print("action is sum")
bar(1, action = "sum", exception="No")
2
{'x': 3, 'y': 2}
<class 'dict'>

the get method of a dict object returns None when it does not have the key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment