In [1]:
import matplotlib.pyplot as plt
In [2]:
plt.plot(xrange(10))
Out[2]:
In [3]:
import numpy as np
In [4]:
import pandas
In [5]:
plt.ylabel('some numbers')
Out[5]:
In [7]:
plt.scatter(np.arange(1,10,.5), np.arange(10,20,.5))
In [8]:
np.arange(1,10,.5)
Out[8]:
In [9]:
len(np.arange(1,10,.5))
Out[9]:
In [11]:
len(np.arange(10,20,.5))
Out[11]:
In [13]:
plt.plot([x for x in np.arange(1,10,.5)], [x*x for x in np.arange(1,10,.5)])
plt.xlabel("Numbers:1-10")
plt.ylabel("Squares of numbers")
Out[13]:
In [19]:
plt.plot([x for x in xrange(10)] , [np.exp(x) for x in xrange(10)], 'rs--')
Out[19]:
In [24]:
t = np.arange(0., 5, .2)
In [25]:
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
Out[25]:
In [35]:
plt.plot([1,2,3,4], [1,4,9,16], 'g^--')
plt.axis([0, 8, 0, 25])
plt.show()
In [36]:
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
In [37]:
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
In [38]:
t1
Out[38]:
In [40]:
len(t2)
Out[40]:
In [59]:
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
In [46]:
plt.figure(2)
plt.subplot(1,2,1)
plt.plot(t2, np.exp(-t2), 'gd')
Out[46]:
In [55]:
plt.figure(1)
plt.subplot(311)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(3,1,2)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.subplot(3,1,3)
plt.plot(t2, np.exp(-t2), 'gd')
plt.show()
In [60]:
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1,2,3])
plt.subplot(212) # the second subplot in the first figure
plt.plot([4,5,6])
plt.figure(2) # a second figure
plt.plot([4,5,6]) # creates a subplot(111) by default
plt.figure(1) # figure 1 current; subplot(212) still current
plt.subplot(211) # make subplot(211) in figure1 current
plt.title('Easy as 1,2,3') # subplot 211 title
Out[60]:
In [61]:
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
In [62]:
x
Out[62]:
In [63]:
len(x)
x.shape
Out[63]:
In [64]:
len(x)
Out[64]:
In [65]:
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
In [66]:
n
Out[66]:
In [67]:
len(n)
Out[67]:
In [68]:
n.shape
Out[68]:
In [69]:
bins.shape
Out[69]:
In [79]:
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='c', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
In [81]:
ax = plt.subplot(111)
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t, s, lw=2)
plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='green', shrink=0.05),
)
plt.ylim(-2,2)
plt.show()
In [1]:
from sklearn.datasets import make_classification
In [2]:
from sklearn.cross_validation import cross_val_score
In [3]:
from sklearn import svm, metrics
In [4]:
X,y = make_classification(n_samples=5000, n_features=5, random_state=0)
In [5]:
X.shape, y.shape
Out[5]:
In [6]:
y
Out[6]:
In [7]:
clf = svm.SVC()
In [11]:
cross_val_score(clf, X, y, cv=10)
Out[11]:
In [12]:
clf = svm.SVC(kernel='rbf')
In [13]:
cross_val_score(clf, X, y, cv=10)
Out[13]:
In [8]:
X.shape
Out[8]:
In [9]:
from sklearn.decomposition import RandomizedPCA
In [10]:
X_pca = RandomizedPCA(n_components=2).fit_transform(X)
In [11]:
X_pca.shape
Out[11]:
In [12]:
import pylab as pl
In [26]:
pl.scatter(X_pca[y==0,0], X_pca[y==0,1], c='b')
Out[26]:
In [27]:
pl.scatter(X_pca[y==1,0], X_pca[y==1, 1], c='g')
Out[27]:
In [25]:
pl.scatter(X_pca[y==0,0], X_pca[y==0,1], c='b')
pl.scatter(X_pca[y==1,0], X_pca[y==1, 1], c='g')
Out[25]:
In [24]:
color = ['b','g']
from itertools import cycle
for i,c in zip(np.unique(y),cycle(color)):
pl.scatter(X_pca[y==i,0], X_pca[y==i, 1], c=c, alpha=0.8)
In [29]:
from sklearn.lda import LDA
In [34]:
clf = LDA().fit(X,y)
In [35]:
clf
Out[35]:
In [33]:
y.shape, X.shape
Out[33]:
In [36]:
X[0]
Out[36]:
In [37]:
clf.predict(X[0])
Out[37]:
In [38]:
y[1]
Out[38]:
In [39]:
y[0]
Out[39]:
In [43]:
X_lda=LDA(n_components=2).fit_transform(X,y)
In [44]:
X_lda.shape
Out[44]:
In [69]:
np.unique(y)
Out[69]:
In [42]:
lda.shap
Out[42]:
In [48]:
for i,c in zip(np.unique(y), cycle(color)):
pl.scatter(X_lda[y==i, 0], X_lda[y==i, 1], c=c, alpha=.8)
In [49]:
pl.scatter(X_lda[y==0, 0], X_lda[y==0, 1], c='b', alpha=.8)
Out[49]:
In [50]:
pl.scatter(X_lda[y==1, 0], X_lda[y==1, 1], c='g', alpha=.8)
Out[50]:
In [51]:
from sklearn import datasets
In [52]:
iris = datasets.load_iris()
In [55]:
iris.DESCR
Out[55]:
In [56]:
iris.data.shape
Out[56]:
In [57]:
iris.target.shape
Out[57]:
In [58]:
clf = svm.SVC(kernel='rbf').fit(iris.data, iris.target)
In [59]:
clf
Out[59]:
In [61]:
clf.predict(iris.data[:5])
Out[61]:
In [62]:
iris.target_names
Out[62]:
In [63]:
iris_pca = RandomizedPCA(n_components=2).fit_transform(iris.data)
In [80]:
iris_pca.shape
Out[80]:
In [81]:
color = ['b', 'g', 'r']
for i, c in zip(np.unique(iris.target), cycle(color)):
pl.scatter(iris_pca[iris.target==i, 0], iris_pca[iris.target==0, 1], c=c, alpha=.8)
print i
In [75]:
np.unique(iris.target)
Out[75]:
In [85]:
from sklearn.decomposition import PCA
iris_pca_basic = PCA(n_components=2).fit_transform(iris.data)
In [86]:
iris_pca_basic.shape
Out[86]:
In [88]:
color =['b', 'g', 'r']
for i, c in zip(np.unique(iris.target), cycle(color)):
pl.scatter(iris_pca_basic[iris.target==i,0], iris_pca_basic[iris.target==i,1],
c=c, alpha=.8)
In [89]:
from sklearn.decomposition import ProbabilisticPCA, KernelPCA
iris_pca_probalistic = KernelPCA(n_components=2).fit_transform(iris.data)
In [91]:
iris_pca_probalistic.shape
Out[91]:
In [93]:
X_p= iris_pca_probalistic
for i, c in zip(np.unique(iris.target), cycle(color)):
pl.scatter(X_p[iris.target==i,0], X_p[iris.target==i,1], c=c, alpha=.7)
In [ ]: