本文整理匯總了Python中sklearn.preprocessing.StandardScaler.as_matrix方法的典型用法代碼示例。如果您正苦於以下問題:Python StandardScaler.as_matrix方法的具體用法?Python StandardScaler.as_matrix怎麽用?Python StandardScaler.as_matrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.preprocessing.StandardScaler
的用法示例。
在下文中一共展示了StandardScaler.as_matrix方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: standardize
# 需要導入模塊: from sklearn.preprocessing import StandardScaler [as 別名]
# 或者: from sklearn.preprocessing.StandardScaler import as_matrix [as 別名]
def standardize(df):
X = df.drop(['id', 'group'], axis=1)
X.ix[X.gender == 'f', 'gender'] = 1.0
X.ix[X.gender == 'm', 'gender'] = -1.0
X['gender'] = pd.to_numeric(X.gender)
X = StandardScaler().fit_transform(X.as_matrix())
return X
示例2: LabelEncoder
# 需要導入模塊: from sklearn.preprocessing import StandardScaler [as 別名]
# 或者: from sklearn.preprocessing.StandardScaler import as_matrix [as 別名]
# Encode the nta column
le = LabelEncoder()
nta_encoded = le.fit_transform(data.nta)
data['nta_encoded'] = nta_encoded
# Drop the columns we won't use for our model
data.drop(['nta','date_hour','nta_dt','nbhd_name','Unnamed: 0'],axis=1,inplace=True)
# Scale our data (not every column needs to be scaled)
to_scale = data[(data.columns-['month','day','hour','nta_encoded'])]
X = StandardScaler().fit_transform(to_scale)
X = pd.DataFrame(X)
for col in ['month','day','hour','nta_encoded']:
X[col] = data[col]
X = X.as_matrix()
# We want to determine the number of bins we are going to classify into by using
# k-means clustering different values of n clusters. We are going to choose the
# n with the best silhouette score
cluster_scores = []
for n in range(3,6):
# Initialize and fit a KMeans object
k_means = cluster.KMeans(n_clusters=n,n_jobs=-1,verbose=1)
k_means.fit(X)
# Get the cluster labels for each point
labels = k_means.labels_
# Initialize a list to store this n's silhouette scores
scores = []
# We need to limit the sample_size when calculating silhouette scores due to
# computation time