Introduction to Numpy -1 : An absolute beginners guide to Machine Learning and Data science.

 

https://hackernoon.com/introduction-to-numpy-1-an-absolute-beginners-guide-to-machine-learning-and-data-science-5d87f13f0d51

 

 

Lets get started quickly. Numpy is a math library for python. It enables us to do computation efficiently and effectively. It is better than regular python because of it’s amazing capabilities.

In this article I’m just going to introduce you to the basics of what is mostly required for machine learning and datascience. I’m not going to cover everything that’s possible with numpy library. This is the part one of numpy tutorial series.

The first thing I want to introduce you to is the way you import it.

import numpy as np

Okay, now we’re telling python that “np” is the official reference to numpy from further on.

Let’s create python array and np array.

# python array
a = [1,2,3,4,5,6,7,8,9]
# numpy array
A = np.array([1,2,3,4,5,6,7,8,9])

If I were to print them, I wouldn’t see much difference.

print(a)
print(A)
====================================================================[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1 2 3 4 5 6 7 8 9]

Okay, but why do I have to use an np array instead of a regular array?

The answer is that np arrays are better interms of faster computation and ease of manipulation.

More on those details here, if you’re interested:

Let’s proceed further with more cool stuff. Wait, there was nothing cool we saw yet! Okay, here’s something:

np.arange()

np.arange(0,10,2)
====================================================================array([0, 2, 4, 6, 8])

What arange([start],stop,[step]) does is that it arranges numbers from starting to stop, in steps of step. Here is what it means for np.arange(0,10,2):

return an np list starting from 0 all the way upto 10 but don’t include 10 and increment numbers by 2 each time.

So, that’s how we get :

array([0, 2, 4, 6, 8])

important thing remember here is that the stopping number is not going to be included in the list.

another example:

np.arange(2,29,5)
====================================================================
array([ 2, 7, 12, 17, 22, 27])

Before I proceed further, I’ll have to warn you that this “array” is interchangeably called “matrix” or also “vector”. So don’t get panicked when I say for example “Matrix shape is 2 X 3”. All it means is that array looks something like this:

array([ 2,  7, 12,], 
[17, 22, 27])

Now, Let’s talk about the shape of a default np array.

Shape is an attribute for np array. When a default array, say for example A is called with shape, here is how it looks.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9] 
A.shape
====================================================================
(9,)

This is a rank 1 matrix(array), where it just has 9 elements in a row. 
Ideally it should be a 1 X 9 matrix right?

I agree with you, so that’s where reshape() comes into play. It is a method that changes the dimensions of your original matrix into your desired dimension.

Let’s look at reshape in action. You can pass a tuple of whatever dimension you want as long as the reshaped matrix and original matrix have the same number of elements.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
A.reshape(1,9)
====================================================================
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])

Notice that reshape returns a multi-dim matrix. Two square brackets in the beginning indicate that. [[1, 2, 3, 4, 5, 6, 7, 8, 9]] is a potentially multi-dim matrix as opposed to [1, 2, 3, 4, 5, 6, 7, 8, 9].

Another example:

B = [1, 2, 3, 4, 5, 6, 7, 8, 9] 
B.reshape(3,3)
====================================================================
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

If I look at B’s shape, it’s going to be (3,3):

B.shape
====================================================================
(3,3)

Perfect. Let’s proceed to np.zeros().

This time it’s your job to tell me what happens looking at this code:

np.zeros((4,3))
====================================================================
???????????

Good, if you thought it’s going to print a 4 X 3 matrix filled with zeros. Here’s the output:

np.zeros((4,3))
====================================================================
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])

np.zeros((n,m)) returns an n x m matrix that contains zeros. It’s as simple as that.

Let’s guess again here: what does is np.eye() do?

Hint: eye() stands for Identity.

np.eye(5)
====================================================================
array([[ 1., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 1.]])

np.eye() returns an identity matrix with the specified dimensions.

What if we have to multiply 2 matrices?

No problem, we have np.dot().

np.dot() performs matrix multiplication, provided both the matrices are “multiply-able”. It just means that the number of columns of the first matrix must match the number of rows in second matrix.

ex: A = (2,3) & B=(3,2). Here number of cols in A= 3. Number of rows in B = 3. Since they match, multiplication is possible.

Let’s illustrate multiplication via np code:

# generate an identity matrix of (3 x 3)
I = np.eye(3)
I
====================================================================
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
# generate another (3 x 3) matrix to be multiplied.
D = np.arange(1,10).reshape(3,3)
D
====================================================================
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

We now prepared both the matrices to be multiplied. Let’s see them in action.

# perform actual dot product.
M = np.dot(D,I)
M
====================================================================
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])

Great! Now you know how easy and possible it is to multiply matrices! Also, notice that the entire array is now float type.

What about adding Elements of the matrix?

# add all the elements of matrix.
sum_val = np.sum(M)
sum_val
====================================================================
45.0

np.sum() adds all the elements of the matrix.

However are 2 variants.

1. Sum along the rows.

# sum along the rows
np.sum(M,axis=1)
====================================================================
array([ 6., 15., 24.])

6 is the sum of 1st row (1, 2, 3).

15 is the sum of 2nd row (4, 5, 6).

24 is the sum of 3rd row (7, 8, 9).

2. Sum along the columns.

# sum along the cols
np.sum(M,axis=0)
====================================================================
array([ 12., 15., 18.])

12 is the sum of 1st col (1, 4, 7).

15 is the sum of 2nd col (2, 5, 8).

18 is the sum of 3rd col (3, 6, 9).

Here is the follow up tutorial — part 2 . That’s it at this point.

Here’s a video tutorial explaining everything that I did if you’re interested to consume via video.

If you liked this article, a clap/recommendation would be really appreciated. It helps me to write more such articles.

 

Posted by uniqueone
,

[pythone IDE]PyCharm 설치

Python 2017. 3. 25. 20:52

http://blog.uthline.net/71


Python IDE

점프 투 파이썬을 따라서 간단한 예제를 연습할때는 몰랐지만, cherrymusic처럼 규모가 꺼진 프로젝트를 디버깅할때는 기존에 사용하던 에디터로는 한계를 느꼈다. 구글링을 통해 PyCharm이라는 쓸만한 IDE가 있다는 것을 확인. 바로 설치했다. 각 Os별로 버젼이 존재하는데, 그중 linux버젼을 설치하였다.

Download & Install

http://www.jetbrains.com/pycharm/download/ 사이트에서 파일을 다운 받는다. 30일 trial 버전도 있지만, 그냥 공짜 다운 받았다.

Download폴더에 pycharm-community-3.4.1.tar.gz파일이 있을 것이다. 일단 압축을 풀자.

tar -xvf pycharm-community-3.4.1.tar.gz

압축 해제후 bin폴더가면 pycharm.sh가 있다. 이 놈을 실행. 앞으로도 pycharm을 실행시키기 위해서는 이놈을 실행해주면 된다. 즉, 별도의 install과정이 필요없는 놈이다. 최초에 실행하면 설정해주는 창들이 뜨긴함.

./pycharm.sh

만약 다음과 같은 에러를 출력하고 설치를 못한다면 Java를 선택해줘야 한다.

Unrecognized VM option '+UseCodeCacheFlushing'
Could not create the Java virtual machine.
alternatives --config java
There are 3 programs which provide 'java'.
  Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
   2           /usr/lib/jvm/jre-1.5.0-gcj/bin/java
 + 3           /usr/lib/jvm/jdk1.5.0_17/jre/bin/java

java의 version을 올려줬더니 정상적으로 설치가 되었다. 이후부터는 next next next....as usual.

PyCharm실행

pycharm.sh를 실행하면, 다음과 같은 시작화면이 나온다. 자 이제 python을 작성하면 된다.



출처: http://blog.uthline.net/71 [uthline]


Posted by uniqueone
,
Python

A Byte of Python 한글 번역 by Jeongbin Park (PDF)
모두의 파이썬: 20일 만에 배우는 프로그래밍 기초
왕초보를 위한 Python 2.7
점프 투 파이썬 - Python 3
R

R을 이용한 데이터 처리 & 분석 실무 - 서민구 (HTML, PDF - 이전 버젼)
The R Manuals (translated in Korean)
Posted by uniqueone
,

Jupyter notebook 이해하기
https://www.slideshare.net/mobile/dahlmoon/jupyter-notebok-20160815?from_m_app=ios
Posted by uniqueone
,
무료 온라인 파이썬(Python) 한글 교재(빠릿베짱이)

파이썬을 공부하기 위한 강좌같은 교재, 무료라서 더 Good!!!

* 파이썬(Python) 교재 : http://byteofpython-korean.sourceforge.net/byte_of_python.html

* PDF(한글) : http://byteofpython-korean.sourceforge.net/byte_of_python.pdf

* PDF(영문) : http://files.swaroopch.com/python/byte_of_python.pdf

* 출처 : http://sijoo.tistory.com/m/282

※ Table of Contents

1. 책머리

2. 헌정

3. 서문

4. 소개

5. 설치

6. 첫 걸음

7. 기초

8. 연산자와 수식

9. 흐름 제어

10. 함수

11. 모듈

12. 자료 구조

13. 실생활 문제 해결

14. 객체 지향 프로그래밍

15. 입력과 출력

16. 예외 처리

17. 표준 라이브러리

18. 더 많은 것들

19. 부록: FLOSS

20. 부록: 끝맺음

21. 부록: History Lesson

22. 부록: 변경 기록

23. 부록: 리비전 기록

24. 번역

25. 번역 방법

*********************************************************
- 통계분석연구회
- 카페 : http://cafe.daum.net/statsas
- 통계분석연구회(Statistics Analysis Study) 그룹 :https://www.facebook.com/groups/statsas

#통계 #빅데이터 #통계분석연구회 #데이터과학자 #bigdata #dataviz #statistics #Analytics
Posted by uniqueone
,