Bnava13 commited on
Commit
eb9e9d0
·
verified ·
1 Parent(s): 6cfc78d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -17
app.py CHANGED
@@ -43,18 +43,37 @@ if len(data) > 0:
43
  else:
44
  data['rating_ratio'] = 0.5 # Default neutral rating
45
 
46
- # Add playtime features if available
47
  if 'average_playtime_forever' in data.columns:
48
  # Log transform to handle skewed distribution
49
  data['log_playtime'] = np.log1p(data['average_playtime_forever'])
50
- scaler = MinMaxScaler()
51
- data['playtime_scaled'] = scaler.fit_transform(data[['log_playtime']])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  else:
53
  data['playtime_scaled'] = 0.5
54
 
55
- # Add user score features if available
56
  if 'user_score' in data.columns:
57
- data['user_score_scaled'] = data['user_score'] / 100.0 # Assuming user_score is out of 100
 
 
 
58
  else:
59
  data['user_score_scaled'] = 0.5
60
 
@@ -84,16 +103,21 @@ if len(data) > 0:
84
 
85
  # Vectorize with improved parameters
86
  try:
87
- # Use more n-grams and increased max_features for better semantic understanding
88
- vectorizer = TfidfVectorizer(
89
- stop_words='english',
90
- ngram_range=(1, 3), # Capture phrases up to 3 words
91
- max_features=10000, # Increase features for more nuanced relationships
92
- min_df=2, # Ignore very rare terms
93
- max_df=0.9 # Ignore very common terms
94
- )
95
- feature_vectors = vectorizer.fit_transform(data['combined_features'])
96
- print(f"Vectorization complete. Shape: {feature_vectors.shape}")
 
 
 
 
 
97
  except Exception as e:
98
  print(f"Vectorization error: {e}")
99
  feature_vectors = np.zeros((len(data), 1))
@@ -102,8 +126,23 @@ if len(data) > 0:
102
  if 'positive_ratings' in data.columns and len(data) > 0:
103
  # Log transform to handle skewed distribution of ratings
104
  data['log_ratings'] = np.log1p(data['positive_ratings'])
105
- scaler = MinMaxScaler()
106
- data['positive_ratings_scaled'] = scaler.fit_transform(data[['log_ratings']])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  else:
108
  data['positive_ratings_scaled'] = 0
109
 
 
43
  else:
44
  data['rating_ratio'] = 0.5 # Default neutral rating
45
 
46
+ # Add playtime features if available - FIX: Added proper checks for empty data
47
  if 'average_playtime_forever' in data.columns:
48
  # Log transform to handle skewed distribution
49
  data['log_playtime'] = np.log1p(data['average_playtime_forever'])
50
+
51
+ # FIX: Check if we have valid data before scaling
52
+ if len(data) > 0 and not data['log_playtime'].isna().all():
53
+ try:
54
+ scaler = MinMaxScaler()
55
+ # FIX: Only scale non-NA values
56
+ valid_playtime_mask = ~data['log_playtime'].isna()
57
+ if valid_playtime_mask.any(): # Only if we have any valid values
58
+ data.loc[valid_playtime_mask, 'playtime_scaled'] = scaler.fit_transform(
59
+ data.loc[valid_playtime_mask, ['log_playtime']]
60
+ )
61
+ else:
62
+ data['playtime_scaled'] = 0.5
63
+ except Exception as e:
64
+ print(f"Error in playtime scaling: {e}")
65
+ data['playtime_scaled'] = 0.5
66
+ else:
67
+ data['playtime_scaled'] = 0.5
68
  else:
69
  data['playtime_scaled'] = 0.5
70
 
71
+ # Add user score features if available - FIX: Added proper checks
72
  if 'user_score' in data.columns:
73
+ # FIX: Handle potential non-numeric values
74
+ data['user_score'] = pd.to_numeric(data['user_score'], errors='coerce')
75
+ # FIX: Check for NaN values before scaling
76
+ data['user_score_scaled'] = data['user_score'].fillna(50) / 100.0 # Assuming user_score is out of 100
77
  else:
78
  data['user_score_scaled'] = 0.5
79
 
 
103
 
104
  # Vectorize with improved parameters
105
  try:
106
+ # FIX: Check if we have enough data for vectorization
107
+ if len(data) > 1:
108
+ # Use more n-grams and increased max_features for better semantic understanding
109
+ vectorizer = TfidfVectorizer(
110
+ stop_words='english',
111
+ ngram_range=(1, 3), # Capture phrases up to 3 words
112
+ max_features=10000, # Increase features for more nuanced relationships
113
+ min_df=2, # Ignore very rare terms
114
+ max_df=0.9 # Ignore very common terms
115
+ )
116
+ feature_vectors = vectorizer.fit_transform(data['combined_features'])
117
+ print(f"Vectorization complete. Shape: {feature_vectors.shape}")
118
+ else:
119
+ print("Not enough data for vectorization")
120
+ feature_vectors = np.zeros((len(data), 1))
121
  except Exception as e:
122
  print(f"Vectorization error: {e}")
123
  feature_vectors = np.zeros((len(data), 1))
 
126
  if 'positive_ratings' in data.columns and len(data) > 0:
127
  # Log transform to handle skewed distribution of ratings
128
  data['log_ratings'] = np.log1p(data['positive_ratings'])
129
+
130
+ # FIX: Check for valid data before scaling
131
+ if not data['log_ratings'].isna().all():
132
+ try:
133
+ scaler = MinMaxScaler()
134
+ valid_ratings_mask = ~data['log_ratings'].isna()
135
+ if valid_ratings_mask.any():
136
+ data.loc[valid_ratings_mask, 'positive_ratings_scaled'] = scaler.fit_transform(
137
+ data.loc[valid_ratings_mask, ['log_ratings']]
138
+ )
139
+ else:
140
+ data['positive_ratings_scaled'] = 0
141
+ except Exception as e:
142
+ print(f"Error in ratings scaling: {e}")
143
+ data['positive_ratings_scaled'] = 0
144
+ else:
145
+ data['positive_ratings_scaled'] = 0
146
  else:
147
  data['positive_ratings_scaled'] = 0
148