本文整理汇总了Python中sklearn.preprocessing.StandardScaler.mean方法的典型用法代码示例。如果您正苦于以下问题:Python StandardScaler.mean方法的具体用法?Python StandardScaler.mean怎么用?Python StandardScaler.mean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.preprocessing.StandardScaler
的用法示例。
在下文中一共展示了StandardScaler.mean方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_classifiers_classes
# 需要导入模块: from sklearn.preprocessing import StandardScaler [as 别名]
# 或者: from sklearn.preprocessing.StandardScaler import mean [as 别名]
def check_classifiers_classes(name, Classifier):
X, y = make_blobs(n_samples=30, random_state=0, cluster_std=0.1)
X, y = shuffle(X, y, random_state=7)
X = StandardScaler().fit_transform(X)
# We need to make sure that we have non negative data, for things
# like NMF
X -= X.min() - .1
y_names = np.array(["one", "two", "three"])[y]
for y_names in [y_names, y_names.astype('O')]:
if name in ["LabelPropagation", "LabelSpreading"]:
# TODO some complication with -1 label
y_ = y
else:
y_ = y_names
classes = np.unique(y_)
# catch deprecation warnings
with warnings.catch_warnings(record=True):
classifier = Classifier()
if name == 'BernoulliNB':
classifier.set_params(binarize=X.mean())
set_fast_parameters(classifier)
# fit
classifier.fit(X, y_)
y_pred = classifier.predict(X)
# training set performance
assert_array_equal(np.unique(y_), np.unique(y_pred))
if np.any(classifier.classes_ != classes):
print("Unexpected classes_ attribute for %r: "
"expected %s, got %s" %
(classifier, classes, classifier.classes_))
示例2: standardize
# 需要导入模块: from sklearn.preprocessing import StandardScaler [as 别名]
# 或者: from sklearn.preprocessing.StandardScaler import mean [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
示例3: classify
# 需要导入模块: from sklearn.preprocessing import StandardScaler [as 别名]
# 或者: from sklearn.preprocessing.StandardScaler import mean [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
#.........这里部分代码省略.........