当前位置: 首页>>代码示例>>Python>>正文


Python StandardScaler.std方法代码示例

本文整理汇总了Python中sklearn.preprocessing.StandardScaler.std方法的典型用法代码示例。如果您正苦于以下问题:Python StandardScaler.std方法的具体用法?Python StandardScaler.std怎么用?Python StandardScaler.std使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.preprocessing.StandardScaler的用法示例。


在下文中一共展示了StandardScaler.std方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: standardize

# 需要导入模块: from sklearn.preprocessing import StandardScaler [as 别名]
# 或者: from sklearn.preprocessing.StandardScaler import std [as 别名]
def standardize(array, name):
    """Recieves a dataFrame or Series (from pandas) and returns a numpy array with zero mean and unit variance."""
    # Transform to numpy array
    nparray = array.as_matrix().reshape(array.shape[0],1).astype('float32')
    print('------------')
    print(name)
    print('Different values before:', np.unique(nparray).shape[0])

    # Standardize the data
    nparray = StandardScaler().fit_transform(nparray)

    # Print some information
    print('Mean:', nparray.mean())
    print('Max:', nparray.max())
    print('Min:', nparray.min())
    print('Std:', nparray.std())
    print('Different values after:', np.unique(nparray).shape[0])

    return nparray
开发者ID:SetaSouto,项目名称:BimboInvDemand,代码行数:21,代码来源:data.py

示例2: classify

# 需要导入模块: from sklearn.preprocessing import StandardScaler [as 别名]
# 或者: from sklearn.preprocessing.StandardScaler import std [as 别名]
def classify(filename='4_14_type4_apollo3d.txt', useFrac=1.0, trainFraction=0.5, equalClassSize=True, 
             thres=0.5, useFeatures=[0], useAll=True, batch=False, useCache=True, 
             featureSelect=False, kickType=[11], draw=False, scale=False, C=1.0, B=1.0, returnProb=False): 
  
  features, labels = parse(filename=filename, useCache=useCache, ezKickSuccess=False, 
                           kickType=kickType, ignoreSelfFailure=False, useDirectFeatures=True, 
                           nfeatures=8)
  num2Use = int(useFrac*len(features))
  features = features[:num2Use]; labels = labels[:num2Use]
  if scale:
    features = StandardScaler().fit_transform(features)
  print "features mean:", features.mean(axis=0)
  print "features std:", features.std(axis=0)
  if not useAll:
#     labels = np.random.random(features.shape[0]) < 0.5
    newFeatures = features[:, useFeatures]
#     print newFeatures[:100,:]
#     newFeatures = np.random.random((features.shape[0], 9))
  else:
    newFeatures = features
  
  if equalClassSize:
    newFeatures, labels = balanceClasses(newFeatures, labels)
  
  print "we have " + str(newFeatures.shape[0]) + " samples."
  print "we have " + str(np.sum(labels == 1)) + " positive labels"
  print "ratio: " + str(float(np.sum(labels == -1))/np.sum(labels == 1))
  print "using approximately " + str(trainFraction*100) + "% as training examples"
  
  r = np.random.random(newFeatures.shape[0]) < trainFraction; r2 = np.invert(r)
  trainingSet = newFeatures[r, :]; trainLabels = labels[r]
  testingSet = newFeatures[r2, :]; testLabels = labels[r2]
      
  if not equalClassSize:
    testingSet, testLabels = balanceClasses(testingSet, testLabels)
    clf = LogisticRegression(C=C, class_weight='auto', intercept_scaling=B, penalty='l2')
#     clf = svm.SVC(C=C, kernel='rbf', class_weight='auto', probability=returnProb)
  else:
    clf = LogisticRegression(C=C, intercept_scaling=B, penalty='l2')
#     clf = svm.SVC(C=C, kernel='rbf', class_weight='auto', probability=returnProb)
#     clf = RandomForestClassifier()
#     clf = KNeighborsClassifier(n_neighbors=15)
#   print np.arange(20)[clf2.get_support()]
#     clf = AdaBoostClassifier()
#   clf = GradientBoostingClassifier(init=LogisticRegression)
#     clf = GaussianNB()
#   clf = DecisionTreeClassifier()
  
  if featureSelect:
    rfecv = RFE(estimator=clf, step=1,  n_features_to_select=8)
#     rfecv = RFECV(estimator=clf, step=1, cv=10)
    rfecv.fit(newFeatures, labels)
    print("Optimal number of features : %d" % rfecv.n_features_)
    print rfecv.ranking_
    print np.arange(20)[rfecv.support_]
    return
  
  clf.fit(trainingSet, trainLabels)
  
  def myPredict(clf, x, thres=0.5):
    probArray = clf.predict_proba(x)[:,1]
    predictLabels = 1*(probArray > thres)
    predictLabels = 2*predictLabels - 1
    return predictLabels, probArray
  
#   d = np.reshape(np.linspace(0, 10, num=1000), (-1, 1))
# #   print d.shape
#   results = clf.predict(d)
#   for i in xrange(1000):
#     if results[i] == 1:
#       print "dist:", i*0.01
#       break
  
  if returnProb:
    predictLabels, probArray = myPredict(clf, testingSet, thres=thres)
  else:
    predictLabels = clf.predict(testingSet)
#     print "accuracy rate from classifier: " + str(clf.score(testingSet, testLabels))
    
  suffix = "" if useAll else str(features)
  
  if draw and returnProb:
    area = drawPrecisionRecallCurve(filename[:-4] + suffix, testLabels, probArray)
    roc_auc = drawROCCurve(filename[:-4] + suffix, testLabels, probArray)
  
  false_neg = false_pos = true_neg = true_pos = 0
  for i in xrange(len(predictLabels)):
    if predictLabels[i] == testLabels[i] == -1:
      true_neg += 1
    elif predictLabels[i] == testLabels[i] == 1:
      true_pos += 1
    elif predictLabels[i] == -1 and testLabels[i] == 1:
      false_neg += 1
    else:
      false_pos += 1
  good = true_neg + true_pos
  print "accuracy rate: ", good/float(len(predictLabels)), good
  print "true negative rate: ", true_neg/float(len(predictLabels)), true_neg
  print "true positive rate: ", true_pos/float(len(predictLabels)), true_pos
  print "false negative rate: ", false_neg/float(len(predictLabels)), false_neg
#.........这里部分代码省略.........
开发者ID:jasonzliang,项目名称:kick_classifier,代码行数:103,代码来源:classify_old.py


注:本文中的sklearn.preprocessing.StandardScaler.std方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。