from scipy.interpolate import spline
T = np.array([6, 7, 8, 9, 10, 11, 12])
power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])
#300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(),T.max(),300)
power_smooth = spline(T,power,xnew)
plt.plot(xnew,power_smooth)
plt.show()
Not smooth | Smooth |
---|---|
参考这篇文章,对自己的notebook进行配置,方便快捷。比如可以在打开notebook时直接先加载一些模块等。
# creat新建配置文件
gongjing@MBP ~/.ipython/profile_default % ipython profile create
[ProfileCreate] Generating default config file: u'/Users/gongjing/.ipython/profile_default/ipython_config.py'
[ProfileCreate] Generating default config file: u'/Users/gongjing/.ipython/profile_default/ipython_kernel_config.py'
gongjing@MBP ~/.ipython/profile_default % ll
total 16304
drwxr-xr-x 2 gongjing staff 68B Jun 18 2016 db
-rw-r--r-- 1 gongjing staff 7.9M Feb 10 09:42 history.sqlite
-rw-r--r-- 1 gongjing staff 22K Feb 10 09:43 ipython_config.py
-rw-r--r-- 1 gongjing staff 18K Feb 10 09:43 ipython_kernel_config.py
drwxr-xr-x 2 gongjing staff 68B Jun 18 2016 log
drwx------ 2 gongjing staff 68B Jun 18 2016 pid
drwx------ 2 gongjing staff 68B Jun 18 2016 security
drwxr-xr-x 3 gongjing staff 102B Jun 18 2016 startup
ipython_config.py 这个文件是配置ipython的;
ipython_kernel_config.py 这个文件是配置notebook的;
加载常用模块,matplot设置成inline
# Configuration file for ipython-kernel.
c = get_config()
c.InteractiveShellApp.exec_lines = [
"import pandas as pd",
"import numpy as np",
"import scipy.stats as spstats",
"import scipy as sp",
"import matplotlib.pyplot as plt",
"import seaborn as sns",
"sns.set(style='white')",
"sns.set_context('poster')",
]
c.IPKernelApp.matplotlib = 'inline'
# login the server
$ ssh -X -p 12000 zhangqf7@166.111.152.116
$ export PYTHONPATH=~/anaconda2/lib/python2.7/site-packages
# specify a port
$ jupyter-notebook --port 9988
# local
$ ssh -N -f -L localhost:9987:localhost:9988 zhangqf7@166.111.152.116 -p 12000
# type url: localhost:9987
# may need token
主要是参考这里 为Jupyter Notebook添加目录:
# 安装后重启
~/anaconda3/bin/conda install -c conda-forge jupyter_contrib_nbextensions
install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
devtools::install_github('IRkernel/IRkernel')
# 只在当前用户下安装(在集群上安装)
IRkernel::installspec()
# 或者是在系统下安装
IRkernel::installspec(user = FALSE)
比如是在集群上安装的,之后打开jupyter-lab后,可以看到启动界面多了一个R的选择:
有时在显示df(比如head,describe),数字会以科学计数法的方式展现,而不知道具体的比如百分比数字大小,可以设置禁用科学计数法表示:
pd.set_option('display.float_format',lambda x : '%.2f' % x)
通过下面的display命令可以将df显示为带有表格的table,而不是直接打印print(df)的效果:
from IPython.display import display
display(df)
shift+tab
,参考这里ctl+shift+[
左切,ctl+shift+]
右切C+arrowUp
将cell上移动,C+arrowDown
将cell上移动,O
将cell的输出collapse,OO
将cell的输出拓展开。首先要使用esc
退出编辑模式,进入命令模式。
{
// Move cell up
"shortcuts": [
{
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-up",
"keys": [
"C",
"ArrowUp"
]
},
// Move cell down
{
"selector": ".jp-Notebook:focus",
"command": "notebook:move-cell-down",
"keys": [
"C",
"ArrowDown"
]
},
{
"command": "notebook:hide-cell-outputs",
"keys": [
"O"
],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:show-cell-outputs",
"keys": [
"O",
"O"
],
"selector": ".jp-Notebook:focus"
},
]
}
参考这里:
# for all columns
df.head().style.format("{:,.0f}")
# per column
df.head().style.format({"col1": "{:,.0f}", "col2": "{:,.0f}"})
bubble plot可以结合展示三个变量之间的关系,本文主要是演练了这里这里的代码:
# create data
x = np.random.rand(5)
y = np.random.rand(5)
z = np.random.rand(5)
控制bubble的颜色、透明度:
# Change color with c and alpha
plt.scatter(x, y, s=z*4000, c="red", alpha=0.4)
控制bubble的形状:
plt.scatter(x, y, s=z*4000, marker="D")
控制bubble的大小:
plt.scatter(x, y, s=z*200)
控制bubble的边缘(线条粗细等):
plt.scatter(x, y, s=z*4000, c="green", alpha=0.4, linewidth=6)
先导入seaborn,则采用seaborn的主题:
import seaborn as sns
plt.scatter(x, y, s=z*4000, c="green", alpha=0.4, linewidth=6)
同时给气泡上颜色和大小,相当于展示了4个变量:
# create data
x = np.random.rand(15)
y = x+np.random.rand(15)
z = x+np.random.rand(15)
z=z*z
# Change color with c and alpha. I map the color to the X axis value.
plt.scatter(x, y, s=z*2000, c=x, cmap="Blues", alpha=0.4, edgecolors="grey", linewidth=2)
### plot color bar
### https://stackoverflow.com/questions/6063876/matplotlib-colorbar-for-scatter
sc = plt.scatter(x, y, s=z*2000, c=x, cmap="Blues", alpha=0.4, edgecolors="grey", linewidth=2)
plt.colorbar(sc)
递归实现:
# A Naive recursive Python implementation of LCS problem
# X: string1, Y: string2
# m: len(X), n: len(Y)
def lcs(X, Y, m, n):
if m == 0 or n == 0:
return 0;
elif X[m-1] == Y[n-1]:
return 1 + lcs(X, Y, m-1, n-1);
else:
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
# Driver program to test the above function
X = "AGGTAB"
Y = "GXTXAYB"
print "Length of LCS is ", lcs(X, Y, len(X), len(Y))
# Length of LCS is 4
# Dynamic Programming implementation of LCS problem
def lcs(X, Y):
# find the length of the strings
m = len(X)
n = len(Y)
# declaring the array for storing the dp values
L = [[None]*(n + 1) for i in xrange(m + 1)]
"""Following steps build L[m + 1][n + 1] in bottom up fashion
Note: L[i][j] contains length of LCS of X[0..i-1]
and Y[0..j-1]"""
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0 :
L[i][j] = 0
elif X[i-1] == Y[j-1]:
L[i][j] = L[i-1][j-1]+1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
return L[m][n]
# end of function lcs
# Driver program to test the above function
X = "AGGTAB"
Y = "GXTXAYB"
print "Length of LCS is ", lcs(X, Y)
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
# Length of LCS is 4
clustalo -i input.fa -o input.align.fa
这里有两张图解释了进化树的各含义,可以参考一下。
主要的几点如下:
贪婪算法:
3)依次类推,直到装不下
# You pass an array in, and it gets converted to a set.
states_needed = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"])
stations = {}
stations["kone"] = set(["id", "nv", "ut"])
stations["ktwo"] = set(["wa", "id", "mt"])
stations["kthree"] = set(["or", "nv", "ca"])
stations["kfour"] = set(["nv", "ut"])
stations["kfive"] = set(["ca", "az"])
final_stations = set()
while states_needed:
best_station = None
states_covered = set()
for station, states_for_station in stations.items():
covered = states_needed & states_for_station
if len(covered) > len(states_covered):
best_station = station
states_covered = covered
states_needed -= states_covered
final_stations.add(best_station)
print(final_stations)
定义:以难解著称的问题。需要计算所有的解,从中选出最小、最短的那个,比如旅行商问题或者集合覆盖问题
如何识别?
例子: