本文整理匯總了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
示例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
#.........這裏部分代碼省略.........