2019-04-03
TIL python list assignment does not make new list!

Well, TIL python list just store the pointer to the actually list in memory. So simple list assignment does not make new list!

1
2
3
4
5
list_a = [1,2,3,4]
list_b = list_a
list_b[2] = 10
print(list_a)
print(list_b)
Read More

2019-02-22
TIL Using Pickle to save sklearn model

The models in sklearn are objects, using Pickle to save and restore the trained classify or cluster models. Do not us numpy.save and numpy.load to do this, it’s not working.

An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
import Pickle
from sklearn.cluster import Kmeans
# suppose we want to save this kmeans model
kmeans_results = KMeans(n_clusters=2).fit(X)
kmeans_results_file = "kmeans.pkl"

# saving results
with open(kmeans_results_file, 'wb') as f:
pickle.dump(kmeans_results, f)

# loading results
with open(kmeans_results_file, 'rb') as f:
kmeans_results = pickle.load(f)

Read More