本文整理汇总了Python中sklearn.manifold.Isomap.fit方法的典型用法代码示例。如果您正苦于以下问题:Python Isomap.fit方法的具体用法?Python Isomap.fit怎么用?Python Isomap.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.manifold.Isomap
的用法示例。
在下文中一共展示了Isomap.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dimension_reduce
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def dimension_reduce():
''' This compares a few different methods of
dimensionality reduction on the current dataset.
'''
pca = PCA(n_components=2) # initialize a dimensionality reducer
pca.fit(digits.data) # fit it to our data
X_pca = pca.transform(digits.data) # apply our data to the transformation
plt.subplot(1, 3, 1)
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=digits.target)# plot the manifold
se = SpectralEmbedding()
X_se = se.fit_transform(digits.data)
plt.subplot(1, 3, 2)
plt.scatter(X_se[:, 0], X_se[:, 1], c=digits.target)
isomap = Isomap(n_components=2, n_neighbors=20)
isomap.fit(digits.data)
X_iso = isomap.transform(digits.data)
plt.subplot(1, 3, 3)
plt.scatter(X_iso[:, 0], X_iso[:, 1], c=digits.target)
plt.show()
plt.matshow(pca.mean_.reshape(8, 8)) # plot the mean components
plt.matshow(pca.components_[0].reshape(8, 8)) # plot the first principal component
plt.matshow(pca.components_[1].reshape(8, 8)) # plot the second principal component
plt.show()
示例2: ML
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def ML( self ):
data = self.data.values[ :, :-3 ]
scaler = MinMaxScaler()
#scaler = StandardScaler()
X = scaler.fit_transform( data )
#X = data
isomap = Isomap( n_components = 2 )
isomap.fit( X )
#print pca.explained_variance_ratio_
import pdb; pdb.set_trace()
示例3: __init__
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
class FloorplanEstimator:
"""
Simple estimator for rough floorplans
"""
def __init__(self):
"""
Instantiate floorplan estimator
"""
self.dimred = Isomap(n_neighbors=25, n_components=2)
self._fingerprints = None
self._label = None
def fit(self, fingerprints, label):
"""
Estimate floorplan from labeled fingerprints
:param fingerprints: list of fingerprints
:param label: list of corresponding labels
"""
self.dimred.fit(fingerprints)
self._fingerprints = fingerprints
self._label = label
def transform(self, fingerprints):
"""
Get x,y coordinates of fingerprints on floorplan
:param fingerprints: list of fingerprints
:return: list of [x,y] coordinates
"""
return self.dimred.transform(fingerprints)
def draw(self):
"""
Draw the estimated floorplan in the current figure
"""
xy = self.dimred.transform(self._fingerprints)
x_min, x_max = xy[:,0].min(), xy[:,0].max()
y_min, y_max = xy[:,1].min(), xy[:,1].max()
xx, yy = np.meshgrid(np.arange(x_min, x_max, 1.0),
np.arange(y_min, y_max, 1.0))
clf = RadiusNeighborsClassifier(radius=3.0, outlier_label=0)
clf.fit(xy, self._label)
label = clf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
plt.pcolormesh(xx, yy, label)
plt.scatter(xy[:,0], xy[:,1], c=self._label, vmin=0)
示例4: compute_iso_map
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def compute_iso_map(self, original_features):
feature_matrix = original_features.drop('file', 1).as_matrix()
feature_matrix = np.nan_to_num(feature_matrix)
dimen_reductor = Isomap(n_components=self.n_components)
full_size = feature_matrix.shape[0]
train_size = int(self.ratio * full_size)
row_indices = list(range(full_size))
feature_training_indices = np.random.choice(row_indices, size = train_size)
training_feature_matrix = feature_matrix[feature_training_indices, :]
dimen_reductor.fit(training_feature_matrix)
reduced_features = dimen_reductor.transform(feature_matrix)
reduced_normalized_features = reduced_features - reduced_features.min(axis=0)
reduced_normalized_features /= reduced_normalized_features.max(axis=0)
return reduced_normalized_features
示例5: mult_scl
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def mult_scl(X, labels):
print('labels:')
for i, label in zip(range(1, len(labels) + 1), labels):
print('{}: {}'.format(i, label))
isomap = Isomap()
points = isomap.fit(np.nan_to_num(X)).embedding_
f, (ax1, ax2, ax3) = plt.subplots(1, 3)
plot_location(labels, ax3)
ax1.scatter(points[:, 0], points[:, 1], s=20, c='r')
ax1.set_title('Isomap')
add_labels(labels, points, ax1)
mds = MDS()
points = mds.fit(np.nan_to_num(X)).embedding_
ax2.scatter(points[:, 0], points[:, 1], s=20, c='g')
ax2.set_title('MDS')
add_labels(labels, points, ax2)
plt.show()
示例6: isomap
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def isomap(self, n_components=2, n_neighbors=3, show=False):
"""
Calculates lower dimention coordinates using the isomap algorithm.
:param n_components: dimentionality of the reduced space
:type n_components: int, optional
:param n_neighbors: Used by isomap to determine the number of neighbors
for each point. Large neighbor size tends to produce a denser map.
:type n_neighbors: int, optional
:param show: Shows the calculated coordinates if true.
:type show: boolean, optional
"""
model = Isomap(n_components=n_components, n_neighbors=n_neighbors)
self.pos = model.fit(self.dismat).embedding_
if show:
return self.pos
示例7: isoMap
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def isoMap(X, y):
im = Isomap(n_components = 1, eigen_solver = "dense", n_neighbors = 20)
im.fit(X)
transformX = im.transform(X)
return transformX
示例8: PCA
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
#maxabsscaler = pp.MaxAbsScaler()
#maxabsscaler.fit(X)
#X = maxabsscaler.transform(X)
#print('MaxAbsScaler\n========')
#X = pp.normalize(X)
#print('normalizer\n========')
# TODO: Use PCA to reduce noise, n_components 4-14
nc = 5
#pca = PCA(n_components=nc)
#pca.fit(X)
#X = pca.transform(X)
#print('PCA: ', nc)
# Use Isomap to reduce noise, n_neighbors 2-5
nn = 4
im = Isomap(n_neighbors=nn, n_components=nc)
im.fit(X)
X = im.transform(X)
print('Isomap: ',nn, ' comp: ', nc)
# TODO: train_test_split 30% and random_state=7
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=7)
# TODO: Create an SVC, train and score against defaults
result = findMaxSVC()
print(result['score'])
示例9: PCA
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
scaler = preprocessing.StandardScaler() #0.966101694915
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
#pcaComponent = 4
#pca = PCA(n_components=pcaComponent)
#pca.fit(X_train)
#X_train = pca.transform(X_train)
#X_test = pca.transform(X_test)
neighbors = 2
components = 4
isomap = Isomap(n_neighbors=neighbors, n_components=components)
isomap.fit(X_train)
X_train = isomap.transform(X_train)
X_test = isomap.transform(X_test)
#svc = SVC()
#svc.fit(X_train, y_train)
#print svc.score(X_test, y_test)
best_score = 0
best_C = 0
best_gamma = 0
for C in np.arange(0.05, 2.05, 0.05):
for gamma in np.arange(0.001, 1.001, 0.001):
svc = SVC(C = C, gamma = gamma)
svc.fit(X_train, y_train)
score = svc.score(X_test, y_test)
示例10:
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
samples.append(img.reshape(-1))
color_sample.append('r')
#
# TODO: Convert the list to a dataframe
#
# .. your code here ..
df_images = pd.DataFrame(samples)
#df_images_t = df_images.transpose()
#
# TODO: Implement Isomap here. Reduce the dataframe df down
# to three components, using K=6 for your neighborhood size
#
# .. your code here ..
iso_bear=Isomap(n_components=3,n_neighbors=6)
iso_bear.fit(df_images)
T_iso_bear = iso_bear.transform(df_images)
#
# TODO: Create a 2D Scatter plot to graph your manifold. You
# can use either 'o' or '.' as your marker. Graph the first two
# isomap components
#
# .. your code here ..
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('Manifold Scatterplot')
ax.set_xlabel('Component: {0}'.format(0))
ax.set_ylabel('Component: {0}'.format(1))
ax.scatter(T_iso_bear[:,0],T_iso_bear[:,1], marker='.',alpha=0.7, c=color_sample)
示例11: PCA
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
if basic_plots:
ax = pp.subplot(2, 1, 1)
train.describe()[1:].plot(legend=False, ax=ax)
pp.title("Description of training data.")
ax = pp.subplot(2, 1, 2)
train.loc[:,:5].plot(legend=False, ax=ax)
pp.title("First 5 series plotted.")
pp.show()
if do_pca:
x = train.values
pca = PCA(n_components=3)
pca.fit(x)
y = pca.transform(x)
print 'Orig shape: ', x.shape, 'New shape: ', y.shape
pp.scatter(y[:,0], y[:,1], c=target.values)
pp.show()
if do_isomap:
x = train.values
from sklearn.manifold import Isomap
isomap = Isomap(n_components=2, n_neighbors=20)
isomap.fit(x)
y = isomap.transform(x)
pp.scatter(y[:,0], y[:,1], c=target.values)
pp.show()
示例12: scoreFromPvalues
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
print 'offset1: ' , offset1
print 'offset2: ' , offset2
#HERE structures must have only atoms of selected chain
TM_align = rcu.TM_aligned_residues(pdb1,pdb2,offset1, offset2)
individualjammings1 = np.asarray(get_permutations(nj1['individual'],TM_align['alignedList1']))
individualjammings2 = np.asarray(get_permutations(nj2['individual'],TM_align['alignedList2']))
PValsScore = scoreFromPvalues(individualjammings1,individualjammings2)
print 'PValsScore: ', PValsScore
clf = Isomap(n_components=2)#Isomap(n_components=2)
clf.fit(individualjammings1)
ij1 = clf.transform(individualjammings1)
ij2 = clf.transform(individualjammings2)
print ij1
f, (ax1, ax2,ax3) = pl.subplots(1,3, sharex=True, sharey=True)
pl.ioff()
pl.title('ensemble correlation: %.4f'%PValsScore)
#pl.subplot(1,2,1)
ax1.scatter(ij1[:,0],ij1[:,1],marker='o',s=45,facecolor='0.6',edgecolor='r')
#pl.subplot(1,2,2)
ax2.scatter(ij2[:,0],ij2[:,1],marker='o',s=45,facecolor='0.6',edgecolor='r')
ax3.scatter(ij2[:,0],ij2[:,1],marker='o',s=25,facecolor='y',edgecolor='0.05',alpha=0.6)
ax3.scatter(ij1[:,0],ij1[:,1],marker='o',s=25,facecolor='b',edgecolor='0.05',alpha=0.5)
ax1.axes.get_xaxis().set_visible(False)
ax2.axes.get_xaxis().set_visible(False)
示例13: preprocess
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
return features_train_transformed, lables, vectorizer, selector, le, features
# nFeatures = np.arange(50, 1000, 50)
nISOMAP = np.arange(20, 200, 20)
data = {}
for k in nISOMAP:
features, labels, vectorizer, selector, le, features_data = preprocess("pkl/article_2_people.pkl", "pkl/lable_2_people.pkl")
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(features, labels, test_size=0.1, random_state=42)
t0 = time()
iso = Isomap(n_neighbors=15, n_components=k, eigen_solver='auto')
iso.fit(features_train)
print ("Dimension Reduction time:", round(time()-t0, 3), "s")
features_train = iso.transform(features_train)
features_test = iso.transform(features_test)
for name, clf in [
('AdaBoostClassifier', AdaBoostClassifier(algorithm='SAMME.R')),
('BernoulliNB', BernoulliNB(alpha=1)),
('GaussianNB', GaussianNB()),
('DecisionTreeClassifier', DecisionTreeClassifier(min_samples_split=100)),
('KNeighborsClassifier', KNeighborsClassifier(n_neighbors=50, algorithm='ball_tree')),
('RandomForestClassifier', RandomForestClassifier(min_samples_split=100)),
('SVC', SVC(kernel='linear', C=1))
]:
示例14: PCA
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
# title is your chart title
# x is the principal component you want displayed on the x-axis, Can be 0 or 1
# y is the principal component you want displayed on the y-axis, Can be 1 or 2
#
# .. your code here ..
from sklearn.decomposition import PCA
pca = PCA(n_components=3)
pca.fit(df)
T = pca.transform(df)
Plot2D(T, "PCA 1 2", 1, 2)
#
# TODO: Implement Isomap here. Reduce the dataframe df down
# to THREE components. Once you've done that, call Plot2D using
# the first two components.
#
# .. your code here ..
from sklearn.manifold import Isomap
imap = Isomap(n_neighbors=8, n_components=3)
imap.fit(df)
T2 = imap.transform(df)
Plot2D(T2, "Isomap", 1, 2)
#
# TODO: If you're up for a challenge, draw your dataframes in 3D
# Even if you're not, just do it anyway.
#
# .. your code here ..
plt.show()
示例15: main
# 需要导入模块: from sklearn.manifold import Isomap [as 别名]
# 或者: from sklearn.manifold.Isomap import fit [as 别名]
def main():
#Load the dataset from Matlab
data = sio.loadmat('baseline2.mat')
n_train = int(data['n_train'])
n_test = int(data['n_test'])
train_x = np.array(data['train_x'])
train_t = np.array(data['train_t']).reshape(n_train)
test_x = np.array(data['test_x'])
test_t = np.array(data['test_t']).reshape(800)
X_indices = np.arange(train_x.shape[-1])
#SVM Fitting
C = [-10,5,10]
G = [-10,5,10]
CF = [-10,5,10]
# Plot the cross-validation score as a function of percentile of features
NG = [10,20,50,100,200]
components = (10,20,50,100,200)
scores = list()
svcs = list()
isos = list()
for cc in components:
for nn in NG:
best_c = 0
best_g = 0
best_cf = 0
best_iso = None
max_score = -np.inf
iso = Isomap(n_components=cc, n_neighbors=nn)
iso.fit(train_x)
train = iso.transform(train_x)
for c in C:
for g in G:
for cf in CF:
#Find best C, gamma
svc = svm.SVC(C=2**c, gamma=2**g, coef0=2**cf, degree=3, kernel='poly',max_iter=1000000)
this_scores = cross_validation.cross_val_score(svc, train, train_t, n_jobs=-1, cv=5, scoring='accuracy')
mean_score = sum(this_scores)/len(this_scores)
print("C: "+str(c)+" G: "+str(g)+" CMPS: "+str(cc)+" A: "+str(mean_score) + " CF: " +str(cf) + "N: "+str(nn))
if mean_score > max_score:
max_score = mean_score
best_svm = svc
best_iso = iso
svcs.append(best_svm)
isos.append(best_iso)
scores.append(max_score)
m_ind = scores.index(max(scores))
best_s = svcs[m_ind]
iso = isos[m_ind]
# Test final model
test = iso.transform(test_x)
train = iso.transform(train_x)
best_s.fit(train,train_t)
pred = best_s.predict(test)
sio.savemat('predicted_iso.mat',dict(x=range(800),pred_t=pred))
final_score = best_s.score(test,test_t)
print(best_s)
print("Final Accuracy: "+str(final_score))
print(scores)