<jupyter_start><jupyter_text>1. Data Cleaning2. Feature Engineering3. Correlation Matrix4. top 20/25 parameters5. Machine Learning Models using LazyPredict6. Tablular Analysis of which model is best7. Graphs along the way 1. Data Cleaning -Convert Date column to desirable type - DoneCompany Name remove or keep?<jupyter_code>dummy = SPRatings.iloc[1,2]
dum_year = int(str(dummy)[:4])
dum_day = int(str(dummy)[4:6])
dum_month = int(str(dummy)[6:])
print("dum_year:", dum_year, "\ndum_day:", dum_day, "\ndum_month", dum_month)
import datetime
SPRatings['Year'] = SPRatings.iloc[:,2]
SPRatings['Month'] = SPRatings.iloc[:,2]
SPRatings['Day'] = SPRatings.iloc[:,2]
for i in range(len(SPRatings['Data Date'])):
    SPRatings['Year'][i] = int(str(SPRatings['Data Date'][i])[:4])
    SPRatings['Month'][i] = int(str(SPRatings['Data Date'][i])[4:6])
    SPRatings['Day'][i] = int(str(SPRatings['Data Date'][i])[6:])
cols=["Year","Month","Day"]
SPRatings['Data Date'] = SPRatings[cols].apply(lambda x: '-'.join(x.values.astype(str)), axis="columns")
SPRatings['Data Date'] = pd.to_datetime(SPRatings['Data Date'])
SPRatings = SPRatings.drop(['Year','Month','Day'], axis=1)
uni = len(pd.unique(SPRatings['S&P Domestic Long Term Issuer Credit Rating']))
uni<jupyter_output><empty_output><jupyter_text>Feature Engineering  Ordinal Encoding for Credit Rating<jupyter_code>list123 = SPRatings['S&P Domestic Long Term Issuer Credit Rating']
list123 = np.unique(list123)
list123
#import category_encoders as ce

# create object of Ordinalencoding
#encoder= ce.OrdinalEncoder(cols=['S&P Domestic Long Term Issuer Credit Rating'],return_df=True,
 #                          mapping=[{'col':'S&P Domestic Long Term Issuer Credit Rating',
#'mapping':{'A':0,'A+':1, 'A-':2, 'AA':3, 'AA+':4, 'AA-':5, 'AAA':6, 'BB+':7, 'BBB':8, 'BBB+':9,'BBB-':10}}])

#SPRatings = encoder.fit_transform(SPRatings)
SPRatings
temp = SPRatings.iloc[1,1]
SPRatings.dtypes
scale_mapper = {'A':0,'A+':1, 'A-':2, 'AA':3, 'AA+':4, 'AA-':5, 'AAA':6, 'BB+':7, 'BBB':8, 'BBB+':9,'BBB-':10}
SPRatings['scale'] = SPRatings['S&P Domestic Long Term Issuer Credit Rating'].replace(scale_mapper)
SPRatings['S&P Domestic Long Term Issuer Credit Rating']=SPRatings['scale']
SPRatings<jupyter_output><empty_output><jupyter_text>Correlation Matrix<jupyter_code>#Correlation Matrix
from matplotlib import pyplot as plt

f, ax = plt.subplots(1, 1, figsize = (15, 10))
corr = SPRatings.corr()

sns.heatmap(corr,yticklabels=False,xticklabels=False)
df1 = SPRatings["S&P Domestic Long Term Issuer Credit Rating"]
df2 = SPRatings.drop(['S&P Domestic Long Term Issuer Credit Rating'],axis=1)
df1
correlation_KP = df2.corrwith(df1,axis=0)
correlation_KP = correlation_KP.sort_values(ascending=False)
correlation_KP_df = correlation_KP.to_frame()
correlation_KP_df = correlation_KP_df.reset_index()
correlation_KP_df.columns = ['Independent Variables','Rating Correlation']
correlation_KP_df_pos = correlation_KP_df[correlation_KP_df['Rating Correlation']>0]
#not_needed = correlation_KP_df[correlation_KP_df['Rating Correlation']<0]
#not_needed
correlation_KP_df_pos
correlation_kendall = df2.corrwith(df1,axis=0, method = 'spearman')
correlation_kendall.sort_values(ascending=False)
import plotly.graph_objects as go
import pandas as pd

fig = go.Figure(data=[go.Table(
    header=dict(values=['Independent Variables','Rating Correlation'],
                fill_color='white',
                align='center'),
    cells=dict(values=[correlation_KP_df['Independent Variables'],correlation_KP_df['Rating Correlation']],
               fill_color='lavender',
               align='left'))
])

fig.show()
list_not_needed = not_needed["Independent Variables"].tolist()
list_not_needed
#SPRatings_cleaned = SPRatings.drop([list_not_needed],axis=1)
cols_to_keep = correlation_KP_df_pos['Independent Variables']
ML_Ready_SPRating = pd.DataFrame()
for col in cols_to_keep:
    if col in SPRatings.columns:
        ML_Ready_SPRating[col] = SPRatings[col]
ML_Ready_SPRating = ML_Ready_SPRating.assign(Data_Date=SPRatings['Data Date'],Company_Name=SPRatings['Company Name'],Ticker_Symbol=SPRatings['Ticker Symbol'], Credit_Rating=SPRatings['S&P Domestic Long Term Issuer Credit Rating'])
ML_Ready_SPRating
ML_Ready_SPRating
distinct_comp = ML_Ready_SPRating.Company_Name.unique()
distinct_comp
asd = distinct_comp[1]
if ML_Ready_SPRating['Company_Name'] == asd:
    X = ML_Ready_SPRating.drop(["S&P Domestic Long Term Issuer Credit Rating"],axis=1)
    y = ML_Ready_SPRating["S&P Domestic Long Term Issuer Credit Rating"]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
y_test.shape
X = SPRatings_dummy.drop(["S&P Domestic Long Term Issuer Credit Rating"],axis=1)
y = SPRatings_dummy["S&P Domestic Long Term Issuer Credit Rating"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
y_test.shape
ML_Ready_SPRating = ML_Ready_SPRating[ML_Ready_SPRating['Company_Name'] != 'AFFILIATED MANAGERS GRP INC']
ML_Ready_SPRating
for comp in distinct_comp:
    dum_set = ML_Ready_SPRating[ML_Ready_SPRating['Company_Name']==comp]
    X = dum_set
    y = dum_set["Credit_Rating"]
    #X_train_dum, X_test_dum, y_train_dum, y_test_dum = train_test_split(X, y, test_size=0.3,train_size=0.7, random_state=42)
    #X_train.append(X_train_dum)
    #y_train.append(y_train_dum)
    #X_test.append(X_test_dum)
    #y_test.append(y_test_dum)

ML_Ready_SPRating.to_csv('qwer.csv')
X = ML_Ready_SPRating.drop(['Credit_Rating'],axis=1)
X = ML_Ready_SPRating.drop(['Data_Date'],axis=1)
y = ML_Ready_SPRating.Credit_Rating

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)<jupyter_output><empty_output><jupyter_text>Machine Learning to classify and test<jupyter_code>#First Algo - Novel Approach - Logistic Regression

from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression()
lr_model.fit(X_train,y_train)
y_pred = lr_model.predict(x_test)
#!pip install lazypredict 
from lazypredict.Supervised import LazyClassifier
clf = LazyClassifier(verbose=0, ignore_warnings=True, custom_metric = None)
models,predictions = clf.fit(X_train, X_test, y_train, y_test)
print(models)<jupyter_output>100%|██████████| 29/29 [00:17<00:00,  1.69it/s]
{'fraction_non_alphanumeric': 0.14278892072588348, 'fraction_numerical': 0.01973893664438077, 'mean_word_length': 7.15974025974026, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '599218', 'license': 'MIT', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2343, 'n_words': 436, 'path': 'SPRatings - Credit Classification.ipynb', 'repo_name': 'AlpeshPatil34/Moody-Credit-Rating-Prediction', 'size': '6282'}