├── instadata.xlsx ├── instatest.xlsx ├── instatest.csv ├── README.md ├── instadata.csv └── insta.py /instadata.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prachi-mate/Target-Audience_For-Instagram-Ads/HEAD/instadata.xlsx -------------------------------------------------------------------------------- /instatest.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prachi-mate/Target-Audience_For-Instagram-Ads/HEAD/instatest.xlsx -------------------------------------------------------------------------------- /instatest.csv: -------------------------------------------------------------------------------- 1 | id,count,gender,location,age,locationcode,gendercode 2 | srishtinene_,50,F,Mumbai,19,1,1 3 | gautammannan,97,M,Pune,56,2,0 4 | samrudhiyeginwar,70,F,Bengaluru,32,3,1 5 | shivinidixit_,80,F,Hyderabad,26,5,1 6 | poorvz_,76,F,Kolhapur,45,4,1 7 | travuersa29,30,F,Mumbai,56,1,1 8 | rahulsingh0512,56,M,Mumbai,35,1,0 9 | shreyan_1629,69,M,Pune,23,2,0 10 | bhavyaguptaa_,92,M,Pune,40,2,0 11 | _atharva,85,M,Bengaluru,21,3,0 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Target-Audience_For-Instagram-Ads 2 | For a commercial or business account on instagram, this algorithm can be used to find the target audience group for marketing by analyzing demographics of followers such as- age, location, gender and associating them with the number of likes. 3 | 4 | The API-Download contains API library to be used for the code. 5 | 6 | insta.py: python code for the project 7 | instadata.csv: csv file for training data 8 | instadata.xlsx: excel file for training data 9 | instatest.csv: csv file for testing data 10 | instatest.xlsx: excel file for testing data 11 | 12 | -------------------------------------------------------------------------------- /instadata.csv: -------------------------------------------------------------------------------- 1 | id,count,gender,location,age,label,locationcode,gendercode 2 | srushti.11,97,F,mumbai,19,topfans,1,1 3 | rupaldinkar,95,F,pune,32,topfans,2,1 4 | rushidate,94,M,pune,45,topfans,2,0 5 | rachalian._ ,93,F,bengaluru,22,topfans,3,1 6 | _.sonalika._ ,91,F,mumbai,34,topfans,1,1 7 | vaish_.21,87,F,kolhapur,56,topfans,4,1 8 | shreyas_waikar,85,M,hyderabad,20,topfans,5,0 9 | shreya_patil_0410 ,83,F,mumbai,25,topfans,1,1 10 | chouhan_tejal,81,F,pune,40,topfans,2,1 11 | shanky.y_2408,81,M,bengaluru,23,topfans,3,0 12 | kshitijshah17,80,M,delhi,22,topfans,6,0 13 | jaykataria95,80,M,delhi,21,topfans,6,0 14 | harsh__joshi___,79,M,delhi,46,fans,6,0 15 | kale_snegdha,78,F,mumbai,23,fans,1,1 16 | desaidiviyang ,77,M,pune,34,fans,2,0 17 | _aishhwarya,76,F,kolhapur,67,fans,4,1 18 | ___m_a_h_a_d_e_v___ ,76,M,hyderabad,39,fans,5,0 19 | _suraj_nair ,75,M,delhi,26,fans,6,0 20 | a_tinge_of_everything ,74,F,mumbai,27,fans,1,1 21 | mansi_bhanushali29,74,F,mumbai,28,fans,1,1 22 | purna_joshi,74,M,pune,22,fans,2,0 23 | advait_amrutkar ,72,M,pune,45,neutral,2,0 24 | anvi_489,72,F,pune,44,neutral,2,1 25 | notoriousprinces,71,F,pune,38,neutral,2,1 26 | akki__yeola,70,M,pune,36,neutral,2,0 27 | _amit_jaiswar_,70,M,mumbai,39,neutral,1,0 28 | _shraddhapanchal_,69,F,mumbai,41,neutral,1,1 29 | sayalimalave,69,F,delhi,40,neutral,6,1 30 | 40_states_to_go,69,M,bengaluru,21,neutral,3,0 31 | fragtaco,68,M,mumbai,24,neutral,1,0 32 | -------------------------------------------------------------------------------- /insta.py: -------------------------------------------------------------------------------- 1 | #importing libraries 2 | import pandas as pd 3 | from pandas.io.json import json_normalize 4 | from InstagramAPI import InstagramAPI 5 | import time 6 | 7 | #LOGIN 8 | def login_to_instagram(username, password): 9 | api = InstagramAPI(username, password) 10 | api.login() 11 | return api 12 | 13 | api = login_to_instagram('enter_your_username_here','enter_your_password_here') 14 | 15 | #Retriving Posts 16 | def get_my_posts(api): 17 | '''Retrieve all posts from own profile''' 18 | my_posts = [] 19 | has_more_posts = True 20 | max_id= '' 21 | 22 | while has_more_posts: 23 | api.getSelfUserFeed(maxid=max_id) 24 | if api.LastJson['more_available'] is not True: 25 | has_more_posts = False #stop condition 26 | 27 | max_id = api.LastJson.get('next_max_id','') 28 | my_posts.extend(api.LastJson['items']) #merge lists 29 | time.sleep(2) # slows down to avoid flooding 30 | 31 | if has_more_posts: 32 | print(str(len(my_posts)) + ' posts retrieved so far...') 33 | 34 | print('Total posts retrieved: ' + str(len(my_posts))) 35 | 36 | return my_posts 37 | len(my_posts) 38 | my_posts[2] 39 | 40 | my_posts = get_my_posts(api) 41 | 42 | #Retrive Post Likers 43 | def get_posts_likers(api, my_posts): 44 | '''Retrieve all likers on all posts''' 45 | 46 | likers = [] 47 | 48 | print('wait %.1f minutes' % (len(my_posts)*2/60.)) 49 | for i in range(len(my_posts)): 50 | m_id = my_posts[i]['id'] 51 | api.getMediaLikers(m_id) 52 | 53 | likers += [api.LastJson] 54 | 55 | # Include post_id in likers dict list 56 | likers[i]['post_id'] = m_id 57 | 58 | time.sleep(2) 59 | print('done') 60 | 61 | return likers 62 | 63 | 64 | likers = get_posts_likers(api, my_posts) 65 | 66 | 67 | #Likers to pandas df 68 | def posts_likers_to_df(likers): 69 | '''Transforms likers list of dicts into pandas DataFrame''' 70 | 71 | # Normalize likers by getting the 'users' list and the post_id of each like 72 | df_likers = json_normalize(likers, 'users', ['post_id']) 73 | 74 | # Add 'content_type' column to know the rows are likes 75 | df_likers['content_type'] = 'like' 76 | 77 | return df_likers 78 | 79 | df_likers = posts_likers_to_df(likers) 80 | 81 | print('Total posts: ' + str(len(my_posts))) 82 | print('---------') 83 | print('Total likes on profile: ' + str(df_likers.shape[0])) #shape[0] represents number of rows 84 | print('Distinct users that liked your posts: ' +str(df_likers.username.nunique())) # nunique() will count distinct values of a col 85 | print('---------') 86 | 87 | 88 | #Top 30 likers 89 | df_likers.username.value_counts()[:30] 90 | df_likers.username.value_counts()[:30].plot(kind='bar', title='Top 30 media likers', grid=True, figsize=(12,6)) 91 | df_likers.username.value_counts()[:30].plot(kind='pie', title='Top 30 media likers distribution', autopct='%1.1f%%', figsize=(12,6)) 92 | 93 | 94 | 95 | #how many of my topfans are female ? 96 | 97 | import pandas 98 | 99 | df=pd.read_csv("C:\\Users\\prach\\OneDrive\\Desktop\\COLLEGE\\TY\\Machine Learning\\Project\\instadata.csv") 100 | 101 | df.head() 102 | 103 | #SVM 104 | x=df.drop("label", axis=1) 105 | x=x.drop("location", axis=1) 106 | x=x.drop("gender",axis=1) 107 | x=x.drop("id",axis=1) 108 | y=df["label"] 109 | 110 | from sklearn.model_selection import train_test_split 111 | x_train,x_test,y_train, y_test = train_test_split(x,y, test_size=0.4) 112 | z=list(y_train) 113 | ytest=list(y_test) 114 | 115 | from sklearn.svm import SVC 116 | from sklearn.metrics import accuracy_score 117 | 118 | svclassifier = SVC(kernel='linear') 119 | svclassifier.fit(x_train,z) 120 | output=list(svclassifier.predict(x_test)) 121 | output 122 | 123 | accs=accuracy_score(ytest,output) 124 | print("Accuracy score=", accs) 125 | 126 | 127 | #Testing Unknown Data 128 | df1=pd.read_csv("C:\\Users\\prach\\OneDrive\\Desktop\\COLLEGE\\TY\\Machine Learning\\Project\\instatest.csv") 129 | 130 | p=df1.drop("location", axis=1) 131 | p=p.drop("gender",axis=1) 132 | p=p.drop("id",axis=1) 133 | 134 | output=list(svclassifier.predict(p)) 135 | predicted=list(output) 136 | 137 | ids=list(df1["id"]) 138 | print("id class"); 139 | 140 | for x in range(0,len(predicted)): 141 | print(ids[x] +" ------> " +predicted[x]) 142 | 143 | 144 | --------------------------------------------------------------------------------