Monday, September 4, 2017

Python: extracting column from np array and making it flat

>>> import numpy as np
>>> A = np.array([[1,2,3,4],[5,6,7,8]])

>>> A
array([[1, 2, 3, 4],
    [5, 6, 7, 8]])

>>> A[:,2] # returns the third columm
>>> import pandas as pd
>>>aa = pd.DataFrame(A[:,2].ravel())
>>>aa

matlab mat file in python

import scipy.io as sio
test = sio.loadmat('test.mat')
test
{'a': array([[[  1.,   4.,   7.,  10.],
        [  2.,   5.,   8.,  11.],
        [  3.,   6.,   9.,  12.]]]),
 '__version__': '1.0',
 '__header__': 'MATLAB 5.0 MAT-file, written by
 Octave 3.6.3, 2013-02-17 21:02:11 UTC',
 '__globals__': []}
>>> oct_a = test['a']
>>> oct_a

Friday, August 18, 2017

Multi-class vs Multi-label classification

Got nice definitions from the link
https://stats.stackexchange.com/questions/11859/what-is-the-difference-between-multiclass-and-multilabel-problem/168945#168945

Multiclass classification means a classification task with more than two classes; e.g., classify a set of images of fruits which may be oranges, apples, or pears. Multiclass classification makes the assumption that each sample is assigned to one and only one label: a fruit can be either an apple or a pear but not both at the same time.
Multilabel classification assigns to each sample a set of target labels. This can be thought as predicting properties of a data-point that are not mutually exclusive, such as topics that are relevant for a document. A text might be about any of religion, politics, finance or education at the same time or none of these.

Sunday, August 6, 2017

python: removing non ascii character from text

yourstring = yourstring.encode('ascii', 'ignore').decode('ascii')

python: newspaper articles collection

import newspaper
et_paper = newspaper.build('http://cnn.com/',memoize_articles=False)
#for article in et_paper.articles:
# print(article.url)
print(et_paper.size())
for category in et_paper.category_urls():
print(category)