numpy 详细学习

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>  from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
 
SyntaxError: unexpected indent
>>> from sklearn.discriminant_analysis import LinearDiscriminantAnalysis


>>> 
>>> lda = LinearDiscriminantAnalysis(solver="svd", store_covariance=True)
>>> from sklearn import svm
>>> clf = svm.SVC()
>>> clf
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
>>> from sklearn.naive_bayes import GaussianNB
>>> gnb = GaussianNB()
>>> gnb
GaussianNB(priors=None)
>>> from sklearn import linear_model
>>> reg = linear_model.LinearRegression()
>>> import pandas
Traceback (most recent call last):
  File "", line 1, in
    import pandas
ImportError: No module named 'pandas'
>>> from sklearn.neighbors import NearestNeighbors
>>> nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
Traceback (most recent call last):
  File "", line 1, in
    nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
NameError: name 'X' is not defined
>>> from sklearn.neural_network import MLPClassifier
>>> model = MLPClassifier(activation='relu', solver='adam', alpha=0.0001)
>>> import pandas as pd
Traceback (most recent call last):
  File "", line 1, in
    import pandas as pd
ImportError: No module named 'pandas'
>>> import numpy as np
>>> from sklearn.cluster import KMeans
>>> data = np.random.rand(100, 3)
>>> estimator = KMeans(n_clusters=3)
>>> estimator.fit(data)#
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=3, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
>>> estimator.fit(data)
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=3, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
>>> label_pred = estimator.label_
Traceback (most recent call last):
  File "", line 1, in
    label_pred = estimator.label_
AttributeError: 'KMeans' object has no attribute 'label_'
>>> centroids = estimator.cluster_centers
Traceback (most recent call last):
  File "", line 1, in
    centroids = estimator.cluster_centers
AttributeError: 'KMeans' object has no attribute 'cluster_centers'
>>> k = 3
>>> [centroid, label, inertia] = cluster.k_means(data, k)
Traceback (most recent call last):
  File "", line 1, in
    [centroid, label, inertia] = cluster.k_means(data, k)
NameError: name 'cluster' is not defined
>>> from sklearn import cluster
>>> [centroid, label, inertia] = cluster.k_means(data, k)
>>> print(centroid, label, inertia)
[[0.77020287 0.47682911 0.40710137]
 [0.37947896 0.74490381 0.71666167]
 [0.2227436  0.24951828 0.45315423]] [2 2 2 2 1 0 1 1 1 2 1 0 2 2 2 0 0 0 2 2 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 2 0
 1 2 0 1 1 2 1 0 1 2 0 2 0 1 1 2 0 2 2 0 0 2 2 2 2 0 0 2 1 1 2 0 0 0 0 0 0
 1 0 2 2 0 0 2 0 2 0 0 1 1 0 2 0 1 2 2 0 2 1 2 2 2 2] 13.279415153611023
>>> from sklearn import neighbors, datasets
>>> iris = datasets.load_iris()
>>> n_neighbors = 15
>>> X = iris.data[:, :2]
>>> y = iris.target
>>> weights = 'distance' # also set as 'uniform'
>>> clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
>>> clf.fit(X, y)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=15, p=2,
           weights='distance')
>>> x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
>>> y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
>>> xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
Traceback (most recent call last):
  File "", line 1, in
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
NameError: name 'h' is not defined
>>> Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Traceback (most recent call last):
  File "", line 1, in
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
NameError: name 'xx' is not defined
>>> xx, yy = np.meshgrid(np.arange(x_min, x_max,4),
                         np.arange(y_min, y_max, 4))
>>> Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
>>> print (Z)
[0 2 0 2]
>>> X = [[0, 0], [1, 1]]
>>> y = [0, 1]
>>> clf = svm.SVC()
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
>>> res = clf.predict([[2., 2.],[3., 3.]])
>>> print (res)
[1 1]
>>> res = clf.predict([[4, 4],[3., 3.]])
>>> print (res)
[1 1]
>>> print ("support vectors:", clf.support_vectors_)
support vectors: [[0. 0.]
 [1. 1.]]
>>> from sklearn import linear_model
>>> X = [[0, 0], [1, 1]]
>>> y = [0, 1]
>>> logreg = linear_model.LogisticRegression(C=1e5)
>>> logreg.fit(X, y)
LogisticRegression(C=100000.0, class_weight=None, dual=False,
          fit_intercept=True, intercept_scaling=1, max_iter=100,
          multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,
          solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
>>> res = logreg.predict([[2, 2]])
>>> print(res)
[1]
>>> X = [[6, 0], [1, 1]]
>>> logreg.fit(X, y)
LogisticRegression(C=100000.0, class_weight=None, dual=False,
          fit_intercept=True, intercept_scaling=1, max_iter=100,
          multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,
          solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
>>> res = logreg.predict([[2, 2]])
>>> print(res)
[1]
>>> print(logreg.predict([[2, 3]]))
[1]
>>> print(logreg.predict([[-2, 3]]))
[1]
>>> print(logreg.predict([[1, 1]]))
[1]
>>> import numpy as np
>>> from sklearn import preprocessing
>>> X = np.random.rand(3,4)
>>> scaler = preprocessing.MinMaxScaler()
>>> X_scaled = scaler.fit_transform(X)
>>> X_scaled_convinent = preprocessing.minmax_scale(X)
>>> print X
SyntaxError: Missing parentheses in call to 'print'
>>> print(X)
[[0.08899297 0.26731615 0.93473119 0.14691043]
 [0.89912864 0.39756267 0.77040479 0.4667504 ]
 [0.33393367 0.38053358 0.25908147 0.98348048]]
>>> print(X_scaled_convinent)
[[0.         0.         1.         0.        ]
 [1.         1.         0.75678759 0.38232299]
 [0.30234529 0.86925494 0.         1.        ]]
>>> from sklearn.metrics import accuracy_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> ac = accuracy_score(y_true, y_pred)
>>> print(ac)
0.5
>>> ac2 = accuracy_score(y_true, y_pred, normalize=False)
>>> print(ac2)
2
>>> from sklearn.metrics import normalized_mutual_info_score
>>> y_pred = [0,0,1,1,2,2]
>>> y_true = [1,1,2,2,3,3]
>>> nmi = normalized_mutual_info_score(y_true, y_pred)
>>> print(nmi)
1.0
>>> from sklearn.datasets import load_iris
>>> iris = load_iris()
>>> X = iris.data
>>> y = iris.target
>>> print(X)
[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]
 [5.  3.4 1.5 0.2]
 [4.4 2.9 1.4 0.2]
 [4.9 3.1 1.5 0.1]
 [5.4 3.7 1.5 0.2]
 [4.8 3.4 1.6 0.2]
 [4.8 3.  1.4 0.1]
 [4.3 3.  1.1 0.1]
 [5.8 4.  1.2 0.2]
 [5.7 4.4 1.5 0.4]
 [5.4 3.9 1.3 0.4]
 [5.1 3.5 1.4 0.3]
 [5.7 3.8 1.7 0.3]
 [5.1 3.8 1.5 0.3]
 [5.4 3.4 1.7 0.2]
 [5.1 3.7 1.5 0.4]
 [4.6 3.6 1.  0.2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 [6.3 2.8 5.1 1.5]
 [6.1 2.6 5.6 1.4]
 [7.7 3.  6.1 2.3]
 [6.3 3.4 5.6 2.4]
 [6.4 3.1 5.5 1.8]
 [6.  3.  4.8 1.8]
 [6.9 3.1 5.4 2.1]
 [6.7 3.1 5.6 2.4]
 [6.9 3.1 5.1 2.3]
 [5.8 2.7 5.1 1.9]
 [6.8 3.2 5.9 2.3]
 [6.7 3.3 5.7 2.5]
 [6.7 3.  5.2 2.3]
 [6.3 2.5 5.  1.9]
 [6.5 3.  5.2 2. ]
 [6.2 3.4 5.4 2.3]
 [5.9 3.  5.1 1.8]]
>>> print(y)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]
>>> import numpy as np
>>> X = np.array([[1,1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]])
>>> from sklearn.decomposition import PCA
>>> print(X)
[[1.  1. ]
 [2.  1. ]
 [3.  1.2]
 [4.  1. ]
 [5.  0.8]
 [6.  1. ]]
>>> model = PCA(n_components=2)
>>> print(X)
[[1.  1. ]
 [2.  1. ]
 [3.  1.2]
 [4.  1. ]
 [5.  0.8]
 [6.  1. ]]
>>> model.fit(X)
PCA(copy=True, iterated_power='auto', n_components=2, random_state=None,
  svd_solver='auto', tol=0.0, whiten=False)
>>> print model.components_
SyntaxError: Missing parentheses in call to 'print'
>>> print (model.components_)
[[-0.99973675  0.02294398]
 [ 0.02294398  0.99973675]]
>>> print (model.n_components_)
2
>>> print (model.explained_variance_)
[3.501836 0.014164]
>>> print (model.mean_)
[3.5 1. ]
>>> print (model.noise_variance_)
0.0
>>> import matplotlib.pyplot as plt
>>> plt.figure(1)

>>> plt.figure(2)

>>> ax1 = plt.subplot(211) # 在图表2中创建子图1
>>> ax2 = plt.subplot(212) # 在图表2中创建子图2
>>> x = np.linspace(0, 3, 100)
>>> for i in xrange(5):
    plt.figure(1)  #❶ # 选择图表1
    plt.plot(x, np.exp(i*x/3))
    plt.sca(ax1)   #❷ # 选择图表2的子图1
    plt.plot(x, np.sin(i*x))
    plt.sca(ax2)  # 选择图表2的子图2
    plt.plot(x, np.cos(i*x))


    
Traceback (most recent call last):
  File "", line 1, in
    for i in xrange(5):
NameError: name 'xrange' is not defined
>>> for i in xrange(5):
   plt.figure(1)  #❶ # 选择图表1
   plt.plot(x, np.exp(i*x/3))
   plt.sca(ax1)   #❷ # 选择图表2的子图1
   plt.plot(x, np.sin(i*x))
   plt.sca(ax2)  # 选择图表2的子图2
   plt.plot(x, np.cos(i*x))


   
Traceback (most recent call last):
  File "", line 1, in
    for i in xrange(5):
NameError: name 'xrange' is not defined
>>> for i in range(5):
    plt.figure(1)  #❶ # 选择图表1
    plt.plot(x, np.exp(i*x/3))
    plt.sca(ax1)   #❷ # 选择图表2的子图1
    plt.plot(x, np.sin(i*x))
    plt.sca(ax2)  # 选择图表2的子图2
    plt.plot(x, np.cos(i*x))


    

[]
[]
[]

[]
[]
[]

[]
[]
[]

[]
[]
[]

[]
[]
[]
>>> plt.show()
Traceback (most recent call last):
  File "", line 1, in
    plt.show()
  File "C:\Users\liubao\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\pyplot.py", line 253, in show
    return _show(*args, **kw)
  File "C:\Users\liubao\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\backend_bases.py", line 208, in show
    cls.mainloop()
  File "C:\Users\liubao\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\backends\_backend_tk.py", line 1073, in mainloop
    Tk.mainloop()
  File "C:\Users\liubao\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 405, in mainloop
    _default_root.tk.mainloop(n)
KeyboardInterrupt
>>> data = [1,2,3,4,5,6]
>>> x = numpy.array(data)
Traceback (most recent call last):
  File "", line 1, in
    x = numpy.array(data)
NameError: name 'numpy' is not defined
>>> import numpy
>>> x = numpy.array(data)
>>> print (x)
[1 2 3 4 5 6]
>>> print(x.dtype)
int32
>>> data = [[1,2],[3,4],[5,6]]
>>> x = numpy.array(data)
>>> print (x)
[[1 2]
 [3 4]
 [5 6]]
>>> print

>>> print(x.dtype)
int32
>>> print(x.ndim)
2
>>> print x.shape
SyntaxError: Missing parentheses in call to 'print'
>>> print (x.shape)
(3, 2)
>>> x = numpy.zeros(6)
>>> print(x)
[0. 0. 0. 0. 0. 0.]
>>> x = numpy.zeros((2,3))
>>> print(x)
[[0. 0. 0.]
 [0. 0. 0.]]
>>> x = numpy.ones((2,3))
>>> print(x)
[[1. 1. 1.]
 [1. 1. 1.]]
>>> x = numpy.empty((3,3))
>>> print(x)
[[496.    0.   80. ]
 [  0.  168.   52.8]
 [  0.    0.    1. ]]
>>> print numpy.arange(6) \
      
SyntaxError: invalid syntax
>>> print numpy.arange(6)
SyntaxError: invalid syntax
>>> print (numpy.arange(6) )
[0 1 2 3 4 5]
>>> x = numpy.array([1,2.6,3],dtype = numpy.int64)
>>> print(x)
[1 2 3]
>>> x = numpy.array([1,2,3],dtype = numpy.float64)
>>> print(x)
[1. 2. 3.]
>>> y = x.astype(numpy.int32)
>>> print(y)
[1 2 3]
>>> x = numpy.array([[1,2],[3,4],[5,6]])
>>> print(x[0])
[1 2]
>>> print(x[3])
Traceback (most recent call last):
  File "", line 1, in
    print(x[3])
IndexError: index 3 is out of bounds for axis 0 with size 3
>>> print(x[2])
[5 6]
>>> y = x[0].copy()
>>> print (y)
[1 2]
>>> x = numpy.array([1,2,3,4,5])
>>> print (x[1:3])
[2 3]
>>> print (x[:3])
[1 2 3]
>>> print x[1:-1]
SyntaxError: Missing parentheses in call to 'print'
>>> print (x[1:-1])
[2 3 4]
>>> print (x[1:])
[2 3 4 5]
>>> print (x[1:7])
[2 3 4 5]
>>> x = numpy.array([3,2,3,1,3,0])
>>> y = numpy.array([True,False,True,False,True,False])
>>> print(y)
[ True False  True False  True False]
>>> print (x[y])
[3 3 3]
>>> print (x[y==False] )
[2 1 0]
>>> k = numpy.arange(9)
>>> print (k.reshape((3,3)))
[[0 1 2]
 [3 4 5]
 [6 7 8]]
>>> print (k.reshape((3,3)).T)
[[0 3 6]
 [1 4 7]
 [2 5 8]]
>>> print(numpy.dot(k,k.T) )
204
>>> k = numpy.arange(8).reshape(2,2,2)
>>> print(k)
[[[0 1]
  [2 3]]


 [[4 5]
  [6 7]]]
>>> m = k.transpose((1,0,2))
>>> print(m)
[[[0 1]
  [4 5]]


 [[2 3]
  [6 7]]]
>>> print (m[0][1][0])
4
>>> x = numpy.arange(6)
>>> print (numpy.square(x) )
[ 0  1  4  9 16 25]
>>> x = numpy.array([1.5,1.6,1.7,1.8])
>>> y,z = numpy.modf(x)
>>> print(y)
[0.5 0.6 0.7 0.8]
>>> print(z)
[1. 1. 1. 1.]
>>> x = numpy.array([[1,4],[6,7]])
>>> y = numpy.array([[2,3],[5,8]])
>>> print numpy.maximum(x,y)
SyntaxError: invalid syntax
>>> print (numpy.maximum(x,y))
[[2 4]
 [6 8]]
>>> print (numpy.minimum(x,y))
[[1 3]
 [5 7]]
>>> x = numpy.array([[1,2],[3,3],[1,2]])
>>> print(x.mean)

>>> print(x.mean())
2.0
>>> print (x.mean(axis=1) )
[1.5 3.  1.5]
>>> print (x.sum())
12
>>> print (x.sum(axis=1) )
[3 6 3]
>>> print (x.max(axis=1))
[2 3 2]
>>> print (x.cumsum() )
[ 1  3  6  9 10 12]
>>> print (x.cumprod())
[ 1  2  6 18 18 36]
>>> x = numpy.array([[1,6,2],[6,1,3],[1,5,2]])
>>> print(x.sort())
None
>>> print(x.sort(axis=1) 
KeyboardInterrupt
>>> print(x.sort(axis=1) )
None
>>> x = numpy.array([[1,6,2],[6,1,3],[1,5,2]])
>>> print(numpy.unique(x))
[1 2 3 5 6]
>>> y = numpy.array([1,6,5])
>>> print (numpy.in1d(x,y))
[ True  True False  True  True False  True  True False]
>>> print (numpy.setdiff1d(x,y) )
[2 3]
>>> print (numpy.intersect1d(x,y))
[1 5 6]
>>> x = numpy.array([[1,2],[3,4]])
>>> y = numpy.array([[1,3],[2,4]])
>>> print (x.dot(y))
[[ 5 11]
 [11 25]]
>>> print (numpy.dot(x,y))
[[ 5 11]
 [11 25]]
>>> import numpy.linalg as nla
>>> x = numpy.array([[1,1],[1,2]])
>>> y = nla.inv(x)
>>> print(y)
[[ 2. -1.]
 [-1.  1.]]
>>> print(x.dot(y))
[[1. 0.]
 [0. 1.]]
>>> print (nla.det(x))
1.0
>>> import numpy.random as npr
>>> x = npr.randint(0,2,size=100000)
>>> print (x)
[1 1 1 ... 0 0 1]
>>> x =npr.rand()
>>> print(x)
0.14066507841744136
>>> print(x)
0.14066507841744136
>>> x =npr.rand()
>>> print(x)
0.4669955385714635
>>> print(npr.randint())
Traceback (most recent call last):
  File "", line 1, in
    print(npr.randint())
  File "mtrand.pyx", line 910, in mtrand.RandomState.randint
TypeError: randint() takes at least 1 positional argument (0 given)
>>> x = numpy.array([[1, 2, 3], [4, 5, 6]])
>>> y = numpy.array([[7, 8, 9], [10, 11, 12]])
>>> print (numpy.concatenate([x, y], axis = 0)  )
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
>>> print (numpy.concatenate([x, y], axis = 1)  )
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]
>>> print (numpy.vstack((x, y)))
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
>>> print (numpy.hstack((x, y)))
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]
>>> print (numpy.split(x,2,axis=0) )
[array([[1, 2, 3]]), array([[4, 5, 6]])]
>>> print (numpy.split(x,3,axis=1) )
[array([[1],
       [4]]), array([[2],
       [5]]), array([[3],
       [6]])]
>>> arr2 = np.random.randn(3, 2)
>>> print(arr2)
[[-1.14048163  1.65220643]
 [-0.43853002 -1.08943547]
 [-0.28503684 -0.4961683 ]]
>>> x = numpy.array([[1,2],[3,4]])
>>> print (x.repeat(2))
[1 1 2 2 3 3 4 4]
>>> print (x.repeat(2,axis=0) )
[[1 2]
 [1 2]
 [3 4]
 [3 4]]
>>> print (x.repeat(2,axis=1))
[[1 1 2 2]
 [3 3 4 4]]
>>> from math import sqrt
>>> nums = {int(sqrt(x)) for x in range(30)}
>>> print(nums)
{0, 1, 2, 3, 4, 5}
>>> e = np.random.random((2,2))
>>> print (e)
[[0.10522187 0.14037147]
 [0.34734608 0.46750488]]
>>> e = np.random.random((2,2))
>>> print(2)
2
>>> print(e)
[[0.93570321 0.24201848]
 [0.2572234  0.77359129]]
>>> from sklearn import preprocessing
>>> import numpy as np
>>> X = np.array([[1., -1., 2.], [2., 0., 0.], [0., 1., -1.]])
>>> X_scaled = preprocessing.scale(X)
>>> print(X_scaled)
[[ 0.         -1.22474487  1.33630621]
 [ 1.22474487  0.         -0.26726124]
 [-1.22474487  1.22474487 -1.06904497]]
>>> X_scaled.mean(axis=0)
array([0., 0., 0.])
>>> X_scaled.std(axis=0)
array([1., 1., 1.])
>>> scaler = preprocessing.StandardScaler().fit(X)
>>> scaler.mean_
array([1.        , 0.        , 0.33333333])
>>> scaler.std_
Traceback (most recent call last):
  File "", line 1, in
    scaler.std_
AttributeError: 'StandardScaler' object has no attribute 'std_'
>>> scaler.transform(X)
array([[ 0.        , -1.22474487,  1.33630621],
       [ 1.22474487,  0.        , -0.26726124],
       [-1.22474487,  1.22474487, -1.06904497]])
>>> np.random.seed(123)
    print('use sklearn')
    # 注:shape of data: [n_samples, n_features]
    data = np.random.randn(10, 4)
    scaler = StandardScaler()
    scaler.fit(data)
    trans_data = scaler.transform(data)
    print('original data: ')
    print data
    print('transformed data: ')
    print trans_data
    print('scaler info: scaler.mean_: {}, scaler.var_: {}'.format(scaler.mean_, scaler.var_))
    print('\n')
    
SyntaxError: multiple statements found while compiling a single statement
>>> np.random.seed(123)
>>> data = np.random.randn(10, 4)
>>> print(data)
[[-1.0856306   0.99734545  0.2829785  -1.50629471]
 [-0.57860025  1.65143654 -2.42667924 -0.42891263]
 [ 1.26593626 -0.8667404  -0.67888615 -0.09470897]
 [ 1.49138963 -0.638902   -0.44398196 -0.43435128]
 [ 2.20593008  2.18678609  1.0040539   0.3861864 ]
 [ 0.73736858  1.49073203 -0.93583387  1.17582904]
 [-1.25388067 -0.6377515   0.9071052  -1.4286807 ]
 [-0.14006872 -0.8617549  -0.25561937 -2.79858911]
 [-1.7715331  -0.69987723  0.92746243 -0.17363568]
 [ 0.00284592  0.68822271 -0.87953634  0.28362732]]
>>> print(scaler = StandardScaler())
Traceback (most recent call last):
  File "", line 1, in
    print(scaler = StandardScaler())
NameError: name 'StandardScaler' is not defined
>>> scaler = StandardScaler()
Traceback (most recent call last):
  File "", line 1, in
    scaler = StandardScaler()
NameError: name 'StandardScaler' is not defined
>>> from sklearn.preprocessing import StandardScaler
>>> scaler = StandardScaler()
>>> scaler.fit(data)
StandardScaler(copy=True, with_mean=True, with_std=True)
>>> trans_data = scaler.transform(data)
>>> print (data)
[[-1.0856306   0.99734545  0.2829785  -1.50629471]
 [-0.57860025  1.65143654 -2.42667924 -0.42891263]
 [ 1.26593626 -0.8667404  -0.67888615 -0.09470897]
 [ 1.49138963 -0.638902   -0.44398196 -0.43435128]
 [ 2.20593008  2.18678609  1.0040539   0.3861864 ]
 [ 0.73736858  1.49073203 -0.93583387  1.17582904]
 [-1.25388067 -0.6377515   0.9071052  -1.4286807 ]
 [-0.14006872 -0.8617549  -0.25561937 -2.79858911]
 [-1.7715331  -0.69987723  0.92746243 -0.17363568]
 [ 0.00284592  0.68822271 -0.87953634  0.28362732]]
>>> print (trans_data)
[[-0.94511643  0.58665507  0.5223171  -0.93064483]
 [-0.53659117  1.16247784 -2.13366794  0.06768082]
 [ 0.9495916  -1.05437488 -0.42049501  0.3773612 ]
 [ 1.13124423 -0.85379954 -0.19024378  0.06264126]
 [ 1.70696485  1.63376764  1.22910949  0.8229693 ]
 [ 0.52371324  1.02100318 -0.67235312  1.55466934]
 [-1.08067913 -0.85278672  1.13408114 -0.858726  ]
 [-0.18325687 -1.04998594 -0.00561227 -2.1281129 ]
 [-1.49776284 -0.9074785   1.15403514  0.30422599]
 [-0.06810748  0.31452186 -0.61717074  0.72793583]]
>>> mean = np.mean(data, axis=0)
>>> print(mean)
[ 0.08737571  0.33094968 -0.24989369 -0.50195303]
>>> std = np.std(data, axis=0)
>>> print(std)
[1.24112361 1.13592433 1.02020821 1.07918902]
>>> var =std *std
>>> print(var)
[1.54038781 1.29032409 1.04082479 1.16464894]
>>> another_trans_data = data - mean
>>> print(another_trans_data = data - mean)
Traceback (most recent call last):
  File "", line 1, in
    print(another_trans_data = data - mean)
TypeError: 'another_trans_data' is an invalid keyword argument for this function
>>> print(another_trans_data)
[[-1.17300631  0.66639577  0.53287219 -1.00434168]
 [-0.66597596  1.32048686 -2.17678555  0.0730404 ]
 [ 1.17856055 -1.19769008 -0.42899246  0.40724406]
 [ 1.40401391 -0.96985167 -0.19408827  0.06760176]
 [ 2.11855437  1.85583641  1.25394759  0.88813943]
 [ 0.64999286  1.15978235 -0.68594018  1.67778208]
 [-1.34125638 -0.96870118  1.15699889 -0.92672767]
 [-0.22744443 -1.19270457 -0.00572568 -2.29663607]
 [-1.85890882 -1.03082691  1.17735612  0.32831735]
 [-0.0845298   0.35727303 -0.62964265  0.78558035]]
>>> 

你可能感兴趣的:(随笔)