<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Extracting similar artists for the artists we have in our dataset.
Step1:  The artist dataset contains ids, Artist names, Artist url, and Artist pictureURL.
Step2:  In this notebook we extract the unique mbid code identifiers of each artist using the api.
This mbid code is used later on to extract the similar artists belonging to this artist.
To match the artist we are extracting from the api using the name, to the artist in our dataset, we search through the url of each artist with the same name and cross compare both urls, picking only the one that matches.
There are 3 cases that could happen in here
Step3:  As mentioned above there are cases where there is no output mbid code returned from an artist, and so filtering the dataframe returning only the artists where the mbid code was fetched.
Step4:  Now that we have the codes for the artists, we can extract all the similar artists to them.
Keep in mind that this function returns a massive amount of information about those similar artists, one of which is of a particular interest to us, the "match", that returns a value between 0 and 1 describing how similar both artists are, 1 being the most similar.
Step5:  For each artist, 100 similar artists are returned. To decrease the complexity of the problem and reduce the computational time, we only extracted the top 10 similar artists and saved them into a dataframe that contains the artist, artist id, the top 10 similar artists, and their corresponding weights.
Step6:  Sample output
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import requests
from urllib.parse import quote
from artist_api import *
Explanation: Extracting similar artists for the artists we have in our dataset.
End of explanation
artists_df = pd.read_csv('artists.dat', sep='\t', header=0, index_col=0, skipinitialspace=True)
artists_df.head()
Explanation: The artist dataset contains ids, Artist names, Artist url, and Artist pictureURL.
End of explanation
artists_df['mbid'] = artists_df.apply(parse_artists, axis=1)
artists_df_mbid = pd.read_csv('artist_mbid_codes.csv', sep='\t', header=0, index_col=0, skipinitialspace=True)
artists_df_mbid.head()
Explanation: In this notebook we extract the unique mbid code identifiers of each artist using the api.
This mbid code is used later on to extract the similar artists belonging to this artist.
To match the artist we are extracting from the api using the name, to the artist in our dataset, we search through the url of each artist with the same name and cross compare both urls, picking only the one that matches.
There are 3 cases that could happen in here:
- There is an mbid code available.
- There is no mbid code available.
- We didn't find an artist match, denoted as "not found".
End of explanation
print("The number of artists without an mbid code :",\
len(artists_df_mbid[artists_df_mbid.mbid=='notfound']) + len(artists_df_mbid[artists_df_mbid.mbid.isnull()]))
print("The total number of artists we have :",len(artists_df))
artists_df_mbid = artists_df_mbid[~((artists_df_mbid.mbid =='notfound'))]
artists_df_mbid = artists_df_mbid[~(artists_df_mbid.mbid.isnull())]
len(artists_df_mbid)
Explanation: As mentioned above there are cases where there is no output mbid code returned from an artist, and so filtering the dataframe returning only the artists where the mbid code was fetched.
End of explanation
artists_df_mbid['similar_artists'] = artists_df_mbid.apply(similar_artists, axis=1)
columns=['ArtistID','Artist','Similar_artists','Weight']
artist_artist = pd.DataFrame(columns=columns)
Explanation: Now that we have the codes for the artists, we can extract all the similar artists to them.
Keep in mind that this function returns a massive amount of information about those similar artists, one of which is of a particular interest to us, the "match", that returns a value between 0 and 1 describing how similar both artists are, 1 being the most similar.
End of explanation
for index, row in artists_df_mbid.iterrows():
    # main artist id
    artist_id = index
    # main artist name
    artist_name = row['name']
    # list of dictionaries of similar artists information
    similar_dict = row['similar_artists']
    # the number of similar artists needed
    range_used = 10
    try:
        # Incase list contained less than 10 artists, set the range to that number
        if (len(similar_dict)<10):
            range_used = len(similar_dict)
    except:
        continue
        
    for i in range(range_used):
        # Appending the information obtained to a new dataframe
        org_artist_name = artist_name
        similar_artist_name = similar_dict[i]['name']
        similar_artist_match = similar_dict[i]['match']
        artist_artist = artist_artist.append({'ArtistID':artist_id,'Artist': artist_name,'Similar_artists':similar_artist_name,'Weight':similar_artist_match}, ignore_index=True)
artist_artist_connections = pd.read_csv('artist_artist_connections.csv', sep='\t', header=0, index_col=0, skipinitialspace=True)
Explanation: For each artist, 100 similar artists are returned. To decrease the complexity of the problem and reduce the computational time, we only extracted the top 10 similar artists and saved them into a dataframe that contains the artist, artist id, the top 10 similar artists, and their corresponding weights.
End of explanation
artist_artist_connections.head()
Explanation: Sample output
End of explanation
<END_TASK>