<jupyter_start><jupyter_text><jupyter_code>!pip install kaggle
!pip install numpy==1.15.0
!pip install catboost
import numpy as np
import pandas as pd
from catboost import CatBoostRegressor, Pool
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.svm import NuSVR, SVR
import matplotlib.pyplot as plt
##IMPORT DATASET FROM KAGGLE
from google.colab import files
uploaded = files.upload()

!mkdir -p ~/.kaggle/ && mv kaggle.json ~/.kaggle && chmod 600 ~/.kaggle/kaggle.json

!kaggle competitions list
!kaggle competitions download -c LANL-Earthquake-Prediction
!ls 
!unzip train.csv.zip

!ls
train = pd.read_csv('train.csv',nrows = 6000000, dtype={'acoustic_data':np.int16,'time_to_failure':np.float64})
train.head(10)
train_ad_sample_df = train['acoustic_data'].values[::100]
train_ttf_sample_df = train['time_to_failure'].values[::100]



fig, ax1 = plt.subplots(figsize=(12,8))
plt.title("acoustic data + ttf")
plt.plot(train_ad_sample_df,color='r')
ax1.set_ylabel('acoustic data',color='r')
plt.legend(['acoustic data'],loc=(0.01,0.95))
ax2 = ax1.twinx()
plt.plot(train_ttf_sample_df,color='b')
ax1.set_ylabel('time to failure',color='b')
plt.legend(['time to failure'],loc=(0.01,0.9))
plt.grid(True)



#This is a supervised learning , we need to find co relation betwwen ad and ttf, btw there are only two features.. ideally we need more features
def generatefeatures(X):
  strain = []
  strain.append(X.mean())
  strain.append(X.std())
  strain.append(X.min())
  strain.append(X.max())
  strain.append(X.kurtosis())
  strain.append(X.skew())
  strain.append(np.quantile(X,0.01))
  return pd.Series(strain)
##kurtosis is a measure of whether the data are heavy-tailed or light-tailed relative to a normal distribution.
##Data sets with high kurtosis tend to have heavy tails,or outliers.Data sets with low kurtosis tend to have light tails , or lack of outliers.


##Skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean. The skewness value can be positive or negative,or undefined.
##positive skewness is going to right, left negative and so on.


##quantiles are cut points dividing the range of a probdist into continuous intervals with equal probabilities, or dividing the observations in a sample in the same way.
train = pd.read_csv('train.csv',iterator=True,chunksize=150_000, dtype={'acoustic_data':np.int16,'time_to_failure':np.float64})

X_train = pd.DataFrame()
y_train = pd.Series()
for df in train:
  ch = generatefeatures(df['acoustic_data'])
  X_train = X_train.append(ch,ignore_index=True)
  y_train = y_train.append(pd.Series(df['time_to_failure'].values[-1]))
X_train.describe()
X_train.head(10)
#Catboost explained simply:

"""
1. We first model data with simple models and analyze data for errors.
2. These errors signify data points that are difficult to fit by a simple model
3. Then for later models, we particularly focus on those hard to fit data to get them right.
4. In the end, we combine all the predictors by giving some weights to each predictor.





"""
train_pool = Pool(X_train,y_train)
m = CatBoostRegressor(iterations=10000,loss_function='MAE',boosting_type='Ordered')
m.fit(X_train,y_train,silent=True)
m.best_score_

##we will use SVM for regression
##kernel will be gaussian aka radial basis
##k(x1,x2) = exp(-gamma*(x1-x2)^2)
###using GridSearch for hype-optimization
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)

parameters = [{'gamma': [0.001,0.005,0.01,0.02,0.05,0.1],
              'C':[0.1,0.2,0.25,0.5,1,1.5,2]}]

reg1 = GridSearchCV(SVR(kernel='rbf',tol=0.01),parameters,cv=5)
reg1.fit(X_train_scaled,y_train.values.flatten())
y_pred1 = reg1.predict(X_train_scaled)

print("Best CV Score: {:.4f}".format(reg1.best_score_))
print(reg1.best_params_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12519240636223705, 'fraction_numerical': 0.030271934325295024, 'mean_word_length': 5.65358361774744, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '85974', 'license': 'Apache-2.0', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1345, 'n_words': 373, 'path': 'Earthquake_Prediction_Challenge.ipynb', 'repo_name': 'ucalyptus/Earthquake_', 'size': '3898'}