Numpy
import numpy as np
np.arange(10)
a = np.arange(10)
a ** 2
Scipy
import numpy as np
from scipy import linalg
A = np.array([[1,2],[3,4]])
A
linalg.det(A)
Pandas
import pandas as pd
import numpy as np
s = pd.Series([1,3,5,np.nan,6,8])
s
dates = pd.date_range('20130101',periods=6)
dates
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
df
# df.head()
# df.tail()
# df.describe()
# df.T
df.sort_values(by='B')
Matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.ylabel('some numbers')
plt.show()
import seaborn as sns
sns.set(color_codes=True)
import numpy as np
x = np.random.normal(size=100)
sns.distplot(x);
NLTK & IGRAPH & SCIKIT-LEARN
import nltk
from igraph import *
g = Graph([(0,1), (0,2), (2,3), (3,4), (4,2), (2,5), (5,0), (6,3), (5,6)])
g
summary(g)
g.degree()
import sklearn
Python 2.7.x VS 3.x
3/2
from __future__ import division
3/2
Python 2.7.x 和 Python 3.x 的主要区别: https://segmentfault.com/a/1190000000618286#future_module or https://wizardforcel.gitbooks.io/w3school-python/content/29.html
练习
(请先不要搜索,尝试自己解决,可用多种方法解决,请把代码和结果发在社区上 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:
import numpy as np
array,target = list(np.arange(2,32046,2)),16021