Question: needing help with index error: 9 . Visualize the first five sentences embedding for each tweet using PCA import spacy # Load the large model

needing help with index error:
9.Visualize the first five sentences embedding for each tweet using PCA
import spacy
# Load the large model
nlp = spacy.load("en_core_web_lg")
tweets.index = range(0,len(tweets))
#data_bc =[]
data_ac =[]
# Generate only the first 5 sentence/doc embeddings
top_n =5
for idx, row in tweets.iterrows():
if idx < top_n:
data_ac.append([row['tweet'], nlp(row['tweet']).vector, row['target']])
else:
break
#Store the results in the dfs
df_ac = pd.DataFrame(data_ac, columns=['review_ac', "vector", "target"])
pprint(df_ac)
from sklearn.decomposition import PCA
df_ac.index = range(0,len(df_ac))
X_ac = np.array(list(df_ac["vector"]))
# Extract 2 principal componets using PCA
pca = PCA(n_components=2)
pca_ac_2d = pca.fit_transform(X_ac)
review_ac = df_ac.review_ac
fig, ax = plt.subplots(2,1, figsize =(17.8,10*2))
fig.suptitle('Visualize Sentence Vectors Using PCA', fontsize=24)
# Color the sentence embeddings based on the airline sentiment
colors ={'4':'green', '0':'red'}
ax[1].scatter(pca_ac_2d[:,0],pca_ac_2d[:,1], s =100, c = df_ac["target"].map(colors),
cmap = "coolwarm", edgecolor = "None", alpha=0.5)
# Label the points using the texts
for idx, txt in enumerate(review_ac):
ax[1].annotate(txt,(pca_ac_2d[idx,0],pca_ac_2d[idx,1])) #IndexError: index 5 is out of bounds for axis 0 with size 5
ax[1].set_title('Scatter Plot for Cleaned Texts', fontsize=20)
plt.show()
example of df:
example of df:
review_ac \
0 cant sleep gahh. better try again atleast. ni...
1 @zincous It's still in the works... I promise
2 @realAdamBeyer how can I get drumcode products...
3 @Cupcake_Smile Thanks . i appreciate that .
4 @_MeesLovesYou_ Grounded from the computer... ...
vector target
0[1.5666437,2.5385,-4.0792966,-1.8616177,-0...0
1[0.35162392,2.2831438,-1.9513298,-1.5335256...4
2[-0.29363254,2.0723453,-4.4491,-1.4692746,...4
3[0.84563166,-1.7277744,-2.7835429,-2.918788...4
4[-0.7086367,-0.63313067,-2.168379,-0.609265...0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!