Numpy

In [1]:
import numpy as np
In [2]:
np.arange(10)
Out[2]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [3]:
a = np.arange(10)
a ** 2
Out[3]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

Scipy

In [4]:
import numpy as np
from scipy import linalg
A = np.array([[1,2],[3,4]])
A
Out[4]:
array([[1, 2],
       [3, 4]])
In [5]:
linalg.det(A)
Out[5]:
-2.0

Pandas

In [6]:
import pandas as pd
import numpy as np
s = pd.Series([1,3,5,np.nan,6,8])
s
Out[6]:
0    1.0
1    3.0
2    5.0
3    NaN
4    6.0
5    8.0
dtype: float64
In [7]:
dates = pd.date_range('20130101',periods=6)
dates
Out[7]:
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06'],
              dtype='datetime64[ns]', freq='D')
In [8]:
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
df
Out[8]:
A B C D
2013-01-01 1.429345 -0.511213 1.799192 -1.214054
2013-01-02 0.953658 0.247452 -0.293159 0.142335
2013-01-03 -0.131943 0.169793 -0.701496 0.591950
2013-01-04 -0.464655 -1.821693 -3.814580 0.912962
2013-01-05 -0.195249 0.405789 -0.460643 0.578232
2013-01-06 -0.128790 -0.521260 -0.321348 -0.257807
In [9]:
# df.head()
# df.tail()
# df.describe()
# df.T
df.sort_values(by='B')
Out[9]:
A B C D
2013-01-04 -0.464655 -1.821693 -3.814580 0.912962
2013-01-06 -0.128790 -0.521260 -0.321348 -0.257807
2013-01-01 1.429345 -0.511213 1.799192 -1.214054
2013-01-03 -0.131943 0.169793 -0.701496 0.591950
2013-01-02 0.953658 0.247452 -0.293159 0.142335
2013-01-05 -0.195249 0.405789 -0.460643 0.578232

Matplotlib

In [10]:
%matplotlib inline
In [11]:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.ylabel('some numbers')
plt.show()
In [12]:
import seaborn as sns
sns.set(color_codes=True)
In [13]:
import numpy as np
x = np.random.normal(size=100)
sns.distplot(x);

NLTK & IGRAPH & SCIKIT-LEARN

In [14]:
import nltk
In [15]:
from igraph import *
In [16]:
g = Graph([(0,1), (0,2), (2,3), (3,4), (4,2), (2,5), (5,0), (6,3), (5,6)])
g
Out[16]:
<igraph.Graph at 0x118f9f148>
In [17]:
summary(g)
IGRAPH U--- 7 9 -- 
In [18]:
g.degree()
Out[18]:
[3, 1, 4, 3, 2, 3, 2]
In [19]:
import sklearn

Python 2.7.x VS 3.x

In [20]:
3/2
Out[20]:
1
In [21]:
from __future__ import division
In [22]:
3/2
Out[22]:
1.5

练习

(请先不要搜索,尝试自己解决,可用多种方法解决,请把代码和结果发在社区上 https://ask.julyedu.com/)

题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1。比如numbers={2,7,11,17}; target=9。那么返回一个元组(1,2)。这道题不需要去重,对于每一个target输入,只有一组解,索引要按照大小顺序排列。

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. You may assume that each input would have exactly one solution. Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2

test 1: array,target = [3,2,4],6

test 2: array,target = [0,1,4,0],0

test 3:

In [23]:
import numpy as np
array,target = list(np.arange(2,32046,2)),16021