<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
df = pd.read_csv('/content/youtube-channel-comparison - youtube-channel-comparison.csv')
df.head()
df.shape
df['views'] = df['views'] * 1000000
df['subs'] = df['subs'] * 1000
df['last 30 days views'] = df['last 30 days views'] * 1000
df
df['how old'] = pd.to_datetime(df['how old'])
basedate = pd.Timestamp('2022-01-28')
df['old'] = (basedate - df['how old']).dt.days
df.drop(columns=['how old'],inplace=True)
df
from sklearn.impute import KNNImputer
knn = KNNImputer(n_neighbors=3,weights='distance')

knn.fit_transform(df.iloc[:,1:])[30]
df['subs'].fillna(60580,inplace=True)
df
# Views rank
print(np.where(df.sort_values('views',ascending=False)['channel name'] == 'CampusX')[0][0],'out of ',df.shape[0])
df.sort_values('views',ascending=False)[['channel name','views']].head(10)
# Subs rank
print(np.where(df.sort_values('subs',ascending=False)['channel name'] == 'CampusX')[0][0],'out of ',df.shape[0])

df.sort_values('subs',ascending=False)[['channel name','subs']].head(10)
# Number of videos rank
print(np.where(df.sort_values('num videos',ascending=False)['channel name'] == 'CampusX')[0][0],'out of ',df.shape[0])
df.sort_values('num videos',ascending=False)[['channel name','num videos']].head(10)
# How old
print(np.where(df.sort_values('old',ascending=False)['channel name'] == 'CampusX')[0][0],'out of ',df.shape[0])
old_df = df.sort_values('old',ascending=False)[['channel name','old']].head(15)
old_df['old'] = round(old_df['old']/365)
old_df
# last 30 days performance
print(np.where(df.sort_values('last 30 days views',ascending=False)['channel name'] == 'CampusX')[0][0],'out of ',df.shape[0])
df.sort_values('last 30 days views',ascending=False)[['channel name','last 30 days views']].head(15)
# 1 subscriber ke liye views required
# view/subscriber count
df['v/s count'] = df['views']/df['subs']

print(51 - np.where(df['channel name'] == 'CampusX')[0][0],'out of ',df.shape[0])

df[['channel name','v/s count']].sort_values(['v/s count'],ascending=True)


import plotly.express as px
fig = px.scatter(data_frame=df,x=df['views'], y=df['subs'],hover_name=df['channel name'])
fig.show()
temp_df = df[df['views']>=1000000]
fig = px.scatter(data_frame=temp_df,x=temp_df['views'], y=temp_df['subs'],hover_name=temp_df['channel name'])
fig.show()
# videos/subs ratio
df['vids/subs count'] = df['num videos']/df['subs']
df[['channel name','vids/subs count']].sort_values(['vids/subs count'])
# per day sub gain(time required to gain 1 subscriber)
df['per_day_subs_gain'] = df['old']/df['subs']
temp_df = df[['channel name','per_day_subs_gain']].sort_values(['per_day_subs_gain'])
temp_df['per_day_subs_gain'] = temp_df['per_day_subs_gain']*24*60

print(np.where(temp_df['channel name'] == 'CampusX')[0][0],'out of ',temp_df.shape[0])

temp_df
# aukaad ratio(how many views per subscriber provides)
df['last_month_subs_ratio'] = df['last 30 days views']/df['subs']
df[['channel name','last_month_subs_ratio']].sort_values(['last_month_subs_ratio'],ascending=False)
# views/minute
df['views/day'] = df['views']/df['old']
temp_df = df[['channel name','views/day']].sort_values(['views/day'],ascending=False)

print(np.where(temp_df['channel name'] == 'CampusX')[0][0],'out of ',temp_df.shape[0])

temp_df
# views/video
df['views/num_video'] = df['views']/df['num videos']
temp_df = df[['channel name','views/num_video']].sort_values(['views/num_video'],ascending=False)

print(np.where(temp_df['channel name'] == 'CampusX')[0][0],'out of ',temp_df.shape[0])

temp_df


# pca(higher dim viz)
viz_df = df[['channel name','views','subs','last 30 days views','num videos','old']]

from sklearn.preprocessing import StandardScaler

X = viz_df.iloc[:, 1:].values
# Standardizing the features
X = StandardScaler().fit_transform(X)
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_embedded = pca.fit_transform(X)
X_embedded
fig = px.scatter(data_frame=temp_df,x=X_embedded[:,0], y=X_embedded[:,1],hover_name=viz_df['channel name'])
fig.show()
# hona kitta chahiye tha?
# prepare data
index_of_campusx = np.where(df['channel name'] == 'CampusX')[0][0]

X_df = df[['views','last 30 days views','num videos','old','subs']]

X_df['subs'] = X_df['subs']/1000

X_df
scaler = StandardScaler()
X_input = scaler.fit_transform(X_df.iloc[:,:-1])
campusx_input = X_input[10]
# delete the campusx row
X_input = np.delete(X_input,(10), axis=0)
y = X_df.iloc[:,-1].values
y = np.delete(y,(10))
import tensorflow
from tensorflow import keras
from keras import Sequential
from keras.layers import Dense
model = Sequential()

model.add(Dense(10,activation='relu',input_dim=4))
model.add(Dense(10,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(1,activation='linear'))
model.summary()
model.compile(optimizer='Adam',loss='mean_absolute_error')
model.fit(X_input,y,epochs=1000,validation_split=0.1)
o1 = model.predict(campusx_input.reshape(1,4))
o1 = o1[0][0]
o1
from sklearn.ensemble import RandomForestRegressor

rf = RandomForestRegressor()
rf.fit(X_input,y)

o2 = rf.predict(campusx_input.reshape(1,4))
o2 = o2[0]
o2
from xgboost import XGBRegressor

xg = XGBRegressor()
xg.fit(X_input,y)
o3 = xg.predict(campusx_input.reshape(1,4))
o3 = o3[0]
o3
from sklearn.svm import SVR

lr = SVR()
lr.fit(X_input,y)
o4 = lr.predict(campusx_input.reshape(1,4))
o4 = o4[0]
o4
from sklearn.ensemble import GradientBoostingRegressor

rf = GradientBoostingRegressor()
rf.fit(X_input,y)

o5 = rf.predict(campusx_input.reshape(1,4))
o5 = o5[0]
o5
from sklearn.neighbors import KNeighborsRegressor

rf = KNeighborsRegressor()
rf.fit(X_input,y)

o6 = rf.predict(campusx_input.reshape(1,4))
o6 = o6[0]
o6
(o1+o2+o3+o4+o5+o6)/6<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1973232669869595, 'fraction_numerical': 0.032944406314344546, 'mean_word_length': 8.023219814241486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '573856', 'license': 'MIT', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2315, 'n_words': 387, 'path': 'my_youtube_failure.ipynb', 'repo_name': 'ShivarajShivaprakasha/Machine-Learning', 'size': '5828'}