<jupyter_start><jupyter_text>Exploration, Cleaning<jupyter_code>import pandas as pd
import numpy as np

%%capture
import sys

# If you're on Colab:
if 'google.colab' in sys.modules:
    DATA_PATH = 'https://raw.githubusercontent.com/buildweek-kickstarter/data_science/master/data/'
import pandas as pd

df = pd.read_csv('kickstarter - Sheet1.csv')

print(df.shape)
df
def normalise_row(row):
    if row['state'] == 'successful':
      return 'successful'
    if row['state'] == 'live':
      return 'live'
    else:
      return 'failed'
df['state'] = df.apply(lambda row : normalise_row(row), axis=1)
df['state'].value_counts()
df['launched'][0]
liverows = df[df['state'] == 'live'].index
 
# Delete these row indexes from dataFrame
df.drop(liverows , inplace=True)
df
df['deadline'].dtypes

# Split train into train & val
from sklearn.model_selection import train_test_split
train, val = train_test_split(df, train_size=0.80, test_size=0.20, 
                              stratify=df['state'], random_state=42)
train
val
# added new features to our data and got rid of duplicates
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

def wrangle(X):
    """Wrangle train, validate, and test sets in the same way"""
    
 
    # Stop CopyWarning
    X = X.copy()
    
   
    

    # converting date recorderd into int and making new feature
    X['deadline'] = pd.to_datetime(X['deadline'])
    # X['deadline'] = X['deadline'].dt
    # X['deadline'] =X['deadline'].astype(int)

    X['launched'] = pd.to_datetime(X['launched'])
    # X['launched'] = X['launched'].dt
    # X['launched'] =X['launched'].astype(int)

    X['length_of_campaign'] = (X['deadline'] - X['launched']).dt.days   

    return X
    

train = wrangle(train)
val = wrangle(val)
train
df['state'].value_counts()
# select our target 
target = 'state'

# make train without our target or id
train_features = train.drop(columns=[target])

# make numeric features
numeric_features = train_features.select_dtypes(include='number').columns.tolist()

# make a cardinality feature to help filter
cardinality = train_features.select_dtypes(exclude='number').nunique()

# get a list of relevant categorical data
categorical_features = cardinality[cardinality <=50].index.tolist()

# Combine the lists 
features = numeric_features + categorical_features
print(features)
X_train = train[features]
y_train = train[target]
X_val = val[features]
y_val = val[target]
#X_test = test[features]
 !pip install category_encoders==2.*
df['goal'].dtypes<jupyter_output><empty_output><jupyter_text>NLP Model<jupyter_code>import pandas as pd

df = pd.read_csv('kickstarter - Sheet1.csv')

print(df.shape)
df
df['words'] = df['name'] + df['category']
df['words'] = df['words'] + df['main_category']
sentences = df['words']
y = df['state']
from sklearn.model_selection import train_test_split
sentences_train, sentences_test, y_train, y_test = train_test_split(
    sentences, y, test_size=0.25, random_state=42)
df['category'].value_counts()
df['main_category'].unique()
df['category'].unique()
# Import the tf-idf vectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

# Instantiate and fit the tf-idf vectorizer
vectorizer = TfidfVectorizer(stop_words='english', ngram_range = (2,2))
vectorizer.fit(sentences_train)

# Vectorize the training and testing data
X_train = vectorizer.transform(sentences_train)
X_test  = vectorizer.transform(sentences_test)

# Display the properties of the vectorized text
X_train
# Import the classifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier

# Instantiate and fit a model
classifier = RandomForestClassifier()

classifier.fit(X_train, y_train)
score = classifier.score(X_test, y_test)

print("Accuracy:", score)
from sklearn.pipeline import Pipeline

# Define the Pipeline
pipe = Pipeline([('vect', vectorizer), # vectorizer
                 ('clf', classifier) # classifier
                ])

# Define the parameter space for the grid serach
parameters = {'max_depth': [1, 10, 1000000]} 


# Implement a grid search with cross-validation
from sklearn.model_selection import GridSearchCV

grid_search = GridSearchCV(pipe, parameters, cv=5, n_jobs=-1, verbose=1)
grid_search.fit(sentences, y);

# Print out the best score
grid_search.best_score_<jupyter_output>Fitting 5 folds for each of 3 candidates, totalling 15 fits
{'fraction_non_alphanumeric': 0.11179235642754956, 'fraction_numerical': 0.00797057020232986, 'mean_word_length': 5.2185514612452355, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '189131', 'license': 'MIT', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1544, 'n_words': 409, 'path': 'notebooks/Starks_copy_kickstarter.ipynb', 'repo_name': 'evangrinalds/data-science', 'size': '4893'}