<jupyter_start><jupyter_text>Part-1 Importing the libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Creating the dataset<jupyter_code>dataset=pd.read_csv("QSAR_BCF_Kow.csv")
dataset<jupyter_output><empty_output><jupyter_text>Dropping columns SMILES and KOW<jupyter_code>dataset=dataset.drop(columns=["SMILES","KOW type"],axis=1)
dataset<jupyter_output><empty_output><jupyter_text>Remove the values in column titled “LogKOW” corresponding to the values starting with V-Mey_NA  in the column titled “CAS”<jupyter_code>dataset[dataset['CAS'].astype(str).str.match("^V-Mey_NA.[a-z0-9A-Z]+")]
index_CAS=dataset[dataset['CAS'].astype(str).str.match("^V-Mey_NA.[a-z0-9A-Z]+")].index
dataset=dataset.drop(index_CAS)

dataset<jupyter_output><empty_output><jupyter_text>Check for missing values. If available, fill it with zeros, ones and mean of column.<jupyter_code>dataset.info()
total = dataset['logBCF'].sum(skipna=True)
mean=total/dataset['logBCF'].shape[0]
dataset["logBCF"].fillna(mean, inplace = True)
dataset["LogKOW"].fillna(0, inplace = True)
index_merged=dataset[dataset['LogKOW'].astype(str).str.match("^Merged")].index
dataset=dataset.drop(index_merged)
dataset['LogKOW']=dataset["LogKOW"].astype(float)
dataset.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1048 entries, 0 to 1049
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   CAS     1048 non-null   object 
 1   Name    1048 non-null   object 
 2   LogKOW  1048 non-null   float64
 3   logBCF  1048 non-null   float64
dtypes: float64(2), object(2)
memory usage: 40.9+ KB<jupyter_text>Remove the column titled “CAS”.<jupyter_code>dataset=dataset.drop(columns="CAS",axis=1)
dataset
data=dataset.drop(columns="Name",axis=1)
data<jupyter_output><empty_output><jupyter_text>Perform linearity analysis on the resultant dataset.<jupyter_code>plt.scatter(data["LogKOW"],data["logBCF"])
plt.title("Linearity Analysis")
plt.legend()
plt.xlabel("LogKOW")
plt.ylabel("logBCF")
plt.show()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>Drawing heatmap<jupyter_code>import seaborn as sns
data_corr=data.corr()
sns.heatmap(data_corr,annot=True)<jupyter_output>/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>Normalize the values using min-max normalization.<jupyter_code>data['LogKOW']=((data['LogKOW']-data['LogKOW'].min())/(data['LogKOW'].max()-data['LogKOW'].min()))
data['logBCF']=((data['logBCF']-data['logBCF'].min())/(data['logBCF'].max()-data['logBCF'].min()))
data.head(20)<jupyter_output><empty_output><jupyter_text>Construct a regression equation, y=mx+c with LogKOW as independent attribute(x) and logBCF as dependent attribute(y).<jupyter_code>from sklearn.linear_model import LinearRegression
X=data.iloc[:,:-1].values
y=data.iloc[:,1].values
regressor=LinearRegression()
regressor.fit(X,y)
regressor.coef_[0]
regressor.intercept_<jupyter_output><empty_output><jupyter_text>**Plotting regression equation**<jupyter_code>plt.scatter(data.LogKOW, data.logBCF,  color='blue',label="Actual")
plt.plot(X, regressor.coef_[0]*X + regressor.intercept_, '-r',label="Predicted")
plt.title("Regression plot")
plt.legend()
plt.xlabel("LogKOW")
plt.ylabel("logBCF")
plt.show()<jupyter_output><empty_output><jupyter_text>Identify MAE,MSE and R2 scores. What are you inferring from the scores<jupyter_code>from sklearn import metrics
print('Mean Absolute Error:', metrics.mean_absolute_error(y, regressor.predict(X)))
print('Mean Squared Error:', metrics.mean_squared_error(y,regressor.predict(X)))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y,regressor.predict(X))))<jupyter_output>Mean Absolute Error: 0.09779273507043532
Mean Squared Error: 0.01701920682855145
Root Mean Squared Error: 0.13045768213697287<jupyter_text>Part-2 Repeat Initial steps<jupyter_code>dataset=pd.read_csv("QSAR_BCF_Kow.csv")
dataset<jupyter_output><empty_output><jupyter_text>Dropping columns SMILES and KOW<jupyter_code>dataset=dataset.drop(columns=["SMILES","KOW type"],axis=1)
dataset<jupyter_output><empty_output><jupyter_text>Remove the values in column titled “LogKOW” corresponding to the values starting with V-Mey_NA  in the column titled “CAS”<jupyter_code>dataset[dataset['CAS'].astype(str).str.match("^V-Mey_NA.[a-z0-9A-Z]+")]
index_CAS=dataset[dataset['CAS'].astype(str).str.match("^V-Mey_NA.[a-z0-9A-Z]+")].index
dataset=dataset.drop(index_CAS)

dataset<jupyter_output><empty_output><jupyter_text>Check for missing values. If available, fill it with zeros, ones and mean of column.<jupyter_code>dataset.info()
total = dataset['logBCF'].sum(skipna=True)
mean=total/dataset['logBCF'].shape[0]
dataset["logBCF"].fillna(mean, inplace = True)
dataset["LogKOW"].fillna(0, inplace = True)
index_merged=dataset[dataset['LogKOW'].astype(str).str.match("^Merged")].index
dataset=dataset.drop(index_merged)
dataset['LogKOW']=dataset["LogKOW"].astype(float)
dataset.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1048 entries, 0 to 1049
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   CAS     1048 non-null   object 
 1   Name    1048 non-null   object 
 2   LogKOW  1048 non-null   float64
 3   logBCF  1048 non-null   float64
dtypes: float64(2), object(2)
memory usage: 40.9+ KB<jupyter_text>Remove the column titled “CAS”.<jupyter_code>dataset=dataset.drop(columns="CAS",axis=1)
dataset
data=dataset.drop(columns="Name",axis=1)
data<jupyter_output><empty_output><jupyter_text>Perform linearity analysis on the resultant dataset.<jupyter_code>plt.scatter(data["LogKOW"],data["logBCF"])
plt.title("Linearity Analysis")
plt.legend()
plt.xlabel("LogKOW")
plt.ylabel("logBCF")
plt.show()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>Drawing heatmap<jupyter_code>import seaborn as sns
data_corr=data.corr()
sns.heatmap(data_corr,annot=True)<jupyter_output><empty_output><jupyter_text>Normalize the values using min-max normalization.<jupyter_code>data['LogKOW']=((data['LogKOW']-data['LogKOW'].min())/(data['LogKOW'].max()-data['LogKOW'].min()))
data['logBCF']=((data['logBCF']-data['logBCF'].min())/(data['logBCF'].max()-data['logBCF'].min()))
data.head(20)<jupyter_output><empty_output><jupyter_text>Divide the dataset into having 750 and 308 rows.(It is a 70:30 ratio) Randomization may be applied<jupyter_code>X=data.iloc[:,:-1].values
y=data.iloc[:,1].values
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=42)<jupyter_output><empty_output><jupyter_text>Predict the value of y, dependent value using the calculated m and c values and store in predicted_y.Compare the difference between actual_y and predicted_y.<jupyter_code>from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(X_train,y_train)
regressor.predict(X_test)
y_test<jupyter_output><empty_output><jupyter_text>**Plotting the regression equation**<jupyter_code>plt.scatter(data.LogKOW, data.logBCF,  color='blue',label="Actual")
plt.plot(X_train, regressor.coef_[0]*X_train + regressor.intercept_, '-r',label="Predicted")
plt.title("Regression plot")
plt.legend()
plt.xlabel("LogKOW")
plt.ylabel("logBCF")
plt.show()<jupyter_output><empty_output><jupyter_text>Calculate MAE, MSE and r2. What do you infer from the scores?<jupyter_code>from sklearn import metrics
print('Mean Absolute Error:', metrics.mean_absolute_error(y, regressor.predict(X)))
print('Mean Squared Error:', metrics.mean_squared_error(y,regressor.predict(X)))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y,regressor.predict(X))))<jupyter_output>Mean Absolute Error: 0.09685200614831192
Mean Squared Error: 0.017049511744675366
Root Mean Squared Error: 0.13057377893235445<jupyter_text>From the above observations , we can see that the metrics value from the above model are almost the same as the previous model .But we can infer the following from the above observations :1.   MAE has decreased in the second model2.   MSE has increased in the second model3. RMSE has increased in the second model Part-3 Perform cross – validation with 2, 3, 4 and till 14 folds with r2 as the metric<jupyter_code>from sklearn.model_selection import cross_val_score
vali=[2,3,4,14]
for i in vali:
  accuracy=cross_val_score(regressor,X,y,cv=i,scoring="r2")
  print("Number of folds:",i," Accuracy:",accuracy)<jupyter_output>Number of folds: 2  Accuracy: [0.2940963 0.4917453]
Number of folds: 3  Accuracy: [0.32144317 0.36214809 0.49132267]
Number of folds: 4  Accuracy: [0.12568676 0.34361067 0.48940036 0.45578078]
Number of folds: 14  Accuracy: [ 0.36618316  0.13322445  0.12582733  0.43519506  0.45626569 -0.04868227
  0.29745624  0.3611614   0.52179573  0.5955038   0.54876981  0.45026282
  0.49504824  0.32313235]
{'fraction_non_alphanumeric': 0.1437636761487965, 'fraction_numerical': 0.05142231947483589, 'mean_word_length': 7.378551787351054, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 95, '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': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '464657', 'license': 'MIT', 'n_tokens_mistral': 3737, 'n_tokens_neox': 3301, 'n_words': 625, 'path': 'Simple Linear Regression/QSAR fish bioconcentration factor/QSAR_fish_bioconcentration_factor.ipynb', 'repo_name': 'mehulfollytobevice/MachineLearning', 'size': '9140'}