In [2]:
import matplotlib.pyplot as plt
import numpy as np
arr = np.array([1,2,3,4])
plt.plot(arr)
plt.show()
In [3]:
plt.plot([1,2,3,4], [1,4,9,6], 'ro')
plt.show()
In [4]:
plt.axis([0,5,0,20])
plt.title('My First Plot')
plt.plot([1,2,3,4], [1,4,9,16], 'or')
plt.show()
In [1]:
import math
import numpy as np
t = np.arange(0,2*np.pi,0.01)
y1 = map(math.sin, math.pi*t)
y1 = np.sin(t)
y2 = map(math.sin, math.pi*t+math.pi/2)
y2 = np.sin(math.pi*t+math.pi/2)
y3 = map(math.sin, math.pi*t-math.pi/2)
y3 = np.sin(math.pi*t-math.pi/2)
plt.plot(t, y1, 'b--')
plt.plot(t, y2, 'g')
plt.plot(t, y3, 'r-.')
# plt.show()
In [ ]:
plt.axis([0,5,0,20])
plt.xlabel('Counting', color='gray')
plt.ylabel('Square values', color='gray')
plt.text(1,1.5, 'First')
plt.text(2,4.5, 'Second')
plt.text(3,9.5, 'Third')
plt.text(4,16.5, 'Fourth')
# plt.plot([1,2,3,4],[1,4,9,16], 'r.-')
# plt.plot([1,2,3,4],[1,4,9,16], 'g')
# plt.plot([1,2,3,4],[1,4,9,16], 'g.-')
# plt.plot([1,2,3,4],[1,4,9,16], 'b')
# plt.plot([1,2,3,4],[1,4,9,16], 'b*')
# plt.plot([1,2,3,4],[1,4,9,16], 'b-')
plt.text(1.1, 12, r'$y=x^2$', fontsize=20, bbox={'facecolor':'yellow', 'alpha':0.2})
plt.plot([1,2,3,4],[1,4,9,16], 'b--')
In [ ]:
plt.axis([0,5,0,20])
plt.xlabel('Counting', color='gray')
plt.ylabel('Square values', color='gray')
plt.text(1,1.5, 'First')
plt.text(2,4.5, 'Second')
plt.text(3,9.5, 'Third')
plt.text(4,16.5, 'Fourth')
plt.text(1.1, 12, r'$y=x^2$', fontsize=20, bbox={'facecolor':'yellow', 'alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16], 'or')
In [ ]:
plt.axis([0,5,0,20])
plt.xlabel('Counting', color='gray')
plt.ylabel('Square values', color='gray')
plt.text(1,1.5, 'First')
plt.text(2,4.5, 'Second')
plt.text(3,9.5, 'Third')
plt.text(4,16.5, 'Fourth')
plt.text(1.1, 12, r'$y=x^2$', fontsize=20, bbox={'facecolor':'yellow', 'alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16], 'or')
plt.plot([1,2,3,4],[0.8,3.5,8,15], 'g^')
plt.plot([1,2,3,4],[0.5,2.5,4,12], 'b*')
plt.legend(['First Series','Second Series','Third Series'], loc=2) # loc关键字控制位置0~10,0最佳位置,1右上角,2左上角,3右下角,4左下角,5右侧...
plt.savefig('my_chart.png') # 图表直接保存图片
In [5]:
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
months = mdates.MonthLocator()
days = mdates.DayLocator()
timeFmt = mdates.DateFormatter('%Y-%m')
events = [datetime.date(2015,1,23), datetime.date(2015,1,28),
datetime.date(2015,2,3), datetime.date(2015,2,3),
datetime.date(2015,3,15), datetime.date(2015,3,24),
datetime.date(2015,4,8),datetime.date(2015,4,24)]
readings = [12,22,25,20,18,15,17,14]
fig, ax = plt.subplots()
plt.plot(events, readings)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(timeFmt)
ax.xaxis.set_minor_locator(days)
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y = np.sin(3*x)/x
y1 = np.sin(2*x)/x
y2 = np.sin(x)/x
plt.plot(x,y,'k--', linewidth=3)
plt.plot(x,y1,'m-.')
plt.plot(x,y2,color='#87a3cc', linestyle='--')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],
[r'$-2\pi$',r'$-\pi$',r'$0$',r'$+\pi$',r'$+2\pi$'])
plt.yticks([-1,0,+1,+2,+3],
[r'$-1$',r'$0$',r'$+1$',r'$+2$',r'$+3$'])
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y = np.sin(3*x)/x
y1 = np.sin(2*x)/x
y2 = np.sin(x)/x
plt.plot(x,y,'k--', linewidth=3)
plt.plot(x,y1,'m-.')
plt.plot(x,y2,color='#87a3cc', linestyle='--')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],
[r'$-2\pi$',r'$-\pi$',r'$0$',r'$+\pi$',r'$+2\pi$'])
plt.yticks([-1,0,+1,+2,+3],
[r'$-1$',r'$0$',r'$+1$',r'$+2$',r'$+3$'])
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = {'series1':np.random.randint(5, size=5),
'series2':np.random.randint(5, size=5),
'series3':np.random.randint(5, size=5)}
df = pd.DataFrame(data)
x = np.arange(5)
plt.axis([0,5,0,7])
plt.plot(x, df)
plt.legend(data, loc=2)
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
pop = np.random.randint(0,100,100)
n,bis,patches = plt.hist(pop)
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
index = np.arange(5)
value = [5,7,3,4,6]
std1 = [0.8,1,0.4,0.9,1.3]
plt.title('A Bar Chart')
plt.bar(index, value, yerr=std1, error_kw={'ecolor':'0.1','capsize':6}, alpha=0.7, label='First')
plt.xticks(index+0.4, ['A','B','C','D','E'])
plt.legend(loc=2)
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
index = np.arange(5)
value = [5,7,3,4,6]
std1 = [0.8,1,0.4,0.9,1.3]
plt.title('A Bar Chart')
plt.barh(index, value, xerr=std1, error_kw={'ecolor':'0.1','capsize':6}, alpha=0.7, label='First')
plt.yticks(index+0.4, ['A','B','C','D','E'])
plt.legend(loc=5)
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
index = np.arange(5)
value1 = [5,7,3,4,6]
value2 = [6,6,4,5,7]
value3 = [5,6,5,4,6]
bw = 0.3
plt.axis([0,5,0,8])
plt.title('A Multiseries Bar Chart', fontsize=20)
plt.bar(index, value1, bw, color='b')
plt.bar(index+bw, value2, bw, color='g')
plt.bar(index+2*bw, value3, bw, color='r')
plt.xticks(index+1.5*bw, ['A','B','C','D','E'])
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
index = np.arange(5)
value1 = [5,7,3,4,6]
value2 = [6,6,4,5,7]
value3 = [5,6,5,4,6]
bw = 0.3
plt.axis([0,8,0,5])
plt.title('A Multiseries Bar Chart', fontsize=20)
plt.barh(index, value1, bw, color='b')
plt.barh(index+bw, value2, bw, color='g')
plt.barh(index+2*bw, value3, bw, color='r')
plt.yticks(index+0.4, ['A','B','C','D','E'])
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
s1 = np.array([3,4,5,3])
s2 = np.array([1,2,2,5])
s3 = np.array([2,3,3,4])
index = np.arange(4)
plt.axis([0,4,0,15])
plt.bar(index, s1, color='r')
plt.bar(index, s2, color='b', bottom=s1)
plt.bar(index, s3, color='g', bottom=(s1+s2))
plt.xticks(index+0.4,['Jan15','Feb15','Mar15','Apr15'])
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
s1 = np.array([3,4,5,3])
s2 = np.array([1,2,2,5])
s3 = np.array([2,3,3,4])
index = np.arange(4)
plt.axis([0,15,0,4])
plt.barh(index, s1, color='r')
plt.barh(index, s2, color='b', left=s1)
plt.barh(index, s3, color='g', left=(s1+s2))
plt.yticks(index+0.4,['Jan15','Feb15','Mar15','Apr15'])
In [ ]:
import matplotlib.pyplot as plt
import pandas as pd
data = {
's1':[1,3,4,3,5],
's2':[2,3,5,2,4],
's3':[3,2,3,1,3]
}
df = pd.DataFrame(data)
df.plot(kind='bar', stacked=True)
In [ ]:
import matplotlib.pyplot as plt
import pandas as pd
data = {
's1':[1,3,4,3,5],
's2':[2,3,5,2,4],
's3':[3,2,3,1,3]
}
df = pd.DataFrame(data)
df['s1'].plot(kind='pie', figsize=(6,6))
In [ ]:
from sklearn import datasets
iris = datasets.load_iris()
iris.data
print(iris.data)
x = iris.data[:,0]
print(x)
y = iris.data[:,1]
print(y)
In [ ]:
iris.target
In [ ]:
iris.target_names
In [ ]:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn import datasets
iris = datasets.load_iris()
x = iris.data[:,0]
y = iris.data[:,1]
species = iris.target
x_min, x_max = x.min() - .5, x.max() + .5
y_min, y_max = y.min() - .5, y.max() + .5
plt.figure()
plt.title('Iris Dataset - Classification By Sepal Sizes')
plt.scatter(x,y,c=species)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
In [ ]:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn import datasets
iris = datasets.load_iris()
x = iris.data[:,2] # 改动
y = iris.data[:,3] # 改动
species = iris.target
x_min, x_max = x.min() - .5, x.max() + .5
y_min, y_max = y.min() - .5, y.max() + .5
plt.figure()
plt.title('Iris Dataset - Classification By Petal Sizes', size=14) # 改动
plt.scatter(x,y,c=species)
plt.xlabel('Petal length') # 改动
plt.ylabel('petal width') # 改动
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
In [ ]:
# 使用fit_transform()函数进行降维
from sklearn.decomposition import PCA
x_reduce = PCA(n_components=3).fit_transform(iris.data)
print(x_reduce)
In [ ]:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA
iris = datasets.load_iris()
x = iris.data[:,1]
y = iris.data[:,2]
species = iris.target
x_reducted = PCA(n_components=3).fit_transform(iris.data)
fig = plt.figure()
ax = Axes3D(fig)
ax.set_title('Iris Dataset - By PCA', size=14)
ax.scatter(x_reduce[:,0],x_reduce[:,1],x_reduce[:,2],c=species)
ax.set_xlabel('First')
ax.set_ylabel('Second')
ax.set_zlabel('Third')
ax.w_xaxis.set_ticklabels(())
ax.w_yaxis.set_ticklabels(())
ax.w_zaxis.set_ticklabels(())
In [ ]:
import numpy as np
from sklearn import datasets
np.random.seed(0)
iris = datasets.load_iris()
x = iris.data
y = iris.target
i = np.random.permutation(len(iris.data))
x_train = x[i[:-10]]
y_train = y[i[:-10]]
x_test = x[i[-10:]]
y_test = y[i[-10:]]
In [ ]:
# 导入KneighborsClassifier, 调用分类器的构造函数,然后用fit()函数进行训练
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
In [ ]:
knn.predict(x_test)
In [ ]:
y_test
# 错误率为10%
In [ ]:
# 用萼片测量数据绘制的2d散点图中,画出决策边界(decision boundary)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
iris = datasets.load_iris()
x = iris.data[:,:2]
y = iris.target
x_min, x_max = x[:,0].min() - .5, x[:,0].max() + .5
y_min, y_max = x[:,1].min() - .5, x[:,1].max() + .5
cmap_light = ListedColormap(['#AAAAFF','#AAFFAA','#FFAAAA'])
h = .02
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
knn = KNeighborsClassifier()
knn.fit(x,y)
Z = knn.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
plt.scatter(x[:,0],x[:,1],c=y)
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
In [ ]:
# 用花瓣绘制的散点图中,也可以画出决策边界
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
iris = datasets.load_iris()
y = iris.data[:,2:4]
y = iris.target
x_min, x_max = x[:,0].min() - .5, x[:,0].max() + .5
y_min, y_max = x[:,1].min() - .5, x[:,1].max() + .5
cmap_light = ListedColormap(['#AAAAFF','#AAFFAA','#FFAAAA'])
h = .02
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
knn = KNeighborsClassifier()
knn.fit(x,y)
Z = knn.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
plt.scatter(x[:,0], x[:,1], c=y)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
In [ ]:
# 糖尿病数据集合
from sklearn import datasets
diabetes = datasets.load_diabetes()
diabetes.data[0]
np.sum(diabetes.data[:,0]**2) # 验证任何一列的所有数值只和为1, 比如对年龄列求和
In [ ]:
diabetes.target # 表明疾病进展的数据
diabetes.feature_names
In [ ]:
# 线性回归:最小平方回归
from sklearn import linear_model
linreg = linear_model.LinearRegression()
In [ ]:
from sklearn import datasets
diabetes = datasets.load_diabetes()
x_train = diabetes.data[:-20]
y_train = diabetes.target[:-20]
x_test = diabetes.data[-20:]
y_test = diabetes.target[-20:]
# 预测模型上调用fit()函数,使用训练集做训练
linreg.fit(x_train, y_train)
linreg.coef_
In [ ]:
linreg.predict(x_test)
In [ ]:
y_test
In [ ]:
# 方差是评价预测结果好坏的一个不错的指标,方差越是接近与1,说明预测结果越准确
linreg.score(x_test, y_test)
In [ ]:
# 使用线性回归方法分析单个生理因素与目标值之间的关系
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn import datasets
diabetes = datasets.load_diabetes()
x_train = diabetes.data[:-20]
y_train = diabetes.target[:-20]
x_test = diabetes.data[-20:]
y_test = diabetes.target[-20:]
# 第一个元素和糖尿病病情进展之间的相关性, age年龄
x0_test = x_test[:,0]
x0_train = x_train[:,0]
x0_test = x0_test[:, np.newaxis]
x0_train = x0_train[:,np.newaxis]
linreg = linear_model.LinearRegression()
linreg.fit(x0_train, y_train)
y = linreg.predict(x0_test)
plt.scatter(x0_test, y_test, color='k')
plt.plot(x0_test, y, color='b', linewidth=3)
In [ ]:
# 使用线性回归方法分析单个生理因素与目标值之间的关系
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn import datasets
diabetes = datasets.load_diabetes()
x_train = diabetes.data[:-20]
y_train = diabetes.target[:-20]
x_test = diabetes.data[-20:]
y_test = diabetes.target[-20:]
plt.figure(figsize=(8,12))
for f in range(0,10):
x0_test = x_test[:,f]
x0_train = x_train[:,f]
x0_test = x0_test[:, np.newaxis]
x0_train = x0_train[:,np.newaxis]
linreg = linear_model.LinearRegression()
linreg.fit(x0_train, y_train)
y = linreg.predict(x0_test)
plt.subplot(5,2,f+1)
plt.scatter(x0_test, y_test, color='k')
plt.plot(x0_test, y, color='b', linewidth=3)
In [ ]: